forked from rafi/vim-config
-
Notifications
You must be signed in to change notification settings - Fork 3
/
lspconfig.lua
218 lines (194 loc) · 7.67 KB
/
lspconfig.lua
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
-- plugin: nvim-lspconfig
-- see: https://github.com/neovim/nvim-lspconfig
-- https://github.com/kabouzeid/nvim-lspinstall
-- https://github.com/ray-x/lsp_signature.nvim
-- https://github.com/kosayoda/nvim-lightbulb
-- rafi settings
-- Buffer attached
local on_attach = function(client, bufnr)
local function map_buf(...) vim.api.nvim_buf_set_keymap(bufnr, ...) end
-- local function opt_buf(...) vim.api.nvim_buf_set_option(bufnr, ...) end
-- Disable diagnostics for Helm template files
if vim.bo[bufnr].buftype ~= '' or vim.bo[bufnr].filetype == 'helm' then
vim.lsp.diagnostic.disable()
return
end
if client.config.flags then
client.config.flags.allow_incremental_sync = true
client.config.flags.debounce_text_changes = 100
end
-- Keyboard mappings
local opts = { noremap = true, silent = true }
map_buf('n', 'gD', '<cmd>lua vim.lsp.buf.declaration()<CR>', opts)
map_buf('n', 'gd', '<cmd>lua vim.lsp.buf.definition()<CR>', opts)
map_buf('n', 'gr', '<cmd>lua vim.lsp.buf.references()<CR>', opts)
map_buf('n', 'gy', '<cmd>lua vim.lsp.buf.type_definition()<CR>', opts)
map_buf('n', 'K', '<cmd>lua vim.lsp.buf.hover()<CR>', opts)
map_buf('n', 'gi', '<cmd>lua vim.lsp.buf.implementation()<CR>', opts)
map_buf('n', ',s', '<cmd>lua vim.lsp.buf.signature_help()<CR>', opts)
map_buf('n', ',wa', '<cmd>lua vim.lsp.buf.add_workspace_folder()<CR>', opts)
map_buf('n', ',wr', '<cmd>lua vim.lsp.buf.remove_workspace_folder()<CR>', opts)
map_buf('n', ',wl', '<cmd>lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<CR>', opts)
map_buf('n', ',rn', '<cmd>lua vim.lsp.buf.rename()<CR>', opts)
map_buf('n', '[d', '<cmd>lua vim.lsp.diagnostic.goto_prev()<CR>', opts)
map_buf('n', ']d', '<cmd>lua vim.lsp.diagnostic.goto_next()<CR>', opts)
map_buf('n', '<Leader>ca', '<cmd>lua vim.lsp.buf.code_action()<CR>', opts)
map_buf('n', '<Leader>ce', '<cmd>lua vim.lsp.diagnostic.show_line_diagnostics()<CR>', opts)
-- lspsaga
-- buf_set_keymap('n', '<Leader>f', '<cmd>lua require("lspsaga.provider").lsp_finder()<CR>', opts)
-- buf_set_keymap('n', 'K', '<cmd>lua require("lspsaga.hover").render_hover_doc()<CR>', opts)
-- buf_set_keymap('n', ',s', '<cmd>lua require("lspsaga.signaturehelp").signature_help()<CR>', opts)
-- buf_set_keymap('n', ',rn', '<cmd>lua require("lspsaga.rename").rename()<CR>', opts)
-- buf_set_keymap('n', '<Leader>ca', '<cmd>lua require("lspsaga.codeaction").code_action()<CR>', opts)
-- buf_set_keymap('v', '<Leader>ca', ':<C-u>lua require("lspsaga.codeaction").range_code_action()<CR>', opts)
-- See https://github.com/ray-x/lsp_signature.nvim
require('lsp_signature').on_attach({
bind = true,
hint_enable = false,
hint_prefix = ' ', --
handler_opts = { border = 'shadow' },
zindex = 50,
}, bufnr)
-- Set some keybinds conditional on server capabilities
if client.resolved_capabilities.document_formatting then
map_buf('n', ',f', '<cmd>lua vim.lsp.buf.formatting()<CR>', opts)
end
if client.resolved_capabilities.document_range_formatting then
map_buf('x', ',f', '<cmd>lua vim.lsp.buf.range_formatting()<CR>', opts)
end
-- Set autocommands conditional on server_capabilities
if client.resolved_capabilities.document_highlight then
vim.api.nvim_exec([[
highlight! LspReferenceRead ctermbg=237 guibg=#3D3741
highlight! LspReferenceText ctermbg=237 guibg=#373B41
highlight! LspReferenceWrite ctermbg=237 guibg=#374137
augroup lsp_document_highlight
autocmd! * <buffer>
autocmd CursorHold <buffer> lua vim.lsp.buf.document_highlight()
autocmd CursorMoved <buffer> lua vim.lsp.buf.clear_references()
augroup END
]], false)
end
end
-- Diagnostics signs and highlights
-- Error: ✘
-- Warning: ⚠
-- Hint:
-- Information: ⁱ
local signs = { Error = '✘', Warning = '', Hint = '', Information = 'ⁱ'}
for type, icon in pairs(signs) do
local hl = 'LspDiagnosticsSign' .. type
vim.fn.sign_define(hl, { text = icon, texthl = hl, numhl = hl })
end
-- Setup CompletionItemKind symbols, see lua/lsp_kind.lua
-- require('lsp_kind').init()
-- Configure diagnostics publish settings
vim.lsp.handlers['textDocument/publishDiagnostics'] = vim.lsp.with(
vim.lsp.diagnostic.on_publish_diagnostics, {
update_in_insert = false,
underline = false,
virtual_text = {
spacing = 4,
-- prefix = '',
},
signs = true,
-- signs = function(bufnr, _)
-- return vim.bo[bufnr].buftype == ''
-- end,
}
)
-- Open references in quickfix window and jump to first item.
-- local on_references = vim.lsp.handlers['textDocument/references']
-- vim.lsp.handlers['textDocument/references'] = vim.lsp.with(
-- on_references, {
-- -- Use location list instead of quickfix list
-- loclist = true,
-- }
-- )
-- vim.lsp.handlers['textDocument/references'] = function(_, _, result, _, bufnr, _)
-- if not result or vim.tbl_isempty(result) then
-- vim.notify('No references found')
-- else
-- vim.lsp.util.set_qflist(vim.lsp.util.locations_to_items(result, bufnr))
-- require('user').qflist.open()
-- -- vim.api.nvim_command('.cc')
-- end
-- end
-- Configure hover (normal K) handle
vim.lsp.handlers['textDocument/hover'] = vim.lsp.with(
vim.lsp.handlers.hover, {
-- Use a sharp border with `FloatBorder` highlights
border = 'rounded'
}
)
-- Combine base config for each server and merge user-defined settings.
local function make_config(server_name)
-- Setup base config for each server.
local c = {}
c.on_attach = on_attach
c.capabilities = vim.lsp.protocol.make_client_capabilities()
c.capabilities = require('cmp_nvim_lsp').update_capabilities(c.capabilities)
-- c.capabilities.textDocument.completion.completionItem.snippetSupport = true
-- c.capabilities.textDocument.completion.completionItem.resolveSupport = {
-- properties = {
-- 'documentation',
-- 'detail',
-- 'additionalTextEdits',
-- }
-- }
-- Merge user-defined lsp settings.
-- These can be overridden locally by lua/lsp-local/<server_name>.lua
local exists, module = pcall(require, 'lsp-local.'..server_name)
if not exists then
exists, module = pcall(require, 'lsp.'..server_name)
end
if exists then
local user_config = module.config(c)
for k, v in pairs(user_config) do c[k] = v end
end
return c
end
-- Iterate and setup all language servers and trigger FileType in windows.
local function setup_servers()
local lsp_install = require('lspinstall')
lsp_install.setup()
local servers = lsp_install.installed_servers()
for _, server in pairs(servers) do
local config = make_config(server)
require'lspconfig'[server].setup(config)
end
-- Reload if files were supplied in command-line arguments
if vim.fn.argc() > 0 and not vim.o.modified then
vim.cmd('windo e') -- triggers the FileType autocmd that starts the server
end
end
-- main
if vim.fn.has('vim_starting') then
-- Setup LSP with lspinstall
setup_servers()
-- Automatically reload after `:LspInstall <server>`
require'lspinstall'.post_install_hook = function()
setup_servers() -- reload installed servers
if not vim.bo.modified and vim.bo.buftype == '' then
vim.cmd('bufdo e') -- starts server by triggering the FileType autocmd
end
end
-- global custom location-list diagnostics window toggle.
local args = { noremap = true, silent = true }
vim.api.nvim_set_keymap(
'n',
'<Leader>a',
'<cmd>lua require("user").diagnostic.publish_loclist(true)<CR>',
args
)
vim.api.nvim_exec([[
augroup user_lspconfig
autocmd!
" See https://github.com/kosayoda/nvim-lightbulb
" autocmd CursorHold,CursorHoldI * lua require'nvim-lightbulb'.update_lightbulb()
" Automatic diagnostic hover
" autocmd CursorHold * lua vim.lsp.diagnostic.show_line_diagnostics({ focusable=false })
augroup END
]], false)
end
return { make_config = make_config }