2021-05-14 09:10:13 +00:00
|
|
|
local log = require"navigator.util".log
|
2021-05-21 11:39:46 +00:00
|
|
|
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})"},
|
2021-05-17 03:15:15 +00:00
|
|
|
{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
|
|
|
}
|
|
|
|
|
2021-06-02 15:25:56 +00:00
|
|
|
local double = {"╔", "═", "╗", "║", "╝", "═", "╚", "║"}
|
|
|
|
local single = {"╭", "─", "╮", "│", "╯", "─", "╰", "│"}
|
2021-06-29 03:32:42 +00:00
|
|
|
-- LuaFormatter off
|
2021-04-19 02:56:32 +00:00
|
|
|
local key_maps = {
|
2021-06-29 03:32:42 +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-02 15:25:56 +00:00
|
|
|
{key = "gD", func = "declaration({ popup_opts = { border = 'single' }})"},
|
2021-05-17 03:15:15 +00:00
|
|
|
{key = "gp", func = "require('navigator.definition').definition_preview()"},
|
2021-05-03 10:45:51 +00:00
|
|
|
{key = "gT", func = "require('navigator.treesitter').buf_ts()"},
|
2021-06-02 15:25:56 +00:00
|
|
|
{key = "GT", func = "require('navigator.treesitter').bufs_ts()"},
|
|
|
|
{key = "K", func = "hover({ popup_opts = { border = single }})"},
|
2021-05-17 03:15:15 +00:00
|
|
|
{key = "ga", mode = "n", func = "code_action()"},
|
2021-06-29 03:32:42 +00:00
|
|
|
{key = "ga", mode = "v", func = "range_code_action()"},
|
|
|
|
{key = "<Leader>re", func = "rename()"},
|
2021-06-17 22:27:58 +00:00
|
|
|
{key = "<Space>re", func = "require('navigator.rename').rename()"},
|
2021-06-29 03:32:42 +00:00
|
|
|
{key = "<Leader>gi", func = "incoming_calls()"},
|
|
|
|
{key = "<Leader>go", func = "outgoing_calls()"},
|
|
|
|
{key = "gi", func = "implementation()"},
|
|
|
|
{key = "gt", func = "type_definition()"},
|
2021-06-10 12:12:25 +00:00
|
|
|
{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()"},
|
2021-06-02 15:25:56 +00:00
|
|
|
{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-06-29 03:32:42 +00:00
|
|
|
{key = "<C-LeftMouse>", func = "definition()"},
|
|
|
|
{key = "g<LeftMouse>", func = "implementation()"},
|
2021-06-30 21:50:20 +00:00
|
|
|
{key = "<Leader>k", func = "require('navigator.dochighlight').hi_symbol()"},
|
2021-04-19 02:56:32 +00:00
|
|
|
}
|
2021-06-29 03:32:42 +00:00
|
|
|
-- LuaFormatter on
|
2021-04-19 02:56:32 +00:00
|
|
|
|
2021-06-23 14:09:33 +00:00
|
|
|
local ccls_mappings = {
|
|
|
|
{key = "<Leader>gi", func = "require('navigator.cclshierarchy').incoming_calls()"},
|
|
|
|
{key = "<Leader>go", func = "require('navigator.cclshierarchy').outgoing_calls()"}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
2021-05-21 11:39:46 +00:00
|
|
|
local function buf_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
|
|
|
|
for _, v in pairs(user_key) do
|
|
|
|
local exists = false
|
|
|
|
for _, default in pairs(key_maps) do
|
2021-06-29 03:29:20 +00:00
|
|
|
if v.func == default.func and (not default.override) then
|
|
|
|
default.key, default.override, exists = v.key, true, true
|
2021-05-03 10:45:51 +00:00
|
|
|
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"
|
2021-06-09 10:51:38 +00:00
|
|
|
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
|
2021-06-23 14:09:33 +00:00
|
|
|
local ccls = false
|
2021-06-25 10:18:55 +00:00
|
|
|
-- log(vim.lsp.buf_get_clients(0))
|
2021-05-03 10:45:51 +00:00
|
|
|
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
|
2021-05-21 11:39:46 +00:00
|
|
|
if value.resolved_capabilities.document_range_formatting then
|
|
|
|
range_fmt = true
|
|
|
|
end
|
2021-06-23 14:09:33 +00:00
|
|
|
|
2021-06-25 10:18:55 +00:00
|
|
|
-- log("override ccls", value.config)
|
2021-06-23 14:09:33 +00:00
|
|
|
if value.config.name == "ccls" then
|
|
|
|
|
|
|
|
ccls = true
|
|
|
|
end
|
2021-05-03 10:45:51 +00:00
|
|
|
end
|
|
|
|
|
2021-06-23 14:09:33 +00:00
|
|
|
if ccls then
|
2021-07-04 22:52:48 +00:00
|
|
|
log("override ccls", ccls_mappings)
|
2021-06-23 14:09:33 +00:00
|
|
|
for _, value in pairs(ccls_mappings) do
|
|
|
|
f = "<Cmd>lua " .. value.func .. "<CR>"
|
|
|
|
local k = value.key
|
|
|
|
local m = value.mode or "n"
|
2021-07-04 22:52:48 +00:00
|
|
|
log(f, k, m)
|
2021-06-23 14:09:33 +00:00
|
|
|
set_keymap(m, k, f, opts)
|
|
|
|
end
|
|
|
|
end
|
2021-05-03 10:45:51 +00:00
|
|
|
-- 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)
|
2021-05-21 11:39:46 +00:00
|
|
|
if _NgConfigValues.lsp.format_on_save then
|
2021-06-12 02:15:16 +00:00
|
|
|
vim.cmd([[
|
|
|
|
aug NavigatorAuFormat
|
|
|
|
au!
|
|
|
|
autocmd BufWritePre <buffer> lua vim.lsp.buf.formatting()
|
|
|
|
aug END
|
|
|
|
]])
|
2021-05-21 11:39:46 +00:00
|
|
|
end
|
2021-05-03 10:45:51 +00:00
|
|
|
end
|
|
|
|
-- if user_opts.cap.document_range_formatting then
|
2021-05-17 03:15:15 +00:00
|
|
|
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)
|
2021-05-17 03:15:15 +00:00
|
|
|
end
|
2021-06-09 10:51:38 +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)
|
|
|
|
vim.api.nvim_exec([[
|
2021-06-12 02:15:16 +00:00
|
|
|
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 {}
|
2021-05-21 11:39:46 +00:00
|
|
|
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)
|
2021-06-09 10:51:38 +00:00
|
|
|
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)
|
2021-05-21 11:39:46 +00:00
|
|
|
|
|
|
|
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
|
2021-05-17 03:15:15 +00:00
|
|
|
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
|
|
|
|
|
2021-05-17 03:15:15 +00:00
|
|
|
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-05-17 03:15:15 +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
|
2021-05-17 03:15:15 +00:00
|
|
|
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:
|
2021-05-03 10:45:51 +00:00
|
|
|
local hassig, sig = pcall(require, "lsp_signature")
|
2021-05-17 03:15:15 +00:00
|
|
|
if not hassig then
|
2021-05-28 00:10:28 +00:00
|
|
|
vim.lsp.handlers["textDocument/signatureHelp"] = require"navigator.signature".signature_handler
|
2021-05-17 03:15:15 +00:00
|
|
|
end
|
2021-05-08 04:54:37 +00:00
|
|
|
|
2021-06-02 15:25:56 +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
|