diff options
author | Maria José Solano <majosolano99@gmail.com> | 2023-10-20 23:51:26 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-21 08:51:26 +0200 |
commit | f1775da07fe48da629468bcfcc2a8a6c4c3f40ed (patch) | |
tree | f268521a16f35ed9a58acc758a87e791d0dd6d6e /runtime/lua/vim/lsp/_snippet_grammar.lua | |
parent | 330444994616e48e5e4d15bbf72d7c5346943565 (diff) | |
download | rneovim-f1775da07fe48da629468bcfcc2a8a6c4c3f40ed.tar.gz rneovim-f1775da07fe48da629468bcfcc2a8a6c4c3f40ed.tar.bz2 rneovim-f1775da07fe48da629468bcfcc2a8a6c4c3f40ed.zip |
feat(lsp): add snippet API (#25301)
Diffstat (limited to 'runtime/lua/vim/lsp/_snippet_grammar.lua')
-rw-r--r-- | runtime/lua/vim/lsp/_snippet_grammar.lua | 34 |
1 files changed, 30 insertions, 4 deletions
diff --git a/runtime/lua/vim/lsp/_snippet_grammar.lua b/runtime/lua/vim/lsp/_snippet_grammar.lua index 0a4d669fb9..9318fefcbc 100644 --- a/runtime/lua/vim/lsp/_snippet_grammar.lua +++ b/runtime/lua/vim/lsp/_snippet_grammar.lua @@ -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 |