repositories / dotfiles
dotfiles
bugabingas dorkfiles
owned by bugabinga
neovim/lua/bugabinga/lsp.lua
Rawlocal dbg = require 'std.dbg'
local icon = require 'std.icon'
local map = require 'std.map'
local auto = require 'std.auto'
local table = require 'std.table'
local user_command = require 'std.user_command'
if dbg.get() then
vim.lsp.set_log_level 'debug'
end
-- enable multiline tokens for all lsps
vim.lsp.config( '*', {
capabilities = {
textDocument = {
semanticTokens = {
multilineTokenSupport = true,
},
},
},
} )
local lsp_configs = vim.api.nvim_get_runtime_file( 'lsp/*.lua', true )
local servers = {}
for _, path in ipairs( lsp_configs ) do
local server_name = vim.fn.fnamemodify( path, ':t:r' ) -- remove .lua extension
table.insert( servers, server_name )
end
user_command.LspRestart 'Restarts LSP servers for current filetype or specified server' (
function ( opts )
local server = opts.args
-- TODO of server was not given, restart all active lsps
vim.lsp.enable( server, false )
vim.lsp.enable( server, true )
end
)
local lsp_attach = function ( args )
local bufnr = args.buf
local client_id = args.data.client_id
local client = vim.lsp.get_client_by_id( client_id )
if not client then return end
if client:supports_method 'textDocument/completion' then
vim.lsp.completion.enable( true, client.id, bufnr, { autotrigger = true } )
end
-- the following are already set by default
-- but I will remap them anyways for consistency
-- create mappings for rest of lsp functions in the default style: gr prefix
map.normal {
description = 'code action',
category = 'lsp',
keys = 'gra',
command = vim.lsp.buf.code_action,
}
map.normal {
description = 'implementation',
category = 'lsp',
keys = 'gri',
command = vim.lsp.buf.implementation,
}
map.normal {
description = 'rename',
category = 'lsp',
keys = 'grn',
command = vim.lsp.buf.rename,
}
map.normal {
description = 'references',
category = 'lsp',
keys = 'grr',
command = vim.lsp.buf.references,
}
map.normal {
description = 'definition',
category = 'lsp',
keys = 'grd',
command = vim.lsp.buf.definition,
}
map.normal {
description = 'list workspace folders',
category = 'lsp',
keys = 'grwl',
command = vim.lsp.buf.list_workspace_folders,
}
map.normal {
description = 'add workspace folder',
category = 'lsp',
keys = 'grwa',
command = vim.lsp.buf.add_workspace_folder,
}
map.normal {
description = 'remove workspace folder',
category = 'lsp',
keys = 'grwr',
command = vim.lsp.buf.remove_workspace_folder,
}
map.normal {
description = 'incoming calls',
category = 'lsp',
keys = 'grc',
command = vim.lsp.buf.incoming_calls,
}
map.normal {
description = 'outgoing calls',
category = 'lsp',
keys = 'grC',
command = vim.lsp.buf.outgoing_calls,
}
map.normal {
description = 'type definition',
category = 'lsp',
keys = 'grt',
command = vim.lsp.buf.type_definition,
}
map.normal {
description = 'type hierarchy (subtypes)',
category = 'lsp',
keys = 'grh',
command = function () vim.lsp.buf.typehierarchy 'subtypes' end,
}
map.normal {
description = 'type hierarchy (supertypes)',
category = 'lsp',
keys = 'grH',
command = function () vim.lsp.buf.typehierarchy 'supertypes' end,
}
map.insert {
description = 'signature help',
category = 'lsp',
keys = '<c-s>',
command = vim.lsp.buf.signature_help,
}
if client.server_capabilities.inlayHintProvider then
map.normal {
description = 'Toggle inlay hints',
category = 'lsp',
buffer = bufnr,
keys = '<leader>ti',
command = function ()
vim.lsp.inlay_hint.enable( not vim.lsp.inlay_hint.is_enabled() )
end,
}
end
end
auto 'lsp' {
description = 'lsp: client attached to buffer',
events = 'LspAttach',
command = lsp_attach,
}
local completion_icons = {
Class = icon.class,
Color = icon.color,
Constant = icon.constant,
Constructor = icon.constructor,
Enum = icon.enum,
EnumMember = icon.enum_member,
Field = icon.field,
File = icon.file,
Folder = icon.folder,
Function = icon['function'],
Interface = icon.interface,
Keyword = icon.keyword,
Method = icon.method,
Module = icon.module,
Property = icon.property,
Snippet = icon.snippet,
Struct = icon.struct,
Text = icon.text,
Unit = icon.unit,
Value = icon.value,
Variable = icon.variable,
}
local completion_kinds = vim.lsp.protocol.CompletionItemKind
for i, kind in ipairs( completion_kinds ) do
completion_kinds[i] = completion_icons[kind] or kind
end
user_command.LspCapabilities 'Show capabilities of currently running lsp server' ( function ()
local caps = vim.lsp.get_clients()[1].server_capabilities
vim.notify( vim.inspect( caps ) )
end )
user_command.LspReload 'Reload current lsp server and buffer' ( function ()
vim.lsp.stop_client( vim.lsp.get_clients() )
vim.cmd.edit()
end )
user_command.LspConfig 'Show configuration of current lsp server' ( function ()
local client = vim.lsp.get_clients()[1]
local config = client.config
local server_info = client.settings
vim.notify( vim.inspect( config ) )
vim.notify( vim.inspect( server_info ) )
end )
user_command.LspInfo 'Show basic info about running LSP servers in a popup' ( function ()
local clients = vim.lsp.get_clients()
if #clients == 0 then
vim.notify( 'No active LSP clients', vim.log.levels.INFO )
return
end
-- Create a buffer for the popup
local buf = vim.api.nvim_create_buf( false, true )
-- Create the floating window
local width = math.floor( vim.o.columns * 0.8 )
local height = math.floor( vim.o.lines * 0.8 )
local row = math.floor( (vim.o.lines - height) / 2 )
local col = math.floor( (vim.o.columns - width) / 2 )
local win = vim.api.nvim_open_win( buf, true, {
relative = 'editor',
width = width,
height = height,
row = row,
col = col,
style = 'minimal',
border = 'rounded',
title = icon.lsp .. ' LSP Info',
title_pos = 'center',
} )
-- Build the content with tab-like sections
local lines = {}
for i, client in ipairs( clients ) do
if i > 1 then table.insert( lines, '' ) end
table.insert( lines, string.format( '# %s %s', icon.lsp, client.name ) )
local root = client.config.root_dir or 'No root'
table.insert( lines, string.format( '**Root:** %s', root ) )
table.insert( lines, string.format( '**ID:** %d', client.id ) )
if client.rpc and client.rpc.pid then
table.insert( lines, string.format( '**PID:** %d', client.rpc.pid ) )
end
table.insert( lines, string.format( '**Attached Buffers:** %d', #client.attached_buffers ) )
if client.server_info and client.server_info.version then
table.insert( lines, string.format( '**Version:** %s', client.server_info.version ) )
end
table.insert( lines, '**Capabilities:**' )
local caps = client.server_capabilities
local cap_list = {}
if caps.hoverProvider then table.insert( cap_list, 'Hover' ) end
if caps.definitionProvider then table.insert( cap_list, 'Definition' ) end
if caps.referencesProvider then table.insert( cap_list, 'References' ) end
if caps.completionProvider then table.insert( cap_list, 'Completion' ) end
if caps.renameProvider then table.insert( cap_list, 'Rename' ) end
if caps.codeActionProvider then table.insert( cap_list, 'Code Actions' ) end
if caps.documentFormattingProvider then table.insert( cap_list, 'Formatting' ) end
if caps.documentSymbolProvider then table.insert( cap_list, 'Document Symbols' ) end
if caps.workspaceSymbolProvider then table.insert( cap_list, 'Workspace Symbols' ) end
if caps.inlayHintProvider then table.insert( cap_list, 'Inlay Hints' ) end
if caps.semanticTokensProvider then table.insert( cap_list, 'Semantic Tokens' ) end
table.insert( lines, ' - ' .. table.concat( cap_list, ', ' ) )
-- Add workspace folders if any
if client.workspace_folders and #client.workspace_folders > 0 then
table.insert( lines, '**Workspace Folders:**' )
for _, wf in ipairs( client.workspace_folders ) do
table.insert( lines, string.format( ' - %s', wf.name ) )
end
end
end
vim.api.nvim_buf_set_lines( buf, 0, -1, false, lines )
-- Set syntax for markdown-like highlighting
vim.api.nvim_buf_set_option( buf, 'filetype', 'markdown' )
vim.api.nvim_buf_set_option( buf, 'wrap', true )
-- Key mappings to close
vim.api.nvim_buf_set_keymap( buf, 'n', 'q', '<cmd>close<CR>', { noremap = true, silent = true } )
vim.api.nvim_buf_set_keymap( buf, 'n', '<Esc>', '<cmd>close<CR>', { noremap = true, silent = true } )
-- Focus the window
vim.api.nvim_set_current_win( win )
end )