forked from rafi/vim-config
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlspconfig.lua
More file actions
206 lines (174 loc) · 6.95 KB
/
lspconfig.lua
File metadata and controls
206 lines (174 loc) · 6.95 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
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
-- plugin: nvim-lspconfig
-- see: https://github.com/neovim/nvim-lspconfig
-- https://github.com/williamboman/mason.nvim
-- https://github.com/williamboman/mason-lspconfig.nvim
-- 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
-- Keyboard mappings
local opts = { noremap = true, silent = true }
map_buf('n', 'K', '<cmd>lua vim.lsp.buf.hover()<CR>', opts)
-- Short-circuit for Helm template files
if vim.bo[bufnr].buftype ~= '' or vim.bo[bufnr].filetype == 'helm' then
require('user').diagnostic.disable(bufnr)
return
end
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', '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 =vim.lsp.buf.list_workspace_folders()<CR>', opts)
map_buf('n', ',rn', '<cmd>lua vim.lsp.buf.rename()<CR>', opts)
map_buf('n', '<Leader>ca', '<cmd>lua vim.lsp.buf.code_action()<CR>', opts)
map_buf('x', '<Leader>ca', '<cmd>lua vim.lsp.buf.code_action()<CR>', opts)
map_buf('n', '<Leader>ce', '<cmd>lua vim.diagnostic.open_float()<CR>', opts)
if vim.fn.has('nvim-0.8') == 1 then
map_buf('n', ',f', '<cmd>lua vim.lsp.buf.format({ timeout_ms = 2000 })<CR>', opts)
else
map_buf('n', ',f', '<cmd>lua vim.lsp.buf.formatting(nil, 2000)<CR>', opts)
end
if client.supports_method('textDocument/rangeFormatting') then
map_buf('x', ',f', '<cmd>lua vim.lsp.buf.range_formatting()<CR>', opts)
end
-- lspsaga.nvim
-- See https://github.com/glepnir/lspsaga.nvim
-- 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)
-- lsp_signature.nvim
-- See https://github.com/ray-x/lsp_signature.nvim
-- Highlights: LspSignatureActiveParameter
-- require('lsp_signature').on_attach({
-- bind = true,
-- hint_prefix = ' ', --
-- handler_opts = { border = 'rounded' },
-- -- zindex = 50,
-- }, bufnr)
if client.config.flags then
client.config.flags.allow_incremental_sync = true
-- client.config.flags.debounce_text_changes = vim.opt.updatetime:get()
end
-- Set autocommands conditional on server capabilities
if client.supports_method('textDocument/documentHighlight') then
vim.api.nvim_exec([[
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
-- 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
local capabilities = require('cmp_nvim_lsp').default_capabilities()
c.capabilities = capabilities
-- 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
-- main
local function setup()
-- Config
vim.diagnostic.config({
virtual_text = false,
signs = true,
underline = true,
update_in_insert = false,
severity_sort = true,
})
-- Diagnostics signs and highlights
-- Error: ✘
-- Warn: ⚠
-- Hint:
-- Info: ⁱ
local signs = { Error = '✘', Warn = '', Hint = '', Info = 'ⁱ' }
for type, icon in pairs(signs) do
local hl = 'DiagnosticSign' .. type
vim.fn.sign_define(hl, { text = icon, texthl = hl, numhl = '' })
end
-- Setup CompletionItemKind symbols, see lua/lsp_kind.lua
-- require('lsp_kind').init()
-- Configure LSP Handlers
-- ---
-- Configure help hover (normal K) handler
vim.lsp.handlers['textDocument/hover'] = vim.lsp.with(
vim.lsp.handlers.hover, { border = 'rounded' }
)
-- Configure signature help (,s) handler
vim.lsp.handlers['textDocument/signatureHelp'] = vim.lsp.with(
vim.lsp.handlers.signature_help, { border = 'rounded' }
)
-- See https://github.com/folke/neodev.nvim
require('neodev').setup({})
-- Setup language servers using mason and mason-lspconfig
-- See https://github.com/williamboman/mason.nvim
-- and https://github.com/williamboman/mason-lspconfig.nvim
require('mason').setup()
local mason_lspconfig = require('mason-lspconfig')
mason_lspconfig.setup()
local packages = mason_lspconfig.get_installed_servers()
-- Setup language servers using nvim-lspconfig
local lspconfig = require('lspconfig')
for _, ls in pairs(packages) do
local opts = make_config(ls)
lspconfig[ls].setup(opts)
end
-- Reload if files were supplied in command-line arguments
if vim.fn.argc() > 0
and vim.fn.has('vim_starting')
and not vim.o.modified
then
-- triggers the FileType autocmd that starts the servers
vim.cmd('windo e')
end
-- global custom location-list diagnostics window toggle.
local args = { noremap = true, silent = true }
local function nmap(lhs, rhs) vim.api.nvim_set_keymap('n', lhs, rhs, args) end
nmap('<Leader>a', '<cmd>lua require("user").diagnostic.publish_loclist(true)<CR>')
nmap('[d', '<cmd>lua vim.diagnostic.goto_prev()<CR>')
nmap(']d', '<cmd>lua vim.diagnostic.goto_next()<CR>')
-- See https://github.com/kosayoda/nvim-lightbulb
require('nvim-lightbulb').setup({ ignore = { 'null-ls' } })
vim.api.nvim_exec([[
augroup user_lspconfig
autocmd!
" See https://github.com/kosayoda/nvim-lightbulb
autocmd CursorHold,CursorHoldI * lua require'nvim-lightbulb'.update_lightbulb()
" Update loclist with diagnostics for the current file
autocmd DiagnosticChanged * lua vim.diagnostic.setloclist({ open=false })
" Automatic diagnostic hover
" autocmd CursorHold * lua require("user").diagnostic.open_float({ focusable=false })
augroup END
]], false)
end
return {
setup = setup,
on_attach = on_attach,
}
-- vim: set ts=2 sw=2 tw=80 noet :