1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
return {
{
"VonHeikemen/lsp-zero.nvim",
branch = 'v2.x',
dependencies = {
-- LSP Support
{'neovim/nvim-lspconfig'}, -- Required
{'williamboman/mason.nvim'}, -- Optional
{'williamboman/mason-lspconfig.nvim'}, -- Optional
-- Autocompletion
{'hrsh7th/nvim-cmp'}, -- Required
{'hrsh7th/cmp-nvim-lsp'}, -- Required
{'hrsh7th/cmp-buffer'}, -- Required
{'hrsh7th/cmp-nvim-lua'},
{'L3MON4D3/LuaSnip'}, -- Required
},
config = function()
local lsp = require('lsp-zero').preset({})
-- @notes on Lsp and Cmp configs here:
--[[
# LSP
1. Renamed the keymaps to what I use
2. Added a custom handler for formatting files.
It checks the explicitly defined language servers passed and if they allow formatting, only then will it attempt
to format the buffer
# Complete
Completion is done by nvim-cmp. The setup for that does a few things.
1. Defines the sources to use for autocompletion:
a. LSP server
b. nvim-lua: only active in lua file I think
c. buffer: uses the current file for auto-completion (similar to default nvim behavior)
2. By default it pops up a list for auto-completion, we have that disabled.
3. When having auto-complete disabled, it takes extra steps to trigger autocomplete, due
to this we need to have it preselect the first item upon triggering autocomplete.
4. We define some custom mappings
--]]
local function allow_format(servers)
return function(client) return vim.tbl_contains(servers, client.name) end
end
lsp.on_attach(function(client, bufnr)
-- see :help lsp-zero-keybindings
-- to learn the available actions
local opts = {buffer = bufnr, remap = false}
vim.keymap.set("n", "gd", function() vim.lsp.buf.definition() end, opts)
vim.keymap.set("n", "gD", function() vim.lsp.buf.declaration() end, opts)
vim.keymap.set("n", "K", function() vim.lsp.buf.hover() end, opts)
vim.keymap.set("n", "<leader>vws", function() vim.lsp.buf.workspace_symbol() end, opts)
vim.keymap.set("n", "<leader>vd", function() vim.diagnostic.open_float() end, opts)
vim.keymap.set("n", "[d", function() vim.diagnostic.goto_prev() end, opts)
vim.keymap.set("n", "]d", function() vim.diagnostic.goto_next() end, opts)
vim.keymap.set("n", "<leader>vca", function() vim.lsp.buf.code_action() end, opts)
vim.keymap.set("n", "<leader>vrr", function() vim.lsp.buf.references() end, opts)
vim.keymap.set("n", "<leader>vrn", function() vim.lsp.buf.rename() end, opts)
vim.keymap.set("i", "<C-s>", function() vim.lsp.buf.signature_help() end, opts)
vim.keymap.set("n", "<leader>vlf", function()
vim.lsp.buf.format({
async = false,
timeout_ms = 10000,
filter = allow_format({'rust_analyzer', 'tsserver', 'gopls', 'clangd', 'html-lsp'}),
})
end, opts)
end)
require('lspconfig').lua_ls.setup({
settings = {
Lua = {
diagnostics = {
globals = { 'vim' }
}
}
}
})
lsp.setup()
-- @todo: Look at luasnip and why I would require it
local cmp = require('cmp')
local cmp_action = require('lsp-zero').cmp_action()
cmp.setup({
sources = {
{ name = 'nvim_lsp' },
{ name = 'nvim_lua' },
{ name = 'buffer' },
},
preselect = 'item',
completion = {
autocomplete = false,
completeopt = 'menu,menuone,noinsert'
},
mapping = cmp.mapping.preset.insert({
-- Ctrl+Space to trigger completion menu
['<C-Space>'] = cmp.mapping.complete(),
-- Navigate between snippet placeholder
-- @note: don't know what this is
['<C-f>'] = cmp_action.luasnip_jump_forward(),
['<C-b>'] = cmp_action.luasnip_jump_backward(),
-- Scroll up and down in the completion documentation
['<C-u>'] = cmp.mapping.scroll_docs(-4),
['<C-d>'] = cmp.mapping.scroll_docs(4),
}),
snippet = {
expand = function(args)
require('luasnip').lsp_expand(args.body)
end,
},
})
end
},
}
|