Overview
Qt-TreeSitterHighlighter is a tree-sitter based syntax highlighter library for Qt. It can be used with any QTextDocument, in particular with QTextEdit and QPlainTextEdit, and rehighlights the source code immediately whenever it changes, e.g. when typing.
To use Qt-TreeSitterHighlighter, it must be provided with:
- A tree-sitter language to parse the source and construct a syntax tree.
- A tree-sitter highlighting query string which extracts the relevant captures from the syntax tree.
- A highlighting format map which maps capture names to their desired QTextCharFormat format.
The highlighter performs the following steps:
- The QTextDocuments text is parsed to create a syntax tree.
- The highlighting query is applied to the syntax tree to extract highlighting captures.
- The text of each capture is highlighted according to its format specified in the format map.
When the QTextDocuments text is updated, e.g. by typing, the steps above are applied incrementally.