Greetings, Vimlings! I’m in the process of moving so these missives, but I’ve been taking breaks to do the LSP research and boy howdy do we have a treat for you today.
One of the main differences between Vim and Neovim is that Neovim has support for the Language Server Protocol (LSP). The LSP is an open standard for interacting with a language server. This is going to be mostly general background on what a language server is and how Nvim integrates with it. Next time we’ll get into the crunchy details of how to set it up to help you navigate your codebases.
What’s a language server?
Briefly, a language server is a program for doing all that fancy-ass IDE nonsense: Highlights, go-to-definition, maybe a little refactoring help. It’s a program that runs in the background while you’re editing text. Your text editor sends it messages when it needs language-specific information about the files you have open. It analyzes your codebase and can answer questions like, “where is this function defined?” or “where do I need to make changes in order to do this refactor?”
(Incidentally— LSP integration is one of the main reasons this newsletter is about Neovim, and not Vim, because it means that we can do a lot of code search tasks in Neovim without using ctags. I’m not anti-ctags or anything, but they need to be generated and regenerated, and I’ve only ever personally interacted with them when generation was failing in a large Vim configuration that I didn’t understand very well. We still discuss ctags at some point if we run into something that they can do but the LSP can’t, but for code navigation we’re going to start with the LSP.)
Why do I need a protocol for interacting with a language server?
The Language Server Protocol, while open source, was developed by Microsoft, which gives a clue as to its primary purpose: It’s designed to make it easier for editors, like their VSCode, to support language-aware features for many languages. It also helps language developers support many editors. A smaller, newer language like Elixir can develop a language server that implements the protocol and provide support for Neovim, VSCode, and any other editor that supports the protocol at the same time.
What does “supports the Language Server Protocol” mean for Neovim? How is this different from Vim?
Neovim has a Lua interpreter, and can run generic Lua code, but it also has a “standard library” that it loads into the Lua environment for doing Vim-specific things. This library includes an LSP client, that provides access to the LSP API through Lua. (Note that I’m not 100% sure whether this is technically a library or a module — I’ll need to do a write up on Lua specifically to cover that distinction.)
What this means is that it’s simpler to work with a language server in Neovim than it would be in Vim. There’s nothing that Neovim can do here that Vim can’t, but your or a plugin maintainer don’t need to write as much code, since the editor has a lot of the basic client work built in. You also don’t need to compile Vim with Lua to get any of the functions that are difficult to provide with Vimscript.
What language servers do Jetbrains IDEs use?
If you’ve used both Rubymine and Nvim to edit Ruby, or compared another Jetbrains editor to the same language in Nvim, you may have noticed that the Jetbrains autocomplete was much better in some cases. The Rubymine autocomplete knows about the “magic” Rails methods that are implemented by meta-programming, for instance.
Unfortunately, these features aren’t available in editors that use the language server model, because Jetbrains doesn’t use language servers. Instead, Jetbrains editors use something called the Program Structure Interface (PSI). They build a hierarchical representation of your code, and then the editor and its plugins can query and transform that representation directly. If they want to do a very language-specific lookup or transformation, they don’t need to wait for the LSP to be able provide that operation.
So sometimes these features will be more powerful in Jetbrains editors in the languages that they support. But the LSP is only about 7 years old at this point — the project started in 2016 — so it may catch up somewhat over time.
So what do I need to get this LSP stuff running in my Neovim?
The details depend on the language that you’re using but probably what you want to do is look up your language in the list of Language Server implementations, go to the repository, find the Nvim plugin, and configure that with your favorite plugin manager according to its instructions. install the nvim-lspconfig plugin and configure a server for your language of choice from the list of server configurations. You may need to tweak some additional settings to enable highlighting and folding, and you may need some additional plugins to enable things like snippets.
I’ll get into more detail about how to configure an LSP in the next few issues. (If I’m reading the documentation correctly, using at least some LSP features should be possible without any plugins whatsoever which is interesting.) I’ll probably use Elixir since that’s what I have working codebases available for testing in, but let me know if there’s a specific language you’d like to see a walkthrough for.
Recap
The LSP is a standard protocol for interacting with language servers
Using the LSP allows us to separate text editing (provided by Nvim) from language analysis (provided by the language server) and develop each kind of program separately as composable components
The LSP was originally developed by Microsoft, starting in 2016, but it’s an open standard, with clients written for Emacs, Neovim, Atom, Eclipse, Vscode, and more, and language servers written for many of languages
Language servers that use this protocol can’t do everything that dedicated IDEs like Rubymine can do, but they support many more languages and many more editors
You can get started configuring Nvim with your language of choice atthe LSP’s list of implementationsGet started configuring Nvim with your language of choice at with the Nvim LSP config plugin and its list of server configurations
References
Home page for the project. Has links to the specification and implementations.
Documentation on the LSP client that Nvim provides, and how to get started by setting up a language server and configuring the client.