navigator.lua/lua/navigator/lspclient/mapping.lua

314 lines
11 KiB
Lua
Raw Normal View History

2022-02-17 11:21:34 +00:00
local util = require('navigator.util')
local log = util.log
local trace = util.trace
2021-04-19 02:56:32 +00:00
local event_hdlrs = {
2022-01-12 09:12:51 +00:00
{ ev = 'BufWritePre', func = [[require "navigator.diagnostics".set_diag_loclist()]] },
{ ev = 'CursorHold', func = 'document_highlight()' },
{ ev = 'CursorHoldI', func = 'document_highlight()' },
{ ev = 'CursorMoved', func = 'clear_references()' },
2021-04-19 02:56:32 +00:00
}
2022-01-12 09:12:51 +00:00
local double = { '', '', '', '', '', '', '', '' }
local single = { '', '', '', '', '', '', '', '' }
-- TODO https://github.com/neovim/neovim/pull/16591 use vimkeymap.set/del
-- LuaFormatter off
2021-04-19 02:56:32 +00:00
local key_maps = {
{ key = 'gr', func = "require('navigator.reference').reference()" },
2021-12-23 08:17:48 +00:00
{ key = 'Gr', func = "require('navigator.reference').async_ref()" },
{ mode = 'i', key = '<M-k>', func = 'signature_help()' },
{ key = '<c-k>', func = 'signature_help()' },
{ key = 'g0', func = "require('navigator.symbols').document_symbols()" },
2021-12-24 06:30:56 +00:00
{ key = 'gW', func = "require('navigator.workspace').workspace_symbol()" },
{ key = '<c-]>', func = "require('navigator.definition').definition()" },
2022-02-17 03:54:52 +00:00
{ key = 'gd', func = "require('navigator.definition').definition()" },
{ key = 'gD', func = "declaration({ border = 'rounded', max_width = 80 })" },
{ key = 'gp', func = "require('navigator.definition').definition_preview()" },
2022-01-12 09:12:51 +00:00
{ key = '<Leader>gt', func = "require('navigator.treesitter').buf_ts()" },
{ key = '<Leader>gT', func = "require('navigator.treesitter').bufs_ts()" },
{ key = 'K', func = 'hover({ popup_opts = { border = single, max_width = 80 }})' },
{ key = '<Space>ca', mode = 'n', func = "require('navigator.codeAction').code_action()" },
{ key = '<Space>cA', mode = 'v', func = 'range_code_action()' },
2021-12-24 06:30:56 +00:00
-- { key = '<Leader>re', func = 'rename()' },
{ key = '<Space>rn', func = "require('navigator.rename').rename()" },
{ key = '<Leader>gi', func = 'incoming_calls()' },
{ key = '<Leader>go', func = 'outgoing_calls()' },
{ key = 'gi', func = 'implementation()' },
{ key = '<Space>D', func = 'type_definition()' },
{ key = 'gL', func = "require('navigator.diagnostics').show_diagnostics()" },
{ key = 'gG', func = "require('navigator.diagnostics').show_buf_diagnostics()" },
{ key = '<Leader>dt', func = "require('navigator.diagnostics').toggle_diagnostics()" },
{ key = ']d', func = "diagnostic.goto_next({ border = 'rounded', max_width = 80})" },
{ key = '[d', func = "diagnostic.goto_prev({ border = 'rounded', max_width = 80})" },
2022-02-17 11:21:34 +00:00
{ key = ']O', func = 'diagnostic.set_loclist()' },
{ key = ']r', func = "require('navigator.treesitter').goto_next_usage()" },
{ key = '[r', func = "require('navigator.treesitter').goto_previous_usage()" },
{ key = '<C-LeftMouse>', func = 'definition()' },
{ key = 'g<LeftMouse>', func = 'implementation()' },
{ key = '<Leader>k', func = "require('navigator.dochighlight').hi_symbol()" },
2021-12-24 06:30:56 +00:00
{ key = '<Space>wa', func = "require('navigator.workspace').add_workspace_folder()" },
{ key = '<Space>wr', func = "require('navigator.workspace').remove_workspace_folder()" },
{ key = '<Space>ff', func = 'formatting()', mode = 'n' },
{ key = '<Space>ff', func = 'range_formatting()', mode = 'v' },
2021-12-24 06:30:56 +00:00
{ key = '<Space>wl', func = "require('navigator.workspace').list_workspace_folders()" },
{ key = '<Space>la', mode = 'n', func = "require('navigator.codelens').run_action()" },
2021-04-19 02:56:32 +00:00
}
local key_maps_help = {}
-- LuaFormatter on
local M = {}
2021-04-19 02:56:32 +00:00
2021-06-23 14:09:33 +00:00
local ccls_mappings = {
2022-01-12 09:12:51 +00:00
{ key = '<Leader>gi', func = "require('navigator.cclshierarchy').incoming_calls()" },
{ key = '<Leader>go', func = "require('navigator.cclshierarchy').outgoing_calls()" },
2021-06-23 14:09:33 +00:00
}
2021-10-14 03:55:40 +00:00
local check_cap = function(cap)
2021-09-07 11:08:00 +00:00
-- log(vim.lsp.buf_get_clients(0))
local fmt, rfmt, ccls
2021-10-14 03:55:40 +00:00
if cap and cap.document_formatting then
fmt = true
end
if cap and cap.document_range_formatting then
rfmt = true
end
2021-09-07 11:08:00 +00:00
for _, value in pairs(vim.lsp.buf_get_clients(0)) do
2021-10-14 03:55:40 +00:00
trace(value)
if value ~= nil and value.resolved_capabilities == nil then
if value.resolved_capabilities.document_formatting then
fmt = true
end
if value.resolved_capabilities.document_range_formatting then
rfmt = true
end
2021-09-07 11:08:00 +00:00
log('override ccls', value.config)
if value.config.name == 'ccls' then
ccls = true
end
2021-09-07 11:08:00 +00:00
end
end
return fmt, rfmt, ccls
2021-09-07 11:08:00 +00:00
end
2021-04-19 02:56:32 +00:00
local function set_mapping(user_opts)
log('setup mapping')
2022-01-12 09:12:51 +00:00
local opts = { noremap = true, silent = true }
2021-05-03 10:45:51 +00:00
user_opts = user_opts or {}
2021-09-07 11:08:00 +00:00
local user_key = _NgConfigValues.keymaps or {}
2021-05-03 10:45:51 +00:00
local bufnr = user_opts.bufnr or 0
2021-09-07 11:08:00 +00:00
local function del_keymap(...)
vim.api.nvim_buf_del_keymap(bufnr, ...)
end
2021-05-03 10:45:51 +00:00
2021-09-07 11:08:00 +00:00
local function set_keymap(...)
vim.api.nvim_buf_set_keymap(bufnr, ...)
end
2021-05-03 10:45:51 +00:00
-- local function buf_set_option(...)
-- vim.api.nvim_buf_set_option(bufnr, ...)
-- end
2021-10-14 03:55:40 +00:00
local doc_fmt, range_fmt, ccls = check_cap(user_opts.cap)
2021-09-07 11:08:00 +00:00
if ccls then
2021-09-07 15:06:58 +00:00
vim.list_extend(key_maps, ccls_mappings)
2021-09-07 11:08:00 +00:00
end
if _NgConfigValues.default_mapping ~= false then
for _, v in pairs(user_key) do
trace('binding', v)
local exists = false
for _, default in pairs(key_maps) do
2021-12-25 01:43:40 +00:00
if v.func == default.func and (v.mode or 'n') == (default.mode or 'n') and not default.override then
default.key, default.override, exists = v.key, true, true
break
end
end
if not exists then
table.insert(key_maps, v)
2021-05-03 10:45:51 +00:00
end
2021-04-19 02:56:32 +00:00
end
else
key_maps = _NgConfigValues.keymaps or {}
log('setting maps to ', key_maps)
2021-05-03 10:45:51 +00:00
end
2021-09-07 11:08:00 +00:00
local fmtkey, rfmtkey
2021-05-03 10:45:51 +00:00
for _, value in pairs(key_maps) do
local f = '<Cmd>lua vim.lsp.buf.' .. value.func .. '<CR>'
if string.find(value.func, 'require') then
f = '<Cmd>lua ' .. value.func .. '<CR>'
elseif string.find(value.func, 'diagnostic') then
local diagnostic = '<Cmd>lua vim.'
2022-02-17 11:21:34 +00:00
if vim.diagnostic ~= nil then
diagnostic = '<Cmd>lua vim.'
else
util.error('Please update nvim to 0.6.1+')
end
f = diagnostic .. value.func .. '<CR>'
elseif string.find(value.func, 'vim.') then
f = '<Cmd>lua ' .. value.func .. '<CR>'
2021-05-02 13:54:30 +00:00
end
2021-05-03 10:45:51 +00:00
local k = value.key
local m = value.mode or 'n'
if string.find(value.func, 'range_formatting') then
2021-09-07 11:08:00 +00:00
rfmtkey = value.key
elseif string.find(value.func, 'formatting') then
2021-09-07 11:08:00 +00:00
fmtkey = value.key
2021-06-23 14:09:33 +00:00
end
2022-02-17 11:21:34 +00:00
trace('binding', k, f)
2021-09-07 11:08:00 +00:00
set_keymap(m, k, f, opts)
2021-05-03 10:45:51 +00:00
end
for _, val in pairs(key_maps) do
table.insert(key_maps_help, (val.mode or 'n') .. '|' .. val.key .. '|' .. val.func)
end
2021-05-03 10:45:51 +00:00
-- if user_opts.cap.document_formatting then
2021-10-20 00:22:41 +00:00
if doc_fmt and _NgConfigValues.lsp.format_on_save then
vim.cmd([[
aug NavigatorAuFormat
au!
autocmd BufWritePre <buffer> lua vim.lsp.buf.formatting()
aug END
]])
elseif fmtkey then
2021-09-07 11:08:00 +00:00
del_keymap('n', fmtkey)
2021-05-03 10:45:51 +00:00
end
if user_opts.cap and user_opts.cap.document_range_formatting then
log('formatting enabled', user_opts.cap)
2021-10-14 03:55:40 +00:00
end
2022-02-21 04:22:05 +00:00
if not range_fmt and rfmtkey then
del_keymap('v', rfmtkey)
end
2021-09-07 11:08:00 +00:00
log('enable format ', doc_fmt, range_fmt)
2021-04-19 02:56:32 +00:00
end
2021-05-29 11:08:30 +00:00
local function autocmd(user_opts)
2022-01-12 09:12:51 +00:00
vim.api.nvim_exec(
[[
aug NavigatorDocHlAu
2021-05-29 11:08:30 +00:00
au!
au CmdlineLeave : lua require('navigator.dochighlight').cmd_nohl()
aug END
2022-01-12 09:12:51 +00:00
]],
false
)
2021-05-29 11:08:30 +00:00
end
2021-04-19 02:56:32 +00:00
local function set_event_handler(user_opts)
2021-05-03 10:45:51 +00:00
user_opts = user_opts or {}
local file_types =
2022-01-12 09:12:51 +00:00
'c,cpp,h,go,python,vim,sh,javascript,html,css,lua,typescript,rust,javascriptreact,typescriptreact,json,yaml,kotlin,php,dart,nim,terraform,java'
2021-05-03 10:45:51 +00:00
-- local format_files = "c,cpp,h,go,python,vim,javascript,typescript" --html,css,
vim.api.nvim_command([[augroup nvim_lsp_autos]])
vim.api.nvim_command([[autocmd!]])
2021-05-03 10:45:51 +00:00
for _, value in pairs(event_hdlrs) do
local f = ''
if string.find(value.func, 'require') ~= nil then
f = 'lua ' .. value.func
2021-05-03 10:45:51 +00:00
else
f = 'lua vim.lsp.buf.' .. value.func
2021-04-19 02:56:32 +00:00
end
local cmd = 'autocmd FileType ' .. file_types .. ' autocmd nvim_lsp_autos ' .. value.ev .. ' <buffer> silent! ' .. f
2021-05-03 10:45:51 +00:00
vim.api.nvim_command(cmd)
end
vim.api.nvim_command([[augroup END]])
2021-04-19 02:56:32 +00:00
end
M.toggle_lspformat = function(on)
if on == nil then
_NgConfigValues.lsp.format_on_save = not _NgConfigValues.lsp.format_on_save
else
_NgConfigValues.lsp.format_on_save = on
end
if _NgConfigValues.lsp.format_on_save then
if on == nil then
vim.notify('format on save true', vim.lsp.log_levels.INFO)
end
vim.cmd([[set eventignore-=BufWritePre]])
else
if on == nil then
vim.notify('format on save false', vim.lsp.log_levels.INFO)
end
vim.cmd([[set eventignore+=BufWritePre]])
end
end
2021-04-19 02:56:32 +00:00
function M.setup(user_opts)
user_opts = user_opts or _NgConfigValues
set_mapping(user_opts)
2021-05-28 00:10:28 +00:00
2021-05-29 11:08:30 +00:00
autocmd(user_opts)
2021-05-03 10:45:51 +00:00
set_event_handler(user_opts)
2022-02-17 11:21:34 +00:00
local client = user_opts.client or {}
local cap = client.resolved_capabilities or vim.lsp.protocol.make_client_capabilities()
log('lsp cap:', cap)
2021-05-14 09:10:13 +00:00
if cap.call_hierarchy or cap.callHierarchy then
vim.lsp.handlers['callHierarchy/incomingCalls'] = require('navigator.hierarchy').incoming_calls_handler
vim.lsp.handlers['callHierarchy/outgoingCalls'] = require('navigator.hierarchy').outgoing_calls_handler
2021-05-03 10:45:51 +00:00
end
vim.lsp.handlers['textDocument/references'] = require('navigator.reference').reference_handler
-- vim.lsp.handlers["textDocument/codeAction"] = require"navigator.codeAction".code_action_handler
vim.lsp.handlers['textDocument/definition'] = require('navigator.definition').definition_handler
2021-05-03 10:45:51 +00:00
if cap.declaration then
vim.lsp.handlers['textDocument/declaration'] = require('navigator.definition').declaration_handler
2021-05-03 10:45:51 +00:00
end
vim.lsp.handlers['textDocument/typeDefinition'] = require('navigator.definition').typeDefinition_handler
vim.lsp.handlers['textDocument/implementation'] = require('navigator.implementation').implementation_handler
2021-05-03 10:45:51 +00:00
2021-12-24 06:30:56 +00:00
-- vim.lsp.handlers['textDocument/documentSymbol'] = require('navigator.symbols').document_symbol_handler
vim.lsp.handlers['workspace/symbol'] = require('navigator.symbols').workspace_symbol_handler
vim.lsp.handlers['textDocument/publishDiagnostics'] = require('navigator.diagnostics').diagnostic_handler
2021-05-03 10:45:51 +00:00
2021-06-17 23:24:02 +00:00
-- TODO: when active signature merge to neovim, remove this setup:
2022-01-12 09:12:51 +00:00
if
_NgConfigValues.signature_help_cfg and #_NgConfigValues.signature_help_cfg > 0 or _NgConfigValues.lsp_signature_help
then
log('setup signature from navigator')
local hassig, sig = pcall(require, 'lsp_signature')
if hassig then
sig.setup(_NgConfigValues.signature_help_cfg or {})
2021-09-14 00:41:13 +00:00
end
else
vim.lsp.handlers['textDocument/signatureHelp'] = vim.lsp.with(require('navigator.signature').signature_handler, {
2022-01-12 09:12:51 +00:00
border = { '', '', '', '', '', '', '', '' },
})
end
2021-05-08 04:54:37 +00:00
2022-01-12 09:12:51 +00:00
vim.lsp.handlers['textDocument/hover'] = vim.lsp.with(vim.lsp.handlers.hover, { border = single })
if cap.document_formatting then
log('formatting enabled setup hdl')
vim.lsp.handlers['textDocument/formatting'] = require('navigator.formatting').format_hdl
end
end
2021-08-01 13:32:55 +00:00
M.get_keymaps_help = function()
local ListView = require('guihua.listview')
local win = ListView:new({
loc = 'top_center',
border = 'none',
prompt = true,
enter = true,
2022-01-12 09:12:51 +00:00
rect = { height = 20, width = 90 },
data = key_maps_help,
})
return win
2021-04-19 02:56:32 +00:00
end
return M