![]() |
The features of program code: syntax trees and language servers | ![]() |
Two features know program code: tree-sitter (the syntax of a piece of code, at once, in source files, code blocks and sessions) and the language servers (the meaning of the code, over a project, after a while, in source files and, when the user asks for it, in the code blocks of documents). Either may be left out of a build (--disable-tree-sitter, --disable-lsp, in
tm_code.m4
; USE_TREE_SITTER, USE_LSP in CMake). What both use is in the core, which names neither: the features register themselves. The design, its phases and the comparison of the two are in
src/docs/code-features.md
.
code_lines.cpp
keeps the state of a line which is not in the document on the highlight observers of the line, which an edit of the line detaches (so that an edited line forgets it):
its folds: the number of the folds which hide it (code_line_fold_state, code-line-fold-state); the bridge of the line does not show a hidden line, but keeps its boxes for the cursor (bridge_rep::fold_line), except when the document is printed;
its layers: colours of pieces of the line and hints before some of its characters (code_set_line_layers, code-set-line-layers), which typeset_layered_string (
concat_text.cpp
) draws over the colours of the language of the line;
positions: the column of an offset, a tab <#9> going to its stop (code_column), the UTF-16 units of the servers and back (code_utf16_column, code_utf16_offset), the end of the indentation (code_indent_end).
code_regions.cpp
gives the changes of a region (a document of lines): the common first and last lines of two versions (code_lines_common, which the incremental parse of tree-sitter uses), and snapshots of the lines as a file has them, which give the change since them as a range of positions and its text (code-snapshot-new, code-snapshot-change), by which the language servers get the changes of a buffer.
A feature which is not in all builds registers itself in the core: tree-sitter wraps the program languages (set_prog_language_wrapper,
language.hpp
), the language servers process their messages at each turn of the loop of the server (add_link_processor,
tm_link.hpp
). Their glue is in
build-glue-treesit.scm
and
build-glue-lsp.scm
, compiled only with them (
glue.cpp
).
code-features.scm
has the table of the features of program code and of their providers (code-provider-set!: a feature, a name, a rank, whether it is available at the cursor, a procedure). code-ask asks the providers of a feature by rank, the next one when one answers #f; the preference code:<feature> chooses one. The providers are registered by the adapters
ts-code.scm
(rank 20, 'syntax) and
lsp-code.scm
(rank 10, 'server), which are loaded when their feature is part of the build (its glue). Without tree-sitter, its functions which the editing of programs calls answer that the code has no syntax tree (
code-syntax-none.scm
): the modules which call them load
code-syntax.scm
, which loads
ts-edit.scm
or those stubs. code-ask goes to the next provider when one answers #f or '(): a server which has no block at the cursor lets tree-sitter fold.
code-fold.scm
The folds (std ( and std )): the blocks ('fold-ranges, 'fold-all-ranges) from the server when one follows the file, else from the syntax tree; the cursor (code-cursor-in-fold, called by
edit_cursor.cpp
with the last place of the cursor of its editor outside of the folds): the arrows go over a fold, a jump opens the folds around, from the innermost. A fold nests in another one (at most 9 deep: the states of the lines); the first line of a fold ends with a mark, which the bridge of the line adds when the next line is hidden.
code-navigate.scm
The outline ('outline, std O, a page which follows the buffer) and the definitions ('definition, F12).
code-diagnostics.scm
The diagnostics of a buffer by source (the server; the syntax errors of tree-sitter, 'syntax-errors, where no server follows the code), underlined (the alternative selections code-errors, code-warnings, code-infos) and visited by F8.
code-complete.scm
Where the cursor is ('completion-context: no completion of a server in a string or a comment) and the names of the code ('local-names, which tab completes where no server follows the code).
code-menu.scm
The menu Focus of program code: the entries of the features which a provider serves at the cursor, the diagnostics, then those which only a server gives (code-server-menu, from lsp-code.scm) and those of the syntax tree (code-syntax-menu, from ts-edit.scm), and those of a session or a program input.
The language servers see the code blocks of a document (when the preference lsp:code blocks is on) as files which are never written, each block one file, or the blocks of a language together (a unit, the default):
lsp-blocks.scm
. The functions of the buffer of the servers, called without a buffer, are those of the target, the code at the cursor: the lines of a source file, of the block or of its unit (lsp-target-root, and lsp-target-path, lsp-target-lo, between the lines and the places of the document); the features of program code are given at the cursor in the same way in all. The diagnostics of all the blocks of a document are those of the source 'server of
code-diagnostics.scm
, each at the place of its block.
A line of program code is typeset as tokens (typeset_prog_string,
concat_text.cpp
), which the language of the line gives (advance, get_color). Where tree-sitter colours a line, they are the leaves of its syntax tree, cut at the spaces (highlighting); elsewhere, those of the language of TeXmacs (syntax highlighting). The layers of the servers are drawn over either.
The suite code (
code-test.scm
) tests the core with providers of its own, in every build; the suites treesit and lsp run when their feature is there.