Important: Please read the Qt Code of Conduct -

Extend C++ Text highlighting with Plugin

  • Hi,

    I am trying to investigate the possibility of extending the normal C++ Syntax highlighter with a plugin. For the moment I am only interested in working with comments, but in future this might extend beyond comments.

    Is it possible to extend the syntax highlighting purely using a plugin? If yes, how would I approach this?

    I have done a bit already (testing/prototyping), but could not get a good solution, or rather any solution. This is what I have done:
    I have created my own TextEditor::SyntaxHighlighter and added it to the QTextDocument of the current editor. But if I do this, it replaces the C++ Syntax highlighter, and that functionality is lost. I though about extending the CppHighlighter by deriving my class from it, but the highlighter is in the Internal namespace, so that is not possible without editing the CppEditor plugin (not an option I guess).

    My next attempt was to use a cursor to change the format (using QTextCursor::mergeCharFormat()) of a section of the comment without using my SyntaxHighlighter. I did this in my slot called when the document gets updated. But this format change causes the documentUpdated() signal to be emitted, which causes my formatter to update the format which causes... (see the infinite loop)

    I see there is a CppHighlightingSupport class in the CppTools plugin, but I can't figure out what this is for, nor if it will help me with my problem. One thing that I saw about this 'tool' that make me think this is not the correct things is the fact that only one can be set on the CppModelManager. Again I can't extend the base one since it is also Internal. And then again, what if someone else decides to add another plugin, that also extends the syntax highlighting. Then depending on the one that sets the support factory last will be the one that gets used.

    I don't know how to approach this so that I can keep the highlighting that the CppHighlighter applies and to highlight some specific sections of comments differently.

    Any help will be appreciated.

    PS: If this is the wrong forum, please point me in the right direction

  • Moderators

    I assume you are talking about Qt Creator here. Just ignore me if you don't:-)

    Unfortunately I do not know too many details about the editors and the code models, so I can not really help with the issue at hand, but it is always an option to export some symbols that are needed. All you need to do is to provide a reasonable use-case and a patch:) We have most symbols as internal, since each symbol needs resolving at startup and there is less need to worry about changing internal symbols.

    The fastest way to get going is most likely to chat people up on IRC. Many devs hang out in #qt-creator on the freenode irc network, many only during european business hours (the core team is located in Berlin, Germany). Another great way to reach the team is our "mailing list": .

  • Yes, forgot to mention that I am talking about Qt Creator, 2.6.0 to be specific.

    I will look into making some modifications to the other relevant plugins, with the intention to provide a patch. I would however prefer not to do this if this is not necessary. If there are any other suggestions I would like to hear them.

    Thanks, I will keep this in mind. I am subscribed on the relevant mailing lists and use them now and again. I was hoping the forum was a good alternative for a question (and here we get points :P ). I will give this post a bit more time and then perhaps send my question to the mailing list as well.

  • Qt Creator usually do not use plugins for small additions, rather merge that functionality into the major plugins, especially if the addition is considered generally useful. So it depends a bit on what exactly you want to highlight, but doing it in the normal C++ support is very likely the way to go.

    The mailing list (and #qt-creator on FreeNode IRC outside Central European sleeping time) is usually the best place to discuss this stuff. Not many of the developers read the Forum, in fact there there is probably exactly one who does it on a regular base.

Log in to reply