1 +require 'bugabinga.lsp.lightbulb' 7 -local map
= require 'std.keymap' 3 +local defer
= require 'std.defer' 4 +local map
= require 'std.map' 8 5 local auto
= require 'std.auto' 6 +local want
= require 'std.want' 9 7 local ui
= require 'bugabinga.ui' 10 8 local table = require 'std.table' 11 9 local ignored
= require 'std.ignored' 10 +local user_command
= vim.api.nvim_create_user_command
13 11 local lsp_client_configs
= require 'bugabinga.lsp.clients' 17 14 local token_update = function ( lsp_client )
33 -local expand_path = function(path)
34 - if vim.fn.executable(path)
== 1 then 35 - return vim.fn.exepath(path)
30 +local expand_path = function ( path )
31 + if vim.fn.executable( path )
== 1 then 32 + return vim.fn.exepath( path )
41 -local expand_command = function(command)
42 - vim.validate{
command = { command, {
'string',
'table' } }}
38 +local expand_command = function ( command )
39 + vim.validate {
command = { command, {
'string',
'table' } } }
44 - if type(command)
== 'table' then 45 - command[
1]
= expand_path(command[
1])
43 + if type( command )
== 'table' then 44 + command[
1]
= expand_path( command[
1] )
49 - return expand_path(command)
48 + return expand_path( command )
52 51 local lsp_start = function ( file_type_event )
53 54 local match
= file_type_event.match
55 56 local matches_to_ignore
= ignored.filetypes
56 58 if vim.iter( matches_to_ignore ):find( match )
then return end 60 60 local bufnr
= file_type_event.buf
61 61 local buffer_path
= vim.api.nvim_buf_get_name( bufnr )
65 65 local potential_client_configs
= vim.iter( lsp_client_configs )
66 66 :map(
function ( config )
68 - config = { config,
'table' },
68 + [
'config'] = { config,
'table' },
69 69 [
'config.name'] = { config.name,
'string' },
70 70 [
'config.filetypes'] = { config.filetypes, {
'string',
'table' } },
71 71 [
'config.command'] = { config.command, {
'string',
'table' } },
72 72 [
'config.environment'] = { config.environment,
'table',
true },
73 73 [
'config.root_dir'] = { config.root_dir,
'function',
'string' },
74 - [
'config.single_file_support'] = { config.single_file_support,
'boolean' },
74 + [
'config.single_file_support'] = { config.single_file_support,
'boolean',
true },
75 75 [
'config.settings'] = { config.settings,
'table',
true },
76 - [
'config.workspaces'] = { config.workspaces,
'boolean' },
76 + [
'config.workspaces'] = { config.workspaces,
'boolean',
true },
77 77 [
'config.before_init'] = { config.before_init,
'function',
true },
78 78 [
'config.init_options'] = { config.init_options,
'table',
true },
79 79 [
'config.capabilities'] = { config.capabilities,
'table',
true },
80 + [
'config.custom_start'] = { config.custom_start,
'function',
true },
83 - config.command
= type( config.command )
== 'string' and { expand_command(config.command) }
or expand_command(config.command)
84 + config.command
= type( config.command )
== 'string' and { expand_command( config.command ) }
or 85 + expand_command( config.command )
86 + config.environment
= config.environment
or {}
84 87 config.filetypes
= type( config.filetypes )
== 'string' and { config.filetypes }
or config.filetypes
88 + config.single_file_support
= config.single_file_support
== nil and false or config.single_file_support
89 + config.workspaces
= config.workspaces
== nil and false or config.workspaces
85 90 config.capabilities
= config.capabilities
or {}
86 91 config.init_options
= config.init_options
or {}
98 - local config
= vim.iter( potential_client_configs ):find(
99 - function ( _ )
return _.root_dir( buffer_path )
~= nil end 105 - config
= config
or vim.iter( potential_client_configs ):find(
106 - function ( _ )
return _.single_file_support
end 110 - vim.print(
'FOUND NO LSP CLIENT FOR', buffer_path)
102 + if table.is_empty( potential_client_configs )
then 114 - assert( config,
'a matching LSP client config shoud have been found by now!' )
107 + for _, config
in ipairs( potential_client_configs )
do 108 + local root_dir
= type( config.root_dir )
== 'string' and config.root_dir
or config.root_dir( buffer_path )
109 + local capabilities
= vim.lsp.protocol.make_client_capabilities()
110 + local cmp_capabilities
= want
'cmp_nvim_lsp' (
function ( cmp )
111 + return cmp.default_capabilities()
113 + capabilities
= table.extend(
'force', capabilities, cmp_capabilities, config.capabilities )
116 - local root_dir
= type(config.root_dir)
== "string" and config.root_dir
or config.root_dir( buffer_path )
117 - local capabilities
= vim.lsp.protocol.make_client_capabilities()
118 - capabilities
= table.extend(
"force", capabilities, config.capabilities)
116 + local start_config
= {
117 + cmd = config.command,
118 + cmd_cwd = root_dir,
122 + capabilities = capabilities,
124 + settings = config.settings,
126 + init_options = config.init_options,
127 + name = config.name,
130 + on_error =
function ( code,
... ) vim.print(
'LSP ERROR', code,
... )
end,
131 + before_init = config.before_init,
136 + flags = {
allow_incremental_sync =
true,
debounce_text_changes =
250,
exit_timeout =
200 },
137 + root_dir = root_dir,
120 - local start_config
= {
121 - cmd = config.command,
122 - cmd_cwd = root_dir,
126 - capabilities = capabilities,
128 - settings = config.settings,
130 - init_options = config.init_options,
131 - name = config.name,
134 - on_error =
function ( code,
... ) vim.print(
'LSP ERROR', code,
... )
end,
135 - before_init = config.before_init,
140 - flags = {
allow_incremental_sync =
true,
debounce_text_changes =
50,
exit_timeout =
200 },
141 - root_dir = root_dir,
140 + local lsp_starter
= config.custom_start
or vim.lsp.start
144 - local client_id
= vim.lsp.start( start_config, {
151 - vim.print(
"ATTACHING LSP CLIENT", client_id)
144 + lsp_starter( start_config, {
154 161 local old_formatexpr
155 162 local old_omnifunc
156 163 local old_tagfunc
166 +local add = function ( keybind )
167 + table.
insert( keybinds, keybind )
158 170 local lsp_attach = function ( args )
159 171 local bufnr
= args.buf
160 172 local client
= vim.lsp.get_client_by_id( args.data.client_id )
177 189 vim.bo[bufnr].omnifunc
= 'v:vim.lsp.omnifunc' 184 - name =
'Show available code actions on current line.',
187 - command = vim.lsp.buf.code_action,
191 - name =
'Goto to definition of symbol under cursor.',
194 - command = vim.lsp.buf.definition,
197 - name =
'Goto to definition of symbol under cursor.',
198 - category =
'telescope',
199 - keys =
'<c-2><c-2>',
200 - command =
'<cmd>Telescope lsp_definitions<cr>',
203 192 if client.server_capabilities.documentHighlightProvider
then 195 + local debounced_highlight
= defer.debounce_leading( vim.lsp.buf.document_highlight, delay )
196 + local debounced_clear
= defer.debounce_leading( vim.lsp.buf.clear_references, delay )
204 198 auto
'document_highlight' {
206 200 description =
'Highlight symbol under cursor on hold',
207 201 events =
'CursorHold',
208 - pattern =
'<buffer>',
209 - command = vim.lsp.buf.document_highlight,
203 + command = debounced_highlight,
213 206 description =
'Remove highlight from symbol under cursor on move',
214 207 events = {
'CursorMoved',
'InsertEnter' },
215 - pattern =
'<buffer>',
216 - command = vim.lsp.buf.clear_references,
209 + command = debounced_clear,
224 - if client.server_capabilities.executeCommandProvider
then 234 - name =
'Format current buffer',
220 + description =
'Format current buffer',
235 221 category =
'lsp',
237 225 command = vim.lsp.buf.format,
241 - name =
'Show hover documentation above cursor.',
229 + description =
'Show hover documentation above cursor.',
242 230 category =
'lsp',
232 + keys = {
'K',
'<leader>lk' },
244 233 command = vim.lsp.buf.hover,
248 - name =
'Show implementations of symbol under cursor.',
237 + description =
'Show signature help under cursor.',
249 238 category =
'lsp',
251 - command = vim.lsp.buf.implementation,
254 - name =
'Show implementations of symbol under cursor.',
255 - category =
'telescope',
256 - keys =
'<c-4><c-4>',
257 - command =
'<cmd>Telescope lsp_implementations<cr>',
261 - name =
'Show incoming calls of symbol under cursor.',
264 - command = vim.lsp.buf.incoming_calls,
267 - name =
'Show incoming calls of symbol under cursor.',
268 - category =
'telescope',
269 - keys =
'<c-5><c-5>',
270 - command =
'<cmd>Telescope lsp_incoming_calls<cr>',
273 - name =
'Show outgoing calls of symbol under cursor.',
276 - command = vim.lsp.buf.outgoing_calls,
279 - name =
'Show outgoing calls of symbol under cursor.',
280 - category =
'telescope',
281 - keys =
'<c-s-5><c-s-5>',
282 - command =
'<cmd>Telescope lsp_outgoing_calls<cr>',
286 - name =
'Show references of symbol under cursor.',
289 - command = vim.lsp.buf.references,
292 - name =
'Show references of symbol under cursor.',
293 - category =
'telescope',
294 - keys =
'<c-6><c-6>',
295 - command = vim.lsp.buf.references,
299 - name =
'Rename symbol under cursor.',
302 - command = vim.lsp.buf.rename,
306 - name =
'Show signature help under cursor.',
240 + keys =
'<leader>ls',
309 241 command = vim.lsp.buf.signature_help,
313 - name =
'Show signature help under cursor.',
314 - category =
'telescope',
315 - keys =
'<c-7><c-7>',
316 - command =
'<cmd>Telescope lsp_signature_help<cr>',
245 + description =
'Show available code actions on current line.',
248 + keys =
'<leader>la',
249 + command = vim.lsp.buf.code_action,
253 + description =
'Goto to definition of symbol under cursor.',
256 + keys =
'<leader>ld',
257 + command = vim.lsp.buf.definition,
261 + description =
'Show implementations of symbol under cursor.',
264 + keys =
'<leader>li',
265 + command = vim.lsp.buf.implementation,
269 + description =
'Show incoming calls of symbol under cursor.',
272 + keys =
'<leader>lci',
273 + command = vim.lsp.buf.incoming_calls,
277 + description =
'Show outgoing calls of symbol under cursor.',
280 + keys =
'<leader>lco',
281 + command = vim.lsp.buf.outgoing_calls,
285 + description =
'Show references of symbol under cursor.',
288 + keys =
'<leader>lr',
289 + command = vim.lsp.buf.references,
293 + description =
'Rename symbol under cursor.',
296 + keys = {
'<f2>',
'<leader>ln' },
297 + command = vim.lsp.buf.rename,
320 301 local lsp_detach = function ( args )
335 316 if client.server_capabilities.completionProvider
then 336 317 vim.bo[bufnr].omnifunc
= old_omnifunc
320 + for _, keybind_delete
in ipairs(keybinds)
do 363 349 local lsp_info = function ( command )
364 350 local all_or_current
= command.args
and command.args
== 'all' and {}
or {
bufnr =
0 }
365 - local active_clients
= vim.lsp.get_clients( all_or_current )
351 + local active_clients
= vim.lsp.get_active_clients( all_or_current )
367 353 local infos
= vim.iter( active_clients )
368 354 :map(
function ( client )
395 381 ui.show_tree( active_clients )
398 -vim.api.nvim_create_user_command(
'LspInfo',
402 - complete =
function ()
return {
'all' }
end,
403 - desc =
'Shows basic information about running LSP clients.',
384 +user_command(
'LspInfo', lsp_info, {
386 + complete =
function ()
return {
'all' }
end,
387 + desc =
'Shows basic information about running LSP clients.',
390 +user_command(
'LspInfoExtended', lsp_info_extended, {
392 + complete =
function ()
return {
'all' }
end,
393 + desc =
'Shows detailed information about running LSP clients.',
406 -vim.api.nvim_create_user_command(
'LspInfoExtended',
410 - complete =
function ()
return {
'all' }
end,
411 - desc =
'Shows detailed information about running LSP clients.',
413 396 return setmetatable( _, {} )