1 -local dbg
= require 'std.dbg' 2 -local icon
= require 'std.icon' 3 -local defer
= require 'std.defer' 4 -local map
= require 'std.map' 5 -local auto
= require 'std.auto' 6 -local table = require 'std.table' 7 -local ignored
= require 'std.ignored' 8 -local user_command
= require 'std.user_command' 9 -local lsp_client_configs
= require 'bugabinga.lsp.clients' 10 -local localrc
= require 'std.localrc' 1 +local puk
= require 'std.puk' 2 +local project
= require 'std.project' 3 +local dbg
= require 'std.dbg' 4 +local icon
= require 'std.icon' 5 +local map
= require 'std.map' 6 +local auto
= require 'std.auto' 7 +local table = require 'std.table' 8 +local localrc
= require 'std.localrc' 14 -local token_update = function ( lsp_client )
15 - local token
= lsp_client.data.token
16 - local buffer
= lsp_client.buffer
17 - local client_id
= lsp_client.data.client_id
18 - local mutable_variable_highlight
= 'MutableVariable' 20 - if token.type
== 'variable' and not token.modifiers.readonly
then 21 - vim.lsp.semantic_tokens.highlight_token( token, buffer, client_id, mutable_variable_highlight )
11 + vim.lsp.set_log_level(
'debug')
30 -local expand_path = function ( path )
31 - if vim.fn.executable( path )
== 1 then 32 - return vim.fn.exepath( path )
14 + local settings_override
= project_local_lsp_settings[language_server_name]
15 + if settings_override
then 16 + return settings_override
38 -local expand_command = function ( command )
39 - vim.validate {
command = { command, {
'string',
'table' } } }
41 - if type( command )
== 'table' then 42 - command[
1]
= expand_path( command[
1] )
46 - return expand_path( command )
49 -local lsp_start = function ( file_type_event )
50 - dbg.print
'trying to start lsp' 51 - local match
= file_type_event.match
53 - dbg.print(
'filetypes to ignore', ignored.filetypes,
'current filetype', match )
54 - if vim.iter( ignored.filetypes ):find( match )
then return end 56 - local bufnr
= file_type_event.buf
57 - local buftype
= vim.api.nvim_get_option_value(
'buftype', {
buf = bufnr } )
58 - dbg.print(
'buftypes to ignore', ignored.buftypes,
'current buftype', buftype )
59 - if vim.iter( ignored.buftypes ):find( buftype )
then return end 61 - local buffer_path
= vim.api.nvim_buf_get_name( bufnr )
63 - dbg.print(
string.
format(
'searching lsp client for: match: %s, buffer: %s, path: %s', match, bufnr, buffer_path ) )
65 - local potential_client_configs
= vim.iter( lsp_client_configs )
66 - :map(
function ( config )
68 - [
'config'] = { config,
'table' },
69 - [
'config.name'] = { config.name,
'string' },
70 - [
'config.filetypes'] = { config.filetypes, {
'string',
'table' } },
71 - [
'config.command'] = { config.command, {
'string',
'table' } },
72 - [
'config.environment'] = { config.environment,
'table',
true },
73 - [
'config.root_dir'] = { config.root_dir,
'function',
'string' },
74 - [
'config.single_file_support'] = { config.single_file_support,
'boolean',
true },
75 - [
'config.settings'] = { config.settings,
'table',
true },
76 - [
'config.workspaces'] = { config.workspaces,
'boolean',
true },
77 - [
'config.before_init'] = { config.before_init, {
'function',
'table' },
true },
78 - [
'config.init_options'] = { config.init_options,
'table',
true },
79 - [
'config.capabilities'] = { config.capabilities,
'table',
true },
80 - [
'config.custom_start'] = { config.custom_start,
'function',
true },
84 - config.command
= type( config.command )
== 'string' and { expand_command( config.command ) }
or 85 - expand_command( config.command )
86 - config.environment
= config.environment
or {}
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
90 - config.capabilities
= config.capabilities
or {}
91 - config.init_options
= config.init_options
or {}
95 - :filter(
function ( config )
96 - return vim.iter( config.filetypes ):find( match )
100 - dbg.print(
'POTENTIAL CLIENTS',
101 - vim.iter( potential_client_configs ):map(
function ( config )
103 - name = config.name,
110 - if table.is_empty( potential_client_configs )
then 111 - dbg.print(
'found no lsp client for', buffer_path )
117 - local project_local_lsp_settings
= localrc(
'.lsp.settings.lua',
'table' )
or {}
118 - local project_local_workspace_folders
= localrc(
'.lsp.workspace_folders.lua',
'table' )
or {}
121 - for lsp, folders
in pairs( project_local_workspace_folders )
do 122 - for idx, path
in ipairs( folders )
do 123 - local cwd
= vim.uv.cwd()
124 - local abs_path
= vim.fs.normalize( vim.fs.joinpath( cwd, path ) )
126 - local exists
= vim.uv.fs_stat( abs_path )
127 - if not exists
or exists.type
~= 'directory' then 128 - vim.error(
'the project local workspace folder ' .. 129 - abs_path
.. ' does not exist or is not a directory.' )
132 - project_local_workspace_folders[lsp][idx]
= {
uri =
'file://' .. abs_path,
name = path }
136 - dbg.print(
'patched project local workspace folders', project_local_workspace_folders )
138 - for _, config
in ipairs( potential_client_configs )
do 139 - local path
= type( config.command )
== 'table' and config.command[
1]
or config.command
140 - if vim.fn.executable( path )
~= 1 then 141 - vim.notify(
'The LSP ' .. config.name
.. ' does not seem to be installed!' )
23 +local language_servers
= {
25 + root_dir = project.find_lua_project_root,
28 + hint = {
enable =
true,
setType =
true, },
29 + telemetry = {
enable =
false, },
34 + jdtls = {
root_dir = project.find_java_project_root, },
35 + zls = {
root_dir = project.find_zig_project_root, },
36 + ziggy = {
root_dir = project.find_vcs_project_root, },
37 + ziggy_schema = {
root_dir = project.find_vcs_project_root, },
38 + asm_lsp = {
root_dir = project.find_vcs_project_root, },
39 + clangd = {
root_dir = project.find_vcs_project_root, },
40 + denols = {
root_dir = project.find_vcs_project_root, },
41 + dotls = {
root_dir = project.find_vcs_project_root, },
42 + docker_compose_language_service = {
root_dir = project.find_vcs_project_root, },
43 + dockerls = {
root_dir = project.find_vcs_project_root, },
44 + ast_grep = {
root_dir = project.find_vcs_project_root, },
45 + cssls = {
root_dir = project.find_vcs_project_root, },
46 + css_variables = {
root_dir = project.find_vcs_project_root, },
47 + emmet_language_server = {
root_dir = project.find_vcs_project_root, },
48 + gopls = {
root_dir = project.find_vcs_project_root, },
49 + gradle_ls = {
root_dir = project.find_vcs_project_root, },
50 + harper_ls = {
root_dir = project.find_vcs_project_root, },
51 + html = {
root_dir = project.find_vcs_project_root, },
52 + htmx = {
root_dir = project.find_vcs_project_root, },
53 + hyprls = {
root_dir = project.find_vcs_project_root, },
54 + jsonls = {
root_dir = project.find_vcs_project_root, },
55 + lemminx = {
root_dir = project.find_vcs_project_root, },
56 + lsp_ai = {
root_dir = project.find_vcs_project_root, },
57 + ltex = {
root_dir = project.find_vcs_project_root, },
58 + marksman = {
root_dir = project.find_vcs_project_root, },
59 + markdown_oxide = {
root_dir = project.find_vcs_project_root, },
60 + nil_ls = {
root_dir = project.find_vcs_project_root, },
61 + nixd = {
root_dir = project.find_vcs_project_root, },
62 + nushell = {
root_dir = project.find_vcs_project_root, },
63 + ocamllsp = {
root_dir = project.find_vcs_project_root, },
64 + protols = {
root_dir = project.find_vcs_project_root, },
65 + roc_ls = {
root_dir = project.find_vcs_project_root, },
66 + rust_analyzer = {
root_dir = project.find_vcs_project_root, },
67 + statix = {
root_dir = project.find_vcs_project_root, },
68 + superhtml = {
root_dir = project.find_vcs_project_root, },
69 + tabby_ml = {
root_dir = project.find_vcs_project_root, },
70 + taplo = {
root_dir = project.find_vcs_project_root, },
71 + ts_ls = {
root_dir = project.find_vcs_project_root, },
72 + zk = {
root_dir = project.find_vcs_project_root, },
75 +puk.defer
'lspconfig' (
function(lspconfig)
76 + for server, config
in pairs(language_servers)
do 145 77 local capabilities
= vim.lsp.protocol.make_client_capabilities()
146 - capabilities
= table.extend(
'force', capabilities, config.capabilities )
148 - local settings
= config.settings
149 - local settings_override
= project_local_lsp_settings[config.name]
150 - if settings_override
then 151 - dbg.print(
'overriding settings with project local settings', settings, settings_override )
152 - settings
= vim.tbl_deep_extend(
'force', settings, settings_override )
155 - local root_dir
= type( config.root_dir )
== 'string' and config.root_dir
or config.root_dir( buffer_path )
156 - local root_workspace
= root_dir
and {
uri =
'file://' .. root_dir,
name = root_dir }
or nil 157 - local workspace_folders
= root_workspace
and { root_workspace }
or nil 158 - local workspace_folders_local
= project_local_workspace_folders[config.name]
159 - if not workspace_folders_local
then dbg.print(
'no local workspace folders found for ' .. config.name )
end 160 - if workspace_folders_local
and config.workspaces
then 161 - dbg.print(
'setting workspace folders to project local workspace folders', workspace_folders_local )
162 - workspace_folders
= workspace_folders
and 163 - vim.tbl_deep_extend(
'force', workspace_folders, workspace_folders_local )
or workspace_folders_local
166 - dbg.print(
'starting command:', config.command )
168 - local start_config
= {
169 - cmd = config.command,
170 - cmd_cwd = root_dir,
173 - root_dir = root_dir,
174 - workspace_folders = workspace_folders,
175 - capabilities = capabilities,
177 - settings = settings,
179 - init_options = config.init_options,
180 - name = config.name,
183 - on_error =
function ( code,
... ) vim.print(
'LSP ERROR', code,
... )
end,
184 - before_init = config.before_init,
186 - on_exit =
function ( code, signal, client_id ) dbg.print(
'LSP CLIENT EXIT', code, signal, client_id )
end,
187 - on_attach =
function ( client, attach_bufnr ) dbg.print(
'LSP CLIENT ATTACH', client, attach_bufnr )
end,
188 - trace = dbg.get()
and 'on' or 'off',
189 - flags = {
allow_incremental_sync =
true,
debounce_text_changes =
250,
exit_timeout =
200 },
192 - local lsp_starter
= config.custom_start
or vim.lsp.start
194 - dbg.print(
'starting lsp', start_config.name, root_dir )
196 - lsp_starter( start_config, {
200 - reuse_client =
function ( existing_client, new_config )
202 - 'lsp reuse client',
203 - existing_client.name,
204 - existing_client.workspace_folders,
206 - new_config.root_dir
210 - local same_name
= existing_client.name
== new_config.name
212 - local new_root_raw
= new_config.root_dir
or nil 213 - local existing_root_raw
= existing_client.workspace_folders
and #existing_client.workspace_folders
>= 1 and 214 - existing_client.workspace_folders[
1].name
or nil 216 - if not (new_root_raw
and existing_root_raw)
then return false end 218 - local new_root
= vim.fs.normalize( new_root_raw )
219 - local existing_root
= vim.fs.normalize( existing_root_raw )
220 - local same_root
= new_root
== existing_root
222 - local reuse
= same_name
and same_root
223 - dbg.print(
'reusing lsp client?', reuse )
228 - dbg.print(
'STARTING LSP CLIENT', config.name )
78 + config.capabilities
= require(
'blink.cmp').get_lsp_capabilities(config.capabilities)
79 + capabilities
= table.extend(
'force', capabilities, config.capabilities)
80 + local settings
= config.settings
or {}
81 + local settings_override
= local_settings(server)
or {}
82 + config.settings
= table.extend(
'force', settings, settings_override)
83 + lspconfig[server].setup(config)
235 -local lsp_attach = function ( args )
87 +local lsp_attach = function(args)
236 88 local bufnr
= args.buf
237 89 local client_id
= args.data.client_id
238 - local client
= vim.lsp.get_client_by_id( client_id )
90 + local client
= vim.lsp.get_client_by_id(client_id)
239 91 if not client
then return end 241 - dbg.print( client.server_capabilities )
243 - if client.server_capabilities.definitionProvider
then 244 - vim.bo[bufnr].tagfunc
= 'v:lua.vim.lsp.tagfunc' 247 - if client.server_capabilities.documentFormattingProvider
then 248 - vim.bo[bufnr].formatexpr
= 'v:lua.vim.lsp.formatexpr' 251 - if client.server_capabilities.completionProvider
then 252 - vim.bo[bufnr].omnifunc
= 'v:lua.vim.lsp.omnifunc' 93 + vim.lsp.completion.enable(
true, client_id, bufnr)
255 95 if client.server_capabilities.inlayHintProvider
then 260 100 keys
= '<leader>ti',
261 - command = function ()
262 - vim.lsp.inlay_hint.enable(
not vim.lsp.inlay_hint.is_enabled() )
101 + command = function()
102 + vim.lsp.inlay_hint.enable(
not vim.lsp.inlay_hint.is_enabled())
268 - description =
'Show hover documentation above cursor.',
271 - keys = {
'K',
'<leader>lk' },
272 - command = vim.lsp.buf.hover,
276 - description =
'Show signature help under cursor.',
279 - keys = {
'<C-h>' },
280 - command = vim.lsp.buf.signature_help,
284 - description =
'Show available code actions on current line.',
287 - keys =
'<leader>la',
288 - command = vim.lsp.buf.code_action,
292 - description =
'Goto to definition of symbol under cursor.',
295 - keys =
'<leader>ld',
296 - command = vim.lsp.buf.definition,
300 - description =
'Show implementations of symbol under cursor.',
303 - keys =
'<leader>li',
304 - command = vim.lsp.buf.implementation,
308 - description =
'Show incoming calls of symbol under cursor.',
311 - keys =
'<leader>lc',
312 - command = vim.lsp.buf.incoming_calls,
316 - description =
'Show outgoing calls of symbol under cursor.',
319 - keys =
'<leader>lC',
320 - command = vim.lsp.buf.outgoing_calls,
324 - description =
'Show references of symbol under cursor.',
327 - keys =
'<leader>ln',
328 - command = vim.lsp.buf.references,
332 - description =
'Rename symbol under cursor.',
335 - keys = {
'<f2>',
'<leader>lr' },
336 - command = vim.lsp.buf.rename,
342 - description =
'Start or reuse LSP client',
343 - events =
'FileType',
344 - command = lsp_start,
347 - description =
'Hook: LSP client attached to buffer',
348 - events =
'LspAttach',
349 - command = lsp_attach,
352 - description =
'Semantic Tokens from the LSP',
353 - events =
'LspTokenUpdate',
354 - command = token_update,
109 + description =
'lsp: client attached to buffer',
110 + events =
'LspAttach',
111 + command = lsp_attach,
358 -local lsp_info = function ( command )
359 - local all_or_current
= command.args
and command.args
== 'all' and {}
or {
bufnr =
0 }
360 - local active_clients
= vim.lsp.get_clients( all_or_current )
362 - local infos
= vim.iter( active_clients )
363 - :map(
function ( client )
364 - local name
= client.name
365 - local config
= vim.iter( lsp_client_configs ):find(
function ( config )
return config.name
== name
end )
366 - assert( config, (
'How did we not find a config for %s ?!?!'):format( name ) )
368 - local full_cmd
= table.deep_copy( client.config.cmd )
369 - full_cmd[
1]
= vim.fn.exepath( full_cmd[
1] )
373 - cmd =
table.
concat( full_cmd,
' ' ),
374 - workspace_folders = vim.iter( client.workspace_folders ):map(
function ( w )
return vim.fs.normalize( w.name )
end )
376 - filetypes =
table.
concat( config.filetypes,
', ' ),
377 - single_file_support = config.single_file_support,
378 - workspaces = config.workspaces,
384 - vim.print( vim.inspect( infos ) )
387 -local lsp_info_extended = function ( command )
388 - local all_or_current
= command.args
and command.args
== 'all' and {}
or {
bufnr =
0 }
389 - local active_clients
= vim.lsp.get_clients( all_or_current )
390 - vim.print( vim.inspect( active_clients ) )
393 -user_command.LspInfo
394 -'Shows basic information about running LSP clients.' 395 - {
nargs =
'?',
complete =
function ()
return {
'all' }
end } (
399 -user_command.LspInfoExtended
400 -'Shows detailed information about running LSP clients.' 401 - {
nargs =
'?',
complete =
function ()
return {
'all' }
end } (
409 114 local completion_icons
= {
410 115 Class = icon.class,
411 116 Color = icon.color,
433 138 local completion_kinds
= vim.lsp.protocol.CompletionItemKind
434 -for i, kind
in ipairs( completion_kinds )
do 139 +for i, kind
in ipairs(completion_kinds)
do 435 140 completion_kinds[i]
= completion_icons[kind]
or kind
439 -vim.lsp.handlers[
'textDocument/hover']
= vim.lsp.with(
440 - vim.lsp.handlers.hover, {
441 - border = vim.g.border_style,
442 - title =
' ' .. icon.hover
.. ' ',
446 -return setmetatable( _, {} )