mirror of
https://github.com/neovim/neovim.git
synced 2026-04-29 02:34:10 +00:00
feat(lsp): add snippet API (#25301)
This commit is contained in:
committed by
GitHub
parent
3304449946
commit
f1775da07f
@@ -81,14 +81,40 @@ local Type = {
|
||||
M.NodeType = Type
|
||||
|
||||
--- @class vim.snippet.Node<T>: { type: vim.snippet.Type, data: T }
|
||||
--- @class vim.snippet.TabstopData: { tabstop: number }
|
||||
--- @class vim.snippet.TabstopData: { tabstop: integer }
|
||||
--- @class vim.snippet.TextData: { text: string }
|
||||
--- @class vim.snippet.PlaceholderData: { tabstop: vim.snippet.TabstopData, value: vim.snippet.Node<any> }
|
||||
--- @class vim.snippet.ChoiceData: { tabstop: vim.snippet.TabstopData, values: string[] }
|
||||
--- @class vim.snippet.PlaceholderData: { tabstop: integer, value: vim.snippet.Node<any> }
|
||||
--- @class vim.snippet.ChoiceData: { tabstop: integer, values: string[] }
|
||||
--- @class vim.snippet.VariableData: { name: string, default?: vim.snippet.Node<any>, regex?: string, format?: vim.snippet.Node<vim.snippet.FormatData|vim.snippet.TextData>[], options?: string }
|
||||
--- @class vim.snippet.FormatData: { capture: number, modifier?: string, if_text?: string, else_text?: string }
|
||||
--- @class vim.snippet.SnippetData: { children: vim.snippet.Node<any>[] }
|
||||
|
||||
--- @type vim.snippet.Node<any>
|
||||
local Node = {}
|
||||
|
||||
--- @return string
|
||||
--- @diagnostic disable-next-line: inject-field
|
||||
function Node:__tostring()
|
||||
local node_text = {}
|
||||
local type, data = self.type, self.data
|
||||
if type == Type.Snippet then
|
||||
--- @cast data vim.snippet.SnippetData
|
||||
for _, child in ipairs(data.children) do
|
||||
table.insert(node_text, tostring(child))
|
||||
end
|
||||
elseif type == Type.Choice then
|
||||
--- @cast data vim.snippet.ChoiceData
|
||||
table.insert(node_text, data.values[1])
|
||||
elseif type == Type.Placeholder then
|
||||
--- @cast data vim.snippet.PlaceholderData
|
||||
table.insert(node_text, tostring(data.value))
|
||||
elseif type == Type.Text then
|
||||
--- @cast data vim.snippet.TextData
|
||||
table.insert(node_text, data.text)
|
||||
end
|
||||
return table.concat(node_text)
|
||||
end
|
||||
|
||||
--- Returns a function that constructs a snippet node of the given type.
|
||||
---
|
||||
--- @generic T
|
||||
@@ -96,7 +122,7 @@ M.NodeType = Type
|
||||
--- @return fun(data: T): vim.snippet.Node<T>
|
||||
local function node(type)
|
||||
return function(data)
|
||||
return { type = type, data = data }
|
||||
return setmetatable({ type = type, data = data }, Node)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -742,7 +742,6 @@ function protocol.make_client_capabilities()
|
||||
-- this should be disabled out of the box.
|
||||
-- However, users can turn this back on if they have a snippet plugin.
|
||||
snippetSupport = false,
|
||||
|
||||
commitCharactersSupport = false,
|
||||
preselectSupport = false,
|
||||
deprecatedSupport = false,
|
||||
|
||||
@@ -616,35 +616,7 @@ function M.parse_snippet(input)
|
||||
return input
|
||||
end
|
||||
|
||||
--- @param node vim.snippet.Node<any>
|
||||
--- @return string
|
||||
local function node_to_string(node)
|
||||
local insert_text = {}
|
||||
if node.type == snippet.NodeType.Snippet then
|
||||
for _, child in
|
||||
ipairs((node.data --[[@as vim.snippet.SnippetData]]).children)
|
||||
do
|
||||
table.insert(insert_text, node_to_string(child))
|
||||
end
|
||||
elseif node.type == snippet.NodeType.Choice then
|
||||
table.insert(insert_text, (node.data --[[@as vim.snippet.ChoiceData]]).values[1])
|
||||
elseif node.type == snippet.NodeType.Placeholder then
|
||||
table.insert(
|
||||
insert_text,
|
||||
node_to_string((node.data --[[@as vim.snippet.PlaceholderData]]).value)
|
||||
)
|
||||
elseif node.type == snippet.NodeType.Text then
|
||||
table.insert(
|
||||
insert_text,
|
||||
node
|
||||
.data --[[@as vim.snippet.TextData]]
|
||||
.text
|
||||
)
|
||||
end
|
||||
return table.concat(insert_text)
|
||||
end
|
||||
|
||||
return node_to_string(parsed)
|
||||
return tostring(parsed)
|
||||
end
|
||||
|
||||
--- Sorts by CompletionItem.sortText.
|
||||
|
||||
Reference in New Issue
Block a user