196 lines
6.1 KiB
Lua
196 lines
6.1 KiB
Lua
local Popup = require("lsp.completion.popup")
|
|
local request = require("lsp.completion.request")
|
|
|
|
local GROUP = vim.api.nvim_create_augroup("ow.lsp.completion", { clear = true })
|
|
|
|
local popup = Popup.new()
|
|
|
|
local M = {}
|
|
|
|
---@param capabilities lsp.ClientCapabilities
|
|
function M.apply_capabilities(capabilities)
|
|
capabilities.textDocument.completion.completionItem.snippetSupport = false
|
|
end
|
|
|
|
---@param client vim.lsp.Client
|
|
---@param buf integer
|
|
function M.on_attach(client, buf)
|
|
if
|
|
not client:supports_method(
|
|
vim.lsp.protocol.Methods.textDocument_completion
|
|
)
|
|
then
|
|
return
|
|
end
|
|
|
|
vim.bo[buf].omnifunc = "v:lua.ow_lsp_completion.omnifunc"
|
|
|
|
vim.api.nvim_clear_autocmds({ group = GROUP, buffer = buf })
|
|
vim.api.nvim_create_autocmd("InsertCharPre", {
|
|
buffer = buf,
|
|
group = GROUP,
|
|
callback = request.on_insert_char_pre,
|
|
})
|
|
end
|
|
|
|
function M.setup()
|
|
vim.api.nvim_create_autocmd("CompleteChanged", {
|
|
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)
|
|
|
|
if not lsp_item or not client then
|
|
vim.schedule(function()
|
|
popup:close()
|
|
end)
|
|
return
|
|
end
|
|
|
|
local ft = vim.bo[ev.buf].filetype
|
|
---@type ow.lsp.completion.Pum
|
|
local pum = {
|
|
row = vim.v.event.row,
|
|
col = vim.v.event.col,
|
|
width = vim.v.event.width,
|
|
height = vim.v.event.height,
|
|
scrollbar = vim.v.event.scrollbar,
|
|
}
|
|
|
|
if
|
|
client:supports_method(
|
|
vim.lsp.protocol.Methods.completionItem_resolve
|
|
)
|
|
then
|
|
popup:dispatch_resolve(
|
|
client,
|
|
lsp_item,
|
|
ft,
|
|
pum,
|
|
completed.word,
|
|
ev.buf
|
|
)
|
|
else
|
|
vim.schedule(function()
|
|
popup:show(lsp_item, ft, pum)
|
|
end)
|
|
end
|
|
end,
|
|
})
|
|
|
|
vim.api.nvim_create_autocmd({ "CompleteDonePre", "InsertLeave" }, {
|
|
group = GROUP,
|
|
callback = function()
|
|
popup:close()
|
|
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
|
|
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)
|
|
if not client then
|
|
return
|
|
end
|
|
|
|
local function apply(item)
|
|
if item.additionalTextEdits then
|
|
vim.lsp.util.apply_text_edits(
|
|
item.additionalTextEdits,
|
|
ev.buf,
|
|
client.offset_encoding
|
|
)
|
|
end
|
|
if item.command then
|
|
client:request("workspace/executeCommand", {
|
|
command = item.command.command,
|
|
arguments = item.command.arguments,
|
|
}, nil, ev.buf)
|
|
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.
|
|
local cached = popup:resolved_for(completed.word)
|
|
if cached then
|
|
apply(cached)
|
|
elseif
|
|
client:supports_method(
|
|
vim.lsp.protocol.Methods.completionItem_resolve
|
|
)
|
|
and not original.additionalTextEdits
|
|
and not original.command
|
|
then
|
|
client:request(
|
|
vim.lsp.protocol.Methods.completionItem_resolve,
|
|
original,
|
|
function(err, resolved)
|
|
if err or not resolved then
|
|
return
|
|
end
|
|
apply(resolved)
|
|
end,
|
|
ev.buf
|
|
)
|
|
else
|
|
apply(original)
|
|
end
|
|
end,
|
|
})
|
|
|
|
---@param key string
|
|
---@param action fun()
|
|
local function scroll_map(key, action)
|
|
vim.keymap.set("i", key, function()
|
|
if popup:is_visible() then
|
|
vim.schedule(action)
|
|
return ""
|
|
end
|
|
return key
|
|
end, { expr = true, replace_keycodes = true })
|
|
end
|
|
scroll_map("<C-d>", function()
|
|
popup:scroll_half(vim.keycode("<C-e>"))
|
|
end)
|
|
scroll_map("<C-u>", function()
|
|
popup:scroll_half(vim.keycode("<C-y>"))
|
|
end)
|
|
scroll_map("<C-j>", function()
|
|
popup:scroll_line(vim.keycode("<C-e>"))
|
|
end)
|
|
scroll_map("<C-k>", function()
|
|
popup:scroll_line(vim.keycode("<C-y>"))
|
|
end)
|
|
end
|
|
|
|
return M
|