Skip to content

Commit 6783907

Browse files
committed
feat(regex): Added aupport for lazy(non-greedy) version of quantifiers
Closes #7
1 parent d614170 commit 6783907

3 files changed

Lines changed: 139 additions & 33 deletions

File tree

lua/patterns/parsers/regex.lua

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,12 @@ end
144144
------------------------------------------------------------------------------
145145

146146
regex.count_quantifier = function (buffer, node)
147-
local text = vim.treesitter.get_node_text(node, buffer):match("^%{(.-)%}$");
148-
149147
return {
150148
kind = "quantifier_count",
151149
current = node:equal(regex.current),
152150
level = regex.level + 1,
153151

154-
text = text,
152+
text = vim.treesitter.get_node_text(node, buffer),
155153
range = { node:range() }
156154
};
157155
end
@@ -189,6 +187,17 @@ regex.zero_or_more = function (buffer, node)
189187
};
190188
end
191189

190+
regex.lazy = function (buffer, node)
191+
return {
192+
kind = "lazy",
193+
current = node:equal(regex.current),
194+
level = regex.level + 1,
195+
196+
text = vim.treesitter.get_node_text(node, buffer),
197+
range = { node:range() }
198+
};
199+
end
200+
192201
------------------------------------------------------------------------------
193202

194203
---@param buffer integer

lua/patterns/renderers/regex.lua

