Neovim and Language Server Protocol (LSP)
Language Server Protocol (LSP) helps computer programmers working on editors and get information about the languages they are working on. The LSP is an interface between editors (files, codes, cursor, state of code) and language server(s) running on the computer and the language server provides information on the state of the code.
Neovim LSP Setup Native and options with Plugins
Section titled “Neovim LSP Setup Native and options with Plugins”Source: Neovim LSP Myth: You DON’T Need Plugins (The 5-Tier Setup) - smnatale - YouTube
Video covers setting up LSP in Neovim with default functionality and no
plugins. It then goes through options that add more automation with
plugins 1. nvim-lspconfig, 2. mason.nvim, 3. mason-lspconfig, 4.
mason-tool-installer and when to use the automation those plugins help
with.
Option 1: Native Set up, No Plugins
Section titled “Option 1: Native Set up, No Plugins”Example using Lua LSP
- Install Lua language server on your system
- Copy
lua_ls.luafromnvim-lspconfigrepository to the Neovim configuration directory - In
init.luaaddvim.lsp.enable({"lua_ls"}) - Repeat for other languages as needed
Option 2: with LSP configuration plugin
Section titled “Option 2: with LSP configuration plugin”- Reuse LSP configurations maintained by the community using the
nvim-lspconfigplugin. Add plugin to Neovim configuration. - Optionally, override any settings.
- Install language servers you want to use on your system
- In
init.luaaddvim.lsp.enable({"lua_ls", "ts_ls"})for Lua and Typescript or choose appropriate LSP for the language.
Option 3: with LSP installer and LSP configuration plugins
Section titled “Option 3: with LSP installer and LSP configuration plugins”- Set up Option 2. Use
mason.nvimto install relevant language servers and in Neovim use:Mason - Use
mason-lspconfigto set which LSPs to install and enable LSPs. - Update Neovim configuration to set which LSPs to install and enable:
require("mason-lspconfig").setup({ ensure_installed = {"lua_ls", "ts_ls"}})- Use
mason-tool-installerto set which should be installed and not installed (for example, you manage the install yourself).
Rust Setup for Neovim
Section titled “Rust Setup for Neovim”Source: Rust Setup For Neovim (ft BashBunni) #bash2basics - YouTube with TJ DeVries
Installing rust
Section titled “Installing rust”- Use rustup.rs
- rustc, cargo
- Get Rust LSP: rust-analyzer
New rust project, look at rust files
Section titled “New rust project, look at rust files”# Install rust LSPrustup component add rust-analyzer
# Create new projectcargo new projectname
# run project1cd projectnamecargo runCan prefer cargo run to check for errors
Getting lspconfig setup
Section titled “Getting lspconfig setup”What is an LSP?
- Language Server Protocol (LSP)
- Just a program
- Helps your editor figure syntax of language, helps navigate
Example lsp.lua
lspconfig.rust_analyzer.setup { capabilities = capabilities, on_attach = on_attach, -- so it can find rust-analyzer cmd = { "rustup", "run", "stable", "rust-analyzer" }}- Restart nvim
- Find help at lspconfig site or the LSP server site for the language on how to integrate
Testing out Rust basics
Section titled “Testing out Rust basics”main.rs
fn main() { println!("{} world", "hello");}
fn truthy() -> bool { return false;}
#[cfg(test)]mod test { use super::truthy;
#[test] fn test_something() { assert_eq!(truthy(), true); }}Writing rust tests, cargo-watch
Section titled “Writing rust tests, cargo-watch”cargo test
# Install tool to monitor for file changes and then do testscargo install cargo-watch# Tell cargo watch to run tests on source changescargo watch -x test# During later changes to source code, tests will automatically be run