TreeSitterHighlighter
class TreeSitterHighlighter: public QObject
TreeSitterHighlighter is a syntax highlighter for QTextDocument based on tree-sitter.
For functional highlighting it must be provided with the following:
- 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 can be applied to any QTextDocument, in particular to the documents of QPlainTextEdit and QTextEdit and will rehighlight the document whenever it changes.
Variables
Name | Description |
---|---|
language | The highlighters language. |
parser | The highlighters parser. |
tree | The current syntax tree of the document. |
query | The highlight query. |
format_map | The highlight format map. |
document | The document to which the highlighter is connected to. |
Functions
Name | Description |
---|---|
TreeSitterHighlighter | Constructor. |
TreeSitterHighlighter | Constructor. |
~TreeSitterHighlighter | Destructor. |
set_document | Connects the syntax highlighter to the given QTextDocument. |
get_document | Return the QTextDocument to which this syntax highlighter is connected. |
set_query | Set the query from a query pattern string to use for syntax highlighting. |
set_format_map | Set the format map. |
get_format_map | Return the highlighters format map. |
reparse | Reparse the whole document. |
rehighlight | Apply syntax highlighting to the whole document. |
clear | Clear the highlighting of the whole document. |
Variable Details
language
const TSLanguage* language
The highlighters language.
parser
TSParser* parser
The highlighters parser.
tree
TSTree* tree
The current syntax tree of the document.
query
TSQuery* query
The highlight query.
format_map
FormatMap format_map
The highlight format map.
document
QPointer<QTextDocument> document
The document to which the highlighter is connected to.
Function Details
TreeSitterHighlighter
TreeSitterHighlighter(const TSLanguage* language, QTextDocument* document)
Constructor. Constructs a minimal TreeSitterHighlighter.
-
language
- A tree-sitter language.
-
document
- A QTextDocument to which the highlighter will be connected and which becomes its parent.
TreeSitterHighlighter(const TSLanguage* language, std::string query_str, FormatMap format_map, QTextDocument* document)
Constructor. Constructs a fully functional TreeSitterHighlighter and connects it to document.
-
language
- A tree-sitter language.
-
query_string
- A tree-sitter highlighting query string.
-
format_map
- A map of query capture names to their QTextCharFormat format.
-
document
- A QTextDocument document to which the highlighter will be connected and which becomes its parent.
~TreeSitterHighlighter
~TreeSitterHighlighter()
Destructor. Disconnects the syntax highlighter from its document.
set_document
void set_document(QTextDocument* document)
Connects the syntax highlighter to the given [QTextDocument](https://doc.qt.io/qt-6/qtextdocument.html). This will disconnect it from the previous document.
-
document
- The QTextDocument document to connect to.
get_document
const QTextDocument* get_document()
Return the [QTextDocument](https://doc.qt.io/qt-6/qtextdocument.html) to which this syntax highlighter is connected.
- Return
- The highlighters document.
set_query
bool set_query(std::string query_string)
Set the query from a [query pattern string](https://tree-sitter.github.io/tree-sitter/using-parsers#query-syntax) to use for syntax highlighting.
-
query_string
- A highlighting query string.
- Return
- Indicates if the query could be created successfully.
set_format_map
void set_format_map(FormatMap format_map)
Set the format map.
-
format_map
- A map of capture names to their desired QTextCharFormat format.
get_format_map
const FormatMap get_format_map()
Return the highlighters format map.
- Return
- The highlighters frmat map.
reparse
void reparse()
Reparse the whole document.
rehighlight
void rehighlight()
Apply syntax highlighting to the whole document.
clear
void clear()
Clear the highlighting of the whole document.