![]() |
Editing programs with the syntax tree | ![]() |
The editing of programs by the tree is in
ts-edit.scm
. It only uses the glue, so that the editing modules load it without the generator. They load it through
code-syntax.scm
, never directly: in a build with tree-sitter this module uses
ts-edit.scm
, otherwise
code-syntax-none.scm
, whose functions answer that the code has no tree. The callers are
prog-edit.scm
, the indentation of
python-edit.scm
,
java-edit.scm
and
scheme-edit.scm
, and Return in
session-edit.scm
and
program-edit.scm
.
A function of the tree works where tree-sitter colours the code: syntax-cursor-language gives, in program mode, the language of tree-sitter of the prog-language at the cursor (tree-sitter-program-language), which exists once the highlighter of the language is installed. Everywhere else the functions give #f, and the callers, which test it first ((:require (syntax-cursor-language)) or or), keep their behaviour without the tree. The code at the cursor is a region: the document of the line of the cursor, or the string itself when it is not a line of a document, with the point (row column) of the cursor in it.
Selection: syntax-select-enlarge (for program-select-enlarge and kbd-select-enlarge) selects the smallest node around the selection which is larger than it.
Matching: syntax-matching-bracket matches a bracket token with the bracket at the other end of its parent node, so that brackets in strings and comments, which are not tokens of the tree, match nothing; syntax-select-brackets shows the pair as the alternative selection brackets.
Balance (preference prog:balanced brackets, on by default): bracket-open and bracket-close of
prog-edit.scm
are redefined under the condition of a tree. An opening bracket or quote comes with its closing one (alone before a word; around the selection when there is one), a closing bracket is typed over the matching one in front of it, Backspace or Delete on one of an empty pair deletes both and on one of a pair with content selects the pair. In strings and comments (nodes whose types contain string, comment, char, heredoc or regex) they are plain characters, but the closing quote of a string is typed over. Pasted text is left as it is.
Closing keywords: after Return (insert-return), syntax-close-block tries the keywords which close blocks in the grammar (closers.scm: end, fi, done…) and, in LaTeX, the \end of the last \begin of the previous line. Each candidate is tried on a copy of the tree (syntax-region-errors-with), and the first one which leaves fewer errors is inserted on the next line: error recovery alone does not tell, since some grammars give a MISSING closer and others an ERROR. In HTML, the > of a start tag inserts its end tag (syntax-close-tag), but for void elements and elements which have their end tag. Like the balance of the brackets, both depend on the preference prog:balanced brackets (prog-balanced-brackets?).
syntax-compute-indentation, which the definitions of program-compute-indentation ask first, uses the query indents.scm of the grammar when there is one (syntax-region-indent, the rules of nvim-treesitter: @indent.begin, .end, .branch, .dedent, .align, .auto, .ignore, .zero, with their properties). The query of Scheme is generated from the keywords of
tm-mode.el
(
gen-scheme-indents.py
): every list is @indent.lisp, and lisp_indent applies on the tree the rules which
scheme-edit.scm
applies on the characters (aligned with the first argument on the line of the last one, or the column of the operator plus 3 for the first arguments of a keyword and plus 1 for its body), which the captures of nvim-treesitter cannot say. The grammars without such a query have rules in syntax-indent-rules: YAML and LaTeX (indenting node types, branches, the brackets of an offside language), Lean, and a rule of layout for Haskell. Columns are counted as the core counts them (code_column: an entity is one column, a tab goes to its stop).
The structured moves of TeXmacs in program code (traverse-left, traverse-right, traverse-up, traverse-down) go from node to node, to the node around and into it (syntax-move-node, through syntax-region-node-move), and traverse-next and traverse-previous from definition to definition (syntax-move-definition). The structural edits take their ranges from syntax-region-node-info: delete the node at the cursor or the one which ends there (syntax-delete-node, also for the structured removals), swap it with a neighbour, raise it, remove the brackets around the cursor (syntax-splice-brackets, for program-remove-structure), wrap it in brackets, comment or uncomment it (line comments for whole lines, else the block comment of the language). They rewrite the lines of the region as strings, so that the brackets stay balanced. code-syntax-menu adds these commands to the Focus menu of program code. syntax-toggle-comment comments the lines of the selection, with the line comment of the language or, without one, a block comment per line.
The definitions of a region (syntax-outline) are the captures @definition.<kind> of its tags.scm, each with the first @name inside it, as tree-sitter-tags reads them; a definition of the same kind and name just after another one (the equations of a Haskell function) is not repeated. Going to the definition of the name at the cursor stays in the same code.
A fold of the tree hides the lines of a node of several lines after its first one, but its last line when it holds only the closing token of the node. The folds themselves are those of the core (
code-fold.scm
, through code-fold-lines and code-unfold-lines), which the language servers fold too; the tree only gives the blocks: those around the cursor, and the definitions of several lines, or the blocks at the top level in a language without tags.scm.
ts-code.scm
registers these as providers of the features of program code, under the name 'syntax and the rank 20 (after the servers, rank 10, which see the other files of a project), available where syntax-cursor-language is: 'fold-ranges, 'fold-all-ranges, 'outline, 'definition, 'syntax-errors (the ERROR and MISSING nodes as items of
code-diagnostics.scm
, an empty node covering one character), 'completion-context ('string, 'comment or 'code, from the node before the cursor) and 'local-names (the names of the definitions). How the features choose among their providers is described in the features of program code.
In a session and in a program field, Return evaluates the input unless syntax-input-incomplete? says that only more lines can complete it: an error or a missing node reaches the end of the text (an open bracket), or a named node at the end of the text is empty (syntax-region-last-nodes: Python accepts def f(x): at the end of a text, with an empty block). Return then opens a new line. Other errors go to the interpreter, which reports them in its own terms.