refactor(lsp): turn completion popup and request into classes
This commit is contained in:
+122
-92
@@ -3,39 +3,6 @@ local util = require("util")
|
||||
|
||||
local REQUEST_DEBOUNCE_MS = 100
|
||||
|
||||
local M = {}
|
||||
|
||||
---@class ow.lsp.completion.Request
|
||||
---@field generation integer
|
||||
---@field phase ('awaiting' | 'ready' | 'done')?
|
||||
---@field result table?
|
||||
---@field cancel function?
|
||||
|
||||
---@type ow.lsp.completion.Request
|
||||
local request = {
|
||||
generation = 0,
|
||||
phase = nil,
|
||||
result = nil,
|
||||
cancel = nil,
|
||||
}
|
||||
|
||||
local function discard_request()
|
||||
if request.cancel then
|
||||
pcall(request.cancel)
|
||||
end
|
||||
request.generation = request.generation + 1
|
||||
request.phase = nil
|
||||
request.result = nil
|
||||
request.cancel = nil
|
||||
end
|
||||
|
||||
local function buffer_has_completion_client()
|
||||
return #vim.lsp.get_clients({
|
||||
bufnr = 0,
|
||||
method = vim.lsp.protocol.Methods.textDocument_completion,
|
||||
}) > 0
|
||||
end
|
||||
|
||||
--- Normalize an `itemDefaults.editRange` to a plain Range, since it may be
|
||||
--- either `Range` or `{ insert: Range, replace: Range }`.
|
||||
---@param edit_range table?
|
||||
@@ -83,19 +50,6 @@ local function response_edit_start(response)
|
||||
return nil
|
||||
end
|
||||
|
||||
---@return integer byte_col 0-indexed byte column for completefunc findstart
|
||||
local function resolve_start_col()
|
||||
for _, response in pairs(request.result or {}) do
|
||||
local col = response_edit_start(response)
|
||||
if col then
|
||||
return col
|
||||
end
|
||||
end
|
||||
local line = vim.api.nvim_get_current_line()
|
||||
local cursor_col = vim.fn.col(".") - 1
|
||||
return vim.fn.match(line:sub(1, cursor_col), "\\k*$")
|
||||
end
|
||||
|
||||
---@param item lsp.CompletionItem
|
||||
---@return string
|
||||
local function item_word(item)
|
||||
@@ -129,11 +83,14 @@ local function apply_defaults(items, defaults)
|
||||
end
|
||||
end
|
||||
|
||||
--- Transform raw LSP completion responses into Vim |complete()| items,
|
||||
--- filtered by `base` and sorted by sortText/label.
|
||||
---@param responses table map of client_id -> response from buf_request_all
|
||||
---@param base string
|
||||
---@return table[]
|
||||
local function to_complete_items(base)
|
||||
local function build_items(responses, base)
|
||||
local entries = {}
|
||||
for client_id, response in pairs(request.result or {}) do
|
||||
for client_id, response in pairs(responses) do
|
||||
if response.result and not (response.err or response.error) then
|
||||
local raw = response.result.items or response.result
|
||||
if type(raw) == "table" then
|
||||
@@ -187,39 +144,11 @@ local function to_complete_items(base)
|
||||
return result
|
||||
end
|
||||
|
||||
local function feed_user_completion()
|
||||
if vim.fn.mode() ~= "i" or vim.fn.pumvisible() ~= 0 then
|
||||
return
|
||||
end
|
||||
vim.api.nvim_feedkeys(vim.keycode("<C-x><C-o>"), "n", false)
|
||||
end
|
||||
|
||||
---@param trigger_kind integer
|
||||
---@param trigger_char string?
|
||||
local function dispatch_request(trigger_kind, trigger_char)
|
||||
discard_request()
|
||||
request.phase = "awaiting"
|
||||
local gen = request.generation
|
||||
local buf = vim.api.nvim_get_current_buf()
|
||||
local params = vim.lsp.util.make_position_params(0, "utf-16")
|
||||
---@cast params lsp.CompletionParams
|
||||
params.context = {
|
||||
triggerKind = trigger_kind,
|
||||
triggerCharacter = trigger_char,
|
||||
}
|
||||
request.cancel = vim.lsp.buf_request_all(
|
||||
buf,
|
||||
vim.lsp.protocol.Methods.textDocument_completion,
|
||||
params,
|
||||
function(result)
|
||||
if request.generation ~= gen or request.phase ~= "awaiting" then
|
||||
return
|
||||
end
|
||||
request.phase = "ready"
|
||||
request.result = result
|
||||
feed_user_completion()
|
||||
end
|
||||
)
|
||||
local function buffer_has_completion_client()
|
||||
return #vim.lsp.get_clients({
|
||||
bufnr = 0,
|
||||
method = vim.lsp.protocol.Methods.textDocument_completion,
|
||||
}) > 0
|
||||
end
|
||||
|
||||
---@param char string
|
||||
@@ -243,13 +172,117 @@ local function is_trigger_char(char)
|
||||
return false
|
||||
end
|
||||
|
||||
local function feed_omnifunc()
|
||||
if vim.fn.mode() ~= "i" or vim.fn.pumvisible() ~= 0 then
|
||||
return
|
||||
end
|
||||
vim.api.nvim_feedkeys(vim.keycode("<C-x><C-o>"), "n", false)
|
||||
end
|
||||
|
||||
---@class ow.lsp.completion.Request
|
||||
---@field private generation integer
|
||||
---@field private phase ('awaiting' | 'ready' | 'done')?
|
||||
---@field private result table?
|
||||
---@field private cancel function?
|
||||
local Request = {}
|
||||
Request.__index = Request
|
||||
|
||||
---@return ow.lsp.completion.Request
|
||||
function Request.new()
|
||||
return setmetatable({
|
||||
generation = 0,
|
||||
phase = nil,
|
||||
result = nil,
|
||||
cancel = nil,
|
||||
}, Request)
|
||||
end
|
||||
|
||||
---@return boolean
|
||||
function Request:is_awaiting()
|
||||
return self.phase == "awaiting"
|
||||
end
|
||||
|
||||
---@return boolean
|
||||
function Request:is_ready()
|
||||
return self.phase == "ready"
|
||||
end
|
||||
|
||||
--- Cancel any in-flight LSP request and clear cached results.
|
||||
function Request:discard()
|
||||
if self.cancel then
|
||||
pcall(self.cancel)
|
||||
end
|
||||
self.generation = self.generation + 1
|
||||
self.phase = nil
|
||||
self.result = nil
|
||||
self.cancel = nil
|
||||
end
|
||||
|
||||
---@param trigger_kind integer
|
||||
---@param trigger_char string?
|
||||
function Request:dispatch(trigger_kind, trigger_char)
|
||||
self:discard()
|
||||
self.phase = "awaiting"
|
||||
local gen = self.generation
|
||||
local buf = vim.api.nvim_get_current_buf()
|
||||
local params = vim.lsp.util.make_position_params(0, "utf-16")
|
||||
---@cast params lsp.CompletionParams
|
||||
params.context = {
|
||||
triggerKind = trigger_kind,
|
||||
triggerCharacter = trigger_char,
|
||||
}
|
||||
self.cancel = vim.lsp.buf_request_all(
|
||||
buf,
|
||||
vim.lsp.protocol.Methods.textDocument_completion,
|
||||
params,
|
||||
function(result)
|
||||
if self.generation ~= gen or self.phase ~= "awaiting" then
|
||||
return
|
||||
end
|
||||
self.phase = "ready"
|
||||
self.result = result
|
||||
feed_omnifunc()
|
||||
end
|
||||
)
|
||||
end
|
||||
|
||||
--- Byte column where the cached LSP edits start, for |complete-functions|
|
||||
--- `findstart == 1`. Falls back to the regex end of the word under cursor.
|
||||
---@return integer
|
||||
function Request:start_col()
|
||||
for _, response in pairs(self.result or {}) do
|
||||
local col = response_edit_start(response)
|
||||
if col then
|
||||
return col
|
||||
end
|
||||
end
|
||||
local line = vim.api.nvim_get_current_line()
|
||||
local cursor_col = vim.fn.col(".") - 1
|
||||
return vim.fn.match(line:sub(1, cursor_col), "\\k*$")
|
||||
end
|
||||
|
||||
--- Build the Vim |complete()| items from the cached LSP results filtered by
|
||||
--- `base`, and transition this request to the "done" phase. Subsequent
|
||||
--- completion triggers will dispatch a fresh request.
|
||||
---@param base string
|
||||
---@return table[]
|
||||
function Request:consume(base)
|
||||
local items = build_items(self.result or {}, base)
|
||||
self.phase = "done"
|
||||
return items
|
||||
end
|
||||
|
||||
local request = Request.new()
|
||||
|
||||
local debounce = util.debounce(function(_, trigger_kind, char)
|
||||
if vim.fn.mode() ~= "i" then
|
||||
return
|
||||
end
|
||||
dispatch_request(trigger_kind, char)
|
||||
request:dispatch(trigger_kind, char)
|
||||
end, REQUEST_DEBOUNCE_MS)
|
||||
|
||||
local M = {}
|
||||
|
||||
function M.on_insert_char_pre()
|
||||
local char = vim.v.char
|
||||
if char == "" then
|
||||
@@ -273,6 +306,8 @@ function M.on_insert_char_pre()
|
||||
debounce:call(nil, kind_num, is_trigger and char or nil)
|
||||
end
|
||||
|
||||
-- Global entry point for Vim's 'omnifunc' option. Referenced via
|
||||
-- `v:lua.ow_lsp_completion.omnifunc` from on_attach's `vim.bo.omnifunc`.
|
||||
_G.ow_lsp_completion = _G.ow_lsp_completion or {}
|
||||
|
||||
--- Returns cached items without re-requesting, which is what lets Vim filter
|
||||
@@ -281,22 +316,17 @@ _G.ow_lsp_completion = _G.ow_lsp_completion or {}
|
||||
---@param base string
|
||||
---@return integer | table
|
||||
function _G.ow_lsp_completion.omnifunc(findstart, base)
|
||||
if not buffer_has_completion_client() or request.phase == "awaiting" then
|
||||
if not buffer_has_completion_client() or request:is_awaiting() then
|
||||
return findstart == 1 and -3 or {}
|
||||
end
|
||||
|
||||
if request.phase ~= "ready" then
|
||||
dispatch_request(vim.lsp.protocol.CompletionTriggerKind.Invoked)
|
||||
if not request:is_ready() then
|
||||
request:dispatch(vim.lsp.protocol.CompletionTriggerKind.Invoked)
|
||||
return findstart == 1 and -3 or {}
|
||||
end
|
||||
|
||||
if findstart == 1 then
|
||||
return resolve_start_col()
|
||||
return request:start_col()
|
||||
end
|
||||
|
||||
local items = to_complete_items(base)
|
||||
request.phase = "done"
|
||||
return items
|
||||
return request:consume(base)
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
Reference in New Issue
Block a user