feat(lsp): manage completion docs popup ourselves
This commit is contained in:
@@ -5,6 +5,11 @@ return {
|
|||||||
check = {
|
check = {
|
||||||
command = "clippy",
|
command = "clippy",
|
||||||
},
|
},
|
||||||
|
completion = {
|
||||||
|
fullFunctionSignatures = {
|
||||||
|
enable = true,
|
||||||
|
},
|
||||||
|
},
|
||||||
diagnostics = {
|
diagnostics = {
|
||||||
styleLints = {
|
styleLints = {
|
||||||
enable = true,
|
enable = true,
|
||||||
|
|||||||
@@ -29,7 +29,6 @@ vim.opt.foldignore = ""
|
|||||||
vim.opt.completeopt = {
|
vim.opt.completeopt = {
|
||||||
"menu",
|
"menu",
|
||||||
"menuone",
|
"menuone",
|
||||||
"popup",
|
|
||||||
"noinsert",
|
"noinsert",
|
||||||
"noselect",
|
"noselect",
|
||||||
}
|
}
|
||||||
|
|||||||
+287
-118
@@ -1,40 +1,224 @@
|
|||||||
local WORD_CHARS =
|
local WORD_CHARS =
|
||||||
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"
|
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"
|
||||||
|
|
||||||
local GROUP = vim.api.nvim_create_augroup("ow.lsp.completion", { clear = true })
|
local MAX_WIDTH = 80
|
||||||
|
local MAX_HEIGHT = 20
|
||||||
|
|
||||||
|
local GROUP = vim.api.nvim_create_augroup("ow.lsp.completion", { clear = true })
|
||||||
|
local NS = vim.api.nvim_create_namespace("ow.lsp.completion")
|
||||||
|
|
||||||
|
---@type table<integer, true>
|
||||||
|
local word_chars_extended = {}
|
||||||
|
|
||||||
|
---@alias lsp.completion.Pum { row: integer, col: integer, width: integer, height: integer, scrollbar: boolean }
|
||||||
|
|
||||||
|
---@param ft string
|
||||||
|
---@param text string
|
||||||
|
---@return string
|
||||||
local function fence(ft, text)
|
local function fence(ft, text)
|
||||||
return string.format("```%s\n%s\n```", ft, text)
|
return string.format("```%s\n%s\n```", ft, text)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function style_popup(winid, bufnr, width)
|
---@param item lsp.CompletionItem
|
||||||
if
|
---@return string
|
||||||
not winid
|
local function signature_of(item)
|
||||||
or winid <= 0
|
return item.detail or vim.tbl_get(item, "labelDetails", "description") or ""
|
||||||
or not vim.api.nvim_win_is_valid(winid)
|
end
|
||||||
or vim.api.nvim_win_get_config(winid).relative == ""
|
|
||||||
then
|
---@type { winid: integer?, bufnr: integer?, pending: { client: vim.lsp.Client, id: integer }? }
|
||||||
|
local popup = {
|
||||||
|
winid = nil,
|
||||||
|
bufnr = nil,
|
||||||
|
pending = nil,
|
||||||
|
}
|
||||||
|
|
||||||
|
local function cancel_pending()
|
||||||
|
if popup.pending then
|
||||||
|
pcall(
|
||||||
|
popup.pending.client.cancel_request,
|
||||||
|
popup.pending.client,
|
||||||
|
popup.pending.id
|
||||||
|
)
|
||||||
|
popup.pending = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
---@return integer bufnr
|
||||||
|
local function ensure_buffer()
|
||||||
|
if not popup.bufnr or not vim.api.nvim_buf_is_valid(popup.bufnr) then
|
||||||
|
popup.bufnr = vim.api.nvim_create_buf(false, true)
|
||||||
|
vim.bo[popup.bufnr].buftype = "nofile"
|
||||||
|
vim.bo[popup.bufnr].bufhidden = "hide"
|
||||||
|
vim.bo[popup.bufnr].swapfile = false
|
||||||
|
pcall(vim.treesitter.start, popup.bufnr, "markdown")
|
||||||
|
end
|
||||||
|
return popup.bufnr
|
||||||
|
end
|
||||||
|
|
||||||
|
local function update_indicators()
|
||||||
|
if not popup.winid or not vim.api.nvim_win_is_valid(popup.winid) then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
local cfg = { border = "rounded" }
|
local visible = vim.api.nvim_win_get_height(popup.winid)
|
||||||
if width then
|
local topline, botline = unpack(vim.api.nvim_win_call(popup.winid, function()
|
||||||
cfg.width = math.min(width, 80)
|
return { vim.fn.line("w0"), vim.fn.line("w$") }
|
||||||
|
end))
|
||||||
|
local ok_total, info_total =
|
||||||
|
pcall(vim.api.nvim_win_text_height, popup.winid, {})
|
||||||
|
local total = ok_total and info_total.all or visible
|
||||||
|
|
||||||
|
local display_topline = 1
|
||||||
|
if topline > 1 then
|
||||||
|
local ok, info = pcall(vim.api.nvim_win_text_height, popup.winid, {
|
||||||
|
end_row = topline - 2,
|
||||||
|
})
|
||||||
|
if ok then
|
||||||
|
display_topline = (info.all or 0) + 1
|
||||||
|
end
|
||||||
end
|
end
|
||||||
if bufnr and vim.api.nvim_buf_is_valid(bufnr) then
|
local display_bot = display_topline + visible - 1
|
||||||
vim.wo[winid].wrap = true
|
|
||||||
vim.wo[winid].linebreak = true
|
local has_above = display_topline > 1
|
||||||
vim.wo[winid].conceallevel = 2
|
local has_below = display_bot < total and botline < vim.api.nvim_buf_line_count(
|
||||||
pcall(vim.treesitter.start, bufnr, "markdown")
|
vim.api.nvim_win_get_buf(popup.winid)
|
||||||
|
)
|
||||||
|
pcall(vim.api.nvim_win_set_config, popup.winid, {
|
||||||
|
title = has_above and { { "▲ ", "FloatBorder" } } or "",
|
||||||
|
title_pos = has_above and "right" or nil,
|
||||||
|
footer = has_below and { { "▼ ", "FloatBorder" } } or "",
|
||||||
|
footer_pos = has_below and "right" or nil,
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
|
local function close()
|
||||||
|
cancel_pending()
|
||||||
|
if popup.winid and vim.api.nvim_win_is_valid(popup.winid) then
|
||||||
|
pcall(vim.api.nvim_win_close, popup.winid, true)
|
||||||
end
|
end
|
||||||
pcall(vim.api.nvim_win_set_config, winid, cfg)
|
popup.winid = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param content string
|
||||||
|
---@param pum lsp.completion.Pum
|
||||||
|
---@param width integer
|
||||||
|
local function show(content, pum, width)
|
||||||
|
local bufnr = ensure_buffer()
|
||||||
|
local lines = vim.split(content, "\n", { plain = true })
|
||||||
|
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
|
||||||
|
|
||||||
|
vim.api.nvim_buf_clear_namespace(bufnr, NS, 0, -1)
|
||||||
|
for i, line in ipairs(lines) do
|
||||||
|
if line ~= "" and line:gsub("─", "") == "" then
|
||||||
|
vim.api.nvim_buf_set_extmark(bufnr, NS, i - 1, 0, {
|
||||||
|
end_row = i - 1,
|
||||||
|
end_col = #line,
|
||||||
|
hl_group = "FloatBorder",
|
||||||
|
priority = 200,
|
||||||
|
})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
width = math.min(width, MAX_WIDTH)
|
||||||
|
local height = math.min(#lines, MAX_HEIGHT)
|
||||||
|
|
||||||
|
local right_edge = pum.col + pum.width + (pum.scrollbar and 1 or 0)
|
||||||
|
local right_space = vim.o.columns - right_edge
|
||||||
|
local col
|
||||||
|
if right_space >= width + 2 then
|
||||||
|
col = right_edge
|
||||||
|
else
|
||||||
|
col = math.max(pum.col - width - 2, 0)
|
||||||
|
end
|
||||||
|
|
||||||
|
local cfg = {
|
||||||
|
relative = "editor",
|
||||||
|
row = pum.row,
|
||||||
|
col = col,
|
||||||
|
width = width,
|
||||||
|
height = height,
|
||||||
|
border = "rounded",
|
||||||
|
focusable = false,
|
||||||
|
style = "minimal",
|
||||||
|
}
|
||||||
|
|
||||||
|
if popup.winid and vim.api.nvim_win_is_valid(popup.winid) then
|
||||||
|
pcall(vim.api.nvim_win_set_config, popup.winid, cfg)
|
||||||
|
else
|
||||||
|
cfg.noautocmd = true
|
||||||
|
popup.winid = vim.api.nvim_open_win(bufnr, false, cfg)
|
||||||
|
vim.wo[popup.winid].wrap = true
|
||||||
|
vim.wo[popup.winid].linebreak = true
|
||||||
|
vim.wo[popup.winid].conceallevel = 2
|
||||||
|
end
|
||||||
|
pcall(vim.api.nvim_win_set_cursor, popup.winid, { 1, 0 })
|
||||||
|
|
||||||
|
local ok, info = pcall(vim.api.nvim_win_text_height, popup.winid, {})
|
||||||
|
if ok and info.all and info.all ~= height then
|
||||||
|
pcall(
|
||||||
|
vim.api.nvim_win_set_height,
|
||||||
|
popup.winid,
|
||||||
|
math.min(info.all, MAX_HEIGHT)
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
update_indicators()
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param item lsp.CompletionItem
|
||||||
|
---@param ft string
|
||||||
|
---@return string? content
|
||||||
|
---@return integer? width
|
||||||
|
local function build_content(item, ft)
|
||||||
|
local signature = signature_of(item)
|
||||||
|
local doc = item.documentation
|
||||||
|
if type(doc) == "table" then
|
||||||
|
doc = doc.value
|
||||||
|
end
|
||||||
|
doc = doc or ""
|
||||||
|
|
||||||
|
local code_parts = {}
|
||||||
|
if item.additionalTextEdits then
|
||||||
|
for _, edit in ipairs(item.additionalTextEdits) do
|
||||||
|
local text = (edit.newText or ""):gsub("%s+$", "")
|
||||||
|
if text ~= "" then
|
||||||
|
table.insert(code_parts, fence(ft, text))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if signature ~= "" then
|
||||||
|
table.insert(code_parts, fence(ft, signature))
|
||||||
|
end
|
||||||
|
|
||||||
|
local sections = {}
|
||||||
|
if #code_parts > 0 then
|
||||||
|
table.insert(sections, table.concat(code_parts, "\n\n"))
|
||||||
|
end
|
||||||
|
if doc ~= "" then
|
||||||
|
table.insert(sections, doc)
|
||||||
|
end
|
||||||
|
if #sections == 0 then
|
||||||
|
return nil, nil
|
||||||
|
end
|
||||||
|
|
||||||
|
local max_w = 0
|
||||||
|
for _, s in ipairs(sections) do
|
||||||
|
for _, line in ipairs(vim.split(s, "\n", { plain = true })) do
|
||||||
|
max_w = math.max(max_w, vim.fn.strdisplaywidth(line))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
local sep = "\n" .. string.rep("─", math.min(max_w, MAX_WIDTH)) .. "\n"
|
||||||
|
return table.concat(sections, sep), max_w
|
||||||
end
|
end
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
|
---@param capabilities lsp.ClientCapabilities
|
||||||
function M.apply_capabilities(capabilities)
|
function M.apply_capabilities(capabilities)
|
||||||
capabilities.textDocument.completion.completionItem.snippetSupport = false
|
capabilities.textDocument.completion.completionItem.snippetSupport = false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@param client vim.lsp.Client
|
||||||
|
---@param buf integer
|
||||||
function M.on_attach(client, buf)
|
function M.on_attach(client, buf)
|
||||||
if
|
if
|
||||||
not client:supports_method(
|
not client:supports_method(
|
||||||
@@ -45,9 +229,12 @@ function M.on_attach(client, buf)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local provider = client.server_capabilities.completionProvider
|
local provider = client.server_capabilities.completionProvider
|
||||||
|
if not provider then
|
||||||
|
return
|
||||||
|
end
|
||||||
provider.triggerCharacters = provider.triggerCharacters or {}
|
provider.triggerCharacters = provider.triggerCharacters or {}
|
||||||
if not provider._word_chars_added then
|
if not word_chars_extended[client.id] then
|
||||||
provider._word_chars_added = true
|
word_chars_extended[client.id] = true
|
||||||
for c in WORD_CHARS:gmatch(".") do
|
for c in WORD_CHARS:gmatch(".") do
|
||||||
table.insert(provider.triggerCharacters, c)
|
table.insert(provider.triggerCharacters, c)
|
||||||
end
|
end
|
||||||
@@ -75,18 +262,13 @@ function M.on_attach(client, buf)
|
|||||||
vim.lsp.completion.enable(true, client.id, buf, {
|
vim.lsp.completion.enable(true, client.id, buf, {
|
||||||
autotrigger = true,
|
autotrigger = true,
|
||||||
convert = function(item)
|
convert = function(item)
|
||||||
local signature = vim.tbl_get(
|
|
||||||
item,
|
|
||||||
"labelDetails",
|
|
||||||
"description"
|
|
||||||
) or item.detail or ""
|
|
||||||
return {
|
return {
|
||||||
abbr = item.label:match("[^(]+") or item.label,
|
abbr = item.label:match("[^(]+") or item.label,
|
||||||
menu = "",
|
menu = "",
|
||||||
kind = "",
|
kind = "",
|
||||||
info = signature ~= ""
|
-- non-empty info blocks vim.lsp.completion's built-in
|
||||||
and fence(vim.bo[buf].filetype, signature)
|
-- resolve so our own CompleteChanged handler can run it
|
||||||
or " ",
|
info = " ",
|
||||||
}
|
}
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
@@ -96,12 +278,6 @@ function M.setup()
|
|||||||
vim.api.nvim_create_autocmd("CompleteChanged", {
|
vim.api.nvim_create_autocmd("CompleteChanged", {
|
||||||
group = GROUP,
|
group = GROUP,
|
||||||
callback = function(ev)
|
callback = function(ev)
|
||||||
local cinfo = vim.fn.complete_info({
|
|
||||||
"selected",
|
|
||||||
"preview_winid",
|
|
||||||
"preview_bufnr",
|
|
||||||
})
|
|
||||||
|
|
||||||
local completed = vim.v.event.completed_item or {}
|
local completed = vim.v.event.completed_item or {}
|
||||||
local lsp_item = vim.tbl_get(
|
local lsp_item = vim.tbl_get(
|
||||||
completed,
|
completed,
|
||||||
@@ -113,111 +289,104 @@ function M.setup()
|
|||||||
local client_id =
|
local client_id =
|
||||||
vim.tbl_get(completed, "user_data", "nvim", "lsp", "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 = client_id and vim.lsp.get_client_by_id(client_id)
|
||||||
if
|
|
||||||
not lsp_item
|
if not lsp_item or not client then
|
||||||
or not client
|
vim.schedule(close)
|
||||||
or not client:supports_method(
|
|
||||||
vim.lsp.protocol.Methods.completionItem_resolve
|
|
||||||
)
|
|
||||||
then
|
|
||||||
style_popup(cinfo.preview_winid, cinfo.preview_bufnr)
|
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local selected = cinfo.selected
|
local ft = vim.bo[ev.buf].filetype
|
||||||
|
---@type 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,
|
||||||
|
}
|
||||||
|
|
||||||
|
local will_resolve = client:supports_method(
|
||||||
|
vim.lsp.protocol.Methods.completionItem_resolve
|
||||||
|
)
|
||||||
|
|
||||||
|
if not will_resolve then
|
||||||
|
local initial, width = build_content(lsp_item, ft)
|
||||||
|
vim.schedule(function()
|
||||||
|
if initial and width then
|
||||||
|
show(initial, pum, width)
|
||||||
|
else
|
||||||
|
close()
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
cancel_pending()
|
||||||
local word = completed.word
|
local word = completed.word
|
||||||
client:request(
|
local _, request_id = client:request(
|
||||||
vim.lsp.protocol.Methods.completionItem_resolve,
|
vim.lsp.protocol.Methods.completionItem_resolve,
|
||||||
lsp_item,
|
lsp_item,
|
||||||
function(err, result)
|
function(err, result)
|
||||||
|
popup.pending = nil
|
||||||
if err or not result then
|
if err or not result then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
local cur = vim.fn.complete_info({ "completed" })
|
||||||
local cur = vim.fn.complete_info({
|
|
||||||
"selected",
|
|
||||||
"completed",
|
|
||||||
"preview_winid",
|
|
||||||
})
|
|
||||||
if
|
if
|
||||||
cur.selected ~= selected
|
(vim.tbl_get(cur, "completed", "word") or "") ~= word
|
||||||
or (vim.tbl_get(cur, "completed", "word") or "")
|
|
||||||
~= word
|
|
||||||
then
|
then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
local content, width = build_content(result, ft)
|
||||||
local signature = vim.tbl_get(
|
if content and width then
|
||||||
result,
|
show(content, pum, width)
|
||||||
"labelDetails",
|
else
|
||||||
"description"
|
close()
|
||||||
) or result.detail or ""
|
|
||||||
local doc = result.documentation
|
|
||||||
if type(doc) == "table" then
|
|
||||||
doc = doc.value
|
|
||||||
end
|
|
||||||
doc = doc or ""
|
|
||||||
|
|
||||||
local ft = vim.bo[ev.buf].filetype
|
|
||||||
local code_parts = {}
|
|
||||||
if result.additionalTextEdits then
|
|
||||||
for _, edit in ipairs(result.additionalTextEdits) do
|
|
||||||
local text = (edit.newText or ""):gsub("%s+$", "")
|
|
||||||
if text ~= "" then
|
|
||||||
table.insert(code_parts, fence(ft, text))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if signature ~= "" then
|
|
||||||
table.insert(code_parts, fence(ft, signature))
|
|
||||||
end
|
|
||||||
local sections = {}
|
|
||||||
if #code_parts > 0 then
|
|
||||||
table.insert(sections, table.concat(code_parts, "\n\n"))
|
|
||||||
end
|
|
||||||
if doc ~= "" then
|
|
||||||
table.insert(sections, doc)
|
|
||||||
end
|
|
||||||
if #sections == 0 then
|
|
||||||
if
|
|
||||||
cur.preview_winid
|
|
||||||
and cur.preview_winid > 0
|
|
||||||
and vim.api.nvim_win_is_valid(cur.preview_winid)
|
|
||||||
then
|
|
||||||
pcall(
|
|
||||||
vim.api.nvim_win_close,
|
|
||||||
cur.preview_winid,
|
|
||||||
true
|
|
||||||
)
|
|
||||||
end
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local max_w = 0
|
|
||||||
for _, s in ipairs(sections) do
|
|
||||||
for _, line in
|
|
||||||
ipairs(vim.split(s, "\n", { plain = true }))
|
|
||||||
do
|
|
||||||
max_w =
|
|
||||||
math.max(max_w, vim.fn.strdisplaywidth(line))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
local sep = "\n"
|
|
||||||
.. string.rep("─", math.min(max_w, 80))
|
|
||||||
.. "\n"
|
|
||||||
local combined = table.concat(sections, sep)
|
|
||||||
|
|
||||||
local windata = vim.api.nvim__complete_set(selected, {
|
|
||||||
info = combined,
|
|
||||||
})
|
|
||||||
if windata then
|
|
||||||
style_popup(windata.winid, windata.bufnr, max_w)
|
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
ev.buf
|
ev.buf
|
||||||
)
|
)
|
||||||
|
if request_id then
|
||||||
|
popup.pending = { client = client, id = request_id }
|
||||||
|
end
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
vim.api.nvim_create_autocmd({ "CompleteDonePre", "InsertLeave" }, {
|
||||||
|
group = GROUP,
|
||||||
|
callback = close,
|
||||||
|
})
|
||||||
|
|
||||||
|
---@param key string
|
||||||
|
---@param direction string single-line scroll keycode (<C-e> or <C-y>)
|
||||||
|
---@param count integer
|
||||||
|
local function scroll_map(key, direction, count)
|
||||||
|
vim.keymap.set("i", key, function()
|
||||||
|
if popup.winid and vim.api.nvim_win_is_valid(popup.winid) then
|
||||||
|
vim.schedule(function()
|
||||||
|
if
|
||||||
|
popup.winid
|
||||||
|
and vim.api.nvim_win_is_valid(popup.winid)
|
||||||
|
then
|
||||||
|
vim.api.nvim_win_call(popup.winid, function()
|
||||||
|
vim.cmd.normal({
|
||||||
|
args = { count .. direction },
|
||||||
|
bang = true,
|
||||||
|
})
|
||||||
|
end)
|
||||||
|
update_indicators()
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
return ""
|
||||||
|
end
|
||||||
|
return key
|
||||||
|
end, { expr = true, replace_keycodes = true })
|
||||||
|
end
|
||||||
|
local half = math.floor(MAX_HEIGHT / 2)
|
||||||
|
scroll_map("<C-d>", vim.keycode("<C-e>"), half)
|
||||||
|
scroll_map("<C-u>", vim.keycode("<C-y>"), half)
|
||||||
|
scroll_map("<C-j>", vim.keycode("<C-e>"), 1)
|
||||||
|
scroll_map("<C-k>", vim.keycode("<C-y>"), 1)
|
||||||
end
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|||||||
Reference in New Issue
Block a user