Skip to content

Commit f1e4ca5

Browse files
committed
fix: vim regex were misbehaving
1 parent 8466354 commit f1e4ca5

File tree

2 files changed

+41
-19
lines changed

2 files changed

+41
-19
lines changed

plugin/hmts.lua

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,37 @@
1+
local function get_regex(pattern)
2+
local magic_prefixes = { ["\\v"] = true, ["\\m"] = true, ["\\M"] = true, ["\\V"] = true }
3+
local function check_magic(str)
4+
if string.len(str) < 2 or magic_prefixes[string.sub(str, 1, 2)] then
5+
return str
6+
end
7+
return "\\v" .. str
8+
end
9+
10+
local compiled_vim_regexes = setmetatable({}, {
11+
__index = function(t, pat)
12+
local res = vim.regex(check_magic(pat))
13+
rawset(t, pat, res)
14+
return res
15+
end,
16+
})
17+
18+
return compiled_vim_regexes[pattern]
19+
end
20+
21+
local function regex_match(pattern, str)
22+
local regex = get_regex(pattern)
23+
return regex:match_str(str) ~= nil
24+
end
25+
126
local function path_diff(target_path, node_path)
227
local is_match = false
3-
local function regex_match(pattern, str)
4-
local regex = vim.regex(pattern)
5-
return regex:match_str(str)
6-
end
728

829
while true do
9-
if #node_path == 0 or #target_path == 0 or regex_match(target_path[#target_path], node_path[#node_path]) then
30+
if
31+
#node_path == 0
32+
or #target_path == 0
33+
or not regex_match(target_path[#target_path], node_path[#node_path])
34+
then
1035
break
1136
end
1237
is_match = true

queries/nix/injections.scm

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
;; extends
22

3-
; Generic
3+
; home.file.*.text
44
(binding
55
attrpath: (_) @_path
66
expression: (_
@@ -10,6 +10,7 @@
1010
(#hmts-inject! @_path)
1111
) @combined
1212

13+
; xdg.configFile.*.text
1314
(binding
1415
attrpath: (_) @_path
1516
expression: (_
@@ -19,6 +20,9 @@
1920
(#hmts-inject! @_path)
2021
) @combined
2122

23+
; ''
24+
; #! /bin/lang
25+
; ''
2226
(
2327
(indented_string_expression
2428
(string_fragment) @_lang (#lua-match? @_lang "^%s*#!.*/.") ; use lua regex for consistency with the next line
@@ -31,43 +35,36 @@
3135

3236
; Fish
3337
(binding
34-
attrpath: (_) @_path (#match? @_path "((interactive|login)S|s)hellInit$")
38+
attrpath: (_) @_path (#hmts-path? @_path "programs" "fish" "((interactive|login)S|s)hellInit$")
3539
expression: (_ (string_fragment) @fish)
36-
; TODO: find a way to have proper regex support here
37-
(#hmts-path? @_path "programs" "fish" ".*")
3840
) @combined
3941

4042
(binding
41-
attrpath: (_) @_path
43+
attrpath: (_) @_path (#hmts-path? @_path "programs" "fish" "functions")
4244
expression: (attrset_expression
4345
(binding_set
4446
binding: (binding
4547
attrpath: (_)
4648
expression: (_ (string_fragment) @fish)
4749
)))
48-
(#hmts-path? @_path "programs" "fish" "functions")
4950
) @combined
5051

5152
; Bash
5253
(binding
53-
attrpath: (_) @_path (#match? @_path "(init|logout|profile|bashrc)Extra$")
54+
attrpath: (_) @_path (#hmts-path? @_path "programs" "bash" "(init|logout|profile|bashrc)Extra$")
5455
expression: (_ (string_fragment) @bash)
55-
; TODO: find a way to have proper regex support here
56-
(#hmts-path? @_path "programs" "bash" ".*")
5756
) @combined
5857

5958
; Zsh
6059
(binding
60+
attrpath: (_) @_path
6161
; eww
62-
attrpath: (_) @_path (#match? @_path "(completionInit|envExtra|initExtra|initExtraBeforeCompInit|initExtraFirst|loginExtra|logoutExtra|profileExtra)$")
62+
(#hmts-path? @_path "programs" "bash" "(completionInit|envExtra|initExtra|initExtraBeforeCompInit|initExtraFirst|loginExtra|logoutExtra|profileExtra)$")
6363
expression: (_ (string_fragment) @bash)
64-
; TODO: find a way to have proper regex support here
65-
(#hmts-path? @_path "programs" "bash" ".*")
6664
) @combined
6765

6866
; Firefox
6967
(binding
70-
attrpath: (_) @_path
68+
attrpath: (_) @_path (#hmts-path? @_path "programs" "firefox" "profiles" ".*" "userChrome")
7169
expression: (_ (string_fragment) @css)
72-
(#hmts-path? @_path "programs" "firefox" "profiles" ".*" "userChrome")
7370
) @combined

0 commit comments

Comments
 (0)