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

207 lines
8.1 KiB
Lua
Raw Normal View History

2021-05-14 09:10:13 +00:00
local log = require"navigator.util".log
local function set_keymap(...)
vim.api.nvim_set_keymap(...)
end
2021-04-19 02:56:32 +00:00
local event_hdlrs = {
2021-05-14 09:10:13 +00:00
{ev = "BufWritePre", func = "diagnostic.set_loclist({open_loclist = false})"},
{ev = "CursorHold", func = "document_highlight()"},
{ev = "CursorHoldI", func = "document_highlight()"},
2021-05-03 10:45:51 +00:00
{ev = "CursorMoved", func = "clear_references()"}
2021-04-19 02:56:32 +00:00
}
local double = {"", "", "", "", "", "", "", ""}
local single = {"", "", "", "", "", "", "", ""}
2021-04-19 02:56:32 +00:00
local key_maps = {
2021-05-28 00:10:28 +00:00
{key = "gr", func = "references()"}, {mode = "i", key = "<M-k>", func = "signature_help()"},
{key = "gs", func = "signature_help()"}, {key = "g0", func = "document_symbol()"},
{key = "gW", func = "workspace_symbol()"}, {key = "<c-]>", func = "definition()"},
2021-06-13 04:03:01 +00:00
{key = "gs", func = "signature_help()"}, {key = "g0", func = "document_symbol()"},
{key = "gW", func = "workspace_symbol()"}, {key = "<c-]>", func = "definition()"},
{key = "gD", func = "declaration({ popup_opts = { border = 'single' }})"},
{key = "gp", func = "require('navigator.definition').definition_preview()"},
{key = "gT", func = "require('navigator.treesitter').buf_ts()"},
{key = "GT", func = "require('navigator.treesitter').bufs_ts()"},
{key = "gs", func = "signature_help()"}, {key = "g0", func = "document_symbol()"},
{key = "gW", func = "workspace_symbol()"}, {key = "<c-]>", func = "definition()"},
{key = "gs", func = "signature_help()"}, {key = "g0", func = "document_symbol()"},
{key = "gW", func = "workspace_symbol()"}, {key = "<c-]>", func = "definition()"},
{key = "gD", func = "declaration({ popup_opts = { border = 'single' }})"},
{key = "gp", func = "require('navigator.definition').definition_preview()"},
2021-05-03 10:45:51 +00:00
{key = "gT", func = "require('navigator.treesitter').buf_ts()"},
{key = "GT", func = "require('navigator.treesitter').bufs_ts()"},
{key = "K", func = "hover({ popup_opts = { border = single }})"},
{key = "ga", mode = "n", func = "code_action()"},
2021-05-28 00:10:28 +00:00
{key = "ga", mode = "v", func = "range_code_action()"}, {key = "<Leader>re", func = "rename()"},
2021-06-17 15:39:41 +00:00
{key = "<Space>rn", func = "require('navigator.rename').rename()"},
2021-05-28 00:10:28 +00:00
{key = "<Leader>gi", func = "incoming_calls()"}, {key = "<Leader>go", func = "outgoing_calls()"},
{key = "gi", func = "implementation()"}, {key = "gt", func = "type_definition()"},
{key = "gL", func = "diagnostic.show_line_diagnostics({ popup_opts = { border = single }})"},
2021-05-03 10:45:51 +00:00
{key = "gG", func = "require('navigator.diagnostics').show_diagnostic()"},
{key = "]d", func = "diagnostic.goto_next({ popup_opts = { border = single }})"},
{key = "[d", func = "diagnostic.goto_next({ popup_opts = { border = single }})"},
2021-05-08 04:54:37 +00:00
{key = "]r", func = "require('navigator.treesitter').goto_next_usage()"},
{key = "[r", func = "require('navigator.treesitter').goto_previous_usage()"},
2021-05-29 11:08:30 +00:00
{key = "<C-LeftMouse>", func = "definition()"}, {key = "g<LeftMouse>", func = "implementation()"},
{key = "<Leader>k", func = "require('navigator.dochighlight').hi_symbol()"}
2021-04-19 02:56:32 +00:00
}
local function set_mapping(user_opts)
2021-05-03 10:45:51 +00:00
local opts = {noremap = true, silent = true}
user_opts = user_opts or {}
local user_key = user_opts.keymaps or {}
local bufnr = user_opts.bufnr or 0
local function buf_set_keymap(...)
vim.api.nvim_buf_set_keymap(bufnr, ...)
end
2021-05-03 10:45:51 +00:00
local function set_keymap(...)
vim.api.nvim_set_keymap(...)
end
2021-05-03 10:45:51 +00:00
-- local function buf_set_option(...)
-- vim.api.nvim_buf_set_option(bufnr, ...)
-- end
for _, v in pairs(user_key) do
local exists = false
for _, default in pairs(key_maps) do
if v.func == default.func then
default.key, exists = v.key, true
break
end
2021-04-19 02:56:32 +00:00
end
2021-05-28 00:10:28 +00:00
if not exists then
table.insert(key_maps, v)
end
2021-05-03 10:45:51 +00:00
end
-- local key_opts = {vim.tbl_deep_extend("force", key_maps, unpack(result))}
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
f = "<Cmd>lua vim.lsp." .. 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"
set_keymap(m, k, f, opts)
2021-05-03 10:45:51 +00:00
end
-- format setup
local range_fmt = false
local doc_fmt = false
for _, value in pairs(vim.lsp.buf_get_clients(0)) do
2021-05-28 00:10:28 +00:00
if value == nil or value.resolved_capabilities == nil then
return
end
if value.resolved_capabilities.document_formatting then
doc_fmt = true
end
if value.resolved_capabilities.document_range_formatting then
range_fmt = true
end
2021-05-03 10:45:51 +00:00
end
-- if user_opts.cap.document_formatting then
if doc_fmt then
2021-05-28 00:10:28 +00:00
buf_set_keymap("n", "<space>ff", "<cmd>lua vim.lsp.buf.formatting()<CR>", opts)
if _NgConfigValues.lsp.format_on_save then
vim.cmd([[
aug NavigatorAuFormat
au!
autocmd BufWritePre <buffer> lua vim.lsp.buf.formatting()
aug END
]])
end
2021-05-03 10:45:51 +00:00
end
-- if user_opts.cap.document_range_formatting then
if range_fmt then
2021-05-28 00:10:28 +00:00
buf_set_keymap("v", "<space>ff", "<cmd>lua vim.lsp.buf.range_formatting()<CR>", opts)
end
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)
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
]], false)
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 =
"c,cpp,h,go,python,vim,sh,javascript,html,css,lua,typescript,rust,javascriptreact,typescriptreact,json,yaml,kotlin,php,dart,nim,terraform"
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!]]
for _, value in pairs(event_hdlrs) do
local f = ""
if string.find(value.func, "diagnostic") then
f = "lua vim.lsp." .. value.func
else
f = "lua vim.lsp.buf." .. value.func
2021-04-19 02:56:32 +00:00
end
2021-05-28 00:10:28 +00:00
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
local M = {}
function M.setup(user_opts)
user_opts = user_opts or _NgConfigValues
2021-05-28 00:10:28 +00:00
if _NgConfigValues.default_mapping == true then
set_mapping(user_opts)
end
2021-05-29 11:08:30 +00:00
autocmd(user_opts)
2021-05-03 10:45:51 +00:00
set_event_handler(user_opts)
local cap = user_opts.cap or vim.lsp.protocol.make_client_capabilities()
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
2021-05-28 00:10:28 +00:00
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
2021-05-28 00:10:28 +00:00
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
vim.lsp.handlers["textDocument/documentSymbol"] =
require"navigator.symbols".document_symbol_handler
2021-05-28 00:10:28 +00:00
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
local hassig, sig = pcall(require, "lsp_signature")
if not hassig then
2021-05-28 00:10:28 +00:00
vim.lsp.handlers["textDocument/signatureHelp"] = require"navigator.signature".signature_handler
end
2021-05-08 04:54:37 +00:00
vim.lsp.handlers["textDocument/hover"] = vim.lsp.with(vim.lsp.handlers.hover, {border = single})
2021-06-17 14:49:13 +00:00
vim.lsp.handlers["textDocument/formatting"] = require"navigator.formatting".format_hdl
2021-04-19 02:56:32 +00:00
end
return M