Skip to content

Commit e9aaae6

Browse files
committed
.
1 parent 8e944c7 commit e9aaae6

File tree

3 files changed

+73
-16
lines changed

3 files changed

+73
-16
lines changed

after/ftplugin/markdown.vim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ export var italic_regex = '\v((\\|\*)@<!|(\\\*)@<=)\*\*@!'
1515
export var bold_regex = '\v(\\|\*)@<!\*\*\*@!'
1616
export var strikethrough_regex = '\v(\\|\~)@<!\~\~\~@!'
1717

18+
g:A = ['\v(\\|\~)\v@<!\~\~\~@!', '\v(\\|\*)@<!\*\*\*@!', '\v((\\|\*)@<!|(\\\*)@<=)\*\*@!', '\v(\\|`)@<!``@!']
19+
1820
var text_style_dict = {'`': code_regex,
1921
'*': italic_regex,
2022
'**': bold_regex,

lib/utils.vim

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ 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\)', '', ''))
20+
->join('|')->printf('\v(%s)')
21+
enddef
22+
1723
export def GetTextObject(textobject: string): string
1824
# You pass a text object like "iw" and it returns the text
1925
# associated to it.
@@ -323,26 +329,46 @@ export def GetDelimitersRanges(open_delimiter: string,
323329
return ranges
324330
enddef
325331

332+
export def IsGreater(l1: list<number>, l2: list<number>): bool
333+
# Compare two list of integers of size 2 based on the first element
334+
# return true if l1 > l2
335+
336+
if l1[0] > l2[0]
337+
return true
338+
elseif l1[0] == l2[0] && l1[1] > l2[1]
339+
return true
340+
else
341+
return false
342+
endif
343+
enddef
344+
345+
export def IsGreaterEqual(l1: list<number>, l2: list<number>): bool
346+
# Compare two list of integers of size 2 based on the first element
347+
# return true if l1 >= l2
348+
349+
if l1[0] > l2[0]
350+
return true
351+
elseif l1[0] == l2[0] && l1[1] > l2[1]
352+
return true
353+
elseif l1[0] == l2[0] && l1[1] == l2[1]
354+
return true
355+
else
356+
return false
357+
endif
358+
enddef
359+
326360
export def IsBetweenMarks(A: string, B: string): bool
327361
var cursor_pos = getpos(".")
328-
var A_pos = getcharpos(A)
329-
var B_pos = getcharpos(B)
330-
331-
# Convert in floats of the form "line.column" so the check reduces to a
332-
# comparison of floats.
333-
var lower_float = str2float($'{A_pos[1]}.{A_pos[2]}')
334-
var upper_float = str2float($'{B_pos[1]}.{B_pos[2]}')
335-
var cursor_pos_float =
336-
str2float($'{getcharpos(".")[1]}.{getcharpos(".")[2]}')
337-
338-
# In case the lower limit is larger than the higher limit, swap
339-
if upper_float < lower_float
340-
var tmp = upper_float
341-
upper_float = lower_float
342-
lower_float = tmp
362+
var A_pos = getcharpos(A)[1 : 2]
363+
var B_pos = getcharpos(B)[1 : 2]
364+
365+
if IsGreater(A_pos, B_pos)
366+
var tmp = B_pos
367+
B_pos = A_pos
368+
A_pos = tmp
343369
endif
344370

345-
return lower_float <= cursor_pos_float && cursor_pos_float <= upper_float
371+
return IsGreaterEqual(cursor_pos, A_pos) && IsGreaterEqual(B_pos, cursor_pos)
346372
enddef
347373

348374
export def IsInRange(open_delimiter: string,

test/test_utils.vim

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,24 @@ def g:Test_GetDelimitersRanges()
112112
Cleanup_markdown_testfile()
113113
enddef
114114

115+
def g:Test_ListComparison()
116+
var A = [5, 19]
117+
var B = [3, 43]
118+
assert_true(utils.IsGreater(A, B))
119+
assert_true(utils.IsGreaterEqual(A, B))
120+
121+
A = [3, 48]
122+
B = [3, 21]
123+
assert_true(utils.IsGreater(A, B))
124+
assert_true(utils.IsGreaterEqual(A, B))
125+
126+
# Equality
127+
A = [3, 48]
128+
B = [3, 48]
129+
assert_false(utils.IsGreater(A, B))
130+
assert_true(utils.IsGreaterEqual(A, B))
131+
enddef
132+
115133
def g:Test_IsBetweenMarks()
116134
Generate_markdown_testfile()
117135
exe $"edit {src_name}"
@@ -273,3 +291,14 @@ def g:Test_ZipList()
273291
assert_equal(expected_value, actual_value)
274292

275293
enddef
294+
295+
def g:Test_RegexList2RegexOr()
296+
var A = '\v(\d+|\a)\s'
297+
var B = '\v^\s*\w\d*\w'
298+
var C = '\v^\s*\w\d*\w'
299+
var test_list = [A, B, C]
300+
301+
var expected_string = '\v((\d+|\a)\s|^\s*\w\d*\w|^\s*\w\d*\w)'
302+
var actual_string = utils.RegexList2RegexOR(test_list)
303+
assert_equal(expected_string, actual_string)
304+
enddef

0 commit comments

Comments
 (0)