Migrating from v2.x branch
I will assume you are using Neovim v0.10 or greater.
Here you will find how to re-enable most of the features that were in the v2.x
branch. If you just want to see a complete config, go to example config.
Configure nvim-lspconfig
This is all the configuration that was done automatically in the v2.x
branch (plus keymaps).
-- Reserve a space in the gutter
-- This will avoid an annoying layout shift in the screen
vim.opt.signcolumn = 'yes'
-- Add borders to floating windows
vim.lsp.handlers['textDocument/hover'] = vim.lsp.with(
vim.lsp.handlers.hover,
{border = 'rounded'}
)
vim.lsp.handlers['textDocument/signatureHelp'] = vim.lsp.with(
vim.lsp.handlers.signature_help,
{border = 'rounded'}
)
-- Add cmp_nvim_lsp capabilities settings to lspconfig
-- This should be executed before you configure any language server
local lspconfig_defaults = require('lspconfig').util.default_config
lspconfig_defaults.capabilities = vim.tbl_deep_extend(
'force',
lspconfig_defaults.capabilities,
require('cmp_nvim_lsp').default_capabilities()
)
-- This is where you enable features that only work
-- if there is a language server active in the file
vim.api.nvim_create_autocmd('LspAttach', {
callback = function(event)
local opts = {buffer = event.buf}
vim.keymap.set('n', 'K', '<cmd>lua vim.lsp.buf.hover()<cr>', opts)
vim.keymap.set('n', 'gd', '<cmd>lua vim.lsp.buf.definition()<cr>', opts)
vim.keymap.set('n', 'gD', '<cmd>lua vim.lsp.buf.declaration()<cr>', opts)
vim.keymap.set('n', 'gi', '<cmd>lua vim.lsp.buf.implementation()<cr>', opts)
vim.keymap.set('n', 'go', '<cmd>lua vim.lsp.buf.type_definition()<cr>', opts)
vim.keymap.set('n', 'gr', '<cmd>lua vim.lsp.buf.references()<cr>', opts)
vim.keymap.set('n', 'gs', '<cmd>lua vim.lsp.buf.signature_help()<cr>', opts)
vim.keymap.set('n', 'gl', '<cmd>lua vim.diagnostic.open_float()<cr>', opts)
vim.keymap.set('n', '<F2>', '<cmd>lua vim.lsp.buf.rename()<cr>', opts)
vim.keymap.set({'n', 'x'}, '<F3>', '<cmd>lua vim.lsp.buf.format({async = true})<cr>', opts)
vim.keymap.set('n', '<F4>', '<cmd>lua vim.lsp.buf.code_action()<cr>', opts)
end,
})
Automatic install of language servers
In order to get automatic install of language server you will have to use the module mason-lspconfig
and list the servers in the ensure_installed
option. Like this.
require('mason').setup({})
require('mason-lspconfig').setup({
ensure_installed = {'lua_ls', 'rust_analyzer'},
})
Automatic configuration of language servers
To configure the language servers installed with mason.nvim
automatically you should use the module mason-lspconfig
.
You'll need to use the option handlers
in mason-lspconfig. You can make a "default handler" that will setup the language servers using lspconfig
.
require('mason').setup({})
require('mason-lspconfig').setup({
ensure_installed = {'lua_ls', 'rust_analyzer'},
handlers = {
function(server_name)
require('lspconfig')[server_name].setup({})
end,
},
})
To get more details on how to use mason.nvim read this guide: Integrate with mason.nvim
Exclude a language server from automatic configuration
You'll also need to use the option handlers
in mason-lspconfig in order to disable a language server. This is in place of the skip_server_setup
that was present in the v2.x
branch.
Create an empty function and use it as a handler to make mason-lspconfig ignore the language server.
local noop = function() end
require('mason').setup({})
require('mason-lspconfig').setup({
ensure_installed = {'lua_ls', 'rust_analyzer', 'jdtls'},
handlers = {
-- this first function is the "default handler"
-- it applies to every language server without a "custom handler"
function(server_name)
require('lspconfig')[server_name].setup({})
end,
-- this is the "custom handler" for `jdtls`
-- noop is an empty function that doesn't do anything
jdtls = noop,
},
})
Setup lua_ls using mason-lspconfig
When using mason-lspconfig, if you want to configure a language server you need to add a handler with the name of the language server. In this handler you will assign a lua function, and inside this function you will configure the server.
require('mason').setup({})
require('mason-lspconfig').setup({
ensure_installed = {'lua_ls', 'rust_analyzer'},
handlers = {
-- this first function is the "default handler"
-- it applies to every language server without a "custom handler"
function(server_name)
require('lspconfig')[server_name].setup({})
end,
-- this is the "custom handler" for `lua_ls`
lua_ls = function()
require('lspconfig').lua_ls.setup({
settings = {
Lua = {
runtime = {
version = 'LuaJIT',
},
diagnostics = {
globals = {'vim'},
},
workspace = {
library = {vim.env.VIMRUNTIME},
},
},
},
})
end,
},
})
Configure nvim-cmp
This is a minimal configuration to make autocompletion work.
local cmp = require('cmp')
cmp.setup({
sources = {
{name = 'nvim_lsp'},
},
mapping = cmp.mapping.preset.insert({}),
})
Completion item label
In v2.x
each completion item has a label that shows the source that created the item. This is how you can implement it.
local cmp = require('cmp')
cmp.setup({
formatting = {
fields = {'abbr', 'menu', 'kind'},
format = function(entry, item)
local n = entry.source.name
if n == 'nvim_lsp' then
item.menu = '[LSP]'
else
item.menu = string.format('[%s]', n)
end
return item
end,
},
})
Scroll the documentation of completion item
To scroll the documentation window you will have to set the mappings manually.
local cmp = require('cmp')
cmp.setup({
mapping = cmp.mapping.preset.insert({
-- scroll up and down the documentation window
['<C-u>'] = cmp.mapping.scroll_docs(-4),
['<C-d>'] = cmp.mapping.scroll_docs(4),
}),
})
Example Config
--[[
Make sure you have these plugins installed:
* neovim/nvim-lspconfig
* williamboman/mason.nvim
* williamboman/mason-lspconfig.nvim
* hrsh7th/nvim-cmp
* hrsh7th/cmp-nvim-lsp
* L3MON4D3/LuaSnip
]]
local cmp = require('cmp')
cmp.setup({
sources = {
{name = 'nvim_lsp'},
},
snippet = {
expand = function(args)
require('luasnip').lsp_expand(args.body)
end,
},
formatting = {
fields = {'abbr', 'menu', 'kind'},
format = function(entry, item)
local n = entry.source.name
if n == 'nvim_lsp' then
item.menu = '[LSP]'
else
item.menu = string.format('[%s]', n)
end
return item
end,
},
mapping = cmp.mapping.preset.insert({
-- scroll up and down the documentation window
['<C-u>'] = cmp.mapping.scroll_docs(-4),
['<C-d>'] = cmp.mapping.scroll_docs(4),
}),
})
-- Reserve a space in the gutter
-- This will avoid an annoying layout shift in the screen
vim.opt.signcolumn = 'yes'
-- Add borders to floating windows
vim.lsp.handlers['textDocument/hover'] = vim.lsp.with(
vim.lsp.handlers.hover,
{border = 'rounded'}
)
vim.lsp.handlers['textDocument/signatureHelp'] = vim.lsp.with(
vim.lsp.handlers.signature_help,
{border = 'rounded'}
)
-- Add cmp_nvim_lsp capabilities settings to lspconfig
-- This should be executed before you configure any language server
local lspconfig_defaults = require('lspconfig').util.default_config
lspconfig_defaults.capabilities = vim.tbl_deep_extend(
'force',
lspconfig_defaults.capabilities,
require('cmp_nvim_lsp').default_capabilities()
)
-- This is where you enable features that only work
-- if there is a language server active in the file
vim.api.nvim_create_autocmd('LspAttach', {
callback = function(event)
local opts = {buffer = event.buf}
vim.keymap.set('n', 'K', '<cmd>lua vim.lsp.buf.hover()<cr>', opts)
vim.keymap.set('n', 'gd', '<cmd>lua vim.lsp.buf.definition()<cr>', opts)
vim.keymap.set('n', 'gD', '<cmd>lua vim.lsp.buf.declaration()<cr>', opts)
vim.keymap.set('n', 'gi', '<cmd>lua vim.lsp.buf.implementation()<cr>', opts)
vim.keymap.set('n', 'go', '<cmd>lua vim.lsp.buf.type_definition()<cr>', opts)
vim.keymap.set('n', 'gr', '<cmd>lua vim.lsp.buf.references()<cr>', opts)
vim.keymap.set('n', 'gs', '<cmd>lua vim.lsp.buf.signature_help()<cr>', opts)
vim.keymap.set('n', 'gl', '<cmd>lua vim.diagnostic.open_float()<cr>', opts)
vim.keymap.set('n', '<F2>', '<cmd>lua vim.lsp.buf.rename()<cr>', opts)
vim.keymap.set({'n', 'x'}, '<F3>', '<cmd>lua vim.lsp.buf.format({async = true})<cr>', opts)
vim.keymap.set('n', '<F4>', '<cmd>lua vim.lsp.buf.code_action()<cr>', opts)
end,
})
require('mason').setup({})
require('mason-lspconfig').setup({
ensure_installed = {},
handlers = {
-- this first function is the "default handler"
-- it applies to every language server without a custom handler
function(server_name)
require('lspconfig')[server_name].setup({})
end,
-- this is the "custom handler" for `lua_ls`
lua_ls = function()
require('lspconfig').lua_ls.setup({
settings = {
Lua = {
runtime = {
version = 'LuaJIT',
},
diagnostics = {
globals = {'vim'},
},
workspace = {
library = {vim.env.VIMRUNTIME},
},
},
},
})
end,
},
})