![]() |
Tests and pitfalls of the language servers | ![]() |
The suite lsp is
lsp-test.scm
(lsp-test-failures), run with the other suites of
check-master.scm
(see the tests). Most of it needs no language server:
the link is tested with ordinary commands of the shell as servers: cat sends back the framed messages which it gets, and printf writes the bytes of a server, headers included (several messages at once, headers in lower case or with a Content-Type, a message in several parts, lines without a header), a call back which waits for an answer within a dispatch, and a server which does not read its input (sleep: a long message must not block lsp-send). These tests are skipped on Windows;
the messages, the texts and the UTF-16 offsets (lines of one byte per character, which take a shorter path, and the others), and the URIs of lsp-base (drive letters, file:/x, a % without hexadecimal digits);
the clients, on procedures which record what is sent and are given the answers: initialization, a server which is slow to start or never answers initialize, the state of the diagnostics of the servers, the options of textDocumentSync;
the reading of answers: the entries of an outline, folding ranges, the decoding of semantic tokens and their colours, the labels of inlay hints and their lines in Cork offsets;
points of the protocol: capabilities given as false, a request cancelled during the initialization (never sent), diagnostics of an older version (ignored), stale workspace edits (lsp-workspace-edit-stale?);
requests, requests of the servers, the end of a server;
the edits of the servers (lsp-apply-to-lines, lsp-move-through-edits): edits with the same start, overlapping or beyond the text, the cursor after a longer line, incremental changes and the lines which they change, workspace edits; the parts of a signature (with offsets in UTF-16 units, and a parameter looked for after the name of the function), the progress of a server, the point under the mouse;
the documents and the diagnostics;
a server which ends at once (sh -c 'exit 0') is not started again until it is forgotten;
a C file of a throwaway project (with a compile_flags.txt) in the temporary directory: its format, language, project, lines and cursor position (the group buffer), and the decorations and folds of its lines, without a server (test-lines-of-buffer: a line which is edited, moved or removed; a fold in a fold which stays folded when the outer one goes).
When clangd is installed, the suite also runs it on that project (hover, diagnostics, the client of a buffer) and tests the editor on a C++ file: the buffer followed, the diagnostics underlined (the alternative selection code-errors), F8, the definition, the completion (synchronous and asynchronous), the log page and its refresh, the hover balloon, the didChange, didSave and didClose sent when the buffer is saved and closed, and the servers stopped by lsp-stop-servers. On a third file, it tests the actions: the page of the diagnostics, the focus bar, a quick fix, the signature, the references and the outline, the highlights, folding and unfolding a block, the symbols of the project and the calls, the colours and hints, a rename and the formatting. On a document of the project, it tests the code blocks (the group code blocks, turned on by lsp-blocks-force!, not by the preference): their virtual files, the target at the cursor, their diagnostics at their places, the definition in a block, a paragraph inserted above them, a block removed and one whose language changes, and a place in a block of a closed document; then the blocks of a language as one file (the group code blocks together): the lines of the unit and their places, a name of a block known in the next one (its only error), the definition in the other block, the colours of the server on the lines of the blocks, and a rename which changes both blocks.
The suite runs after kbd-menu in check-master: editing a C++ buffer loads the keyboard maps of programs, and the checks of kbd-menu which type mathematics must come before that (see the comment of test-typing-math-mode in
kbd-menu-test.scm
: once those maps are loaded, without a window, the first key after $ in text still sees the text mode).
No mode predicates in notify-cursor-moved, in the :require of kbd-variant, nor in the redefined mouse-event and keyboard-press. These run while the editor applies its changes (apply_changes), or between a change and the next typesetting, when the environment at the cursor is not up to date: in-prog?, in-math?… then answer for the former environment. Calling one there once made in-math? wrong after (make 'math). The code therefore asks whether the current buffer is followed (the table followed, lsp-buffer-followed?) and looks at the cursor path and the text of the line instead (lsp-completion-here?, lsp-cursor-moved). The mode in-lsp% of the keys is another matter: as any mode, it is evaluated when a key is looked up.
Hooks with :require or :mode must call former. A hook such as notify-cursor-moved is redefined by several modules: the language servers (lsp-edit, lsp-navigate) call former and have no condition, but a definition with a :require or a :mode which holds replaces the definitions loaded before it. If it does not call former, those are skipped: without it, the highlights of brackets of
cpp-edit.scm
keep the language servers from seeing the moves of the cursor in a C++ file (when they were loaded before it). The definitions of notify-cursor-moved in prog/*-edit.scm now call (former status) first.
Strings compared with eq? under S7. A cache which remembers the last string by eq? is only safe while it keeps a reference to that string: a string which is freed may have its address taken by a new one, which would then be taken for it. The same holds for the trees of the lines which the typesetter knows by their addresses: lsp-decorate keeps them (its table decorated) while they are decorated.
tree-bounding-rectangle needs an up-to-date typesetting. show-tooltip places the balloon of a hover with the bounding rectangle of the tree at the cursor, which only exists once that tree has been typeset. In the event loop this is the case before each key; a script (or a test) which moves the cursor and then calls lsp-hover must call update-forced first.
Positions are in UTF-16 units, lengths in bytes. The Content-Length of a header counts the bytes of the UTF-8 text, the positions count UTF-16 units, and the editor counts the characters of Cork strings: always go through the conversions of lsp-base.
Diagnostics refer to an older text. Until new ones come, the diagnostics shown are moved with the lines which a change moves (lsp-document-lines-changed), and those in the changed lines are dropped; the editor drops the ranges which are no longer valid paths before it looks for the visible ones (a binary search, which needs the ranges in the order of the text), and lsp-show-diagnostics leaves out those beyond the last line. The diagnostics are kept by normalized URI (a server may encode a path otherwise), and forgotten when their document is closed or their server ends.
A server which has ended stays ended. Its entry is kept so that it is not restarted at each pause; after changing lsp-server-set! or installing a server, use Restart server (or lsp-client-forget). The table followed also caches the buffers which have no server: lsp-restart empties it and follows all the open buffers again. The project roots are cached per directory too: after adding a .git or a compile_commands.json, call lsp-forget-project-roots.
A server may not read its input. A write to a full pipe would block the editor: the pipes of the servers write without blocking (set_blocking_write, see the link), and a dispatch takes at most about 100 ms. A server which never answers initialize is ended after lsp-initialize-timeout; until then, the whole texts which wait for it replace each other.
Long files. A source file of 100000 lines is a document of 100000 paragraphs, and a Scheme list of its lines at each pause of the typing, or list-ref on such a list for each position, costs much time and memory. The server is therefore given a snapshot of the lines in C++ (code-snapshot-new, code-snapshot-change), whose text is made only when it is asked for, the requests at the cursor read one line (lsp-buffer-line), the edits and the diagnostics use vectors (lsp-line), and the texts are compared by blocks. Converting a whole text between Cork and UTF-8 at once does not work either: a newline is a character of its own in Cork, so the texts are converted line by line. string-recompose (in
base.scm
) is linear (it was quadratic: gigabytes for such a file), and under S7
s7_tm.cpp
sets gc-resize-heap-by-4-fraction to 0.5: the heap is quadrupled only when less than half of it is free after a collection, since a heap never shrinks.
Edit the plug-in in
src/plugins
. The
format and language c live in
c-lang.scm
and
code-format.scm
; TeXmacs/plugins is a copy made by the build.