![]() |
Language servers for source code | ![]() |
When you edit a source file (a C++, Python, Java… file opened in TeXmacs), TeXmacs can ask a language server about it. A language server is an external program, the same one which other editors use through the Language Server Protocol (LSP), which analyzes the code of a whole project. With a server, TeXmacs underlines the errors and warnings of the file while you type, completes names, shows information about the symbol at the cursor and jumps to its definition.
Source files are shown in a monospaced font, Inconsolata by default; another one can be chosen in Edit\(\rightarrow\)Preferences\(\rightarrow\)Other\(\rightarrow\)Font of source code (the change reaches the files which are open). This font is the one of source files only: the code in a document keeps the fonts of the document.
TeXmacs does not include any language server: you have to install the ones you need, so that their programs can be found in your PATH. For each format of source files, TeXmacs knows the following servers and uses the first one which is installed:
|
||||||||||||||||||||||||||||||||||||||||||||||||
For instance, clangd is part of the LLVM tools (on Linux, it is usually in a package clangd), and pylsp is installed with pip install python-lsp-server. For Julia, jetls (JETLS.jl) and fatou are programs of their own, used when they are found; else julia runs the package LanguageServer.jl, which is installed with julia -e 'using Pkg; Pkg.add("LanguageServer")'.
Files with the suffixes .c and .h have their own format “C source code” (c), with the syntax highlighting of C, and are followed by clangd as C files. Files of C++ (.cpp, .hpp…) keep the format cpp.
As long as Tools\(\rightarrow\)Language servers\(\rightarrow\)Follow source files is checked (the default), a source file whose format has an installed server is followed automatically: when it is loaded and after each pause in your typing, its text is given to the server; when it is saved, the server is told so, and when its buffer is closed (or saved under another name), the server forgets it. A server is started the first time a file needs it, and then runs as long as TeXmacs. Unchecking the menu entry stops all the servers; when it is checked again, they are started again as the files need them.
⇧F12 and the keys ⌘⇧F, ⌘⇧E, ⌘., ⌘⇧M, ⌘⇧U, ⌘⇧P and ⌘⇧H described below work in the files which a server follows; elsewhere, ⇧F12 keeps its usual meaning (in presentations). F12, ⌘⇧O, F8 and the keys of the folds work in all program code: from the server where one follows it, else from the syntax tree (see editing program code).
The setting is the preference lsp (on or off). Only files on disk are followed. The other preferences of the servers are lsp:mouse hover, lsp:highlight, lsp:semantic colors and lsp:inlay hints (see below).
When Tools\(\rightarrow\)Language servers\(\rightarrow\)Follow the code blocks of documents is checked (preference lsp:code blocks, off by default), the code blocks of a document saved on disk (Insert\(\rightarrow\)Program\(\rightarrow\)Block of code, in a language which has a server) are followed too. The server sees a file next to the document, named after it, which is never written, in the project of the document: with One file for the blocks of a language (preference lsp:join code blocks, on by default), the blocks of a language are one file, their lines one after the other in the order of the document (.notes.tm-blocks.cpp for the C++ blocks of notes.tm), so that the names defined in a block are known in the next ones; else each block is a file of its own (.notes.tm-block1.cpp…). In a block, the diagnostics, the completion, the information at the cursor, the definitions, the references, the colours and inlay hints of the server, formatting, renaming and quick fixes work as in a source file; an edit of the server which would join or split blocks is not applied. The outline of joined blocks is that of all of them, and a fold of the server is one inside a block.
A server is started for a language and a project, the directory of which it analyzes the files together. The project of a file is the nearest directory, starting from the directory of the file and going up, which contains one of the files or directories
.git compile_commands.json compile_flags.txt pyproject.toml setup.py package.json Cargo.toml go.mod build.sbt pom.xml |
If there is none, the project is the directory of the file. Files of the same language in the same project share one server. A C++ server like clangd needs to know how the files are compiled: give it a compile_commands.json (generated by CMake with -DCMAKE_EXPORT_COMPILE_COMMANDS=ON) or a compile_flags.txt with one compiler option per line, at the top of the project.
A server is a program on your computer, which TeXmacs starts itself and with which it communicates through the standard input and output of the program. There is one server for each language and each project: it is started the first time a file of that language and that project is opened, the other files of the project then share it, and a file of another project gets a server of its own. When it starts, a server is told the directory of its project (the rootUri and the workspaceFolders of the protocol), which is how clangd, for instance, finds the compile_commands.json of the project.
TeXmacs does not send options of its own to the servers: there are no initializationOptions, and a server which asks for its settings (workspace/configuration) is answered that there are none. Each server is configured with its own files, as with other editors, for instance a .clangd file for clangd or the pyproject.toml of a Python project for pylsp and pyright. Servers which run on another computer, reached through a network connection, are not supported yet.
A server which does not answer within two minutes when it is started is ended, with a line in its log (see Server log below).
The servers of a format can be changed in your personal initialization file ~/.TeXmacs/progs/my-init-texmacs.scm. The commands are tried in order, and the first whose program is found is used. For instance, to use pyright for Python files:
(use-modules (lsp lsp-client))
(lsp-server-set! "python" '("pyright-langserver --stdio")) |
A command may also start with the full path of the program (a path without spaces), and may be followed by its options, separated by spaces.
The language of a file is its format (cpp, c, python…) or, for a buffer in program mode, its programming language. A server can be given in the same way for another language, but only once TeXmacs has a format for its files (defined by a plugin, for a language which has none). With a format zig, for instance, the server would be given by
(lsp-server-set! "zig" '("zls")) |
The errors and warnings which the server finds (its diagnostics) are underlined in the file: errors in red, warnings in orange, information and hints in blue. When the cursor is in an underlined part, its message is shown on the status bar. F8 moves the cursor to the next diagnostic, ⇧F8 to the previous one (both start again at the other end of the file).
The diagnostics are those of the text which the server was last given. While you type, the underlines disappear; when you pause, those which are not in the lines you changed come back, moved with the text, until the server sends new diagnostics a moment later.
In a followed file, ⇥ after a word, or after ., -> or ::, asks the server for the completions at the cursor. The answer comes asynchronously, and is applied only if the cursor has not moved meanwhile: the first completion is inserted and, if there are others, they are listed on the status bar and further presses of ⇥ (or ⇧⇥) cycle through them, as for the other completions of TeXmacs. If the server proposes nothing, the editor's own completion is tried instead.
⇧F12 shows what the server says about the symbol at the cursor (its type, its documentation…) in a balloon at the cursor, which is closed when the cursor moves. F12 goes to the definition of the symbol at the cursor, opening its file if needed. On keyboards of Mac computers, the function keys may have to be pressed together with fn.
These commands are also in Tools\(\rightarrow\)Language servers:
Whether the servers follow the source files (see above).
Whether they follow the code blocks of the documents too (see above).
As ⌘⇧U, ⌘⇧O, ⌘⇧P and ⌘⇧H (see below).
As F12, ⇧F12 and ⇥.
As F8 and ⇧F8.
As ⌘⇧M (see below).
As ⌘⇧F, ⌘⇧E and ⌘. (see below).
Whether the information about a symbol is shown when the mouse rests on it (see below).
See “folding” below; the first two are as ⌘( and ⌘).
Whether the uses of the symbol at the cursor are highlighted (see below).
Whether the names are coloured by their kinds, and whether the hints are shown (see below).
Stops the server of the current file (if it runs) and starts it again; the files which are open are given to it again. This also starts a server which has ended or which was stopped, and looks again for the project of each file (after a git init, or once a compile_commands.json has been made).
Opens a page with what the server of the current file has written: its messages, the errors it printed and those of the communication. The page is updated while it stays open.
Stops all the language servers. They stay stopped, also when you continue to edit, until you use Restart server for a file (or uncheck and check again Follow source files).
When a server ends by itself (for instance after a crash), the status bar says so, and the server is not restarted automatically: its log usually tells what went wrong, and Restart server starts it again.
When the mouse rests for a moment on a word of a followed source file, the server is asked about it, and its answer is shown in a balloon next to the word, as with ⇧F12, if the mouse is still there. The balloon is closed when the mouse leaves it. This can be turned off with Tools\(\rightarrow\)Language servers\(\rightarrow\)Information on mouse rest.
After an opening bracket or a comma (the characters which the server asks for), the signature of the function whose arguments are being typed is shown on the status bar, with the current argument between guillemets, as in f (int a, «int b»), and on the focus bar, with the current argument in bold. The closing bracket removes it.
In a source file which a server follows, the focus bar shows, after the language of the file, the name of the server (a menu, the one of Tools\(\rightarrow\)Language servers), whether it is starting or has stopped, the work it is doing (such as the indexing of the project, with its progress), the number of errors and warnings of the file (a click shows their list) and the signature which is being typed.
With View\(\rightarrow\)Interactive status bar (in the versions of TeXmacs which have it), the status bar of a source file only shows its language: the menus of the fonts and the colours of the text are left out, since the code is coloured by its language and its font is the one of the preferences. The code in a document keeps them.
⌘⇧M opens a page with all the diagnostics of the current file, one per line, with its line, its column (as the server counts it: a character beyond U+FFFF, such as an emoji, counts twice), its severity and its message. Clicking on a line goes to the place of the diagnostic. The page is updated while it stays open, each time the server publishes new diagnostics. At most 1000 diagnostics are underlined in a file.
⌘⇧F formats the selection, or the whole file if nothing is selected, as the server does it (for instance with clang-format for clangd). The server is told to indent as the file is indented (by tabs, at its tab stops, or by spaces), see editing source files.
⌘⇧E asks for a new name and renames the symbol at the cursor wherever the server finds it: in the current file and in the other files of the project. The files which are not open are loaded, and are then to be saved.
⌘. asks the server for the corrections at the cursor (for instance “did you mean …?” for a misspelled name), together with the other changes which it proposes there (such as refactorings): a single one is applied at once; when there are several, they are listed on a page, from which one is applied by clicking on it. If the file has been edited since the page was opened, the change is not applied (it was made for the former text): ask for the corrections again. Some changes are sent by the server later, after a command; they are refused too when the file has changed meanwhile.
⌘⇧U lists on a page all the places where the symbol at the cursor is used, in the current file and in the other files of the project, by file, each with its line number and its line. ⌘⇧O shows the outline of the current file: its functions, classes, types, variables…, indented as they are nested in the file, each with its kind and its signature. On both pages, a click on a line goes to its place. The outline follows the file as long as its page stays open: it is made again each time the server has seen a change. At most 2000 references are listed.
⌘⇧P asks for a name and lists the symbols of the whole project whose names match it (functions, types, variables…), with their files and lines. ⌘⇧H shows the calls of the function at the cursor: the functions which call it (a click goes to the call) and the functions which it calls (a click goes to their definitions). At most 500 symbols are listed; a server which has not answered after 10 seconds is given up (5 seconds for the requests at the cursor, such as the definitions and the references).
When the cursor rests in a name, the other places of the file where the same symbol is used are highlighted, as the matches of a search. The highlights go when the cursor leaves them or when you edit. This can be turned off in Tools\(\rightarrow\)Language servers (the preference lsp:highlight).
⌘( folds the block at the cursor (the body of a function, a class, a loop…: the blocks are those of the server, else those of the syntax tree): its first line stays, ended by an ellipsis, and its other lines are no longer shown. ⌘) unfolds it; Tools\(\rightarrow\)Language servers\(\rightarrow\)Fold all folds all the blocks, and Unfold all shows them again.
Folding changes only what is shown: the lines stay in the file, which is saved, searched, undone and given to the server as usual. The cursor does not stay in a folded block: moving from its first line into it (right at the end of the line, or down) goes to the line after it, moving back from the line after it (left at its start, or up) goes to its first line, and a block where the cursor comes otherwise (a search, a definition, a diagnostic) opens. A hidden line which is edited is shown again; the folds stay with their lines when other lines are inserted or removed. The folds are those of all program code (see editing program code): they stay when the language servers are turned off.
When the server can tell the kinds of the names (semantic tokens, as clangd does), the names of functions, of types, of macros and of the members of enumerations get colours of their own, also those which TeXmacs could not recognize by itself (the types and the macros which the program defines). The colours of the types and of the macros are those of the preferences of the syntax of the language; keywords, comments, strings and numbers keep the colours of TeXmacs.
When the server gives inlay hints, they are shown in grey in the code: the names of the parameters before the arguments of a call (answer (x: 1)), the types which the compiler deduces… They are not part of the text: the cursor goes over them, and they are not saved.
The colours and the hints are those of the text which the server was last given: a line which you edit loses them until the server has answered again, a moment after you have paused. They are asked for once the server has published the diagnostics of the text (a server which publishes none gets no colours and no hints). Both can be turned off in Tools\(\rightarrow\)Language servers (the preferences lsp:semantic colors and lsp:inlay hints). They are not asked for in files of more than 20000 lines.
When the server accepts it (as clangd does), only the part of a file which has changed is sent to it after an edit, instead of the whole text, which keeps large files fast.