Applications โ
What you'll learn โ
- How the primitives compose into syntax highlighting, navigation, indexing, and editor updates.
Syntax highlighting โ
Walk with a cursor, classify named nodes by type, and emit spans:
var cursor = tree.cursor();
defer cursor.deinit();
// gotoFirstChild / gotoNextSibling over the tree;
// switch on node.nodeType(): "identifier" โ variable color,
// "number" โ constant color, operators โ punctuation color.Changed ranges limit repainting to dirty spans after each keystroke.
Code navigation โ
gotoDescendant(offset) jumps to the node under a click; parent() climbs to the enclosing construct; childByFieldName("left") pulls out operands. Queries like (identifier) @id build document-symbol lists in one pass.
Source indexing and structural search โ
Compile project-wide queries once ((identifier) @name), execute per file with a reused QueryCursor, and record (file, node.range(), text) triples. Because matching is structural, renames and reformatting do not break the index the way regex would.
Editor integration โ
The incremental loop from Incremental is the whole protocol: keystroke โ InputEdit โ parse(old, edit, text) โ getChangedRanges โ repaint/retokenize only those spans. Parser and cursor reuse keep per-keystroke work proportional to the edit, not the file.
Related guides โ
- Use Cases for full problem โ architecture โ example walkthroughs.
- Performance for budgets.
