303 lines
8.7 KiB
Lua
303 lines
8.7 KiB
Lua
local kind = require("lsp.kind")
|
|
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?
|
|
---@return table?
|
|
local function default_range(edit_range)
|
|
if type(edit_range) ~= "table" then
|
|
return nil
|
|
end
|
|
return edit_range.insert or (edit_range.start and edit_range) or nil
|
|
end
|
|
|
|
--- Normalize an item's `textEdit`, which may be `TextEdit` (has `range`) or
|
|
--- `InsertReplaceEdit` (has `insert`/`replace`).
|
|
---@param item lsp.CompletionItem
|
|
---@return table?
|
|
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 table LSP response entry (as returned by buf_request_all)
|
|
---@return integer? character 0-indexed UTF-16 character where edit starts
|
|
local function response_edit_start(response)
|
|
if not response.result or response.err or response.error then
|
|
return nil
|
|
end
|
|
local defaults =
|
|
default_range(vim.tbl_get(response.result, "itemDefaults", "editRange"))
|
|
if defaults then
|
|
return defaults.start.character
|
|
end
|
|
local items = response.result.items or response.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
|
|
|
|
---@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)
|
|
local edit = item.textEdit
|
|
if type(edit) == "table" and edit.newText then
|
|
return edit.newText
|
|
end
|
|
return item.insertText or item.label
|
|
end
|
|
|
|
---@param items lsp.CompletionItem[]
|
|
---@param defaults? table
|
|
local function apply_defaults(items, defaults)
|
|
if type(defaults) ~= "table" then
|
|
return
|
|
end
|
|
local range = default_range(defaults.editRange)
|
|
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 base string
|
|
---@return table[]
|
|
local function to_complete_items(base)
|
|
local entries = {}
|
|
for client_id, response in pairs(request.result or {}) 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
|
|
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
|
|
|
|
table.sort(entries, function(a, b)
|
|
local ka = a.item.sortText or a.item.label
|
|
local kb = b.item.sortText or b.item.label
|
|
return ka < kb
|
|
end)
|
|
|
|
local result = {}
|
|
for _, entry in ipairs(entries) do
|
|
local item = entry.item
|
|
local kind_icon, kind_hl = kind.get(item.kind)
|
|
result[#result + 1] = {
|
|
word = item_word(item),
|
|
abbr = item.label:match("[^(]+") or item.label,
|
|
menu = "",
|
|
kind = kind_icon,
|
|
kind_hlgroup = kind_hl,
|
|
-- non-empty so our CompleteChanged handler triggers resolve
|
|
info = " ",
|
|
icase = 1,
|
|
dup = 1,
|
|
empty = 1,
|
|
user_data = {
|
|
nvim = {
|
|
lsp = {
|
|
completion_item = item,
|
|
client_id = entry.client_id,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
end
|
|
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
|
|
)
|
|
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 debounce = util.debounce(function(_, trigger_kind, char)
|
|
if vim.fn.mode() ~= "i" then
|
|
return
|
|
end
|
|
dispatch_request(trigger_kind, char)
|
|
end, REQUEST_DEBOUNCE_MS)
|
|
|
|
function M.on_insert_char_pre()
|
|
local char = vim.v.char
|
|
if char == "" then
|
|
return
|
|
end
|
|
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
|
|
debounce:call(nil, kind_num, is_trigger and char or nil)
|
|
end
|
|
|
|
_G.ow_lsp_completion = _G.ow_lsp_completion or {}
|
|
|
|
--- Returns cached items without re-requesting, which is what lets Vim filter
|
|
--- the popup in-place on backspace.
|
|
---@param findstart 0 | 1
|
|
---@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
|
|
return findstart == 1 and -3 or {}
|
|
end
|
|
|
|
if request.phase ~= "ready" then
|
|
dispatch_request(vim.lsp.protocol.CompletionTriggerKind.Invoked)
|
|
return findstart == 1 and -3 or {}
|
|
end
|
|
|
|
if findstart == 1 then
|
|
return resolve_start_col()
|
|
end
|
|
|
|
local items = to_complete_items(base)
|
|
request.phase = "done"
|
|
return items
|
|
end
|
|
|
|
return M
|