dotfiles-ansible/roles/nvim/files/nvim/lua/modules/lsp/util.lua

219 lines
8.4 KiB
Lua

local M = {}
local wk = require('which-key')
local function setup_mappings(bufnr)
local lsp_mappings = {
g = {
d = {':lua vim.lsp.buf.definition()<CR>', 'Goto definition'},
D = {':lua vim.lsp.buf.declaration()<CR>', 'Goto declaration'},
i = {':Lspsaga implement<CR>', 'Implementations'},
I = {':Telescope lsp_implementations<CR>', 'Implementations (Telescope)'},
r = {':TroubleToggle lsp_references<CR>', 'References'},
R = {':Telescope lsp_references<CR>', 'References (Telescope)'}
},
K = {':Lspsaga hover_doc<CR>', 'Hover doc'},
['[d'] = {':Lspsaga diagnostic_jump_prev<CR>', 'Previous diagnostics'},
[']d'] = {':Lspsaga diagnostic_jump_next<CR>', 'Next diagnostics'}
}
local lsp_leader_mappings = {
l = {
name = 'LSP',
a = {':lua vim.lsp.buf.add_workspace_folder()<CR>', 'Add workspace folder'},
b = {':TroubleToggle lsp_document_diagnostics<CR>', 'Buffer diagnostics'},
B = {':Telescope lsp_document_diagnostics<CR>', 'Buffer diagnostics (Telescope)'},
c = {':Lspsaga code_action<CR>', 'Code action'},
C = {':Telescope lsp_code_actions<CR>', 'Code action (Telescope)'},
d = {':TroubleToggle lsp_definitions<CR>', 'Definitions'},
D = {':Telescope lsp_definitions<CR>', 'Definitions (Telescope)'},
e = {':Lspsaga show_line_diagnostics<CR>', 'Line diagnostics'},
f = {':Lspsaga lsp_finder<CR>', 'Finder'},
g = {':SymbolsOutline<CR>', 'Symbol outline'},
i = {':LspInfo<CR>', 'Lsp info'},
l = {':TroubleToggle loclist<CR>', 'Diagnostics loclist'},
n = {':Lspsaga rename<CR>', 'Rename'},
p = {':Lspsaga preview_definition<CR>', 'Preview definition'},
r = {':lua vim.lsp.buf.remove_workspace_folder()<CR>', 'Remove workspace folder'},
s = {':Telescope lsp_document_symbols<CR>', 'Buffer symbols'},
S = {':Telescope lsp_workspace_symbols<CR>', 'Workspace symbols'},
t = {':TroubleToggle<CR>', 'Toggle Trouble'},
w = {':TroubleToggle lsp_workspace_diagnostics<CR>', 'Workspace diagnostics'},
W = {':Telescope lsp_workspace_diagnostics<CR>', 'Workspace diagnostics (Telescope)'},
x = {':lua vim.lsp.buf.signature_help()<CR>', 'Signature help'},
y = {':lua vim.notify(vim.inspect(vim.lsp.buf.list_workspace_folders()), vim.log.levels.INFO)<CR>', 'List workspace folders'}
}
}
local lsp_visual_mappings = {
l = {
name = 'LSP',
c = {':<C-U>Lspsaga range_code_action<CR>', 'Range code action'},
C = {':Telescope lsp_range_code_actions<CR>', 'Range code action (Telescope)'}
}
}
wk.register(lsp_mappings, {buffer = bufnr})
wk.register(lsp_leader_mappings, {buffer = bufnr, prefix = '<leader>'})
wk.register(lsp_visual_mappings, {buffer = bufnr, prefix = '<leader>', mode = 'v'})
local opts = {noremap = true, silent = true}
vim.api.nvim_buf_set_keymap(bufnr, 'n', '<C-f>', ':lua require("lspsaga.action").smart_scroll_with_saga(1)<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', '<C-b>', ':lua require("lspsaga.action").smart_scroll_with_saga(-1)<CR>', opts)
end
local function setup_formatting_mapping(client, bufnr)
if client.resolved_capabilities.document_formatting then
wk.register({
['lo'] = {':lua vim.lsp.buf.formatting()<CR>', 'Format buffer'}
}, {buffer = bufnr, prefix = '<leader>'})
-- vim.api.nvim_command('autocmd BufWritePre <buffer> lua vim.lsp.buf.formatting_sync()')
elseif client.resolved_capabilities.document_range_formatting then
wk.register({
['lo'] = {':lua vim.lsp.buf.range_formatting({},{0,0},{vim.fn.line("$"),0})<CR>', 'Format buffer'}
}, {buffer = bufnr, prefix = '<leader>'})
wk.register({
['lo'] = {':lua vim.lsp.buf.range_formatting()<CR>', 'Format range'}
}, {buffer = bufnr, prefix = '<leader>', mode = 'v'})
-- vim.api.nvim_command('autocmd BufWritePre <buffer> lua vim.lsp.buf.range_formatting({},{0,0},{vim.fn.line("$"),0})')
end
end
local function setup_document_highlight(client)
if client.resolved_capabilities.document_highlight 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
local function setup_codelens(client)
if client.resolved_capabilities.code_lens then
vim.api.nvim_exec(
[[
augroup lsp_code_lens_refresh
autocmd! * <buffer>
autocmd InsertLeave <buffer> lua vim.lsp.codelens.refresh()
autocmd InsertLeave <buffer> lua vim.lsp.codelens.display()
augroup END
]],
false
)
end
end
function M.common_on_attach(client, bufnr)
vim.api.nvim_buf_set_option(bufnr, 'omnifunc', 'v:lua.vim.lsp.omnifunc')
setup_mappings(bufnr)
setup_codelens(client)
setup_document_highlight(client)
setup_formatting_mapping(client, bufnr)
end
function M.common_capabilities()
local capabilities = vim.lsp.protocol.make_client_capabilities()
-- Add additional capabilities supported by nvim-cmp
local completionItem = capabilities.textDocument.completion.completionItem
completionItem.documentationFormat = {'markdown', 'plaintext'}
completionItem.snippetSupport = true
completionItem.preselectSupport = true
completionItem.insertReplaceSupport = true
completionItem.labelDetailsSupport = true
completionItem.deprecatedSupport = true
completionItem.commitCharactersSupport = true
completionItem.tagSupport = {valueSet = {1}}
completionItem.resolveSupport = {
properties = {
'documentation',
'detail',
'additionalTextEdits',
}
}
return capabilities
end
function M.setup_handlers()
local border = {
{'', 'FloatBorder'},
{'', 'FloatBorder'},
{'', 'FloatBorder'},
{'', 'FloatBorder'},
{'', 'FloatBorder'},
{'', 'FloatBorder'},
{'', 'FloatBorder'},
{'', 'FloatBorder'}
}
vim.lsp.handlers['textDocument/publishDiagnostics'] = vim.lsp.with(vim.lsp.diagnostic.on_publish_diagnostics, {
underline = false,
virtual_text = {
prefix = '',
source = 'always'
},
signs = {
active = true,
values = {
{name = 'LspDiagnosticsSignError', text = ''},
{name = 'LspDiagnosticsSignWarning', text = ''},
{name = 'LspDiagnosticsSignInformation', text = ''},
{name = 'LspDiagnosticsSignHint', text = ''}
}
},
update_in_insert = false,
severity_sort = true
})
vim.lsp.handlers['textDocument/hover'] = vim.lsp.with(vim.lsp.handlers.hover, {
border = border,
})
vim.lsp.handlers['textDocument/signatureHelp'] = vim.lsp.with(vim.lsp.handlers.signature_help, {
border = border,
})
end
function M.lua_lib_path()
local lib_path = {}
-- lib_path[vim.fn.expand('~/.luarocks/share/lua/5.3')] = true
-- lib_path[vim.fn.expand('/usr/share/lua/5.3')] = true
lib_path[os.getenv('VIMRUNTIME') .. '/lua'] = true
-- local function add(lib)
-- for _, p in pairs(vim.fn.expand(lib .. '/lua', false, true)) do
-- p = vim.loop.fs_realpath(p)
-- if p then lib_path[p] = true end
-- end
-- end
-- for _, site in pairs(vim.split(vim.opt.packpath:get(), ',')) do
-- add(site .. '/pack/*/opt/*')
-- add(site .. '/pack/*/start/*')
-- end
return lib_path
end
function M.lua_runtime_path()
local lib_path = M.lua_lib_path()
local runtime_path = {}
table.insert(runtime_path, 'lua/?.lua')
table.insert(runtime_path, 'lua/?/init.lua')
-- table.insert(runtime_path, '?.lua')
-- table.insert(runtime_path, '?/?.lua')
-- table.insert(runtime_path, '?/init.lua')
for lib, _ in pairs(lib_path) do
table.insert(runtime_path, lib .. '/?.lua')
table.insert(runtime_path, lib .. '/?/init.lua')
end
return runtime_path
end
return M