GNU TeXmacsUser manualReferenceAboutEnglish
Editing programs with the syntax tree

1.Where the editing asks the 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.

2.Brackets, quotes and closing keywords

3.Indentation

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).

4.Moves, structural edits and comments

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.

5.Outline, folds, diagnostics and completion

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.

6.Inputs of sessions

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.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".