Migrating from v1.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 removed from the v1.x
branch. If you want to see a complete config example, go to example config.
Configure nvim-lspconfig
All options and configurations that were done automatically to nvim-lspconfig are opt-in.
Now you have to call the function lsp-zero.extend_lspconfig().
local lsp_zero = require('lsp-zero')
local lsp_attach = function(client, bufnr)
-- see :help lsp-zero-keybindings
-- to learn the available actions
lsp_zero.default_keymaps({buffer = bufnr})
end
lsp_zero.extend_lspconfig({
capabilities = require('cmp_nvim_lsp').default_capabilities(),
lsp_attach = lsp_attach,
float_border = 'rounded',
sign_text = {
error = '✘',
warn = '▲',
hint = '⚑',
info = ''
},
})
Configure diagnostics
In v4.x
lsp-zero doesn't configure diagnostics automatically. If you want to get the previous behaviour, add this code.
vim.keymap.set('n', 'gl', '<cmd>lua vim.diagnostic.open_float()<cr>')
vim.diagnostic.config({
virtual_text = false,
severity_sort = true,
float = {
style = 'minimal',
border = 'rounded',
source = 'always',
header = '',
prefix = '',
},
})
Configure the lua language server
You will need to setup lua_ls
using lspconfig, and then add the configuration using the function lsp-zero.nvim_lua_ls().
local lsp_zero = require('lsp-zero')
require('lspconfig').lua_ls.setup({
on_init = function(client)
lsp_zero.nvim_lua_settings(client, {})
end,
})
Automatic install of language servers
This can be done using the module mason-lspconfig
. Use the ensure_installed
property of their .setup()
function. There you can list all the language servers you want to install.
require('mason-lspconfig').setup({
ensure_installed = {'lua_ls', 'rust_analyzer'},
})
Enable automatic setup of language servers
This can be done using the module mason-lspconfig
. In their .setup()
function you will need to configure a property called handlers
. You can setup a "default handler" and this will be enough to get the behaviour you want.
require('mason').setup({})
require('mason-lspconfig').setup({
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,
}
})
To add a custom configuration to a server you need to add property to handlers
, this property must be the name of the language server and you must assign a function. In this new function is where you will configure the language server.
require('mason-lspconfig').setup({
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({
on_init = function(client)
require('lsp-zero').nvim_lua_settings(client, {})
end,
})
end,
}
})
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 v1.x
branch.
Use the function lsp-zero.noop() as a handler to make mason-lspconfig ignore the language server.
local lsp_zero = require('lsp-zero')
require('mason').setup({})
require('mason-lspconfig').setup({
ensure_installed = {'lua_ls', 'rust_analyzer', 'jdtls'},
handlers = {
function(server_name)
require('lspconfig')[server_name].setup({})
end,
jdtls = lsp_zero.noop,
}
})
Configure nvim-cmp
lsp-zero version 4 doesn't configure nvim-cmp automatically. You will have to provide the minimal configuration to make autocompletion work.
local cmp = require('cmp')
cmp.setup({
sources = {
{name = 'nvim_lsp'},
},
snippet = {
expand = function(args)
require('luasnip').lsp_expand(args.body)
end,
},
mapping = cmp.mapping.preset.insert({}),
})
Configure completion sources
If you want to use the previous recommended config install these plugins in your Neovim config:
- hrsh7th/nvim-cmp
- hrsh7th/cmp-nvim-lsp
- hrsh7th/cmp-buffer
- hrsh7th/cmp-path
- hrsh7th/cmp-nvim-lua
- saadparwaiz1/cmp_luasnip
- rafamadriz/friendly-snippets
- L3MON4D3/LuaSnip
local cmp = require('cmp')
require('luasnip.loaders.from_vscode').lazy_load()
cmp.setup({
sources = {
{name = 'path'},
{name = 'nvim_lsp'},
{name = 'nvim_lua'},
{name = 'buffer', keyword_length = 3},
{name = 'luasnip', keyword_length = 2},
},
snippet = {
expand = function(args)
require('luasnip').lsp_expand(args.body)
end,
},
mapping = cmp.mapping.preset.insert({})
})
Configure autocomplete mappings
You can add the mappings you want in your cmp setup. This config uses the old mappings from v1.x
.
local cmp = require('cmp')
local cmp_action = require('lsp-zero').cmp_action()
cmp.setup({
sources = {
{name = 'nvim_lsp'},
},
snippet = {
expand = function(args)
require('luasnip').lsp_expand(args.body)
end,
},
mapping = cmp.mapping.preset.insert({
-- confirm completion item
['<CR>'] = cmp.mapping.confirm({select = false}),
-- toggle completion menu
['<C-e>'] = cmp_action.toggle_completion(),
-- tab complete
['<Tab>'] = cmp_action.tab_complete(),
['<S-Tab>'] = cmp.mapping.select_prev_item(),
-- navigate between snippet placeholder
['<C-d>'] = cmp_action.luasnip_jump_forward(),
['<C-b>'] = cmp_action.luasnip_jump_backward(),
-- scroll documentation window
['<C-f>'] = cmp.mapping.scroll_docs(-5),
['<C-d>'] = cmp.mapping.scroll_docs(5),
}),
})
Add borders to documentation window in completion menu
Add nvim-cmp's preset to the window.documentation
property.
local cmp = require('cmp')
cmp.setup({
window = {
documentation = cmp.config.window.bordered(),
}
})
Preselect first completion item
Add the following settings to nvim-cmp.
local cmp = require('cmp')
vim.opt.completeopt = {'menu', 'menuone', 'noselect'}
cmp.setup({
preselect = 'item',
completion = {
completeopt = 'menu,menuone,noinsert',
},
})
Completion item label
In v1.x
each completion item has a label that shows the source that created the item. This feature is now opt-in, you can use the function lsp-zero.cmp_format() to get the settings needed for nvim-cmp.
local cmp = require('cmp')
local cmp_format = require('lsp-zero').cmp_format()
cmp.setup({
formatting = cmp_format,
})
Example config
The following config recreates most of the features that were removed from the v1.x
branch.
local lazypath = vim.fn.stdpath('data') .. '/lazy/lazy.nvim'
vim.opt.rtp:prepend(lazypath)
local ok, lazy = pcall(require, 'lazy')
if not ok then
local msg = 'You need to install the plugin manager lazy.nvim\n'
.. 'in this folder: ' .. lazypath
print(msg)
return
end
lazy.setup({
{'VonHeikemen/lsp-zero.nvim', branch = 'v4.x'},
{'williamboman/mason.nvim'},
{'williamboman/mason-lspconfig.nvim'},
{'neovim/nvim-lspconfig'},
{'hrsh7th/nvim-cmp'},
{'hrsh7th/cmp-nvim-lsp'},
{'hrsh7th/cmp-buffer'},
{'hrsh7th/cmp-path'},
{'saadparwaiz1/cmp_luasnip'},
{'hrsh7th/cmp-nvim-lua'},
{'L3MON4D3/LuaSnip'},
{'rafamadriz/friendly-snippets'},
})
local lsp_zero = require('lsp-zero')
local lsp_attach = function(client, bufnr)
-- see :help lsp-zero-keybindings
-- to learn the available actions
lsp_zero.default_keymaps({buffer = bufnr})
end
lsp_zero.extend_lspconfig({
capabilities = require('cmp_nvim_lsp').default_capabilities(),
lsp_attach = lsp_attach,
float_border = 'rounded',
sign_text = {
error = '✘',
warn = '▲',
hint = '⚑',
info = ''
},
})
require('mason').setup({})
require('mason-lspconfig').setup({
ensure_installed = {},
handlers = {
function(server_name)
require('lspconfig')[server_name].setup({})
end,
lua_ls = function()
require('lspconfig').lua_ls.setup({
on_init = function(client)
lsp_zero.nvim_lua_settings(client, {})
end,
})
end,
}
})
vim.keymap.set('n', 'gl', '<cmd>lua vim.diagnostic.open_float()<cr>')
vim.diagnostic.config({
virtual_text = false,
severity_sort = true,
float = {
style = 'minimal',
border = 'rounded',
source = 'always',
header = '',
prefix = '',
},
})
local cmp = require('cmp')
local cmp_action = lsp_zero.cmp_action()
local cmp_format = lsp_zero.cmp_format()
require('luasnip.loaders.from_vscode').lazy_load()
vim.opt.completeopt = {'menu', 'menuone', 'noselect'}
cmp.setup({
formatting = cmp_format,
preselect = 'item',
completion = {
completeopt = 'menu,menuone,noinsert'
},
window = {
documentation = cmp.config.window.bordered(),
},
sources = {
{name = 'path'},
{name = 'nvim_lsp'},
{name = 'nvim_lua'},
{name = 'buffer', keyword_length = 3},
{name = 'luasnip', keyword_length = 2},
},
mapping = cmp.mapping.preset.insert({
-- confirm completion item
['<CR>'] = cmp.mapping.confirm({select = false}),
-- toggle completion menu
['<C-e>'] = cmp_action.toggle_completion(),
-- tab complete
['<Tab>'] = cmp_action.tab_complete(),
['<S-Tab>'] = cmp.mapping.select_prev_item(),
-- navigate between snippet placeholder
['<C-d>'] = cmp_action.luasnip_jump_forward(),
['<C-b>'] = cmp_action.luasnip_jump_backward(),
-- scroll documentation window
['<C-f>'] = cmp.mapping.scroll_docs(5),
['<C-u>'] = cmp.mapping.scroll_docs(-5),
}),
snippet = {
expand = function(args)
require('luasnip').lsp_expand(args.body)
end,
},
})