![]() |
The client of language servers | ![]() |
TeXmacs is a client of the Language Server Protocol (LSP): for the source files which it edits, it starts the language servers which other editors use (clangd, pylsp, jdtls…), gives them the text of the files, underlines the diagnostics which they publish, and asks them for completions, information at the cursor and definitions. It also asks them for the signatures of functions, references, the outline of a file, the symbols of a project, call hierarchies, highlights of a symbol, folding ranges, semantic colours and inlay hints, and applies their edits (formatting, renames, quick fixes). The user level description is language servers for source code.
A server is a program which reads and writes JSON-RPC messages on its standard input and output, each message preceded by a header with its length. TeXmacs keeps the transport in C++ and the protocol in Scheme:
A C++ link (
lsp_link.cpp
) frames the messages over the pipe of the server, and gives the whole messages, one by one, to a Scheme call back, from the interpose handler of the server (the event loop).
The Scheme modules of
progs/lsp
build and read the messages, keep a client per language and project, send the requests and notifications, answer the requests of the servers, and keep the documents and the diagnostics.
The module
lsp-edit.scm
follows the buffers through two hooks which the editor calls (notify-buffer-changed, notify-buffer-closed), shows the diagnostics as alternative selections which the editor underlines, and implements the commands of Tools\(\rightarrow\)Language servers and their keys.
A change of a followed source file thus goes as follows:
apply_changes (THE_TREE) --> notify-buffer-changed name
--> lsp-follow-buffer --(idle 300 ms)--> lsp-sync-pending
--> lsp-buffer-sync --> lsp-document-change-lines
--> code-snapshot-change (the lines which differ, in C++)
--> textDocument/didChange (the part which differs, or the whole text)
--> lsp-send --> "Content-Length: n\r\n\r\n{…}" on the pipe
server --> pipe --> lsp_link_rep::drain --> process_all_lsp (a link processor)
--> call back "message" --> lsp-client-receive
--> textDocument/publishDiagnostics --> lsp-diagnostics-changed
--> code-set-diagnostics 'server … --> update_diagnostic_rects |
lsp_link.hpp,
lsp_link.cpp
The LSP link (framing of the messages over another link) and the clients of the Scheme programs: lsp_start, lsp_send, lsp_listen, lsp_alive, lsp_stop and process_all_lsp; lsp_document_text, the text of a source document for its server.
tm_link.hpp,
pipe_link.cpp,
QTMPipeLink.cpp
The writes which do not block (set_blocking_write), for the pipes of the servers.
build-glue-lsp.scm
The glue routines lsp-start, lsp-send, lsp-listen, lsp-alive?, lsp-stop and lsp-document-text (in a build with the servers only).
tm_server.cpp,
tm_link.cpp
The interpose handler, which calls the processors of the links (process_all_link_processors), process_all_lsp among them.
lsp-base.scm
Messages as Scheme trees, texts and positions, URIs of files.
lsp-client.scm
The registry of the servers, the clients and their life, requests and notifications, what the servers send.
lsp-documents.scm
The documents of the servers, the diagnostics, the buffers and their projects; the target of the commands, the code at the cursor (the lines of a source file, or of a code block: lsp-target-root, lsp-target-base).
lsp-blocks.scm
The code blocks of documents as documents of the servers (preference lsp:code blocks): the blocks of a body, the block at the cursor, their virtual files, which are never written, and the units (the blocks of a language as one file, lsp:join code blocks).
lsp-edit.scm
The servers in the editor: following the buffers, showing the diagnostics, completion, information at the cursor, definitions, the log page.
lsp-actions.scm
The edits of the servers: formatting, renames, quick fixes.
lsp-navigate.scm
The page of the references of a symbol, the provider of the outline of a file (the page is that of the core, code-navigate.scm), the symbols of a project (workspace/symbol, ⌘⇧P), the call hierarchy (⌘⇧H), and the highlights of the symbol at the cursor (textDocument/documentHighlight, the alternative selection lsp-highlights).
lsp-fold.scm
The blocks of the server for the folds of the core (⌘( and ⌘), the features of program code).
lsp-code.scm
The adapter of the server for the features of program code: its folds, its outline, its definitions and whether it gives the diagnostics.
lsp-decorate.scm,
code_lines.cpp,
concat_text.cpp
The colours of semantic tokens and the inlay hints: asked for in Scheme, kept as the layers of the lines in C++, typeset by typeset_layered_string.
lsp-kbd.scm
The mode in-lsp%, the keys, the menu lsp-menu and the focus bar.
init-texmacs.scm,
tools-menu.scm
The lazy loading of the modules, and the submenu Tools\(\rightarrow\)Language servers.
edit_interface.cpp,
edit_repaint.cpp
The hook notify-buffer-changed and the underlines of the diagnostics.
edit_mouse.cpp
The point under the mouse as a cursor position (xy->path), for the hovers at the mouse.
json.cpp
The JSON converter which reads and writes the messages.
new_buffer.cpp
The hook notify-buffer-closed, and the font of a source file which is opened (attach_subformat sets PROG_FONT to the preference editor:code font).
tm-server.scm,
preferences-widgets.scm
The preference editor:code font: notify-code-font gives its new value to the open source files, and the preferences offer the monospaced fonts.
footer-menu.scm
The status bar of a source file (footer-in-source?): its language, without fonts and colors.
cursor.scm
The default (empty) definitions of the hooks.
c-lang.scm,
code-format.scm
The format c of the .c and .h files and the syntax of the language c.
lsp-test.scm
The test suite lsp.