From e16aedd354d1fef9c848cbfa9328617e8a468849 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Wed, 19 Feb 2025 08:57:53 +0800 Subject: [PATCH] fix(lsp): always setup key mappings for lsp (#709) refactor(blink.cmp): simplify 'cmdline' accept behavior (#709) refactor(glance.nvim): move 'glance' to user plugins (#709) --- lua/builtin/lsp.lua | 232 ++++++++---------------- lua/configs/saghen/blink-cmp/config.lua | 33 +--- lua/plugins/init.lua | 6 - lua/plugins/users_sample.lua | 6 + 4 files changed, 86 insertions(+), 191 deletions(-) diff --git a/lua/builtin/lsp.lua b/lua/builtin/lsp.lua index 3f7919de..8403c5a7 100644 --- a/lua/builtin/lsp.lua +++ b/lua/builtin/lsp.lua @@ -10,10 +10,6 @@ vim.lsp.handlers["textDocument/hover"] = vim.lsp.handlers["textDocument/signatureHelp"] = vim.lsp.with(vim.lsp.handlers.signature_help, { border = constants.window.border }) -local function make_opts(value) - return { buffer = true, desc = value } -end - --- @param next boolean --- @param severity integer? local function goto_diagnostic(next, severity) @@ -23,126 +19,84 @@ local function goto_diagnostic(next, severity) end end -local lsp_goto_mapping = { - definition = { - mode = "n", - lhs = "gd", - rhs = { - "FzfxLspDefinitions", - { "Glance", "definitions" }, - { "lua", "vim.lsp.buf.definition()" }, - }, - desc = "Go to LSP definitions", - }, - type_definition = { - mode = "n", - lhs = "gt", - rhs = { - "FzfxLspTypeDefinitions", - { "Glance", "type_definitions" }, - { "lua", "vim.lsp.buf.type_definition()" }, - }, - desc = "Go to LSP type definitions", - }, - implementation = { - mode = "n", - lhs = "gi", - rhs = { - "FzfxLspImplementations", - { "Glance", "implementations" }, - { "lua", "vim.lsp.buf.implementation()" }, - }, - desc = "Go to LSP implementations", - }, - reference = { - mode = "n", - lhs = "gr", - rhs = { - "FzfxLspReferences", - { "Glance", "references" }, - { "lua", "vim.lsp.buf.reference()" }, - }, - desc = "Go to LSP references", - }, - hover = { - mode = "n", - lhs = "K", - rhs = { - { "lua", "vim.lsp.buf.hover()" }, - }, - desc = "Show hover", - }, - signature_help = { - mode = { "n", "i" }, - lhs = "", - rhs = { - { "lua", "vim.lsp.buf.signature_help()" }, - }, - desc = "Show signature help", - }, - rename = { - mode = "n", - lhs = "rn", - rhs = { - { "lua", "vim.lsp.buf.rename()" }, - }, - desc = "Rename symbol", - }, - code_action = { - mode = "n", - lhs = "ca", - rhs = { - { "lua", "vim.lsp.buf.code_action()" }, - }, - desc = "Run code actions", - }, - range_code_action = { - mode = "x", - lhs = "ca", - rhs = { - { "lua", "vim.lsp.buf.range_code_action()" }, - }, - desc = "Run code actions", - }, -} +set_key("n", "gd", function() + if vim.fn.exists(":FzfxLspDefinitions") > 0 then + vim.cmd("FzfxLspDefinitions") + else + vim.lsp.buf.definition() + end +end, { desc = "Go to LSP definitions" }) -local function lsp_key(name, optional) - local configs = lsp_goto_mapping[name] - assert(type(configs) == "table") +set_key("n", "gt", function() + if vim.fn.exists(":FzfxLspTypeDefinitions") > 0 then + vim.cmd("FzfxLspTypeDefinitions") + else + vim.lsp.buf.type_definition() + end +end, { desc = "Go to LSP type definitions" }) - local hit = 0 - local right_hands = configs.rhs - for _, right_hand in ipairs(right_hands) do - if type(right_hand) == "string" and vim.fn.exists(":" .. right_hand) > 0 then - set_key( - configs.mode, - configs.lhs, - string.format("%s", right_hand), - make_opts(configs.desc) - ) - hit = hit + 1 - break - elseif - type(right_hand) == "table" - and type(right_hand[1]) == "string" - and vim.fn.exists(":" .. right_hand[1]) > 0 - then - set_key( - configs.mode, - configs.lhs, - string.format("%s", table.concat(right_hand, " ")), - make_opts(configs.desc) - ) - hit = hit + 1 - break - end +set_key("n", "gi", function() + if vim.fn.exists(":FzfxLspImplementations") > 0 then + vim.cmd("FzfxLspImplementations") + else + vim.lsp.buf.implementation() end - if optional then - assert(hit <= 1) +end, { desc = "Go to LSP implementations" }) + +set_key("n", "gr", function() + if vim.fn.exists(":FzfxLspReferences") > 0 then + vim.cmd("FzfxLspReferences") else - assert(hit == 1) + vim.lsp.buf.references() end -end +end, { desc = "Go to LSP references" }) + +set_key("n", "K", function() + vim.lsp.buf.hover() +end, { desc = "Show LSP hover" }) + +set_key({ "n", "i" }, "", function() + vim.lsp.buf.signature_help() +end, { desc = "Show LSP signature help" }) + +set_key("n", "rn", function() + vim.lsp.buf.rename() +end, { desc = "Rename LSP symbol" }) + +set_key("n", "ca", function() + vim.lsp.buf.code_action() +end, { desc = "Run LSP code action" }) + +set_key("x", "ca", function() + vim.lsp.buf.range_code_action() +end, { desc = "Run LSP code action on visual selection" }) + +set_key("n", "]d", goto_diagnostic(true), { desc = "Next diagnostic item" }) +set_key("n", "[d", goto_diagnostic(false), { desc = "Previous diagnostic item" }) +set_key( + "n", + "]e", + goto_diagnostic(true, vim.diagnostic.severity.ERROR), + { desc = "Go to next error" } +) +set_key( + "n", + "[e", + goto_diagnostic(false, vim.diagnostic.severity.ERROR), + { desc = "Go to previous error" } +) +set_key( + "n", + "]w", + goto_diagnostic(true, vim.diagnostic.severity.WARN), + { desc = "Go to next warning" } +) +set_key( + "n", + "[w", + goto_diagnostic(false, vim.diagnostic.severity.WARN), + { desc = "Go to previous warning" } +) local builtin_lsp_augroup = vim.api.nvim_create_augroup("builtin_lsp_augroup", { clear = true }) vim.api.nvim_create_autocmd("LspAttach", { @@ -151,45 +105,5 @@ vim.api.nvim_create_autocmd("LspAttach", { vim.bo[ev.buf].formatexpr = nil vim.bo[ev.buf].omnifunc = nil vim.bo[ev.buf].tagfunc = nil - - lsp_key("definition") - lsp_key("type_definition") - lsp_key("implementation") - lsp_key("reference") - - lsp_key("hover") - lsp_key("signature_help") - - lsp_key("rename") - lsp_key("code_action") - lsp_key("range_code_action") - - -- diagnostic - set_key("n", "]d", goto_diagnostic(true), make_opts("Next diagnostic item")) - set_key("n", "[d", goto_diagnostic(false), make_opts("Previous diagnostic item")) - set_key( - "n", - "]e", - goto_diagnostic(true, vim.diagnostic.severity.ERROR), - make_opts("Go to next error") - ) - set_key( - "n", - "[e", - goto_diagnostic(false, vim.diagnostic.severity.ERROR), - make_opts("Go to previous error") - ) - set_key( - "n", - "]w", - goto_diagnostic(true, vim.diagnostic.severity.WARN), - make_opts("Go to next warning") - ) - set_key( - "n", - "[w", - goto_diagnostic(false, vim.diagnostic.severity.WARN), - make_opts("Go to previous warning") - ) end, }) diff --git a/lua/configs/saghen/blink-cmp/config.lua b/lua/configs/saghen/blink-cmp/config.lua index 5ce434c8..0ae3d848 100644 --- a/lua/configs/saghen/blink-cmp/config.lua +++ b/lua/configs/saghen/blink-cmp/config.lua @@ -1,34 +1,15 @@ -local function choose_auto_insert(ctx) - if ctx.mode == "cmdline" then - return true - end - if - type(ctx.bufnr) == "number" - and ctx.bufnr >= 0 - and vim.api.nvim_buf_is_valid(ctx.bufnr) - and vim.api.nvim_get_option_value("buftype", { buf = ctx.bufnr }) == "prompt" - then - return true - end - local cur_win = vim.api.nvim_get_current_win() - if vim.fn.win_gettype(cur_win) == "popup" then - return true - end - return false -end - -local function choose_preselect(ctx) - return not choose_auto_insert(ctx) -end - require("blink.cmp").setup({ completion = { list = { selection = { - -- Use "auto_insert" for specific buf/win. - auto_insert = choose_auto_insert, + -- Use "auto_insert" for cmdline. + auto_insert = function(ctx) + return ctx.mode == "cmdline" + end, -- Otherwise use "preselect". - preselect = choose_preselect, + preselect = function(ctx) + return ctx.mode ~= "cmdline" + end, }, }, accept = { auto_brackets = { enabled = true } }, diff --git a/lua/plugins/init.lua b/lua/plugins/init.lua index 3b865e2d..0647b337 100644 --- a/lua/plugins/init.lua +++ b/lua/plugins/init.lua @@ -236,12 +236,6 @@ local M = { }, config = lua_config("jay-babu/mason-null-ls.nvim"), }, - -- Symbol navigation - { - "DNLHC/glance.nvim", - cmd = { "Glance" }, - config = lua_config("DNLHC/glance.nvim"), - }, -- ---- AUTO-COMPLETE ---- { diff --git a/lua/plugins/users_sample.lua b/lua/plugins/users_sample.lua index debab81b..15e2d9c4 100644 --- a/lua/plugins/users_sample.lua +++ b/lua/plugins/users_sample.lua @@ -23,6 +23,12 @@ -- lazy = false, -- config = lua_config("folke/noice.nvim"), -- }, +-- -- Symbol navigation +-- { +-- "DNLHC/glance.nvim", +-- cmd = { "Glance" }, +-- config = lua_config("DNLHC/glance.nvim"), +-- }, -- -- Yank -- { -- "gbprod/yanky.nvim",