From 92204b06e7365cf4c68e6ea8258dce801f0a5df9 Mon Sep 17 00:00:00 2001 From: Steven Arcangeli <506791+stevearc@users.noreply.github.com> Date: Fri, 22 Dec 2023 02:40:01 -0800 Subject: refactor(lsp): move glob parsing to util (#26519) refactor(lsp): move glob parsing to vim.glob Moving the logic for using vim.lpeg to create a match pattern from a glob into `vim.glob`. There are several places in the LSP spec that use globs, and it's very useful to have glob matching as a generally-available utility. --- runtime/lua/vim/glob.lua | 81 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 runtime/lua/vim/glob.lua (limited to 'runtime/lua/vim/glob.lua') diff --git a/runtime/lua/vim/glob.lua b/runtime/lua/vim/glob.lua new file mode 100644 index 0000000000..731179d727 --- /dev/null +++ b/runtime/lua/vim/glob.lua @@ -0,0 +1,81 @@ +local lpeg = vim.lpeg + +local M = {} + +--- Parses a raw glob into an |lpeg| pattern. +--- +--- This uses glob semantics from LSP 3.17.0: https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#pattern +--- Glob patterns can have the following syntax: +--- `*` to match one or more characters in a path segment +--- `?` to match on one character in a path segment +--- `**` to match any number of path segments, including none +--- `{}` to group conditions (e.g. `**​/*.{ts,js}` matches all TypeScript and JavaScript files) +--- `[]` to declare a range of characters to match in a path segment (e.g., `example.[0-9]` to match on `example.0`, `example.1`, …) +--- `[!...]` to negate a range of characters to match in a path segment (e.g., `example.[!0-9]` to match on `example.a`, `example.b`, but not `example.0`) +---@param pattern string The raw glob pattern +---@return vim.lpeg.Pattern pattern An |lpeg| representation of the pattern +function M.to_lpeg(pattern) + local l = lpeg + + local P, S, V = lpeg.P, lpeg.S, lpeg.V + local C, Cc, Ct, Cf = lpeg.C, lpeg.Cc, lpeg.Ct, lpeg.Cf + + local pathsep = '/' + + local function class(inv, ranges) + for i, r in ipairs(ranges) do + ranges[i] = r[1] .. r[2] + end + local patt = l.R(unpack(ranges)) + if inv == '!' then + patt = P(1) - patt + end + return patt + end + + local function add(acc, a) + return acc + a + end + + local function mul(acc, m) + return acc * m + end + + local function star(stars, after) + return (-after * (l.P(1) - pathsep)) ^ #stars * after + end + + local function dstar(after) + return (-after * l.P(1)) ^ 0 * after + end + + local p = P({ + 'Pattern', + Pattern = V('Elem') ^ -1 * V('End'), + Elem = Cf( + (V('DStar') + V('Star') + V('Ques') + V('Class') + V('CondList') + V('Literal')) + * (V('Elem') + V('End')), + mul + ), + DStar = P('**') * (P(pathsep) * (V('Elem') + V('End')) + V('End')) / dstar, + Star = C(P('*') ^ 1) * (V('Elem') + V('End')) / star, + Ques = P('?') * Cc(l.P(1) - pathsep), + Class = P('[') * C(P('!') ^ -1) * Ct(Ct(C(1) * '-' * C(P(1) - ']')) ^ 1 * ']') / class, + CondList = P('{') * Cf(V('Cond') * (P(',') * V('Cond')) ^ 0, add) * '}', + -- TODO: '*' inside a {} condition is interpreted literally but should probably have the same + -- wildcard semantics it usually has. + -- Fixing this is non-trivial because '*' should match non-greedily up to "the rest of the + -- pattern" which in all other cases is the entire succeeding part of the pattern, but at the end of a {} + -- condition means "everything after the {}" where several other options separated by ',' may + -- exist in between that should not be matched by '*'. + Cond = Cf((V('Ques') + V('Class') + V('CondList') + (V('Literal') - S(',}'))) ^ 1, mul) + + Cc(l.P(0)), + Literal = P(1) / l.P, + End = P(-1) * Cc(l.P(-1)), + }) + + local lpeg_pattern = p:match(pattern) --[[@as vim.lpeg.Pattern?]] + return assert(lpeg_pattern, 'Invalid glob') +end + +return M -- cgit From 5dc0bdfe98b59bb03226167ed541d17cc5af30b1 Mon Sep 17 00:00:00 2001 From: Mathias Fußenegger Date: Tue, 2 Jan 2024 14:32:43 +0100 Subject: docs(glob): add glob module (#26853) --- runtime/lua/vim/glob.lua | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'runtime/lua/vim/glob.lua') diff --git a/runtime/lua/vim/glob.lua b/runtime/lua/vim/glob.lua index 731179d727..49d6f555da 100644 --- a/runtime/lua/vim/glob.lua +++ b/runtime/lua/vim/glob.lua @@ -2,18 +2,20 @@ local lpeg = vim.lpeg local M = {} ---- Parses a raw glob into an |lpeg| pattern. +--- Parses a raw glob into an |lua-lpeg| pattern. --- --- This uses glob semantics from LSP 3.17.0: https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#pattern +--- --- Glob patterns can have the following syntax: ---- `*` to match one or more characters in a path segment ---- `?` to match on one character in a path segment ---- `**` to match any number of path segments, including none ---- `{}` to group conditions (e.g. `**​/*.{ts,js}` matches all TypeScript and JavaScript files) ---- `[]` to declare a range of characters to match in a path segment (e.g., `example.[0-9]` to match on `example.0`, `example.1`, …) ---- `[!...]` to negate a range of characters to match in a path segment (e.g., `example.[!0-9]` to match on `example.a`, `example.b`, but not `example.0`) +--- - `*` to match one or more characters in a path segment +--- - `?` to match on one character in a path segment +--- - `**` to match any number of path segments, including none +--- - `{}` to group conditions (e.g. `*.{ts,js}` matches TypeScript and JavaScript files) +--- - `[]` to declare a range of characters to match in a path segment (e.g., `example.[0-9]` to match on `example.0`, `example.1`, …) +--- - `[!...]` to negate a range of characters to match in a path segment (e.g., `example.[!0-9]` to match on `example.a`, `example.b`, but not `example.0`) +--- ---@param pattern string The raw glob pattern ----@return vim.lpeg.Pattern pattern An |lpeg| representation of the pattern +---@return vim.lpeg.Pattern pattern An |lua-lpeg| representation of the pattern function M.to_lpeg(pattern) local l = lpeg -- cgit From 5aa14e1231b7eccfbc54cba8f20d54105212847d Mon Sep 17 00:00:00 2001 From: altermo <107814000+altermo@users.noreply.github.com> Date: Wed, 17 Jan 2024 20:34:25 +0100 Subject: fix(lua): return after assert returns assert message (#27064) --- runtime/lua/vim/glob.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'runtime/lua/vim/glob.lua') diff --git a/runtime/lua/vim/glob.lua b/runtime/lua/vim/glob.lua index 49d6f555da..764200dd36 100644 --- a/runtime/lua/vim/glob.lua +++ b/runtime/lua/vim/glob.lua @@ -77,7 +77,8 @@ function M.to_lpeg(pattern) }) local lpeg_pattern = p:match(pattern) --[[@as vim.lpeg.Pattern?]] - return assert(lpeg_pattern, 'Invalid glob') + assert(lpeg_pattern, 'Invalid glob') + return lpeg_pattern end return M -- cgit From 9707363b09dbadeb58966d6d45acca17bd74e527 Mon Sep 17 00:00:00 2001 From: altermo Date: Thu, 18 Jan 2024 10:02:35 +0100 Subject: refactor(lua): refactored glob --- runtime/lua/vim/glob.lua | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) (limited to 'runtime/lua/vim/glob.lua') diff --git a/runtime/lua/vim/glob.lua b/runtime/lua/vim/glob.lua index 764200dd36..ad4a915a94 100644 --- a/runtime/lua/vim/glob.lua +++ b/runtime/lua/vim/glob.lua @@ -1,7 +1,11 @@ local lpeg = vim.lpeg +local P, S, V, R, B = lpeg.P, lpeg.S, lpeg.V, lpeg.R, lpeg.B +local C, Cc, Ct, Cf = lpeg.C, lpeg.Cc, lpeg.Ct, lpeg.Cf local M = {} +local pathsep = P('/') + --- Parses a raw glob into an |lua-lpeg| pattern. --- --- This uses glob semantics from LSP 3.17.0: https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#pattern @@ -17,18 +21,8 @@ local M = {} ---@param pattern string The raw glob pattern ---@return vim.lpeg.Pattern pattern An |lua-lpeg| representation of the pattern function M.to_lpeg(pattern) - local l = lpeg - - local P, S, V = lpeg.P, lpeg.S, lpeg.V - local C, Cc, Ct, Cf = lpeg.C, lpeg.Cc, lpeg.Ct, lpeg.Cf - - local pathsep = '/' - local function class(inv, ranges) - for i, r in ipairs(ranges) do - ranges[i] = r[1] .. r[2] - end - local patt = l.R(unpack(ranges)) + local patt = R(unpack(vim.tbl_map(table.concat, ranges))) if inv == '!' then patt = P(1) - patt end @@ -44,11 +38,11 @@ function M.to_lpeg(pattern) end local function star(stars, after) - return (-after * (l.P(1) - pathsep)) ^ #stars * after + return (-after * (P(1) - pathsep)) ^ #stars * after end local function dstar(after) - return (-after * l.P(1)) ^ 0 * after + return (-after * P(1)) ^ 0 * after end local p = P({ @@ -59,11 +53,17 @@ function M.to_lpeg(pattern) * (V('Elem') + V('End')), mul ), - DStar = P('**') * (P(pathsep) * (V('Elem') + V('End')) + V('End')) / dstar, + DStar = (B(pathsep) + -B(P(1))) + * P('**') + * (pathsep * (V('Elem') + V('End')) + V('End')) + / dstar, Star = C(P('*') ^ 1) * (V('Elem') + V('End')) / star, - Ques = P('?') * Cc(l.P(1) - pathsep), - Class = P('[') * C(P('!') ^ -1) * Ct(Ct(C(1) * '-' * C(P(1) - ']')) ^ 1 * ']') / class, - CondList = P('{') * Cf(V('Cond') * (P(',') * V('Cond')) ^ 0, add) * '}', + Ques = P('?') * Cc(P(1) - pathsep), + Class = P('[') + * C(P('!') ^ -1) + * Ct(Ct(C(P(1)) * P('-') * C(P(1) - P(']'))) ^ 1 * P(']')) + / class, + CondList = P('{') * Cf(V('Cond') * (P(',') * V('Cond')) ^ 0, add) * P('}'), -- TODO: '*' inside a {} condition is interpreted literally but should probably have the same -- wildcard semantics it usually has. -- Fixing this is non-trivial because '*' should match non-greedily up to "the rest of the @@ -71,9 +71,9 @@ function M.to_lpeg(pattern) -- condition means "everything after the {}" where several other options separated by ',' may -- exist in between that should not be matched by '*'. Cond = Cf((V('Ques') + V('Class') + V('CondList') + (V('Literal') - S(',}'))) ^ 1, mul) - + Cc(l.P(0)), - Literal = P(1) / l.P, - End = P(-1) * Cc(l.P(-1)), + + Cc(P(0)), + Literal = P(1) / P, + End = P(-1) * Cc(P(-1)), }) local lpeg_pattern = p:match(pattern) --[[@as vim.lpeg.Pattern?]] -- cgit