robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
6.1 KiB179 linesraw
1--- Grammar for LSP snippets, based on https://microsoft.github.io/language-server-protocol/specification/#snippet_syntax
2
3local lpeg = vim.lpeg
4local P, S, R, V = lpeg.P, lpeg.S, lpeg.R, lpeg.V
5local C, Cg, Ct = lpeg.C, lpeg.Cg, lpeg.Ct
6
7local M = {}
8
9local alpha = R('az', 'AZ')
10local backslash = P('\\')
11local colon = P(':')
12local dollar = P('$')
13local int = R('09') ^ 1
14local l_brace, r_brace = P('{'), P('}')
15local pipe = P('|')
16local slash = P('/')
17local underscore = P('_')
18local var = Cg((underscore + alpha) * ((underscore + alpha + int) ^ 0), 'name')
19local format_capture = Cg(int / tonumber, 'capture')
20local format_modifier = Cg(P('upcase') + P('downcase') + P('capitalize'), 'modifier')
21local tabstop = Cg(int / tonumber, 'tabstop')
22
23-- These characters are always escapable in text nodes no matter the context.
24local escapable = '$}\\'
25
26--- Returns a function that unescapes occurrences of "special" characters.
27---
28--- @param special? string
29--- @return fun(match: string): string
30local function escape_text(special)
31  special = special or escapable
32  return function(match)
33    local escaped = match:gsub('\\(.)', function(c)
34      return special:find(c) and c or '\\' .. c
35    end)
36    return escaped
37  end
38end
39
40--- Returns a pattern for text nodes. Will match characters in `escape` when preceded by a backslash,
41--- and will stop with characters in `stop_with`.
42---
43--- @param escape string
44--- @param stop_with? string
45--- @return vim.lpeg.Pattern
46local function text(escape, stop_with)
47  stop_with = stop_with or escape
48  return (backslash * S(escape)) + (P(1) - S(stop_with))
49end
50
51-- For text nodes inside curly braces. It stops parsing when reaching an escapable character.
52local braced_text = (text(escapable) ^ 0) / escape_text()
53
54-- Within choice nodes, \ also escapes comma and pipe characters.
55local choice_text = C(text(escapable .. ',|') ^ 1) / escape_text(escapable .. ',|')
56
57-- Within format nodes, make sure we stop at /
58local format_text = C(text(escapable, escapable .. '/') ^ 1) / escape_text()
59
60local if_text, else_text = Cg(braced_text, 'if_text'), Cg(braced_text, 'else_text')
61
62-- Within ternary condition format nodes, make sure we stop at :
63local if_till_colon_text = Cg(C(text(escapable, escapable .. ':') ^ 1) / escape_text(), 'if_text')
64
65-- Matches the string inside //, allowing escaping of the closing slash.
66local regex = Cg(text('/') ^ 1, 'regex')
67
68-- Regex constructor flags (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/RegExp#parameters).
69local options = Cg(S('dgimsuvy') ^ 0, 'options')
70
71--- @enum vim.snippet.Type
72local Type = {
73  Tabstop = 1,
74  Placeholder = 2,
75  Choice = 3,
76  Variable = 4,
77  Format = 5,
78  Text = 6,
79  Snippet = 7,
80}
81M.NodeType = Type
82
83--- @class vim.snippet.Node<T>: { type: vim.snippet.Type, data: T }
84--- @class vim.snippet.TabstopData: { tabstop: integer }
85--- @class vim.snippet.TextData: { text: string }
86--- @class vim.snippet.PlaceholderData: { tabstop: integer, value: vim.snippet.Node<any> }
87--- @class vim.snippet.ChoiceData: { tabstop: integer, values: string[] }
88--- @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 }
89--- @class vim.snippet.FormatData: { capture: number, modifier?: string, if_text?: string, else_text?: string }
90--- @class vim.snippet.SnippetData: { children: vim.snippet.Node<any>[] }
91
92local Node = {}
93
94--- @param self vim.snippet.Node<any>
95--- @return string
96function Node.__tostring(self)
97  local node_text = {}
98  local type, data = self.type, self.data
99  if type == Type.Snippet then
100    --- @cast data vim.snippet.SnippetData
101    for _, child in ipairs(data.children) do
102      table.insert(node_text, tostring(child))
103    end
104  elseif type == Type.Choice then
105    --- @cast data vim.snippet.ChoiceData
106    table.insert(node_text, data.values[1])
107  elseif type == Type.Placeholder then
108    --- @cast data vim.snippet.PlaceholderData
109    table.insert(node_text, tostring(data.value))
110  elseif type == Type.Text then
111    --- @cast data vim.snippet.TextData
112    table.insert(node_text, data.text)
113  end
114  return table.concat(node_text)
115end
116
117--- Returns a function that constructs a snippet node of the given type.
118---
119--- @generic T
120--- @param type vim.snippet.Type
121--- @return fun(data: T): vim.snippet.Node<T>
122local function node(type)
123  return function(data)
124    return setmetatable({ type = type, data = data }, Node)
125  end
126end
127
128-- stylua: ignore
129--- @diagnostic disable-next-line: missing-fields
130local G = P({
131  'snippet';
132  snippet = Ct(Cg(
133    Ct((
134      V('any') +
135      (Ct(Cg((text(escapable, '$') ^ 1) / escape_text(), 'text')) / node(Type.Text))
136    ) ^ 1), 'children'
137  ) * -P(1)) / node(Type.Snippet),
138  any = V('placeholder') + V('tabstop') + V('choice') + V('variable'),
139  any_or_text = V('any') + (Ct(Cg(braced_text, 'text')) / node(Type.Text)),
140  tabstop = Ct(dollar * (tabstop + (l_brace * tabstop * r_brace))) / node(Type.Tabstop),
141  placeholder = Ct(dollar * l_brace * tabstop * colon * Cg(V('any_or_text'), 'value') * r_brace) / node(Type.Placeholder),
142  choice = Ct(dollar *
143    l_brace *
144    tabstop *
145    pipe *
146    Cg(Ct(choice_text * (P(',') * choice_text) ^ 0), 'values') *
147    pipe *
148    r_brace) / node(Type.Choice),
149  variable = Ct(dollar * (
150    var + (
151    l_brace * var * (
152      r_brace +
153      (colon * Cg(V('any_or_text'), 'default') * r_brace) +
154      (slash * regex * slash * Cg(Ct((V('format') + (C(format_text) / node(Type.Text))) ^ 1), 'format') * slash * options * r_brace)
155    ))
156  )) / node(Type.Variable),
157  format = Ct(dollar * (
158    format_capture + (
159    l_brace * format_capture * (
160      r_brace +
161      (colon * (
162        (slash * format_modifier * r_brace) +
163        (P('+') * if_text * r_brace) +
164        (P('?') * if_till_colon_text * colon * else_text * r_brace) +
165        (P('-') * else_text * r_brace) +
166        (else_text * r_brace)
167      ))
168    ))
169  )) / node(Type.Format),
170})
171
172--- Parses the given input into a snippet tree.
173--- @param input string
174--- @return vim.snippet.Node<vim.snippet.SnippetData>
175function M.parse(input)
176  return (assert(G:match(input), 'snippet parsing failed'))
177end
178
179return M