refactor(completion): drive pum via vim.fn.complete() directly
This commit is contained in:
@@ -31,7 +31,6 @@ vim.opt.completeopt = {
|
|||||||
"menuone",
|
"menuone",
|
||||||
"noinsert",
|
"noinsert",
|
||||||
"noselect",
|
"noselect",
|
||||||
"fuzzy",
|
|
||||||
}
|
}
|
||||||
vim.opt.complete = { "o" }
|
vim.opt.complete = { "o" }
|
||||||
-- set nowrap
|
-- set nowrap
|
||||||
|
|||||||
@@ -17,8 +17,6 @@ local function on_attach(client, buf)
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
vim.bo[buf].omnifunc = "v:lua.ow_lsp_completion.omnifunc"
|
|
||||||
|
|
||||||
vim.api.nvim_clear_autocmds({ group = GROUP, buffer = buf })
|
vim.api.nvim_clear_autocmds({ group = GROUP, buffer = buf })
|
||||||
vim.api.nvim_create_autocmd("InsertCharPre", {
|
vim.api.nvim_create_autocmd("InsertCharPre", {
|
||||||
buffer = buf,
|
buffer = buf,
|
||||||
@@ -208,12 +206,7 @@ function M.setup()
|
|||||||
return "<CR>"
|
return "<CR>"
|
||||||
end, { expr = true, replace_keycodes = true })
|
end, { expr = true, replace_keycodes = true })
|
||||||
|
|
||||||
vim.keymap.set(
|
vim.keymap.set("i", "<C-x><C-o>", session.trigger)
|
||||||
"i",
|
|
||||||
"<C-x><C-o>",
|
|
||||||
session.trigger_manual,
|
|
||||||
{ expr = true, remap = false }
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|||||||
+90
-148
@@ -28,15 +28,15 @@ local function item_range(item)
|
|||||||
end
|
end
|
||||||
|
|
||||||
---@param response ow.lsp.CompletionResponse
|
---@param response ow.lsp.CompletionResponse
|
||||||
---@return integer? character 0-indexed UTF-16 character where edit starts
|
---@return lsp.Position?
|
||||||
local function response_edit_start(response)
|
local function edit_start(response)
|
||||||
if not response.result or response.err then
|
if not response.result or response.err then
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
local result = response.result
|
local result = response.result
|
||||||
local defaults = default_range(result.items and result.itemDefaults)
|
local defaults = default_range(result.items and result.itemDefaults)
|
||||||
if defaults then
|
if defaults then
|
||||||
return defaults.start.character
|
return defaults.start
|
||||||
end
|
end
|
||||||
local items = result.items or result
|
local items = result.items or result
|
||||||
if type(items) ~= "table" then
|
if type(items) ~= "table" then
|
||||||
@@ -45,7 +45,7 @@ local function response_edit_start(response)
|
|||||||
for _, item in ipairs(items) do
|
for _, item in ipairs(items) do
|
||||||
local range = item_range(item)
|
local range = item_range(item)
|
||||||
if range then
|
if range then
|
||||||
return range.start.character
|
return range.start
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return nil
|
return nil
|
||||||
@@ -75,43 +75,20 @@ local function apply_defaults(items, defaults)
|
|||||||
end
|
end
|
||||||
|
|
||||||
---@param responses table<integer, ow.lsp.CompletionResponse>
|
---@param responses table<integer, ow.lsp.CompletionResponse>
|
||||||
---@param base string
|
---@param base string text between start col and cursor, used for fuzzy match
|
||||||
---@return vim.v.completed_item[]
|
---@return vim.v.completed_item[]
|
||||||
local function build_items(responses, base)
|
local function build_items(responses, base)
|
||||||
local entries = {}
|
---@type vim.v.completed_item[]
|
||||||
|
local items = {}
|
||||||
for client_id, response in pairs(responses) do
|
for client_id, response in pairs(responses) do
|
||||||
if response.result and not response.err then
|
if response.result and not response.err then
|
||||||
local raw = response.result.items or response.result
|
local raw_items = response.result.items or response.result
|
||||||
if type(raw) == "table" then
|
if type(raw_items) == "table" then
|
||||||
apply_defaults(raw, response.result.itemDefaults)
|
apply_defaults(raw_items, response.result.itemDefaults)
|
||||||
for _, item in ipairs(raw) do
|
for _, raw in ipairs(raw_items) do
|
||||||
entries[#entries + 1] =
|
local item = Item.from_lsp(raw, client_id)
|
||||||
{ item = item, client_id = client_id }
|
local kind_icon, kind_hl = kind.get(raw.kind)
|
||||||
end
|
items[#items + 1] = {
|
||||||
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,
|
word = item.word,
|
||||||
abbr = item.abbr,
|
abbr = item.abbr,
|
||||||
kind = kind_icon,
|
kind = kind_icon,
|
||||||
@@ -119,10 +96,27 @@ local function build_items(responses, base)
|
|||||||
icase = 1,
|
icase = 1,
|
||||||
dup = 1,
|
dup = 1,
|
||||||
empty = 1,
|
empty = 1,
|
||||||
user_data = { ow = { item = item } },
|
user_data = {
|
||||||
|
ow = {
|
||||||
|
item = item,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
filter = raw.filterText or raw.label,
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
return result
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if base ~= "" then
|
||||||
|
if vim.o.completeopt:find("fuzzy", 1, true) then
|
||||||
|
items = vim.fn.matchfuzzy(items, base, { key = "filter" })
|
||||||
|
else
|
||||||
|
items = vim.tbl_filter(function(item)
|
||||||
|
return vim.startswith(item.filter, base)
|
||||||
|
end, items)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return items
|
||||||
end
|
end
|
||||||
|
|
||||||
local function buffer_has_completion_client()
|
local function buffer_has_completion_client()
|
||||||
@@ -161,19 +155,12 @@ local function is_trigger_char(char)
|
|||||||
return false
|
return false
|
||||||
end
|
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
|
---@class ow.lsp.completion.Session
|
||||||
---@field private generation integer
|
---@field private generation integer monotonic counter for discarding stale async callbacks
|
||||||
---@field private phase ('awaiting' | 'ready' | 'done')?
|
|
||||||
---@field private result table<integer, ow.lsp.CompletionResponse>?
|
|
||||||
---@field private cancel function?
|
---@field private cancel function?
|
||||||
---@field trigger_char string?
|
---@field private trigger_char string?
|
||||||
|
---@field private manual boolean
|
||||||
|
---@field is_incomplete boolean server flagged last response as truncated
|
||||||
local Session = {}
|
local Session = {}
|
||||||
Session.__index = Session
|
Session.__index = Session
|
||||||
|
|
||||||
@@ -181,39 +168,28 @@ Session.__index = Session
|
|||||||
function Session.new()
|
function Session.new()
|
||||||
return setmetatable({
|
return setmetatable({
|
||||||
generation = 0,
|
generation = 0,
|
||||||
phase = nil,
|
|
||||||
result = nil,
|
|
||||||
cancel = nil,
|
cancel = nil,
|
||||||
trigger_char = nil,
|
trigger_char = nil,
|
||||||
|
manual = false,
|
||||||
|
is_incomplete = false,
|
||||||
}, Session)
|
}, Session)
|
||||||
end
|
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()
|
function Session:discard()
|
||||||
if self.cancel then
|
if self.cancel then
|
||||||
pcall(self.cancel)
|
pcall(self.cancel)
|
||||||
end
|
end
|
||||||
self.generation = self.generation + 1
|
self.generation = self.generation + 1
|
||||||
self.phase = nil
|
|
||||||
self.result = nil
|
|
||||||
self.cancel = nil
|
self.cancel = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param trigger_kind integer
|
---@param trigger_kind integer
|
||||||
---@param trigger_char? string
|
---@param trigger_char? string
|
||||||
function Session:dispatch(trigger_kind, trigger_char)
|
---@param manual? boolean true when invoked by the user (via M.trigger)
|
||||||
|
function Session:dispatch(trigger_kind, trigger_char, manual)
|
||||||
self:discard()
|
self:discard()
|
||||||
self.phase = "awaiting"
|
|
||||||
self.trigger_char = trigger_char
|
self.trigger_char = trigger_char
|
||||||
|
self.manual = manual or false
|
||||||
local gen = self.generation
|
local gen = self.generation
|
||||||
local buf = vim.api.nvim_get_current_buf()
|
local buf = vim.api.nvim_get_current_buf()
|
||||||
local params = vim.lsp.util.make_position_params(0, "utf-16")
|
local params = vim.lsp.util.make_position_params(0, "utf-16")
|
||||||
@@ -227,60 +203,63 @@ function Session:dispatch(trigger_kind, trigger_char)
|
|||||||
vim.lsp.protocol.Methods.textDocument_completion,
|
vim.lsp.protocol.Methods.textDocument_completion,
|
||||||
params,
|
params,
|
||||||
function(result)
|
function(result)
|
||||||
if self.generation ~= gen or self.phase ~= "awaiting" then
|
if self.generation ~= gen then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
self.phase = "ready"
|
vim.schedule(function()
|
||||||
self.result = result
|
-- Re-check: another dispatch may have fired between response
|
||||||
feed_omnifunc()
|
-- and the scheduled callback running.
|
||||||
|
if self.generation ~= gen then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
if vim.fn.mode() ~= "i" then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local word_start, cursor = word_bounds()
|
||||||
|
if
|
||||||
|
not self.manual
|
||||||
|
and not self.trigger_char
|
||||||
|
and word_start == cursor
|
||||||
|
then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
self.is_incomplete = false
|
||||||
|
for _, response in pairs(result) do
|
||||||
|
local r = response.result
|
||||||
|
if type(r) == "table" and r.isIncomplete then
|
||||||
|
self.is_incomplete = true
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
local start = word_start
|
||||||
|
for _, response in pairs(result) do
|
||||||
|
local pos = edit_start(response)
|
||||||
|
if pos then
|
||||||
|
start = pos.character
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
local base =
|
||||||
|
vim.api.nvim_get_current_line():sub(start + 1, cursor)
|
||||||
|
vim.fn.complete(start + 1, build_items(result, base))
|
||||||
|
end)
|
||||||
end
|
end
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
---@return integer
|
local session = Session.new()
|
||||||
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)
|
local dispatcher = util.debounce(function(_, trigger_kind, char)
|
||||||
if vim.fn.mode() ~= "i" then
|
if vim.fn.mode() ~= "i" then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
if not session then
|
|
||||||
session = Session.new()
|
|
||||||
end
|
|
||||||
session:dispatch(trigger_kind, char)
|
session:dispatch(trigger_kind, char)
|
||||||
end, REQUEST_DEBOUNCE_MS)
|
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 = {}
|
local M = {}
|
||||||
|
|
||||||
---Expose for the <C-x><C-o> keymap wrapper.
|
function M.trigger()
|
||||||
function M.trigger_manual()
|
session:dispatch(vim.lsp.protocol.CompletionTriggerKind.Invoked, nil, true)
|
||||||
manual_pending = true
|
|
||||||
return vim.keycode("<C-x><C-o>")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.on_insert_char_pre()
|
function M.on_insert_char_pre()
|
||||||
@@ -288,61 +267,24 @@ function M.on_insert_char_pre()
|
|||||||
if char == "" then
|
if char == "" then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
manual_pending = false
|
if not buffer_has_completion_client() then
|
||||||
|
return
|
||||||
|
end
|
||||||
local is_word = char:match("[%w_]") ~= nil
|
local is_word = char:match("[%w_]") ~= nil
|
||||||
local is_trigger = not is_word and is_trigger_char(char)
|
local is_trigger = not is_word and is_trigger_char(char)
|
||||||
if not (is_word or is_trigger) then
|
if not (is_word or is_trigger) then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
-- For word chars, let Vim's native in-place filtering handle things while
|
-- Word chars: let Vim's native in-place filtering narrow the already-
|
||||||
-- the pum is already showing. Trigger chars always schedule a request
|
-- visible pum. Re-dispatch only if the server flagged the last response
|
||||||
-- because they change the completion context (e.g. member access).
|
-- as incomplete (meaning it expects to be re-queried on more input).
|
||||||
if is_word and vim.fn.pumvisible() ~= 0 then
|
if is_word and vim.fn.pumvisible() ~= 0 and not session.is_incomplete then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local kind_num = is_trigger
|
local kind_num = is_trigger
|
||||||
and vim.lsp.protocol.CompletionTriggerKind.TriggerCharacter
|
and vim.lsp.protocol.CompletionTriggerKind.TriggerCharacter
|
||||||
or vim.lsp.protocol.CompletionTriggerKind.Invoked
|
or vim.lsp.protocol.CompletionTriggerKind.Invoked
|
||||||
dispatcher:call(nil, kind_num, is_trigger and char or nil)
|
dispatcher:call(nil, kind_num, is_trigger and char or nil)
|
||||||
end
|
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
|
return M
|
||||||
|
|||||||
Reference in New Issue
Block a user