What is LSP?
The Language Server Protocol (LSP) is a standardized protocol that allows editors and IDEs to communicate with language-specific servers. These servers provide features like:
- Code completion
- Syntax highlighting
- Error detection (diagnostics)
- Go-to-definition
- Refactoring tools
- Documentation on hover
LunarVim leverages LSP via nvim-lspconfig
and mason.nvim
, simplifying server installation and configuration.
Step 1: Installing Language Servers
LunarVim uses Mason to manage LSP servers, linters, formatters, and other tools. Here’s how to install a language server:
- Open Mason: Run
:Mason
to open the plugin’s interface. - Install a Server: Navigate to the server you need (e.g.,
pyright
for Python,tsserver
for TypeScript) and pressi
. - Verify Installation: Run
:LspInfo
to confirm the server is installed and active.
Alternatively, install servers via the command line:
:LspInstall pyright
Step 2: Configuring LSP in LunarVim
LunarVim pre-configures LSP for many languages, but you can customize settings in your config.lua
(~/.config/lvim/config.lua
).
Example: Customizing Python (pyright)
vim.list_extend(lvim.lsp.automatic_configuration.skipped_servers, { "pyright" }) -- Disable default setup
lvim.lsp.automatic_configuration.skipped_servers = { "tsserver" } -- Skip default setup for TS
require("lvim.lsp.manager").setup("pyright", {
on_attach = function(client, bufnr)
-- Custom keybindings or logic here
end,
settings = {
python = {
analysis = {
typeCheckingMode = "strict", -- Enforce strict type checks
},
},
},
})
Step 3: Keybindings and Usage
LunarVim sets up default keybindings for LSP features. Here are the essentials:
K
: Show documentation hover.gd
: Go to definition.gr
: List references.<leader>ca
: Code actions (e.g., fix suggestions).[d
/]d
: Jump between diagnostics.
Step 4: Refactoring with LSP
LSP enables powerful refactoring capabilities directly in LunarVim. Here’s how to leverage them:
Rename Symbols
- Keybinding: Use
<leader>rn
(if configured) to rename a symbol across your project.lvim.lsp.buffer_mappings.normal_mode['<leader>rn'] = { vim.lsp.buf.rename, 'Rename Symbol' }
- Usage: Place cursor on the symbol, press
<leader>rn
, and enter the new name.
Code Actions
- Default Keybinding:
<leader>ca
opens a list of available code actions, including refactorings. - Common Refactorings:
- Extract Variable/Function: Select the code, trigger
<leader>ca
, and choose the extraction. - Organize Imports: Automatically sort and clean up imports (available in many language servers).
- Extract Variable/Function: Select the code, trigger
Example: TypeScript with tsserver
-
Extract to Function:
- Highlight a code block.
- Press
<leader>la
and select “Extract to function”.
-
Organize Imports:
- Press
<leader>la
in a TypeScript file and choose “Organize Imports”.
- Press
Notes:
- Server Support: Refactoring options depend on your language server. Check server documentation for available features.
- Troubleshooting: If refactorings don’t appear, ensure your language server supports them via
:LspInfo
.
Step 5: Diagnostics and Formatting
Diagnostics
- View errors with
:lua vim.diagnostic.open_float()
. - Toggle diagnostics globally with
:lua vim.diagnostic.disable()
/:enable()
.
Formatting
LunarVim uses LSP or null-ls
for formatting. Enable formatting on save:
lvim.format_on_save = true
To specify a formatter:
local formatters = require("lvim.lsp.null-ls.formatters")
formatters.setup({
{ command = "black", filetypes = { "python" } }, -- Python formatter
{ command = "prettier", filetypes = { "javascript", "typescript" } },
})
Step 6: Advanced Setup with null-ls
Some tools (linters, formatters) aren’t LSP servers. Use null-ls to integrate them:
- Install via Mason (
:MasonInstall eslint_d prettier
). - Configure in
config.lua
:
local null_ls = require("null-ls")
null_ls.setup({
sources = {
null_ls.builtins.diagnostics.eslint_d, -- JS/TS linter
null_ls.builtins.formatting.stylua, -- Lua formatter
},
})
Step 7: Troubleshooting Common Issues
-
Server Not Starting:
- Run
:LspInfo
to check if the server is active. - Verify installation with
:Mason
.
- Run
-
Diagnostics Missing:
- Ensure the server supports diagnostics (e.g.,
pyright
does,tsserver
requireseslint
).
- Ensure the server supports diagnostics (e.g.,
-
Conflicting Servers:
- Skip default setup for languages with multiple servers:
vim.list_extend(lvim.lsp.automatic_configuration.skipped_servers, { "jedi_language_server" })
- Skip default setup for languages with multiple servers:
-
Logs and Debugging:
- Check Neovim’s LSP logs:
:LspLog
.
- Check Neovim’s LSP logs:
-
Restarting LSP Servers:
- If your LSP server stops responding or fails to update after file changes, manually restart it with:
:LspRestart