-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
98 lines (77 loc) · 2.84 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <QtGui>
#include <QtWidgets>
#include <string>
#include <TreeSitterHighlighter.h>
// Declare the 'tree_sitter_cpp' function provided by the 'tree-sitter-cpp' parser library.
extern "C" {
const TSLanguage* tree_sitter_cpp(void);
}
// A minimal highlights query for C++
// See https://tree-sitter.github.io/tree-sitter/using-parsers#query-syntax for query syntax.
std::string query = R"(
["extern" "catch" "class" "delete" "namespace" "new" "private" "protected" "public"
"throw" "try" "requires" "virtual" "break" "case" "const" "continue" "do" "else"
"enum" "for" "if" "inline" "return" "static" "struct" "switch" "typedef" "while" ] @keyword
["#define" "#elif" "#else" "#endif" "#if" "#ifdef" "#ifndef" "#include" ] @preproc
(preproc_directive) @preproc
(number_literal) @number
(true) @bool
(false) @bool
(string_literal) @string.std
(raw_string_literal) @string.raw
(type_identifier) @type
(primitive_type) @type
(sized_type_specifier) @type
(field_identifier) @variable
(identifier) @variable
(call_expression
function: (identifier) @function)
(call_expression
function: (field_expression
field: (field_identifier) @function))
(function_declarator
declarator: (identifier) @function)
(comment) @comment
)";
// A format map from capture names to the appropriate QTextCharFormat format
FormatMap format_map() {
FormatMap map;
map[""].setForeground(QColor("black"));
map["keyword"].setForeground(QColor("#b055cc"));
map["preproc"].setForeground(QColor("purple"));
map["number"].setFontUnderline(true);
map["bool"].setFontWeight(QFont::Bold);
map["string"].setForeground(QColor("green"));
map["string.raw"].setForeground(QColor("dark green"));
map["type"].setForeground(QColor("#c08000"));
map["variable"].setForeground(QColor("#ad1d4d"));
map["function"].setForeground(QColor("blue"));
map["comment"].setForeground(QColor("gray"));
map["comment"].setFontItalic(true);
return map;
}
int main(int argc, char* argv[]) {
QApplication app(argc, argv);
QMainWindow window = QMainWindow();
// Create the editor
QPlainTextEdit* editor = new QPlainTextEdit();
// Create the highlighter and use it on the editors document
TreeSitterHighlighter* highlighter = new TreeSitterHighlighter(tree_sitter_cpp(), query, format_map(), editor->document());
// Set font
QFont font;
font.setFamily("Monospace");
font.setFixedPitch(true);
font.setPointSize(12);
editor->setFont(font);
// Open this file in in the QPlainTextEdit as an example
QFile file(__FILE__);
if (file.open(QFile::ReadOnly | QFile::Text)) {
editor->setPlainText(file.readAll());
}
// Setup the main window
window.setCentralWidget(editor);
window.setWindowTitle("Tree-Sitter Syntax Hihglighter");
window.resize(800, 600);
window.show();
return app.exec();
}