forked from nvim-lua/kickstart.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlsp.lua
More file actions
143 lines (121 loc) · 4.94 KB
/
lsp.lua
File metadata and controls
143 lines (121 loc) · 4.94 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
-- LSP settings
local ok, lsp_zero = pcall(require, "lsp-zero")
if not ok then
return
end
local lsp = lsp_zero.preset({})
-- Setup neovim lua configuration
local neodev_ok, neodev = pcall(require, 'neodev')
if neodev_ok then
neodev.setup({})
end
lsp.on_attach(function(client, bufnr)
-- TODO: keymappings ...
-- lsp.default_keymaps({buffer = bufnr})
-- In this case, we create a function that lets us more easily define mappings specific
-- for LSP related items. It sets the mode, buffer and description for us each time.
if client.server_capabilities.documentSymbolProvider then
require("nvim-navic").attach(client, bufnr)
end
local vmap = function(keys, func, desc)
if desc then
desc = 'LSP: ' .. desc
end
vim.keymap.set('v', keys, func, { buffer = bufnr, desc = desc })
end
local nmap = function(keys, func, desc)
if desc then
desc = 'LSP: ' .. desc
end
vim.keymap.set('n', keys, func, { buffer = bufnr, desc = desc })
end
nmap('K', vim.lsp.buf.hover, 'Hover Documentation')
nmap('<leader>lr', vim.lsp.buf.rename, '[R]ename')
nmap('<leader>la', vim.lsp.buf.code_action, 'Code [A]ction')
vmap('<leader>la', vim.lsp.buf.code_action, 'Code [A]ction')
nmap('<leader>ld', require('telescope.builtin').lsp_document_symbols, '[D]ocument symbols')
nmap('gd', require('telescope.builtin').lsp_definitions, '[G]oto [D]efinition')
nmap('gD', require('telescope.builtin').lsp_type_definitions, 'Type [D]efinition')
nmap('gr', require('telescope.builtin').lsp_references, '[G]oto [R]eferences')
nmap('gI', require('telescope.builtin').lsp_implementations, '[G]oto [I]mplementation')
-- nmap('<leader>ws', require('telescope.builtin').lsp_dynamic_workspace_symbols, '[W]orkspace [S]ymbols')
-- Lesser used LSP functionality
-- nmap('<leader>wa', vim.lsp.buf.add_workspace_folder, '[W]orkspace [A]dd Folder')
-- nmap('<leader>wr', vim.lsp.buf.remove_workspace_folder, '[W]orkspace [R]emove Folder')
-- nmap('<leader>wl', function()
-- print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
-- end, '[W]orkspace [L]ist Folders')
-- Create a command `:Format` local to the LSP buffer
vim.api.nvim_buf_create_user_command(bufnr, 'Format', function(_)
vim.lsp.buf.format {
-- disable null-ls is tsserver is attached...
filter = function(c)
if c.name == "null-ls" and #vim.lsp.get_active_clients { name = "tsserver", bufnr = 0 } == 0 then
return true
end
return c.name == "tsserver"
end,
}
end, { desc = 'Format current buffer with LSP' })
end)
-- -- When you don't have mason.nvim installed
-- -- You'll need to list the servers installed in your system
-- -- lsp.setup_servers({'tsserver', 'eslint'})
-- lsp.setup_servers({'tsserver'})
-- -- (Optional) Configure lua language server for neovim
-- require('lspconfig').lua_ls.setup(lsp.nvim_lua_ls())
lsp.setup()
-- You need to setup `cmp` after lsp-zero
local cmp_ok, cmp = pcall(require, 'cmp')
if not cmp_ok then
return
end
local cmp_action = require('lsp-zero').cmp_action()
local luasnip = require 'luasnip'
-- util function
local function has_words_before()
local line, col = unpack(vim.api.nvim_win_get_cursor(0))
return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match "%s" == nil
end
cmp.setup({
mapping = {
["<Up>"] = cmp.mapping.select_prev_item { behavior = cmp.SelectBehavior.Select },
["<Down>"] = cmp.mapping.select_next_item { behavior = cmp.SelectBehavior.Select },
["<C-p>"] = cmp.mapping.select_prev_item { behavior = cmp.SelectBehavior.Insert },
["<C-n>"] = cmp.mapping.select_next_item { behavior = cmp.SelectBehavior.Insert },
["<C-k>"] = cmp.mapping.select_prev_item { behavior = cmp.SelectBehavior.Insert },
["<C-j>"] = cmp.mapping.select_next_item { behavior = cmp.SelectBehavior.Insert },
["<C-u>"] = cmp.mapping(cmp.mapping.scroll_docs(-4), { "i", "c" }),
["<C-d>"] = cmp.mapping(cmp.mapping.scroll_docs(4), { "i", "c" }),
["<C-Space>"] = cmp.mapping(cmp.mapping.complete(), { "i", "c" }),
["<C-y>"] = cmp.config.disable,
["<C-e>"] = cmp.mapping { i = cmp.mapping.abort(), c = cmp.mapping.close() },
["<CR>"] = cmp.mapping.confirm { select = false },
["<Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item()
elseif luasnip.expand_or_jumpable() then
luasnip.expand_or_jump()
elseif has_words_before() then
cmp.complete()
else
fallback()
end
end, { "i", "s" }),
["<S-Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_prev_item()
elseif luasnip.jumpable(-1) then
luasnip.jump(-1)
else
fallback()
end
end, { "i", "s" }),
},
sources = cmp.config.sources {
{ name = "nvim_lsp", priority = 1000 },
{ name = "luasnip", priority = 750 },
{ name = "buffer", priority = 500 },
{ name = "path", priority = 250 },
},
})