refactor(completion): evolve Request into Session + surface-level cleanup
This commit is contained in:
+55
-35
@@ -1,11 +1,5 @@
|
||||
local SNIPPET = vim.lsp.protocol.InsertTextFormat.Snippet
|
||||
|
||||
---@param raw lsp.CompletionItem
|
||||
---@return string?
|
||||
local function edit_text(raw)
|
||||
local edit = raw.textEdit
|
||||
return (type(edit) == "table" and edit.newText) or raw.insertText
|
||||
end
|
||||
local SNIPPET_FORMAT = vim.lsp.protocol.InsertTextFormat.Snippet
|
||||
local SNIPPET_KIND = vim.lsp.protocol.CompletionItemKind.Snippet
|
||||
|
||||
---@param raw lsp.CompletionItem
|
||||
---@return string
|
||||
@@ -14,26 +8,62 @@ local function label_stem(raw)
|
||||
end
|
||||
|
||||
---@param raw lsp.CompletionItem
|
||||
---@return string?
|
||||
local function extract_doc(raw)
|
||||
local doc = raw.documentation
|
||||
if type(doc) == "table" then
|
||||
return doc.value
|
||||
---@return string
|
||||
local function edit_text(raw)
|
||||
if raw.textEdit then
|
||||
return raw.textEdit.newText
|
||||
end
|
||||
---@cast doc string?
|
||||
return doc
|
||||
|
||||
return raw.insertText or raw.label
|
||||
end
|
||||
|
||||
---@param raw lsp.CompletionItem
|
||||
---@return string
|
||||
local function get_word(raw)
|
||||
if raw.insertTextFormat == SNIPPET_FORMAT then
|
||||
return raw.filterText or label_stem(raw)
|
||||
end
|
||||
return edit_text(raw)
|
||||
end
|
||||
|
||||
---@param raw lsp.CompletionItem
|
||||
---@return string
|
||||
local function get_abbr(raw)
|
||||
local abbr = label_stem(raw)
|
||||
if raw.kind == SNIPPET_KIND then
|
||||
abbr = abbr .. "~"
|
||||
end
|
||||
return abbr
|
||||
end
|
||||
|
||||
---@param raw lsp.CompletionItem
|
||||
---@return string?
|
||||
local function extract_detail(raw)
|
||||
local function get_detail(raw)
|
||||
return raw.detail or vim.tbl_get(raw, "labelDetails", "description")
|
||||
end
|
||||
|
||||
---@param raw lsp.CompletionItem
|
||||
---@return string?
|
||||
local function get_doc(raw)
|
||||
local doc = raw.documentation
|
||||
if not doc then
|
||||
return nil
|
||||
end
|
||||
return doc.value or doc
|
||||
end
|
||||
|
||||
---@param raw lsp.CompletionItem
|
||||
---@return string?
|
||||
local function get_snippet(raw)
|
||||
if raw.insertTextFormat == SNIPPET_FORMAT then
|
||||
return edit_text(raw)
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
---@class ow.lsp.completion.Item
|
||||
---@field word string
|
||||
---@field abbr string
|
||||
---@field menu string
|
||||
---@field detail string?
|
||||
---@field doc string?
|
||||
---@field snippet string?
|
||||
@@ -46,22 +76,12 @@ Item.__index = Item
|
||||
---@param client_id integer
|
||||
---@return ow.lsp.completion.Item
|
||||
function Item.from_lsp(raw, client_id)
|
||||
local is_snippet = raw.insertTextFormat == SNIPPET
|
||||
local abbr = label_stem(raw)
|
||||
local word
|
||||
if is_snippet then
|
||||
word = raw.filterText or abbr
|
||||
abbr = abbr .. "~"
|
||||
else
|
||||
word = edit_text(raw) or raw.label
|
||||
end
|
||||
return setmetatable({
|
||||
word = word,
|
||||
abbr = abbr,
|
||||
menu = vim.tbl_get(raw, "labelDetails", "detail") or "",
|
||||
detail = extract_detail(raw),
|
||||
doc = extract_doc(raw),
|
||||
snippet = is_snippet and (edit_text(raw) or raw.label) or nil,
|
||||
word = get_word(raw),
|
||||
abbr = get_abbr(raw),
|
||||
detail = get_detail(raw),
|
||||
doc = get_doc(raw),
|
||||
snippet = get_snippet(raw),
|
||||
client_id = client_id,
|
||||
raw = raw,
|
||||
}, Item)
|
||||
@@ -70,10 +90,10 @@ end
|
||||
---@param resolved lsp.CompletionItem
|
||||
function Item:apply_resolved(resolved)
|
||||
self.raw = vim.tbl_deep_extend("force", self.raw, resolved)
|
||||
self.detail = extract_detail(self.raw)
|
||||
self.doc = extract_doc(self.raw)
|
||||
self.detail = get_detail(self.raw)
|
||||
self.doc = get_doc(self.raw)
|
||||
if self.snippet then
|
||||
self.snippet = edit_text(self.raw) or self.raw.label
|
||||
self.snippet = edit_text(self.raw)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user