feat(lsp): add snippet support and refactor completion into Item class

This commit is contained in:
2026-04-17 00:03:03 +02:00
parent 5ecec7cc6c
commit 47f36adc21
9 changed files with 213 additions and 144 deletions
+47 -45
View File
@@ -1,3 +1,4 @@
local Item = require("lsp.completion.item")
local Popup = require("lsp.completion.popup")
local request = require("lsp.completion.request")
@@ -9,7 +10,7 @@ local M = {}
---@param capabilities lsp.ClientCapabilities
function M.apply_capabilities(capabilities)
capabilities.textDocument.completion.completionItem.snippetSupport = false
capabilities.textDocument.completion.completionItem.snippetSupport = true
end
---@param client vim.lsp.Client
@@ -38,18 +39,10 @@ function M.setup()
group = GROUP,
callback = function(ev)
local completed = vim.v.event.completed_item or {}
local lsp_item = vim.tbl_get(
completed,
"user_data",
"nvim",
"lsp",
"completion_item"
)
local client_id =
vim.tbl_get(completed, "user_data", "nvim", "lsp", "client_id")
local client = client_id and vim.lsp.get_client_by_id(client_id)
local item = Item.from_user_data(completed)
local client = item and vim.lsp.get_client_by_id(item.client_id)
if not lsp_item or not client then
if not item or not client then
vim.schedule(function()
popup:close()
end)
@@ -73,7 +66,7 @@ function M.setup()
then
popup:dispatch_resolve(
client,
lsp_item,
item,
ft,
pum,
completed.word,
@@ -81,7 +74,7 @@ function M.setup()
)
else
vim.schedule(function()
popup:show(lsp_item, ft, pum)
popup:show(item, ft, pum)
end)
end
end,
@@ -94,52 +87,53 @@ function M.setup()
end,
})
-- Apply the LSP item's additionalTextEdits (auto-imports, etc.) and
-- run its post-accept command whenever an item's text was committed to
-- the buffer. This covers both explicit accept (<C-y>) and
-- acceptance-by-continuing-to-type, since Vim keeps the inserted text
-- in both cases. Only <C-e>/discard leaves v:completed_item empty.
vim.api.nvim_create_autocmd("CompleteDone", {
group = GROUP,
callback = function(ev)
local completed = vim.v.completed_item or {}
local original = vim.tbl_get(
completed,
"user_data",
"nvim",
"lsp",
"completion_item"
)
if not original then
local item = Item.from_user_data(completed)
if not item then
return
end
local client_id =
vim.tbl_get(completed, "user_data", "nvim", "lsp", "client_id")
local client = client_id and vim.lsp.get_client_by_id(client_id)
local client = vim.lsp.get_client_by_id(item.client_id)
if not client then
return
end
local function apply(item)
if item.additionalTextEdits then
local function apply(target)
local raw = target.raw
if raw.additionalTextEdits then
vim.lsp.util.apply_text_edits(
item.additionalTextEdits,
raw.additionalTextEdits,
ev.buf,
client.offset_encoding
)
end
if item.command then
if raw.command then
client:request("workspace/executeCommand", {
command = item.command.command,
arguments = item.command.arguments,
command = raw.command.command,
arguments = raw.command.arguments,
}, nil, ev.buf)
end
if target.snippet then
local word = completed.word or ""
local cursor = vim.api.nvim_win_get_cursor(0)
local row = cursor[1] - 1
local col = cursor[2]
vim.api.nvim_buf_set_text(
ev.buf,
row,
col - #word,
row,
col,
{}
)
vim.snippet.expand(target.snippet)
end
end
-- Prefer the resolved item when we have it (servers like
-- rust-analyzer only provide additionalTextEdits in resolve
-- responses). If we haven't cached one yet and the original is
-- missing edits, resolve on the fly.
-- Prefer the resolved item when we have it. If we haven't cached
-- one yet and the original is missing edits, resolve on the fly.
local cached = popup:resolved_for(completed.word)
if cached then
apply(cached)
@@ -147,22 +141,23 @@ function M.setup()
client:supports_method(
vim.lsp.protocol.Methods.completionItem_resolve
)
and not original.additionalTextEdits
and not original.command
and not item.raw.additionalTextEdits
and not item.raw.command
then
client:request(
vim.lsp.protocol.Methods.completionItem_resolve,
original,
item.raw,
function(err, resolved)
if err or not resolved then
return
end
apply(resolved)
item:apply_resolved(resolved)
apply(item)
end,
ev.buf
)
else
apply(original)
apply(item)
end
end,
})
@@ -190,6 +185,13 @@ function M.setup()
scroll_map("<C-k>", function()
popup:scroll_line(vim.keycode("<C-y>"))
end)
vim.keymap.set("i", "<CR>", function()
if vim.fn.pumvisible() ~= 0 then
return "<C-y>"
end
return "<CR>"
end, { expr = true, replace_keycodes = true })
end
return M