refactor(lsp): turn completion popup and request into classes
This commit is contained in:
+167
-171
@@ -1,10 +1,9 @@
|
||||
local MAX_WIDTH = 80
|
||||
local MAX_HEIGHT = 20
|
||||
local HALF_HEIGHT = math.floor(MAX_HEIGHT / 2)
|
||||
|
||||
local NS = vim.api.nvim_create_namespace("ow.lsp.completion.popup")
|
||||
|
||||
local M = {}
|
||||
|
||||
---@class ow.lsp.completion.Pum
|
||||
---@field row integer
|
||||
---@field col integer
|
||||
@@ -16,18 +15,6 @@ local M = {}
|
||||
---@field client vim.lsp.Client
|
||||
---@field id integer
|
||||
|
||||
---@class ow.lsp.completion.Popup
|
||||
---@field winid integer?
|
||||
---@field bufnr integer?
|
||||
---@field pending ow.lsp.completion.PendingResolve?
|
||||
|
||||
---@type ow.lsp.completion.Popup
|
||||
local popup = {
|
||||
winid = nil,
|
||||
bufnr = nil,
|
||||
pending = nil,
|
||||
}
|
||||
|
||||
---@param ft string
|
||||
---@param text string
|
||||
---@return string
|
||||
@@ -41,139 +28,6 @@ local function signature_of(item)
|
||||
return item.detail or vim.tbl_get(item, "labelDetails", "description") or ""
|
||||
end
|
||||
|
||||
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
|
||||
end
|
||||
local visible = vim.api.nvim_win_get_height(popup.winid)
|
||||
local topline, botline =
|
||||
unpack(vim.api.nvim_win_call(popup.winid, function()
|
||||
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
|
||||
local display_bot = display_topline + visible - 1
|
||||
|
||||
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(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
|
||||
|
||||
function M.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
|
||||
popup.winid = nil
|
||||
end
|
||||
|
||||
---@param content string
|
||||
---@param pum ow.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
|
||||
@@ -220,35 +74,64 @@ local function build_content(item, ft)
|
||||
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?
|
||||
local Popup = {}
|
||||
Popup.__index = Popup
|
||||
|
||||
---@return ow.lsp.completion.Popup
|
||||
function Popup.new()
|
||||
return setmetatable({
|
||||
winid = nil,
|
||||
bufnr = nil,
|
||||
pending = nil,
|
||||
}, Popup)
|
||||
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
|
||||
|
||||
--- Build content for `item` and show, or close if there's nothing to render.
|
||||
---@param item lsp.CompletionItem
|
||||
---@param ft string
|
||||
---@param pum ow.lsp.completion.Pum
|
||||
function M.show_for(item, ft, pum)
|
||||
function Popup:show(item, ft, pum)
|
||||
local content, width = build_content(item, ft)
|
||||
if content and width then
|
||||
show(content, pum, width)
|
||||
self:render(content, pum, width)
|
||||
else
|
||||
M.close()
|
||||
self:close()
|
||||
end
|
||||
end
|
||||
|
||||
--- Cancel any in-flight resolve, dispatch a fresh one for `item`, and on
|
||||
--- response render the resolved popup. If the user moved the selection
|
||||
--- before the response landed (`completed.word` mismatch), do nothing.
|
||||
--- before the response landed (word mismatch), do nothing.
|
||||
---@param client vim.lsp.Client
|
||||
---@param item lsp.CompletionItem
|
||||
---@param ft string
|
||||
---@param pum ow.lsp.completion.Pum
|
||||
---@param word string
|
||||
---@param buf integer
|
||||
function M.dispatch_resolve(client, item, ft, pum, word, buf)
|
||||
cancel_pending()
|
||||
function Popup:dispatch_resolve(client, item, ft, pum, word, buf)
|
||||
self:cancel_pending()
|
||||
local _, request_id = client:request(
|
||||
vim.lsp.protocol.Methods.completionItem_resolve,
|
||||
item,
|
||||
function(err, result)
|
||||
popup.pending = nil
|
||||
self.pending = nil
|
||||
if err or not result then
|
||||
return
|
||||
end
|
||||
@@ -256,40 +139,153 @@ function M.dispatch_resolve(client, item, ft, pum, word, buf)
|
||||
if (vim.tbl_get(cur, "completed", "word") or "") ~= word then
|
||||
return
|
||||
end
|
||||
M.show_for(result, ft, pum)
|
||||
self:show(result, ft, pum)
|
||||
end,
|
||||
buf
|
||||
)
|
||||
if request_id then
|
||||
popup.pending = { client = client, id = request_id }
|
||||
self.pending = { client = client, id = request_id }
|
||||
end
|
||||
end
|
||||
|
||||
---@return boolean
|
||||
function M.is_visible()
|
||||
return popup.winid ~= nil and vim.api.nvim_win_is_valid(popup.winid)
|
||||
---@param direction string single-line scroll keycode (<C-e> or <C-y>)
|
||||
function Popup:scroll_line(direction)
|
||||
self:scroll(direction, 1)
|
||||
end
|
||||
|
||||
---@param direction string single-line scroll keycode (<C-e> or <C-y>)
|
||||
---@param direction string
|
||||
function Popup:scroll_half(direction)
|
||||
self:scroll(direction, HALF_HEIGHT)
|
||||
end
|
||||
|
||||
---@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 string
|
||||
---@param count integer
|
||||
local function scroll(direction, count)
|
||||
if not M.is_visible() then
|
||||
function Popup:scroll(direction, count)
|
||||
if not self:is_visible() then
|
||||
return
|
||||
end
|
||||
vim.api.nvim_win_call(popup.winid, function()
|
||||
vim.api.nvim_win_call(self.winid, function()
|
||||
vim.cmd.normal({ args = { count .. direction }, bang = true })
|
||||
end)
|
||||
update_indicators()
|
||||
self:update_indicators()
|
||||
end
|
||||
|
||||
---@param direction string single-line scroll keycode (<C-e> or <C-y>)
|
||||
function M.scroll_line(direction)
|
||||
scroll(direction, 1)
|
||||
function Popup:cancel_pending()
|
||||
if self.pending then
|
||||
self.pending.client:cancel_request(self.pending.id)
|
||||
self.pending = nil
|
||||
end
|
||||
end
|
||||
|
||||
---@param direction string single-line scroll keycode (<C-e> or <C-y>)
|
||||
function M.scroll_half(direction)
|
||||
scroll(direction, math.floor(MAX_HEIGHT / 2))
|
||||
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
|
||||
|
||||
return M
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user