Generic Highlighter in Qt Creator

As you know Qt Creator is primarily focused on C++ and QML development. On the other hand we understand that many projects are comprised of a wide variety of technologies and file types. Therefore it would be nice if we could provide at least basic syntax highlighting for editing Python, Perl, XML, and others. So that is exactly what we did!

Qt Creator's highlighter settings

What we call the generic highlighter is an engine that parses a document which contains information about a particular language structure and then applies the necessary formatting to the text. Our implementation is based on the nice Kate highlight definition specification, which consists of an XML containing, among other things, the keywords of the language, contexts and rules that are used to match blocks of text, and the meaning of such matches. Direct benefits of this choice is that many Linux users might already have the Kate definitions in their system and the specification is straightforward to understand so users can easily write their own.

The screen shot  on the left depicts how the generic highlighter settings looks in our master branch. There is a primary location for the syntax definitions, which can be downloaded from a Kate repository by clicking the Manage button. If you suspect that they might already be present in your system just mark the Use fallback location option and try to Autodetect them. In the case nothing is found, you can always Browse (this is useful even for Windows users who want to play around with two sets of definitions). If the Alert option is checked and Qt Creator cannot configure a proper highlighter for some file you open, it will display an information bar on top of the editor asking whether you would like to try to find a compatible syntax definition (unless that file is matched by any Ignored pattern).

In the best case Qt Creator will automatically detect the definitions available in your system and once a Python file is opened, for example, you should see a colored editor like in the next screenshot.

Qt Creator displaying a Python file using the Generic Highlighter

Before getting your hands into it there are few things you should be aware. We have special highlighters and extra features (code completion, indentation, pop-ups, etc.) for C++, QML and other file types. The generic highlighter is then triggered only when one of these specific editors is not a better match. Therefore this imposes some limitations on how the MIME types from the Kate definitions are treated internally. Basically, this means they have lower priority.

Furthermore, it should be noticed that the color scheme of Qt Creator is different from Kate's. Both regarding the colors themselves and the predefined styles available. For instance, Kate allows the distinction among a decimal, a floating point, and a base-N while currently in Qt Creator, although the engine itself correctly identifies such cases, only a general color configuration for numbers is provided. So do not expect to see the very same highlighting pattern in Kate and Qt Creator.

The last thing I would like to mention is that currently we only support basic indentation. Any value specified in the definition is simply ignored. We are still thinking on a good way to handle that.

The generic highlighter will be available in the next Qt Creator version (2.1). But you can already enjoy it if you are brave!


Blog Topics:

Comments