Skip to content

Commit

Permalink
fix(lsp): always setup key mappings for lsp (#709)
Browse files Browse the repository at this point in the history
refactor(blink.cmp): simplify 'cmdline' accept behavior (#709)
refactor(glance.nvim): move 'glance' to user plugins (#709)
  • Loading branch information
linrongbin16 authored Feb 19, 2025
1 parent d617dfd commit e16aedd
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 191 deletions.
232 changes: 73 additions & 159 deletions lua/builtin/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 = "<C-k>",
rhs = {
{ "lua", "vim.lsp.buf.signature_help()" },
},
desc = "Show signature help",
},
rename = {
mode = "n",
lhs = "<Leader>rn",
rhs = {
{ "lua", "vim.lsp.buf.rename()" },
},
desc = "Rename symbol",
},
code_action = {
mode = "n",
lhs = "<Leader>ca",
rhs = {
{ "lua", "vim.lsp.buf.code_action()" },
},
desc = "Run code actions",
},
range_code_action = {
mode = "x",
lhs = "<Leader>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("<CMD>%s<CR>", 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("<CMD>%s<CR>", 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" }, "<C-k>", function()
vim.lsp.buf.signature_help()
end, { desc = "Show LSP signature help" })

set_key("n", "<Leader>rn", function()
vim.lsp.buf.rename()
end, { desc = "Rename LSP symbol" })

set_key("n", "<Leader>ca", function()
vim.lsp.buf.code_action()
end, { desc = "Run LSP code action" })

set_key("x", "<Leader>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", {
Expand All @@ -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,
})
33 changes: 7 additions & 26 deletions lua/configs/saghen/blink-cmp/config.lua
Original file line number Diff line number Diff line change
@@ -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 } },
Expand Down
6 changes: 0 additions & 6 deletions lua/plugins/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 ----
{
Expand Down
6 changes: 6 additions & 0 deletions lua/plugins/users_sample.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit e16aedd

Please sign in to comment.