glob.lua 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. local lpeg = vim.lpeg
  2. local P, S, V, R, B = lpeg.P, lpeg.S, lpeg.V, lpeg.R, lpeg.B
  3. local C, Cc, Ct, Cf, Cmt = lpeg.C, lpeg.Cc, lpeg.Ct, lpeg.Cf, lpeg.Cmt
  4. local M = {}
  5. local pathsep = P('/')
  6. --- Parses a raw glob into an |lua-lpeg| pattern.
  7. ---
  8. --- This uses glob semantics from LSP 3.17.0: https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#pattern
  9. ---
  10. --- Glob patterns can have the following syntax:
  11. --- - `*` to match one or more characters in a path segment
  12. --- - `?` to match on one character in a path segment
  13. --- - `**` to match any number of path segments, including none
  14. --- - `{}` to group conditions (e.g. `*.{ts,js}` matches TypeScript and JavaScript files)
  15. --- - `[]` to declare a range of characters to match in a path segment (e.g., `example.[0-9]` to match on `example.0`, `example.1`, …)
  16. --- - `[!...]` 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`)
  17. ---
  18. ---@param pattern string The raw glob pattern
  19. ---@return vim.lpeg.Pattern pattern An |lua-lpeg| representation of the pattern
  20. function M.to_lpeg(pattern)
  21. local function class(inv, ranges)
  22. local patt = R(unpack(vim.tbl_map(table.concat, ranges)))
  23. if inv == '!' then
  24. patt = P(1) - patt
  25. end
  26. return patt
  27. end
  28. local function condlist(conds, after)
  29. return vim.iter(conds):fold(P(false), function(acc, cond)
  30. return acc + cond * after
  31. end)
  32. end
  33. local function mul(acc, m)
  34. return acc * m
  35. end
  36. local function star(stars, after)
  37. return (-after * (P(1) - pathsep)) ^ #stars * after
  38. end
  39. local function dstar(after)
  40. return (-after * P(1)) ^ 0 * after
  41. end
  42. -- luacheck: push ignore s
  43. local function cut(_s, idx, match)
  44. return idx, match
  45. end
  46. -- luacheck: pop
  47. local p = P({
  48. 'Pattern',
  49. Pattern = V('Elem') ^ -1 * V('End'),
  50. Elem = Cmt(
  51. Cf(
  52. (V('DStar') + V('Star') + V('Ques') + V('Class') + V('CondList') + V('Literal'))
  53. * (V('Elem') + V('End')),
  54. mul
  55. ),
  56. cut
  57. ),
  58. DStar = (B(pathsep) + -B(P(1)))
  59. * P('**')
  60. * (pathsep * (V('Elem') + V('End')) + V('End'))
  61. / dstar,
  62. Star = C(P('*') ^ 1) * (V('Elem') + V('End')) / star,
  63. Ques = P('?') * Cc(P(1) - pathsep),
  64. Class = P('[')
  65. * C(P('!') ^ -1)
  66. * Ct(Ct(C(P(1)) * P('-') * C(P(1) - P(']'))) ^ 1 * P(']'))
  67. / class,
  68. CondList = P('{') * Ct(V('Cond') * (P(',') * V('Cond')) ^ 0) * P('}') * V('Pattern') / condlist,
  69. -- TODO: '*' inside a {} condition is interpreted literally but should probably have the same
  70. -- wildcard semantics it usually has.
  71. -- Fixing this is non-trivial because '*' should match non-greedily up to "the rest of the
  72. -- pattern" which in all other cases is the entire succeeding part of the pattern, but at the end of a {}
  73. -- condition means "everything after the {}" where several other options separated by ',' may
  74. -- exist in between that should not be matched by '*'.
  75. Cond = Cmt(Cf((V('Ques') + V('Class') + V('Literal') - S(',}')) ^ 1, mul), cut) + Cc(P(0)),
  76. Literal = P(1) / P,
  77. End = P(-1) * Cc(P(-1)),
  78. })
  79. local lpeg_pattern = p:match(pattern) --[[@as vim.lpeg.Pattern?]]
  80. assert(lpeg_pattern, 'Invalid glob')
  81. return lpeg_pattern
  82. end
  83. return M