From f736b075d371dd15fa6fffc907db7952bc19bb9d Mon Sep 17 00:00:00 2001 From: Maria José Solano Date: Sun, 1 Oct 2023 09:53:18 -0700 Subject: feat(lsp): snippet parsing using lpeg --- runtime/lua/vim/lsp/_snippet_grammar.lua | 143 +++++++++++++++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100644 runtime/lua/vim/lsp/_snippet_grammar.lua (limited to 'runtime/lua/vim/lsp/_snippet_grammar.lua') diff --git a/runtime/lua/vim/lsp/_snippet_grammar.lua b/runtime/lua/vim/lsp/_snippet_grammar.lua new file mode 100644 index 0000000000..86f00fea51 --- /dev/null +++ b/runtime/lua/vim/lsp/_snippet_grammar.lua @@ -0,0 +1,143 @@ +--- Grammar for LSP snippets, based on https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#snippet_syntax + +local lpeg = vim.lpeg +local P, S, R, V = lpeg.P, lpeg.S, lpeg.R, lpeg.V +local C, Cg, Ct = lpeg.C, lpeg.Cg, lpeg.Ct + +local M = {} + +local alpha = R('az', 'AZ') +local backslash = P('\\') +local colon = P(':') +local dollar = P('$') +local int = R('09') ^ 1 +local l_brace, r_brace = P('{'), P('}') +local pipe = P('|') +local slash = P('/') +local underscore = P('_') +local var = Cg((underscore + alpha) * ((underscore + alpha + int) ^ 0), 'name') +local format_capture = Cg(int / tonumber, 'capture') +local format_modifier = Cg(P('upcase') + P('downcase') + P('capitalize'), 'modifier') +local tabstop = Cg(int / tonumber, 'tabstop') + +--- Returns a function that unescapes occurrences of "special" characters. +--- +--- @param special string +--- @return fun(match: string): string +local function escape_text(special) + return function(match) + local escaped = match:gsub('\\(.)', function(c) + return special:find(c) and c or '\\' .. c + end) + return escaped + end +end + +-- Text nodes match "any character", but $, \, and } must be escaped. +local escapable = '$}\\' +local text = (backslash * S(escapable)) + (P(1) - S(escapable)) +local text_0, text_1 = (text ^ 0) / escape_text(escapable), text ^ 1 +-- Within choice nodes, \ also escapes comma and pipe characters. +local choice_text = C(((backslash * S(escapable .. ',|')) + (P(1) - S(escapable .. ',|'))) ^ 1) + / escape_text(escapable .. ',|') +local if_text, else_text = Cg(text_0, 'if_text'), Cg(text_0, 'else_text') +-- Within format nodes, make sure we stop at / +local format_text = C(((backslash * S(escapable)) + (P(1) - S(escapable .. '/'))) ^ 1) + / escape_text(escapable) +-- Within ternary condition format nodes, make sure we stop at : +local if_till_colon_text = Cg( + C(((backslash * S(escapable)) + (P(1) - S(escapable .. ':'))) ^ 1) / escape_text(escapable), + 'if_text' +) + +-- Matches the string inside //, allowing escaping of the closing slash. +local regex = Cg(((backslash * slash) + (P(1) - slash)) ^ 1, 'regex') + +-- Regex constructor flags (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/RegExp#parameters). +local options = Cg(S('dgimsuvy') ^ 0, 'options') + +--- @enum vim.snippet.Type +local Type = { + Tabstop = 1, + Placeholder = 2, + Choice = 3, + Variable = 4, + Format = 5, + Text = 6, + Snippet = 7, +} +M.NodeType = Type + +--- @class vim.snippet.Node: { type: vim.snippet.Type, data: T } +--- @class vim.snippet.TabstopData: { tabstop: number } +--- @class vim.snippet.TextData: { text: string } +--- @class vim.snippet.PlaceholderData: { tabstop: vim.snippet.TabstopData, value: vim.snippet.Node } +--- @class vim.snippet.ChoiceData: { tabstop: vim.snippet.TabstopData, values: string[] } +--- @class vim.snippet.VariableData: { name: string, default?: vim.snippet.Node, regex?: string, format?: vim.snippet.Node[], options?: string } +--- @class vim.snippet.FormatData: { capture: number, modifier?: string, if_text?: string, else_text?: string } +--- @class vim.snippet.SnippetData: { children: vim.snippet.Node[] } + +--- Returns a function that constructs a snippet node of the given type. +--- +--- @generic T +--- @param type vim.snippet.Type +--- @return fun(data: T): vim.snippet.Node +local function node(type) + return function(data) + return { type = type, data = data } + end +end + +-- stylua: ignore +local G = P({ + 'snippet'; + snippet = Ct(Cg( + Ct(( + V('any') + + (Ct(Cg(text_1 / escape_text(escapable), 'text')) / node(Type.Text)) + ) ^ 1), 'children' + )) / node(Type.Snippet), + any_or_text = V('any') + (Ct(Cg(text_0 / escape_text(escapable), 'text')) / node(Type.Text)), + any = V('placeholder') + V('tabstop') + V('choice') + V('variable'), + tabstop = Ct(dollar * (tabstop + (l_brace * tabstop * r_brace))) / node(Type.Tabstop), + placeholder = Ct(dollar * l_brace * tabstop * colon * Cg(V('any_or_text'), 'value') * r_brace) / node(Type.Placeholder), + choice = Ct(dollar * + l_brace * + tabstop * + pipe * + Cg(Ct(choice_text * (P(',') * choice_text) ^ 0), 'values') * + pipe * + r_brace) / node(Type.Choice), + variable = Ct(dollar * ( + var + ( + l_brace * var * ( + r_brace + + (colon * Cg(V('any_or_text'), 'default') * r_brace) + + (slash * regex * slash * Cg(Ct((V('format') + (C(format_text) / node(Type.Text))) ^ 1), 'format') * slash * options * r_brace) + )) + )) / node(Type.Variable), + format = Ct(dollar * ( + format_capture + ( + l_brace * format_capture * ( + r_brace + + (colon * ( + (slash * format_modifier * r_brace) + + (P('+') * if_text * r_brace) + + (P('?') * if_till_colon_text * colon * else_text * r_brace) + + (P('-') * else_text * r_brace) + + (else_text * r_brace) + )) + )) + )) / node(Type.Format), +}) + +--- Parses the given input into a snippet tree. +--- @param input string +--- @return vim.snippet.Node +function M.parse(input) + local snippet = G:match(input) + assert(snippet, 'snippet parsing failed') + return snippet --- @type vim.snippet.Node +end + +return M -- cgit From ee156ca60ede95c95160cb8dff0197d40cb1a491 Mon Sep 17 00:00:00 2001 From: Maria José Solano Date: Sat, 14 Oct 2023 00:06:40 -0700 Subject: fix(lsp): refactor escaping snippet text (#25611) --- runtime/lua/vim/lsp/_snippet_grammar.lua | 48 ++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 18 deletions(-) (limited to 'runtime/lua/vim/lsp/_snippet_grammar.lua') diff --git a/runtime/lua/vim/lsp/_snippet_grammar.lua b/runtime/lua/vim/lsp/_snippet_grammar.lua index 86f00fea51..eb72efdf39 100644 --- a/runtime/lua/vim/lsp/_snippet_grammar.lua +++ b/runtime/lua/vim/lsp/_snippet_grammar.lua @@ -20,11 +20,15 @@ local format_capture = Cg(int / tonumber, 'capture') local format_modifier = Cg(P('upcase') + P('downcase') + P('capitalize'), 'modifier') local tabstop = Cg(int / tonumber, 'tabstop') +-- These characters are always escapable in text nodes no matter the context. +local escapable = '$}\\' + --- Returns a function that unescapes occurrences of "special" characters. --- ---- @param special string +--- @param special? string --- @return fun(match: string): string local function escape_text(special) + special = special or escapable return function(match) local escaped = match:gsub('\\(.)', function(c) return special:find(c) and c or '\\' .. c @@ -33,25 +37,33 @@ local function escape_text(special) end end --- Text nodes match "any character", but $, \, and } must be escaped. -local escapable = '$}\\' -local text = (backslash * S(escapable)) + (P(1) - S(escapable)) -local text_0, text_1 = (text ^ 0) / escape_text(escapable), text ^ 1 +--- Returns a pattern for text nodes. Will match characters in `escape` when preceded by a backslash, +--- and will stop with characters in `stop_with`. +--- +--- @param escape string +--- @param stop_with? string +--- @return vim.lpeg.Pattern +local function text(escape, stop_with) + stop_with = stop_with or escape + return (backslash * S(escape)) + (P(1) - S(stop_with)) +end + +-- For text nodes inside curly braces. It stops parsing when reaching an escapable character. +local braced_text = (text(escapable) ^ 0) / escape_text() + -- Within choice nodes, \ also escapes comma and pipe characters. -local choice_text = C(((backslash * S(escapable .. ',|')) + (P(1) - S(escapable .. ',|'))) ^ 1) - / escape_text(escapable .. ',|') -local if_text, else_text = Cg(text_0, 'if_text'), Cg(text_0, 'else_text') +local choice_text = C(text(escapable .. ',|') ^ 1) / escape_text(escapable .. ',|') + -- Within format nodes, make sure we stop at / -local format_text = C(((backslash * S(escapable)) + (P(1) - S(escapable .. '/'))) ^ 1) - / escape_text(escapable) +local format_text = C(text(escapable, escapable .. '/') ^ 1) / escape_text() + +local if_text, else_text = Cg(braced_text, 'if_text'), Cg(braced_text, 'else_text') + -- Within ternary condition format nodes, make sure we stop at : -local if_till_colon_text = Cg( - C(((backslash * S(escapable)) + (P(1) - S(escapable .. ':'))) ^ 1) / escape_text(escapable), - 'if_text' -) +local if_till_colon_text = Cg(C(text(escapable, escapable .. ':') ^ 1) / escape_text(), 'if_text') -- Matches the string inside //, allowing escaping of the closing slash. -local regex = Cg(((backslash * slash) + (P(1) - slash)) ^ 1, 'regex') +local regex = Cg(text('/') ^ 1, 'regex') -- Regex constructor flags (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/RegExp#parameters). local options = Cg(S('dgimsuvy') ^ 0, 'options') @@ -94,11 +106,11 @@ local G = P({ snippet = Ct(Cg( Ct(( V('any') + - (Ct(Cg(text_1 / escape_text(escapable), 'text')) / node(Type.Text)) + (Ct(Cg((text(escapable, '$') ^ 1) / escape_text(), 'text')) / node(Type.Text)) ) ^ 1), 'children' - )) / node(Type.Snippet), - any_or_text = V('any') + (Ct(Cg(text_0 / escape_text(escapable), 'text')) / node(Type.Text)), + ) * -P(1)) / node(Type.Snippet), any = V('placeholder') + V('tabstop') + V('choice') + V('variable'), + any_or_text = V('any') + (Ct(Cg(braced_text, 'text')) / node(Type.Text)), tabstop = Ct(dollar * (tabstop + (l_brace * tabstop * r_brace))) / node(Type.Tabstop), placeholder = Ct(dollar * l_brace * tabstop * colon * Cg(V('any_or_text'), 'value') * r_brace) / node(Type.Placeholder), choice = Ct(dollar * -- cgit From c46a6c065e8d830adb8a2f410d3c92cf5bd4455b Mon Sep 17 00:00:00 2001 From: Maria José Solano Date: Mon, 16 Oct 2023 08:13:37 -0700 Subject: docs: do not hardcode LSP version in URL #25648 --- runtime/lua/vim/lsp/_snippet_grammar.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'runtime/lua/vim/lsp/_snippet_grammar.lua') diff --git a/runtime/lua/vim/lsp/_snippet_grammar.lua b/runtime/lua/vim/lsp/_snippet_grammar.lua index eb72efdf39..0a4d669fb9 100644 --- a/runtime/lua/vim/lsp/_snippet_grammar.lua +++ b/runtime/lua/vim/lsp/_snippet_grammar.lua @@ -1,4 +1,4 @@ ---- Grammar for LSP snippets, based on https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#snippet_syntax +--- Grammar for LSP snippets, based on https://microsoft.github.io/language-server-protocol/specification/#snippet_syntax local lpeg = vim.lpeg local P, S, R, V = lpeg.P, lpeg.S, lpeg.R, lpeg.V -- cgit From f1775da07fe48da629468bcfcc2a8a6c4c3f40ed Mon Sep 17 00:00:00 2001 From: Maria José Solano Date: Fri, 20 Oct 2023 23:51:26 -0700 Subject: feat(lsp): add snippet API (#25301) --- runtime/lua/vim/lsp/_snippet_grammar.lua | 34 ++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) (limited to 'runtime/lua/vim/lsp/_snippet_grammar.lua') 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: { 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 } ---- @class vim.snippet.ChoiceData: { tabstop: vim.snippet.TabstopData, values: string[] } +--- @class vim.snippet.PlaceholderData: { tabstop: integer, value: vim.snippet.Node } +--- @class vim.snippet.ChoiceData: { tabstop: integer, values: string[] } --- @class vim.snippet.VariableData: { name: string, default?: vim.snippet.Node, regex?: string, format?: vim.snippet.Node[], options?: string } --- @class vim.snippet.FormatData: { capture: number, modifier?: string, if_text?: string, else_text?: string } --- @class vim.snippet.SnippetData: { children: vim.snippet.Node[] } +--- @type vim.snippet.Node +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 local function node(type) return function(data) - return { type = type, data = data } + return setmetatable({ type = type, data = data }, Node) end end -- cgit