Lines changed: 80 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,30 +32,94 @@ regex.tips = {
3232
end
3333
end,
3434

35+
3536
quantifier_count = function (_, item)
36-
if string.match(item.text, "^%d+$") then
37-
return string.format("Matches a pattern exactly %s times.", item.text);
38-
elseif string.match(item.text, "^%d+,$") then
37+
---|fS
38+
39+
local is_lazy = string.match(item.text, "%?$") ~= nil;
40+
local desc;
41+
42+
if is_lazy then
43+
desc = "This will match as few times as possible(that successfully matches)."
44+
else
45+
desc = "This will match as many times as possible."
46+
end
47+
48+
if string.match(item.text, "%{(%d+)%}") then
49+
return string.format(
50+
"Matches a pattern exactly %s times.",
51+
string.match(item.text, "^%{(%d+)%}")
52+
);
53+
elseif string.match(item.text, "^%{(%d+),%}") then
3954
return string.format(
40-
"Matches a pattern at least %s times.\n \n This will match the highest number of occurrences.",
41-
string.match(item.text, "^(%d+)")
55+
"Matches a pattern at least %s times. \n \n " .. desc,
56+
string.match(item.text, "^%{(%d+),%}")
4257
);
43-
elseif string.match(item.text, "^,%d+$") then
58+
elseif string.match(item.text, "^%{,(%d+)%}") then
4459
return string.format(
45-
"Matches a pattern at most %s times.\n \n This will match the highest number of occurrences.",
46-
string.match(item.text, "^,(%d+)$")
60+
"Matches a pattern at most %s times. \n \n " .. desc,
61+
string.match(item.text, "^%{,(%d+)%}")
4762
);
4863
else
4964
return string.format(
50-
"Matches a pattern between %s & %s times.\n \n This will match the highest number of occurrences.",
51-
string.match(item.text, "^(%d+),"),
52-
string.match(item.text, "^%d+,(%d+)$")
65+
"Matches a pattern between %s & %s times. \n \n " .. desc,
66+
string.match(item.text, "^%{(%d+),%d+%}"),
67+
string.match(item.text, "^%{%d+,(%d+)%}")
5368
);
5469
end
70+
71+
---|fE
72+
end,
73+
quantifier_optional = function (_, item)
74+
---|fS
75+
76+
local is_lazy = string.match(item.text, "%?$") ~= nil;
77+
local desc;
78+
79+
if is_lazy then
80+
desc = "This will match as few times as possible(that successfully matches)."
81+
else
82+
desc = "This will match as many times as possible."
83+
end
84+
85+
return "Matches a pattern zero or one time. \n \n" .. desc;
86+
87+
---|fE
5588
end,
56-
quantifier_optional = "Matches a pattern zero or one time.",
57-
quantifier_plus = "Matches a pattern one or more times.\n \n This will match the highest number of occurrences.",
58-
quantifier_star = "Matches a pattern zero or more times.\n \n This will match the highest number of occurrences.",
89+
quantifier_plus = function (_, item)
90+
---|fS
91+
92+
local is_lazy = string.match(item.text, "%?$") ~= nil;
93+
local desc;
94+
95+
if is_lazy then
96+
desc = "This will match as few times as possible(that successfully matches)."
97+
else
98+
desc = "This will match as many times as possible."
99+
end
100+
101+
return "Matches a pattern one or more times.\n \n " .. desc;
102+
103+
---|fE
104+
end,
105+
quantifier_star = function (_, item)
106+
---|fS
107+
108+
local is_lazy = string.match(item.text, "%?$") ~= nil;
109+
local desc;
110+
111+
if is_lazy then
112+
desc = "This will match as few times as possible(that successfully matches)."
113+
else
114+
desc = "This will match as many times as possible."
115+
end
116+
117+
return "Matches a pattern zero or more times.\n \n " .. desc;
118+
119+
---|fE
120+
end,
121+
122+
lazy = "Makes the quantifier lazy(non-greedy).",
59123

60124

61125
pattern_character = function (_, item)
@@ -237,7 +301,7 @@ regex.__generic = function (buffer, item)
237301
local win_w = vim.api.nvim_win_get_width(utils.win_findbuf(buffer));
238302
local tip = spec.get({ item.kind }, {
239303
source = regex.tips,
240-
eval_args = { buffer, item}
304+
args = { buffer, item },
241305
});
242306

243307
if tip and config.show_tip ~= false then
@@ -322,7 +386,7 @@ regex.render = function (buffer, content)
322386
local can_render, data = pcall(regex.__generic, buffer, entry);
323387

324388
if can_render == false then
325-
vim.print(data);
389+
-- vim.print(data);
326390
elseif entry.current then
327391
current_line = data;
328392
end

lua/patterns/spec.lua

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -356,23 +356,27 @@ spec.default = {
356356

357357
quantifier_count = {
358358
text = function (_, item)
359-
if string.match(item.text, "^%d+$") then
360-
return string.format(" Repeats exactly %s times", item.text);
361-
elseif string.match(item.text, "^%d+,$") then
359+
local is_lazy = string.match(item.text, "%?$") ~= nil;
360+
361+
if string.match(item.text, "%{(%d+)%}") then
362+
return string.format(" Repeats exactly %s times", string.match(item.text, "%{(%d+)%}"));
363+
elseif string.match(item.text, "^%{(%d+),%}") then
362364
return string.format(
363-
" Repeats at least %s times",
364-
string.match(item.text, "^(%d+)")
365+
" Repeats at least %s times(%s)",
366+
string.match(item.text, "^%{(%d+),%}"),
367+
is_lazy and "Lazy" or "Greedy"
365368
);
366-
elseif string.match(item.text, "^,%d+$") then
369+
elseif string.match(item.text, "^%{,(%d+)%}") then
367370
return string.format(
368-
" Repeats at most %s times",
369-
string.match(item.text, "^,(%d+)$")
371+
" Repeats at most %s times(%s)",
372+
string.match(item.text, "^%{,(%d+)%}"),
373+
is_lazy and "Lazy" or "Greedy"
370374
);
371375
else
372376
return string.format(
373-
" Repeats between %s & %s times",
374-
string.match(item.text, "^(%d+),"),
375-
string.match(item.text, "^%d+,(%d+)$")
377+
" Repeats between %s & %s times(%s)",
378+
string.match(item.text, "^%{(%d+),%d+%}"),
379+
string.match(item.text, "^%{%d+,(%d+)%}")
376380
);
377381
end
378382
end,
@@ -383,23 +387,52 @@ spec.default = {
383387
},
384388

385389
quantifier_optional = {
386-
text = " Repeats zero or one time",
390+
text = function (_, item)
391+
---|fS
392+
393+
local is_lazy = string.match(item.text, "%?%?$") ~= nil;
394+
return string.format(" Matches zero or one time(%s)", is_lazy and "Lazy" or "Greedy");
395+
396+
---|fE
397+
end,
387398
show_tip = on_current,
388399

389400
tip_hl = "PatternsPalette7Bg",
390401
hl = "PatternsPalette7"
391402
},
392403

393404
quantifier_plus = {
394-
text = " Repeats one or more times",
405+
text = function (_, item)
406+
---|fS
407+
408+
local is_lazy = string.match(item.text, "%?$") ~= nil;
409+
return string.format(" Repeats one or more times(%s)", is_lazy and "Lazy" or "Greedy");
410+
411+
---|fE
412+
end,
395413
show_tip = on_current,
396414

397415
tip_hl = "PatternsPalette7Bg",
398416
hl = "PatternsPalette7"
399417
},
400418

401419
quantifier_star = {
402-
text = " Repeats zero or more times",
420+
text = function (_, item)
421+
---|fS
422+
423+
local is_lazy = string.match(item.text, "%?$") ~= nil;
424+
return string.format(" Repeats zero or more times(%s)", is_lazy and "Lazy" or "Greedy");
425+
426+
---|fE
427+
end,
428+
show_tip = on_current,
429+
430+
tip_hl = "PatternsPalette7Bg",
431+
hl = "PatternsPalette7"
432+
},
433+
434+
lazy = {
435+
text = "󰒲 Non-greedy",
403436
show_tip = on_current,
404437

405438
tip_hl = "PatternsPalette7Bg",

0 commit comments

Comments
 (0)