refactor(completion): evolve Request into Session + surface-level cleanup
This commit is contained in:
@@ -0,0 +1,348 @@
|
||||
---@class ow.lsp.CompletionResponse : ow.lsp.Response
|
||||
---@field result lsp.CompletionItem[] | lsp.CompletionList
|
||||
|
||||
local Item = require("lsp.completion.item")
|
||||
local kind = require("lsp.kind")
|
||||
local util = require("util")
|
||||
|
||||
local REQUEST_DEBOUNCE_MS = 50
|
||||
|
||||
---@param item_defaults? lsp.CompletionItemDefaults
|
||||
---@return lsp.Range?
|
||||
local function default_range(item_defaults)
|
||||
local edit_range = item_defaults and item_defaults.editRange
|
||||
if type(edit_range) ~= "table" then
|
||||
return nil
|
||||
end
|
||||
return edit_range.insert or (edit_range.start and edit_range)
|
||||
end
|
||||
|
||||
---@param item lsp.CompletionItem
|
||||
---@return lsp.Range?
|
||||
local function item_range(item)
|
||||
local te = item.textEdit
|
||||
if type(te) ~= "table" then
|
||||
return nil
|
||||
end
|
||||
return te.range or te.insert
|
||||
end
|
||||
|
||||
---@param response ow.lsp.CompletionResponse
|
||||
---@return integer? character 0-indexed UTF-16 character where edit starts
|
||||
local function response_edit_start(response)
|
||||
if not response.result or response.err then
|
||||
return nil
|
||||
end
|
||||
local result = response.result
|
||||
local defaults = default_range(result.items and result.itemDefaults)
|
||||
if defaults then
|
||||
return defaults.start.character
|
||||
end
|
||||
local items = result.items or result
|
||||
if type(items) ~= "table" then
|
||||
return nil
|
||||
end
|
||||
for _, item in ipairs(items) do
|
||||
local range = item_range(item)
|
||||
if range then
|
||||
return range.start.character
|
||||
end
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
---@param items lsp.CompletionItem[]
|
||||
---@param defaults? lsp.CompletionItemDefaults
|
||||
local function apply_defaults(items, defaults)
|
||||
if type(defaults) ~= "table" then
|
||||
return
|
||||
end
|
||||
local range = default_range(defaults)
|
||||
for _, item in ipairs(items) do
|
||||
item.commitCharacters = item.commitCharacters
|
||||
or defaults.commitCharacters
|
||||
item.data = item.data or defaults.data
|
||||
item.insertTextFormat = item.insertTextFormat
|
||||
or defaults.insertTextFormat
|
||||
item.insertTextMode = item.insertTextMode or defaults.insertTextMode
|
||||
if range and type(item.textEdit) ~= "table" then
|
||||
item.textEdit = {
|
||||
range = range,
|
||||
newText = item.textEditText or item.insertText or item.label,
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
---@param responses table<integer, ow.lsp.CompletionResponse>
|
||||
---@param base string
|
||||
---@return vim.v.completed_item[]
|
||||
local function build_items(responses, base)
|
||||
local entries = {}
|
||||
for client_id, response in pairs(responses) do
|
||||
if response.result and not response.err then
|
||||
local raw = response.result.items or response.result
|
||||
if type(raw) == "table" then
|
||||
apply_defaults(raw, response.result.itemDefaults)
|
||||
for _, item in ipairs(raw) do
|
||||
entries[#entries + 1] =
|
||||
{ item = item, client_id = client_id }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if base ~= "" then
|
||||
entries = vim.tbl_filter(function(entry)
|
||||
local key = entry.item.filterText or entry.item.label
|
||||
return vim.startswith(key, base)
|
||||
end, entries)
|
||||
end
|
||||
|
||||
-- for _, entry in ipairs(entries) do
|
||||
-- entry.sort_key = entry.item.sortText or entry.item.label
|
||||
-- end
|
||||
-- table.sort(entries, function(a, b)
|
||||
-- return a.sort_key < b.sort_key
|
||||
-- end)
|
||||
|
||||
---@type vim.v.completed_item[]
|
||||
local result = {}
|
||||
for _, entry in ipairs(entries) do
|
||||
local item = Item.from_lsp(entry.item, entry.client_id)
|
||||
local kind_icon, kind_hl = kind.get(entry.item.kind)
|
||||
result[#result + 1] = {
|
||||
word = item.word,
|
||||
abbr = item.abbr,
|
||||
kind = kind_icon,
|
||||
kind_hlgroup = kind_hl,
|
||||
icase = 1,
|
||||
dup = 1,
|
||||
empty = 1,
|
||||
user_data = { ow = { item = item } },
|
||||
}
|
||||
end
|
||||
return result
|
||||
end
|
||||
|
||||
local function buffer_has_completion_client()
|
||||
return #vim.lsp.get_clients({
|
||||
bufnr = 0,
|
||||
method = vim.lsp.protocol.Methods.textDocument_completion,
|
||||
}) > 0
|
||||
end
|
||||
|
||||
---@return integer start, integer cursor both 0-indexed byte columns
|
||||
local function word_bounds()
|
||||
local line = vim.api.nvim_get_current_line()
|
||||
local cursor = vim.fn.col(".") - 1
|
||||
local start = vim.fn.match(line:sub(1, cursor), "\\k*$")
|
||||
return start, cursor
|
||||
end
|
||||
|
||||
---@param char string
|
||||
---@return boolean
|
||||
local function is_trigger_char(char)
|
||||
local clients = vim.lsp.get_clients({
|
||||
bufnr = 0,
|
||||
method = vim.lsp.protocol.Methods.textDocument_completion,
|
||||
})
|
||||
for _, client in ipairs(clients) do
|
||||
local chars = vim.tbl_get(
|
||||
client,
|
||||
"server_capabilities",
|
||||
"completionProvider",
|
||||
"triggerCharacters"
|
||||
)
|
||||
if chars and vim.list_contains(chars, char) then
|
||||
return true
|
||||
end
|
||||
end
|
||||
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.Session
|
||||
---@field private generation integer
|
||||
---@field private phase ('awaiting' | 'ready' | 'done')?
|
||||
---@field private result table<integer, ow.lsp.CompletionResponse>?
|
||||
---@field private cancel function?
|
||||
---@field trigger_char string?
|
||||
local Session = {}
|
||||
Session.__index = Session
|
||||
|
||||
---@return ow.lsp.completion.Session
|
||||
function Session.new()
|
||||
return setmetatable({
|
||||
generation = 0,
|
||||
phase = nil,
|
||||
result = nil,
|
||||
cancel = nil,
|
||||
trigger_char = nil,
|
||||
}, Session)
|
||||
end
|
||||
|
||||
---@return boolean
|
||||
function Session:is_awaiting()
|
||||
return self.phase == "awaiting"
|
||||
end
|
||||
|
||||
---@return boolean
|
||||
function Session:is_ready()
|
||||
return self.phase == "ready"
|
||||
end
|
||||
|
||||
function Session: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 Session:dispatch(trigger_kind, trigger_char)
|
||||
self:discard()
|
||||
self.phase = "awaiting"
|
||||
self.trigger_char = trigger_char
|
||||
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
|
||||
|
||||
---@return integer
|
||||
function Session:start_col()
|
||||
for _, response in pairs(self.result or {}) do
|
||||
local col = response_edit_start(response)
|
||||
if col then
|
||||
return col
|
||||
end
|
||||
end
|
||||
local start = word_bounds()
|
||||
return start
|
||||
end
|
||||
|
||||
---@param base string
|
||||
---@return vim.v.completed_item[]
|
||||
function Session:finalize(base)
|
||||
local items = build_items(self.result or {}, base)
|
||||
self.phase = "done"
|
||||
return items
|
||||
end
|
||||
|
||||
---@type ow.lsp.completion.Session?
|
||||
local session = nil
|
||||
|
||||
local dispatcher = util.debounce(function(_, trigger_kind, char)
|
||||
if vim.fn.mode() ~= "i" then
|
||||
return
|
||||
end
|
||||
if not session then
|
||||
session = Session.new()
|
||||
end
|
||||
session:dispatch(trigger_kind, char)
|
||||
end, REQUEST_DEBOUNCE_MS)
|
||||
|
||||
-- Set by `trigger_manual` when the user explicitly invokes completion via
|
||||
-- <C-x><C-o>. Survives the dispatch+response round-trip so the omnifunc knows
|
||||
-- to skip the "no word before cursor" guard. Reset on the next auto trigger.
|
||||
local manual_pending = false
|
||||
|
||||
local M = {}
|
||||
|
||||
---Expose for the <C-x><C-o> keymap wrapper.
|
||||
function M.trigger_manual()
|
||||
manual_pending = true
|
||||
return vim.keycode("<C-x><C-o>")
|
||||
end
|
||||
|
||||
function M.on_insert_char_pre()
|
||||
local char = vim.v.char
|
||||
if char == "" then
|
||||
return
|
||||
end
|
||||
manual_pending = false
|
||||
local is_word = char:match("[%w_]") ~= nil
|
||||
local is_trigger = not is_word and is_trigger_char(char)
|
||||
if not (is_word or is_trigger) then
|
||||
return
|
||||
end
|
||||
-- For word chars, let Vim's native in-place filtering handle things while
|
||||
-- the pum is already showing. Trigger chars always schedule a request
|
||||
-- because they change the completion context (e.g. member access).
|
||||
if is_word and vim.fn.pumvisible() ~= 0 then
|
||||
return
|
||||
end
|
||||
|
||||
local kind_num = is_trigger
|
||||
and vim.lsp.protocol.CompletionTriggerKind.TriggerCharacter
|
||||
or vim.lsp.protocol.CompletionTriggerKind.Invoked
|
||||
dispatcher: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 {}
|
||||
|
||||
---@param findstart 0 | 1
|
||||
---@param base string
|
||||
---@return integer | vim.v.completed_item[]
|
||||
function _G.ow_lsp_completion.omnifunc(findstart, base)
|
||||
if not session then
|
||||
session = Session.new()
|
||||
end
|
||||
if not buffer_has_completion_client() or session:is_awaiting() then
|
||||
return findstart == 1 and -3 or {}
|
||||
end
|
||||
if
|
||||
findstart == 1
|
||||
and session:is_ready()
|
||||
and not manual_pending
|
||||
and not session.trigger_char
|
||||
then
|
||||
local start, cursor = word_bounds()
|
||||
if start == cursor then
|
||||
return -3
|
||||
end
|
||||
end
|
||||
if not session:is_ready() then
|
||||
session:dispatch(vim.lsp.protocol.CompletionTriggerKind.Invoked)
|
||||
return findstart == 1 and -3 or {}
|
||||
end
|
||||
if findstart == 1 then
|
||||
return session:start_col()
|
||||
end
|
||||
local items = session:finalize(base)
|
||||
session = nil
|
||||
manual_pending = false
|
||||
return items
|
||||
end
|
||||
|
||||
return M
|
||||
Reference in New Issue
Block a user