Skip to content

Commit 0d5239d

Browse files
committed
lsp
1 parent 3337feb commit 0d5239d

2 files changed

Lines changed: 67 additions & 3 deletions

File tree

‎lua/config/options.lua‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ end, { desc = "Yank file path (absolute)" })
4747
vim.keymap.set("i", "<c-space>", "<c-x><c-o>", { desc = "LSP completion" })
4848
vim.keymap.set("i", "<c-l>", "<c-x><c-l>", { desc = "Line completion" })
4949
vim.keymap.set("i", "<c-f>", "<c-x><c-f>", { desc = "File completion" })
50-
-- lsp keymap: gd set buffer-local in plugin_config.lua via LspAttach
51-
-- todo: it is a bit annoying to have repeated entries when multiple lsps exist, should either dedup or show sources
50+
-- lsp keymap: gd/grr/gri/grt use on_list dedupe in plugin_config.lua (multi-LSP)
5251

5352
-- diagnostics: tag source so multiple LSPs are distinguishable
5453
vim.diagnostic.config({

‎lua/config/plugin_config.lua‎

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,69 @@ if not is_vscode then
149149
root_markers = { "Package.swift", "compile_commands.json", ".git" },
150150
})
151151
vim.lsp.enable("sourcekit")
152+
153+
local function lsp_on_list(what, tag)
154+
vim.list.unique(what.items, function(item)
155+
return ("%s\0%d\0%d\0%d\0%d"):format(
156+
item.filename or "",
157+
item.lnum or 0,
158+
item.col or 0,
159+
item.end_lnum or 0,
160+
item.end_col or 0
161+
)
162+
end)
163+
164+
vim.fn.setqflist({}, " ", what)
165+
166+
local method = what.context and what.context.method
167+
local jump_single = #what.items == 1
168+
and method ~= "textDocument/implementation"
169+
and method ~= "textDocument/references"
170+
171+
if jump_single then
172+
if tag then
173+
vim.fn.settagstack(tag.win, {
174+
items = { { tagname = tag.name, from = tag.from } },
175+
}, "t")
176+
end
177+
vim.cmd("cfirst")
178+
else
179+
vim.cmd("botright copen")
180+
end
181+
end
182+
183+
---@param method fun(opts?: vim.lsp.ListOpts)
184+
local function lsp_jump(method)
185+
local from = vim.fn.getpos(".")
186+
from[1] = vim.api.nvim_get_current_buf()
187+
local tag = {
188+
name = vim.fn.expand("<cword>"),
189+
from = from,
190+
win = vim.api.nvim_get_current_win(),
191+
}
192+
method({
193+
on_list = function(what)
194+
lsp_on_list(what, tag)
195+
end,
196+
})
197+
end
198+
199+
local function lsp_references()
200+
vim.lsp.buf.references(nil, {
201+
on_list = function(what)
202+
lsp_on_list(what)
203+
end,
204+
})
205+
end
206+
207+
vim.keymap.set("n", "grr", lsp_references, { desc = "vim.lsp.buf.references() (deduped)" })
208+
vim.keymap.set("n", "gri", function()
209+
lsp_jump(vim.lsp.buf.implementation)
210+
end, { desc = "vim.lsp.buf.implementation() (deduped)" })
211+
vim.keymap.set("n", "grt", function()
212+
lsp_jump(vim.lsp.buf.type_definition)
213+
end, { desc = "vim.lsp.buf.type_definition() (deduped)" })
214+
152215
vim.api.nvim_create_autocmd("LspAttach", {
153216
callback = function(ev)
154217
local client = vim.lsp.get_client_by_id(ev.data.client_id)
@@ -157,7 +220,9 @@ if not is_vscode then
157220
vim.lsp.completion.enable(true, client.id, ev.buf, { autotrigger = true })
158221
end)
159222
end
160-
vim.keymap.set("n", "gd", vim.lsp.buf.definition, { buffer = ev.buf })
223+
vim.keymap.set("n", "gd", function()
224+
lsp_jump(vim.lsp.buf.definition)
225+
end, { buffer = ev.buf, desc = "LSP definition (deduped)" })
161226
vim.keymap.set("n", "<leader>la", vim.lsp.buf.code_action, { buffer = ev.buf, desc = "LSP code action" })
162227
end,
163228
})

0 commit comments

Comments
 (0)