From a4295395e20819ee36cdddb985c4b6e03c1e169d Mon Sep 17 00:00:00 2001 From: groveer Date: Sun, 14 May 2023 11:07:48 +0800 Subject: [PATCH] chore(keymap): lsp keymap should effective at `LspAttach` event (#735) --- lua/core/event.lua | 8 +++++ lua/keymap/completion.lua | 62 ++++++++++++++++----------------------- 2 files changed, 34 insertions(+), 36 deletions(-) diff --git a/lua/core/event.lua b/lua/core/event.lua index bb5dbf90a..3208992ec 100644 --- a/lua/core/event.lua +++ b/lua/core/event.lua @@ -13,6 +13,14 @@ function autocmd.nvim_create_augroups(definitions) end end +local mapping = require("keymap.completion") +vim.api.nvim_create_autocmd("LspAttach", { + group = vim.api.nvim_create_augroup("UserLspConfig", {}), + callback = function(event) + mapping.lsp(event.buf) + end, +}) + -- auto close NvimTree vim.api.nvim_create_autocmd("BufEnter", { group = vim.api.nvim_create_augroup("NvimTreeClose", { clear = true }), diff --git a/lua/keymap/completion.lua b/lua/keymap/completion.lua index adb473953..953e4c770 100644 --- a/lua/keymap/completion.lua +++ b/lua/keymap/completion.lua @@ -4,41 +4,31 @@ local map_cu = bind.map_cu -- local map_cmd = bind.map_cmd local map_callback = bind.map_callback -local plug_map = { - -- LSP-related keymaps, work only when event = { "InsertEnter", "LspStart" } - ["n|li"] = map_cr("LspInfo"):with_noremap():with_silent():with_nowait():with_desc("lsp: Info"), - ["n|lr"] = map_cr("LspRestart"):with_noremap():with_silent():with_nowait():with_desc("lsp: Restart"), - ["n|go"] = map_cr("Lspsaga outline"):with_noremap():with_silent():with_desc("lsp: Toggle outline"), - ["n|g["] = map_cr("Lspsaga diagnostic_jump_prev"):with_noremap():with_silent():with_desc("lsp: Prev diagnostic"), - ["n|g]"] = map_cr("Lspsaga diagnostic_jump_next"):with_noremap():with_silent():with_desc("lsp: Next diagnostic"), - ["n|ld"] = map_cr("Lspsaga show_line_diagnostics") - :with_noremap() - :with_silent() - :with_desc("lsp: Line diagnostic"), - ["n|gs"] = map_callback(function() +local mapping = {} + +function mapping.lsp(buf) + local map = { + -- LSP-related keymaps, work only when event = { "InsertEnter", "LspStart" } + ["n|li"] = map_cr("LspInfo"):with_buffer(buf):with_desc("lsp: Info"), + ["n|lr"] = map_cr("LspRestart"):with_buffer(buf):with_nowait():with_desc("lsp: Restart"), + ["n|go"] = map_cr("Lspsaga outline"):with_buffer(buf):with_desc("lsp: Toggle outline"), + ["n|g["] = map_cr("Lspsaga diagnostic_jump_prev"):with_buffer(buf):with_desc("lsp: Prev diagnostic"), + ["n|g]"] = map_cr("Lspsaga diagnostic_jump_next"):with_buffer(buf):with_desc("lsp: Next diagnostic"), + ["n|ld"] = map_cr("Lspsaga show_line_diagnostics"):with_buffer(buf):with_desc("lsp: Line diagnostic"), + ["n|gs"] = map_callback(function() vim.lsp.buf.signature_help() - end) - :with_noremap() - :with_silent() - :with_desc("lsp: Signature help"), - ["n|gr"] = map_cr("Lspsaga rename"):with_noremap():with_silent():with_desc("lsp: Rename in file range"), - ["n|gR"] = map_cr("Lspsaga rename ++project") - :with_noremap() - :with_silent() - :with_desc("lsp: Rename in project range"), - ["n|K"] = map_cr("Lspsaga hover_doc"):with_noremap():with_silent():with_desc("lsp: Show doc"), - ["nv|ga"] = map_cr("Lspsaga code_action"):with_noremap():with_silent():with_desc("lsp: Code action"), - ["n|gd"] = map_cr("Lspsaga peek_definition"):with_noremap():with_silent():with_desc("lsp: Preview definition"), - ["n|gD"] = map_cr("Lspsaga goto_definition"):with_noremap():with_silent():with_desc("lsp: Goto definition"), - ["n|gh"] = map_cr("Lspsaga lsp_finder"):with_noremap():with_silent():with_desc("lsp: Show reference"), - ["n|ci"] = map_cr("Lspsaga incoming_calls") - :with_noremap() - :with_silent() - :with_desc("lsp: Show incoming calls"), - ["n|co"] = map_cr("Lspsaga outgoing_calls") - :with_noremap() - :with_silent() - :with_desc("lsp: Show outgoing calls"), -} + end):with_desc("lsp: Signature help"), + ["n|gr"] = map_cr("Lspsaga rename"):with_buffer(buf):with_desc("lsp: Rename in file range"), + ["n|gR"] = map_cr("Lspsaga rename ++project"):with_buffer(buf):with_desc("lsp: Rename in project range"), + ["n|K"] = map_cr("Lspsaga hover_doc"):with_buffer(buf):with_desc("lsp: Show doc"), + ["nv|ga"] = map_cr("Lspsaga code_action"):with_buffer(buf):with_desc("lsp: Code action for cursor"), + ["n|gd"] = map_cr("Lspsaga peek_definition"):with_buffer(buf):with_desc("lsp: Preview definition"), + ["n|gD"] = map_cr("Lspsaga goto_definition"):with_buffer(buf):with_desc("lsp: Goto definition"), + ["n|gh"] = map_cr("Lspsaga lsp_finder"):with_buffer(buf):with_desc("lsp: Show reference"), + ["n|ci"] = map_cr("Lspsaga incoming_calls"):with_buffer(buf):with_desc("lsp: Show incoming calls"), + ["n|co"] = map_cr("Lspsaga outgoing_calls"):with_buffer(buf):with_desc("lsp: Show outgoing calls"), + } + bind.nvim_load_mapping(map) +end -bind.nvim_load_mapping(plug_map) +return mapping