280 lines
7.8 KiB
Lua
280 lines
7.8 KiB
Lua
local log = require("log")
|
|
local MAX_WIDTH = 80
|
|
local MAX_HEIGHT = 20
|
|
local HALF_HEIGHT = math.floor(MAX_HEIGHT / 2)
|
|
local SNIPPET_KIND = vim.lsp.protocol.CompletionItemKind.Snippet
|
|
|
|
local NS = vim.api.nvim_create_namespace("ow.lsp.completion.popup")
|
|
|
|
---@class ow.lsp.completion.Pum
|
|
---@field row integer
|
|
---@field col integer
|
|
---@field width integer
|
|
---@field height integer
|
|
---@field scrollbar boolean
|
|
|
|
---@class ow.lsp.completion.PendingResolve
|
|
---@field client vim.lsp.Client
|
|
---@field id integer
|
|
|
|
---@param ft string
|
|
---@param text string
|
|
---@return string
|
|
local function fence(ft, text)
|
|
return string.format("```%s\n%s\n```", ft, text)
|
|
end
|
|
|
|
---@param item ow.lsp.completion.Item
|
|
---@param ft string
|
|
---@return string? content
|
|
---@return integer? width
|
|
local function build_content(item, ft)
|
|
local sections = {}
|
|
if item.snippet and item.raw.kind == SNIPPET_KIND then
|
|
table.insert(sections, fence(ft, item.snippet))
|
|
else
|
|
if item.detail then
|
|
table.insert(sections, fence(ft, item.detail))
|
|
end
|
|
if item.doc then
|
|
table.insert(sections, item.doc)
|
|
end
|
|
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
|
|
|
|
---@class ow.lsp.completion.Popup
|
|
---@field private winid integer?
|
|
---@field private bufnr integer?
|
|
---@field private pending ow.lsp.completion.PendingResolve?
|
|
---@field private resolved { word: string, item: ow.lsp.completion.Item }?
|
|
local Popup = {}
|
|
Popup.__index = Popup
|
|
|
|
---@return ow.lsp.completion.Popup
|
|
function Popup.new()
|
|
return setmetatable({
|
|
winid = nil,
|
|
bufnr = nil,
|
|
pending = nil,
|
|
resolved = nil,
|
|
}, Popup)
|
|
end
|
|
|
|
---@param word string
|
|
---@return ow.lsp.completion.Item?
|
|
function Popup:resolved_for(word)
|
|
if self.resolved and self.resolved.word == word then
|
|
return self.resolved.item
|
|
end
|
|
return nil
|
|
end
|
|
|
|
---@return boolean
|
|
function Popup:is_visible()
|
|
return self.winid ~= nil and vim.api.nvim_win_is_valid(self.winid)
|
|
end
|
|
|
|
function Popup:close()
|
|
self:cancel_pending()
|
|
if self:is_visible() then
|
|
vim.api.nvim_win_close(self.winid, true)
|
|
end
|
|
self.winid = nil
|
|
end
|
|
|
|
---@param item ow.lsp.completion.Item
|
|
---@param ft string
|
|
---@param pum ow.lsp.completion.Pum
|
|
function Popup:show(item, ft, pum)
|
|
local content, width = build_content(item, ft)
|
|
if content and width then
|
|
self:render(content, pum, width)
|
|
else
|
|
self:close()
|
|
end
|
|
end
|
|
|
|
---@param client vim.lsp.Client
|
|
---@param item ow.lsp.completion.Item
|
|
---@param ft string
|
|
---@param pum ow.lsp.completion.Pum
|
|
---@param word string
|
|
---@param buf integer
|
|
function Popup:dispatch_resolve(client, item, ft, pum, word, buf)
|
|
self:cancel_pending()
|
|
self.resolved = nil
|
|
local method = vim.lsp.protocol.Methods.completionItem_resolve
|
|
local _, request_id = client:request(method, item.raw, function(err, result)
|
|
if err then
|
|
log.warning(
|
|
"client %d: %s failed: %s",
|
|
client.id,
|
|
method,
|
|
err.message
|
|
)
|
|
end
|
|
self.pending = nil
|
|
if err or not result then
|
|
return
|
|
end
|
|
local cur = vim.fn.complete_info({ "completed" })
|
|
if (vim.tbl_get(cur, "completed", "word") or "") ~= word then
|
|
return
|
|
end
|
|
item:apply_resolved(result)
|
|
self.resolved = { word = word, item = item }
|
|
self:show(item, ft, pum)
|
|
end, buf)
|
|
if request_id then
|
|
self.pending = { client = client, id = request_id }
|
|
end
|
|
end
|
|
|
|
Popup.HALF_PAGE = HALF_HEIGHT
|
|
|
|
---@param content string
|
|
---@param pum ow.lsp.completion.Pum
|
|
---@param width integer
|
|
function Popup:render(content, pum, width)
|
|
self:ensure_buffer()
|
|
local lines = vim.split(content, "\n", { plain = true })
|
|
vim.api.nvim_buf_set_lines(self.bufnr, 0, -1, false, lines)
|
|
|
|
vim.api.nvim_buf_clear_namespace(self.bufnr, NS, 0, -1)
|
|
for i, line in ipairs(lines) do
|
|
if line ~= "" and line:gsub("─", "") == "" then
|
|
vim.api.nvim_buf_set_extmark(self.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 self:is_visible() then
|
|
vim.api.nvim_win_set_config(self.winid, cfg)
|
|
else
|
|
cfg.noautocmd = true
|
|
self.winid = vim.api.nvim_open_win(self.bufnr, false, cfg)
|
|
vim.wo[self.winid].wrap = true
|
|
vim.wo[self.winid].linebreak = true
|
|
vim.wo[self.winid].conceallevel = 2
|
|
end
|
|
vim.api.nvim_win_set_cursor(self.winid, { 1, 0 })
|
|
|
|
local actual = vim.api.nvim_win_text_height(self.winid, {
|
|
max_height = MAX_HEIGHT,
|
|
}).all
|
|
if actual ~= height then
|
|
vim.api.nvim_win_set_height(self.winid, actual)
|
|
end
|
|
|
|
self:update_indicators()
|
|
end
|
|
|
|
---@param direction "up" | "down"
|
|
---@param count integer
|
|
function Popup:scroll(direction, count)
|
|
if not self:is_visible() then
|
|
return
|
|
end
|
|
local key = direction == "down" and vim.keycode("<C-e>")
|
|
or vim.keycode("<C-y>")
|
|
vim.api.nvim_win_call(self.winid, function()
|
|
vim.cmd.normal({ args = { count .. key }, bang = true })
|
|
end)
|
|
self:update_indicators()
|
|
end
|
|
|
|
function Popup:cancel_pending()
|
|
if self.pending then
|
|
self.pending.client:cancel_request(self.pending.id)
|
|
self.pending = nil
|
|
end
|
|
end
|
|
|
|
function Popup:ensure_buffer()
|
|
if self.bufnr and vim.api.nvim_buf_is_valid(self.bufnr) then
|
|
return
|
|
end
|
|
self.bufnr = vim.api.nvim_create_buf(false, true)
|
|
vim.bo[self.bufnr].buftype = "nofile"
|
|
vim.bo[self.bufnr].bufhidden = "hide"
|
|
vim.bo[self.bufnr].swapfile = false
|
|
-- Markdown parser may not be installed; fall back to no highlighting.
|
|
pcall(vim.treesitter.start, self.bufnr, "markdown")
|
|
end
|
|
|
|
function Popup:update_indicators()
|
|
if not self:is_visible() then
|
|
return
|
|
end
|
|
|
|
local visible = vim.api.nvim_win_get_height(self.winid)
|
|
local topline, botline = unpack(vim.api.nvim_win_call(self.winid, function()
|
|
return { vim.fn.line("w0"), vim.fn.line("w$") }
|
|
end))
|
|
|
|
local display_topline = 1
|
|
if topline > 1 then
|
|
display_topline = vim.api.nvim_win_text_height(self.winid, {
|
|
end_row = topline - 2,
|
|
}).all + 1
|
|
end
|
|
local display_bot = display_topline + visible - 1
|
|
|
|
local total = vim.api.nvim_win_text_height(self.winid, {
|
|
max_height = display_bot + 1,
|
|
}).all
|
|
|
|
local has_above = display_topline > 1
|
|
local has_below = display_bot < total
|
|
and botline
|
|
< vim.api.nvim_buf_line_count(vim.api.nvim_win_get_buf(self.winid))
|
|
|
|
vim.api.nvim_win_set_config(self.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
|
|
|
|
return Popup
|