Skip to content
mgubi edited this page Nov 3, 2026 · 2 revisions

Code features: tree-sitter and language servers

How TeXmacs Upsilon edits program code: syntax trees with tree-sitter, and the meaning of a project with language servers (LSP), as one mechanism. This page sums up the design decisions and lists what is open for discussion.

The full design is in two notes of the developers, on the branch upsilon_texmacs:

  • src/docs/code-features.md: the mechanism the two features share;
  • src/docs/tree-sitter.md: tree-sitter in TeXmacs, its survey and design.

User and developer pages in TeXmacs: Help > "Editing program code", and the developer page "The features of program code" (devel/source/code-features.en.tm).

Where the work is

  • wip_ts: tree-sitter (the generator in Scheme and C++, a C runtime in Plugins/TreeSitter, the clients in progs/treesit).
  • wip_lsp: the client of language servers (System/Link/lsp_link.cpp, progs/lsp).
  • wip_code: the two together, with the mechanism they share (progs/code, System/Language/code_*.cpp).

wip_code is contained in upsilon_texmacs in substance: its few later commits (the move of the docs, the ts_tree member, the fixes of the Windows and CRLF tests) are on Upsilon under other hashes, and Upsilon has moved on from there.

What each one brings

tree-sitter language servers
knows the syntax of one text the meaning over a project
time synchronous, a few ms per edit asynchronous, when the server answers
where source files, code blocks of documents, session inputs, inline code source files, and (opt-in) code blocks
needs a grammar (22 shipped) a server installed on the system
only it moves, selection, brackets, structural edits, indentation, session input, comments diagnostics, completion, hover, signature, references, rename, fixes, formatting, symbols, calls, inlay hints

Design decisions

The shared mechanism

  1. Tree-sitter does what has to happen at each key; a server refines it when it runs. Syntax and immediate actions (colours, brackets, moves, selection, indentation, session input) come from tree-sitter; meaning and project-wide actions (diagnostics, completion, definitions across files, rename, formatting) come from the server. Outline and folds come from the server when one follows the file, else from tree-sitter.
  2. The core defines the mechanism and a default which does nothing; a feature registers itself; the core never calls a feature by name. Five core pieces: source regions (the change since the last request), positions (Cork offsets, code points, UTF-16, UTF-8, visual columns with tabs), line state (layers of colours, hints and fold state kept on the line, dropped when the line is edited), folds, and a table of features and providers in Scheme (code-provider-set!, the code-* commands asking the providers by rank).
  3. Graceful downgrade. Each feature can be left out: --disable-tree-sitter, --disable-lsp. A build with neither is plain TeXmacs. The browser build has tree-sitter but no servers. A suite code with stub providers runs in every build.
  4. One fold mechanism: the boxes of hidden lines are kept, clipped, so that the cursor always has a box; the arrows go over a fold, and a jump into it opens it.
  5. Each feature keeps its own snapshot of a region, at its own pace: what is shared is the computation of the change, not the moment. Tree-sitter follows a region at each highlight, a server after a pause.
  6. Errors from both, each as it is: unmatched brackets in red at once (tree-sitter); the diagnostics of the server when they come; other syntax errors of tree-sitter are shown as diagnostics only where no server follows the code.
  7. A preference per feature (code:<feature>: auto, syntax, server, off).

Tree-sitter

  1. Additive: a language without a grammar, or a build without tree-sitter, keeps the former tokenizers. No highlighter was removed.
  2. Same documents: no change of the document format, the tags or the syntax:<lan>:<class> preferences. The syntax tree is derived data, never saved.
  3. One service, several clients: highlighting is the first; selection, brackets, indentation, outline and Scheme tools use the same tree.
  4. The parse tables are generated from grammar.json in Scheme and C++ (Plugins/TreeSitter/ts_tables.cpp; the Scheme code is the reference, with byte-identical output), and run by the vendored C runtime of tree-sitter (the "hybrid"): no C generated per grammar and no build step, at the speed of C (the parser.c of tree-sitter parsed in 6 ms, against 2.2 s for the parser in Scheme). Queries (highlights.scm, tags.scm) run in the C engine.
  5. The first languages were json, c and scheme (for TeXmacs's own code, with queries of our own); 22 grammars are shipped now.
  6. The tables are cached in the home folder, and now also shipped, so that opening a file does not wait for their generation.
  7. Long regions are parsed by parts (about 2000 lines, cut at nodes of the top level), so that the colouring after a key in a file of 100 thousand lines costs about 3 ms (a whole key costs about 15-25 ms, mostly typesetting).
  8. The typesetting cuts a line at the leaves of the syntax tree where tree-sitter colours it, instead of calling the tokenizer of the language.

Language servers

  1. Positions in UTF-16 units, converted by the shared position service.
  2. Incremental didChange from a snapshot, not the whole text at each sync.
  3. The servers of a language are in the table of program languages (servers, lsp-id), replacing the former defaults.
  4. Code blocks of documents can be given to a server as virtual files next to the document (never written), opt-in (lsp:code blocks, off by default, since a server is a process). By default the blocks of one language of a document are joined into one virtual file (lsp:join code blocks), so that a name defined in one block is known in the next.

Open questions and discussion

  • Feature branches and the shared code. The shared mechanism lives on wip_code and Upsilon; wip_ts and wip_lsp were kept as feature branches, with only the fixes of the Windows tests and the move of the docs added. Should they be rebased on it, or retired now that Upsilon has both?
  • Grammars added by users. Shared libraries loaded at run time (as Emacs, Neovim and Helix do) were planned for later; impossible in the browser build. Or users could add a grammar.json, which the hybrid can turn into tables, plus an entry in the table of program languages and the port of its scanner if it has one.
  • External scanners are settled for the shipped grammars: all 17 which have one are ported to Scheme (progs/treesit/ts-scanner-*.scm). The open point is the cost of a new port (YAML 266 ms for 180 KB, the scanner of Haskell 3471 lines of C).
  • Query quality. Upstream highlights.scm files are uneven; we will maintain our own for the main languages. Which ones?
  • Markup inside code. TeXmacs lets any markup appear in a line of code (literate documents). A line with markup is parsed as its flattened text and coloured on its plain strings only. Is that enough?
  • Not done yet: selectionRange and documentSymbol from a server for code without a grammar (no language needs them now: every language with a server has a grammar); the cost which still grows with the size of a file (comparing the lines of a region, the reuse checks of the typesetter, the changed rectangles).
  • The browser build: which grammars to ship, given their size, and whether to load them lazily.
  • Servers in a document: the cost of a server process for a document with a small block (the colours and inlay hints of a server in code blocks are done).

Add your comments below each point, or start a new section.

Clone this wiki locally