📜 ⬆️ ⬇️

TODO plugin update for QtCreator 2.5.0

QtCreator 2.5.0 was just released the other day, and the TODO plugin appeared in it. But this plugin supports comments like: <KEYWORD>: <some text>, and I use doxygen comments everywhere: @ <KEYWORD> <SOME_TEXT>. Therefore, I decided to modify the plugin so that it could support comments of both kinds.

Training


So, first of all, I cloned the official repository of the project , set up the environment (QtCreator-2.5.0 requires at least version 4.7.4 to build Qt, so I also had to build Qt, chose version 4.8.1).

Implementation


Adding new functionality

Actually, the code of the plugin itself is very simple and straightforward. The main class that contains information about keywords is Keyword . In order for us to determine the type of the keyword, we introduce a new entity:
 enum KeywordStyle { DefaultKeywordStyle, DoxygenKeywordStyle }; 

And we add a new style field and a new QString searchingText() const; method QString searchingText() const; . This method returns the string by which the keyword is searched, here is its implementation:
 QString Keyword::searchingText() const { if (style == DefaultKeywordStyle) return name + QLatin1Char(':'); else { Q_ASSERT(style == DoxygenKeywordStyle && "keywordStyle is not properly configured"); return QLatin1Char('@') + name.toLower(); } } 

We also add to the equals method an appropriate comparison of the style fields.
')
Use new field

Now, we need to replace the use of the name field with a new method, searchingText . This needs to be done in the LineParser class in three methods:


Configuration support

Now the plugin is already able to search and create a TODO list based on information about keywords. Now we need to refine the mechanism for creating, saving and editing keywords.

To do this, first fix the implementation of the Settings class. In this class, we will loadsave setDefault methods setDefault .

The load method recovers information about keywords from the configuration. Since the configuration may be in the old format, by default we must set the keyword type to DefaultKeywordStyle . Actually, the refinement of this method is rather trivial, only two lines were added:
 const QString styleKey = QLatin1String("style"); //... keyword.style = static_cast<KeywordStyle>(settings->value(styleKey, DefaultKeywordStyle).toInt()); 


The save method was updated in the same way:
 const QString styleKey = QLatin1String("style"); //... settings->setValue(styleKey, keyword.style); 

The setDefault method is used to restore default values:
 void Settings::setDefault() { scanningScope = ScanningScopeCurrentFile; keywords.clear(); setDefaultKeywordStyle(); setDoxygenKeywordStyle(); } void Settings::setDefaultKeywordStyle() { Keyword keyword; keyword.name = QLatin1String("TODO"); keyword.iconResource = QLatin1String(Constants::ICON_WARNING); keyword.color = QColor(QLatin1String(Constants::COLOR_TODO_BG)); keyword.style = DefaultKeywordStyle; keywords.append(keyword); //...     } void Settings::setDoxygenKeywordStyle() { Keyword keyword; keyword.name = QLatin1String("todo"); keyword.iconResource = QLatin1String(Constants::ICON_WARNING); keyword.color = QColor(QLatin1String(Constants::COLOR_TODO_BG)); keyword.style = DoxygenKeywordStyle; keywords.append(keyword); //...     } 


UI support

And finally, it remains to implement support for the new field in the GUI. For this, I added a check box file to the ui in order to be able to determine the type of the keyword.


Now it remains to implement the KeywordDialog work with the new field style and the new check box. In the class constructor, add the code to enable / disable the checkbox:
  if (keyword.style == DoxygenKeywordStyle) ui->doxygenStyleCheckBox->setCheckState(Qt::Checked); else ui->doxygenStyleCheckBox->setCheckState(Qt::Unchecked); 

In the Keyword generation method of ui also add the corresponding code:
  result.style = (ui->doxygenStyleCheckBox->checkState() == Qt::Checked) ? DoxygenKeywordStyle : DefaultKeywordStyle; 


Also, it is necessary to correct the OptionsDialog class, since the objects of the Keyword class are converted into objects of the QListWidgetItem class. To this end, I highlighted such transformations into separate methods:
 void OptionsDialog::itemFromKeyword(const Keyword &keyword, QListWidgetItem *item) { item->setIcon(QIcon(keyword.iconResource)); item->setText(keyword.name); item->setData(Constants::IconResourceRole, keyword.iconResource); item->setData(Constants::KeywordStyleRole, keyword.style); item->setBackgroundColor(keyword.color); } Keyword OptionsDialog::keywordFromItem(const QListWidgetItem *item) { Keyword keyword; keyword.name = item->text(); keyword.iconResource = item->data(Constants::IconResourceRole).toString(); keyword.color = item->backgroundColor(); keyword.style = static_cast<KeywordStyle>(item->data(Constants::KeywordStyleRole).toInt()); return keyword; } 
And in the code I replaced all the transformations with calls to these methods.

Result


Here is how it looks from me:

Also created an application , attached a patch that implements the described functionality.

In principle, you can also add backslash \ support to DoxygenKeywordStyle, since the Doxygen documentation says:
All commands in the documentation start with a backslash (\) or an at-sign (@)

Source: https://habr.com/ru/post/138814/


All Articles