Skip to content

Commit d3eac56

Browse files
committed
fix(hipatterns): mention explicitly how overlapping matching is handled
Resolve #2105
1 parent 9101b9e commit d3eac56

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

doc/mini-hipatterns.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,13 @@ Each entry defines single highlighter as a table with the following fields:
267267
If array:
268268
- Each element is matched and highlighted with the same highlight group.
269269

270+
Note: matching does not result in overlapping (sub)matches (similarly
271+
to how |cpo-c| works). For example, with line `xxxxxxx`:
272+
- Pattern `xxx` matches columns 1-3, 4-6.
273+
- Pattern `()xx()x` matches columns 1-2, 3-4, 5-6.
274+
- Pattern `x()xx()` matches columns 2-3, 5-6.
275+
- Pattern `x()x()x` matches columns 2-2, 4-4, 6-6.
276+
270277
- <group> `(string|function)` - name of highlight group to use. Can be either
271278
string or callable returning the string.
272279
If callable:

lua/mini/hipatterns.lua

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,13 @@ end
273273
--- If array:
274274
--- - Each element is matched and highlighted with the same highlight group.
275275
---
276+
--- Note: matching does not result in overlapping (sub)matches (similarly
277+
--- to how |cpo-c| works). For example, with line `xxxxxxx`:
278+
--- - Pattern `xxx` matches columns 1-3, 4-6.
279+
--- - Pattern `()xx()x` matches columns 1-2, 3-4, 5-6.
280+
--- - Pattern `x()xx()` matches columns 2-3, 5-6.
281+
--- - Pattern `x()x()x` matches columns 2-2, 4-4, 6-6.
282+
---
276283
--- - <group> `(string|function)` - name of highlight group to use. Can be either
277284
--- string or callable returning the string.
278285
--- If callable:
@@ -938,7 +945,7 @@ H.apply_highlighter_pattern = vim.schedule_wrap(function(pattern, hi, buf_id, ns
938945
-- `init` is more than 1
939946
if pattern_has_line_start then break end
940947

941-
from, to, sub_from, sub_to = line:find(pattern, to + 1)
948+
from, to, sub_from, sub_to = line:find(pattern, sub_to + 1)
942949
end
943950
end
944951
end)

tests/test_hipatterns.lua

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ T['Highlighters']['allows submatch in `pattern`'] = function()
472472
set_lines({ 'abcd', 'xabcd', 'xxabcd', 'xxabcdxx' })
473473

474474
local validate = function(pattern)
475-
child.lua([[require('mini.hipatterns').disable()]])
475+
child.lua('require("mini.hipatterns").disable()')
476476
local config = {
477477
highlighters = { abcd = { pattern = pattern, group = 'Error' } },
478478
delay = test_config.delay,
@@ -495,6 +495,29 @@ T['Highlighters']['allows submatch in `pattern`'] = function()
495495
validate('xx()ab()c()d()')
496496
end
497497

498+
T['Highlighters']['works with overlapping matches'] = function()
499+
local validate = function(pattern, ref_ranges)
500+
child.lua('require("mini.hipatterns").disable()')
501+
local config = {
502+
highlighters = { x = { pattern = pattern, group = 'Error' } },
503+
delay = test_config.delay,
504+
}
505+
enable(0, config)
506+
507+
sleep(test_config.delay.text_change + small_time)
508+
local ranges = vim.tbl_map(function(e) return { e.from_col, e.to_col } end, get_hipatterns_extmarks(0, { 'x' }))
509+
eq(ranges, ref_ranges)
510+
end
511+
512+
set_lines({ string.rep('x', 7) })
513+
514+
-- Should behave similarly to how `/` with `\zs` and `\ze` works
515+
validate('xxx', { { 1, 3 }, { 4, 6 } })
516+
validate('()xx()x', { { 1, 2 }, { 3, 4 }, { 5, 6 } })
517+
validate('x()xx()', { { 2, 3 }, { 5, 6 } })
518+
validate('x()x()x', { { 2, 2 }, { 4, 4 }, { 6, 6 } })
519+
end
520+
498521
T['Highlighters']['allows frontier pattern in `pattern`'] = function()
499522
local config = {
500523
highlighters = { abcd = { pattern = '%f[%w]abcd%f[%W]', group = 'Error' } },

0 commit comments

Comments
 (0)