The Language Server Client
An editor can colour code and move text without understanding any of it. Understanding — where is this function defined, what else calls it, is this name misspelled — lives in a separate program per language, called a language server. Servers and editors speak a standard protocol between them, the Language Server Protocol (LSP): the editor sends the user is looking at this position in this file; what is here?, the server — which has read the whole project — sends back the answer. It is the arrangement behind the code intelligence in every modern IDE.
Neovim's part in this is a client, built into the editor itself. No plugin is required to talk to a server; what you supply is the server program for each language you work in, and a few lines telling Neovim where it lives. This chapter does both for Lua itself, against a real server, and finishes by asking the question the whole arrangement exists for: jump from this call to its definition.
Installing a server
Servers are ordinary programs distributed by their own projects. The lab used lua-language-server (the server for Lua, version 3.19.2), fetched as its official release tarball and unpacked beside the lab's other tools, exactly as chapter 2 installed Neovim itself. Other languages are the same shape of errand — a server exists for nearly every language you have heard of, usually installed by a package manager, a release download, or a language's own tooling — and Neovim deliberately does not install them for you. The client is standard; the servers are the languages' own business.
Telling Neovim about it
Since 0.11, configuration is two
calls. The lab's test script
(neovim-lab/scripts/lsp-test.lua)
makes them, and in your own setup the
same lines belong in init.lua:
vim.lsp.config('lua_ls', {
cmd = { '/path/to/lua-language-server' },
filetypes = { 'lua' },
root_markers = { '.luarc.json', '.git' },
})
vim.lsp.enable('lua_ls')
vim.lsp.config describes a server:
the command that starts it, the file
types it serves, and the root
markers — files whose presence in a
folder or its parents marks the top of
a project. The root matters: a server
indexes a project, and the marker is
how the editor knows where yours
begins (a .git folder is the usual
one; the lab's specimen project carries
a .luarc.json, the server's own
configuration file). vim.lsp.enable
then switches it on: open a Lua file
under such a root and the client starts
the server and attaches it to the
buffer.
The proof: a definition jump
The specimen is six lines
(neovim-lab/files/lsp-proj/main.lua):
a function add defined on line 1 and
called on line 5. The lab opened it,
waited for the server to attach, put
the cursor on the call, and sent the
server a definition request — the same
request the editor sends when you use
its built-in go-to-definition in a
session. The result file the script
wrote (transcript
neovim-lab/transcripts/t08-lsp.txt):
client attached: true
client name: lua_ls
definition found in: /home/hatch/workspace/build-check/neovim-scratch/lab/work/lsp-proj/main.lua
definition line (1-based): 1
Line 5's question, answered with line
1. That round trip — attach, ask, be
told — is the foundation under
everything an IDE does with a project:
completion while you type, diagnostics
that underline a problem before you
run the program, hover text, renaming
a symbol in every file at once,
formatting. Neovim exposes each of
them through the same client (they are
gathered under :help vim.lsp.buf),
and you bind the ones you want to keys
of your choosing — commonly inside an
LspAttach autocmd (chapter 5), so
the keys exist exactly in buffers that
have a server. The specific mappings
are a matter of taste, and the lab
verified the round trip rather than
anyone's taste in keys; the
lsp-quickstart section of the help
shows the standard pattern.
Two honest footnotes from the lab's
run. The first attempt at the proof
script crashed on the answer's
shape: a definition reply may be a
Location (uri plus range) or a
LocationLink (targetUri plus
targetSelectionRange), and this
server sends the second kind. The
editor's own commands handle both
without your caring; only script
authors meet the difference — which is
worth knowing before your first script
does. And when a server does not
attach, the causes are mundane and
checkable: the command path is wrong,
the server's own log says why it
exited, or no root marker was found
above the file. :checkhealth vim.lsp
and vim.lsp.get_clients() — the call
the lab's script used — are the two
questions to ask first.