![]() |
The syntax trees of code regions | ![]() |
A region is the unit which gets a syntax tree of its own: a document whose children are the lines of a program typeset in one program language (a source file, the body of a code block, a session input), or a single string (inline code). This is the shape which the features of program code call a document of lines; the syntax trees are derived data, never saved.
syntax_region.cpp
keeps one syntax_region_rep per region:
The tree of the region, which identifies it (strong_equal: the same tree, not an equal one), and the index of its language in the C runtime.
The lines as they were at the last parse (TeXmacs strings, shared, so that the copy is cheap), and the offset of the start of each line in the text of the region, a virtual newline following each line.
The TSParser and the TSTree, with the numbers of full and incremental parses (syntax-region-statistics) and a version which changes at each parse.
The colour codes of each position of each line, and the lines whose colours are out of date (see highlighting), for the query, the injections and the epoch in hl_query, hl_injections, hl_epoch.
The captures of the query of indentation, for ind_query and ind_version.
The trees of the parts in other languages (injections), kept and edited with the region.
A line which is not a string (markup inside code) is given to the parser as its flattened text, the strings of its leaves one after the other.
No copy of the text is made for the parser: region_read gives tree-sitter the rest of a line from the requested byte, or the virtual newline. The lines are in the TeXmacs encoding (Cork bytes, and entities such as <less>, <alpha> or <#3B1>), which is neither UTF-8 nor a sequence of code points. The input therefore uses TSInputEncodingCustom with the decode function texmacs_decode: one Cork byte, or one entity (from < to the next >, at most 64 bytes, no < nor newline inside), gives one code point and the number of bytes it took. Cork bytes above 127 are mapped through cork_to_utf8 once (init_cork_table), entities through a table filled on first use; an unknown entity is U+FFFD.
The consequence is that the byte offsets of tree-sitter are offsets in the TeXmacs strings: a point (row column) of a node is the line and the position in its string, with no conversion either way, and the lexers still see real characters (<alpha> is a letter for the identifiers of Julia). The predicates of the queries, however, compare the text of the region as it is: a query must write "<less>" where it means "<" (the Scheme query of TeXmacs does).
There is no observer: at each request (get_region) the lines of the root are compared with those of the last parse. code_lines_common (
code_regions.cpp
, which the language servers use too) gives the common first and last lines; between them, the common prefix and suffix of the changed text narrow the change to one range. That range becomes one ts_tree_edit (start, old and new end, as bytes and as points), then the parse starts from the edited tree, and ts_tree_get_changed_ranges between the old and the new tree tells which other lines changed in structure (a comment opened above them). Comparing the lines costs one comparison of addresses per line and request (the strings of the lines which did not change are those of the document), and covers every kind of change in the same way: typing, splitting and joining lines, undo, remote edits, a whole line assigned. When the number of the lines stays (a keystroke), the lines, their offsets, the text and the colours of the region are changed in place (update_lines).
An incremental parse of tree-sitter costs more as the nodes at the top level are more (13 ms for 100000 lines of C, after one character). A region of more than 6000 lines is therefore cut into parts of about 2000 lines (syntax_group_rep): each part is a region of its own over a slice of the lines, cut where a node at the top level of the parse of the whole region starts (after a node without error), so that an edit parses its part again only (about 1 ms for a part of C). The requests go to the part of their line, their lines made local to it and those of their answers global again (row_base); those about the whole region (the errors, the captures of the outline, the colours) gather the answers of all the parts. An edit which leaves an error in the last node of a part (an unclosed comment) joins it to the next one, whose text may now be in the comment; an edit over several parts joins them; a part longer than three parts is cut again when it ends without error. The parts do not see each other: a construct which goes over a cut (none at the top level, by its choice) would be parsed in two pieces until the parts are joined.
The regions are kept in a list, the most recently used first, up to max_regions (256) regions and max_region_bytes (32 MB) of text in all; the least recently used is dropped first. Without that cache a document with many code blocks, edited in turn, would parse each block again at every edit. A region whose root is asked for with another language is parsed again from scratch. syntax-region-forget drops a region at once (the tests do).
The injected parts follow the region: an edit inside a part is made on its tree, which is parsed again incrementally; an edit before a part moves it; an edit across its bounds drops its tree. At most 64 trees of parts are kept per region.
No node object crosses into Scheme: a tree changes at each keystroke. The glue takes the root of a region and the index of its language, and gives plain data, the points being lists (row column):
The tree as the S-expression of ts_node_string, for the tests.
The nodes around a point or a range, innermost first, each as (type named? start end field).
The ERROR and MISSING nodes, as ("error" start end) or ("missing" start end (pstart pend)), with the range of the parent of the missing node (the string of a missing closing quote, the statement of a missing ;).
The root, its last child, the last child of that one…, outermost first (where the text ends).
The captures of a query, in the order of the text, after the predicates: (capture start end), capture being an index into tree-sitter-query-capture-names.
The indentation of a line by the query indents.scm (syntax_region_indent, a port of get_indent of nvim-treesitter, with the Lisp rules of TeXmacs for Scheme); -1 for @indent.auto, -2 without a tree.
The number of errors if a text were inserted at a point: the edit and an incremental parse of a copy of the tree.
The targets of the structured moves, and the ranges which the structural edits need (the node at the point, its siblings, its parent, the brackets around, the node which ends there).
The numbers of full and incremental parses; dropping a region.
Each of these brings the region up to date first, so that several requests after an edit parse once.