Qt-TreeSitterHighlighter is a syntax-highlighter for Qt that can be used on QTextEdit, QPlainTextEdit or any other QTextDocument and rehighlights it on every change of the document. It makes use of tree-sitter, an incremental parsing library to build a syntax tree of the document source code for accurate and fast highlighting.
To use Qt-TreeSitterHighlighter, three configuration elements have to be provided:
- A tree-sitter language to parse the source and construct a syntax tree. Most often one of the readily available parsers can be used.
- A tree-sitter highlighting query string to extract the relevant captures from the syntax tree. Most available parsers also come with a highlight query.
- A highlighting format map to map capture names to their desired QTextCharFormat format.
An example on how to use Qt-TreeSitterHighlighter can be found in ./examples/main.cpp.
- C++11
- Qt6
- tree-sitter
To build the example the tree-sitter-cpp parser is needed as well.
tree-sitter
and tree-sitter-cpp
are automatically included if the repository is cloned with the --recurse-submodules
flag.
cd Qt-TreeSitterHighlighter
cmake . -B build
cmake --build build
Run the example to see see Qt-TreeSitterHighlighter in action:
./build/tree-sitter-highlighter-example
Contributions are very welcome!
Qt-TreeSitterHighlighter is licensed under the MIT License.