Files
nvim/lua/lsp/completion/init.lua
T

123 lines
3.4 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,
})
---@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