Skip to content

Commit dd2025f

Browse files
committed
Added very_magic arg to RegexList2RegexOR
1 parent 13ee502 commit dd2025f

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

lib/utils.vim

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,17 @@ export def ZipLists(l1: list<any>, l2: list<any>): list<list<any>>
1414
return map(range(min_len), $'[{l1}[v:val], {l2}[v:val]]')
1515
enddef
1616

17-
export def RegexList2RegexOR(regex_list: list<string>): string
18-
# Valid only for very-magic regex.
19-
return regex_list->map((_, val) => substitute(val, '^\(\\v\)', '', ''))
17+
export def RegexList2RegexOR(regex_list: list<string>,
18+
very_magic: bool = false): string
19+
20+
var result = ''
21+
if very_magic
22+
result = regex_list->map((_, val) => substitute(val, '^\(\\v\)', '', ''))
2023
->join('|')->printf('\v(%s)')
24+
else
25+
result = regex_list->join('\|')->printf('\(%s\)')
26+
endif
27+
return result
2128
enddef
2229

2330
export def GetTextObject(textobject: string): string
@@ -208,7 +215,7 @@ export def g:Surround(open_delimiter: string,
208215
echom "TBD"
209216
# Overwrite everything that is in the middle
210217
var middle = strcharpart(getline(lA), cA - 1, cB - cA)
211-
-> substitute(RegexList2RegexOR(values(open_delimiters_dict)), '', 'g')
218+
-> substitute(RegexList2RegexOR(values(open_delimiters_dict), true), '', 'g')
212219
setline(lA, toA .. middle .. fromB)
213220
elseif lB - lA == 1
214221
echom "TBD"

test/test_utils.vim

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,10 +310,19 @@ enddef
310310
def g:Test_RegexList2RegexOr()
311311
var A = '\v(\d+|\a)\s'
312312
var B = '\v^\s*\w\d*\w'
313-
var C = '\v^\s*\w\d*\w'
313+
var C = '\v\w*\d+\w'
314314
var test_list = [A, B, C]
315315

316-
var expected_string = '\v((\d+|\a)\s|^\s*\w\d*\w|^\s*\w\d*\w)'
317-
var actual_string = utils.RegexList2RegexOR(test_list)
316+
var expected_string = '\v((\d+|\a)\s|^\s*\w\d*\w|\w*\d+\w)'
317+
var actual_string = utils.RegexList2RegexOR(test_list, true)
318+
assert_equal(expected_string, actual_string)
319+
320+
A = '\(\d\+\|\a\)\s'
321+
B = '^\s*\w\d*\w'
322+
C = '\w*\d\+\w'
323+
test_list = [A, B, C]
324+
325+
expected_string = '\(\(\d\+\|\a\)\s\|^\s*\w\d*\w\|\w*\d\+\w\)'
326+
actual_string = utils.RegexList2RegexOR(test_list)
318327
assert_equal(expected_string, actual_string)
319328
enddef

0 commit comments

Comments
 (0)