Skip to content

Commit a53af27

Browse files
committed
Updated tests and bugfix
1 parent 4674fda commit a53af27

File tree

5 files changed

+98
-40
lines changed

5 files changed

+98
-40
lines changed

lib/utils.vim

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ export def SurroundNew(open_delimiter: string, close_delimiter: string, text_obj
159159
#
160160
# Note that Visual Selections and Text Objects are cousins
161161
#
162-
if !empty(CursorDelimitersInterval(open_delimiter, close_delimiter))
162+
if !empty(IsInRange(open_delimiter, close_delimiter))
163163
RemoveSurrounding(open_delimiter, close_delimiter)
164164
else
165165
# Set marks
@@ -215,7 +215,7 @@ export def SurroundNew(open_delimiter: string, close_delimiter: string, text_obj
215215
# Check if A falls in an existing interval
216216
cursor(xA, yA)
217217
for delimiterg in ZipLists(open_delimiters, close_delimiters)
218-
found_delimiters_interval = CursorDelimitersInterval(delimiterg[0], delimiterg[1])
218+
found_delimiters_interval = IsInRange(delimiterg[0], delimiterg[1])
219219
if !empty(found_delimiters_interval)
220220
old_right_delimiter = delimiterg[0]
221221
# Existing blocks shall be disjoint,
@@ -234,7 +234,7 @@ export def SurroundNew(open_delimiter: string, close_delimiter: string, text_obj
234234
# Check if also B falls in an existing interval
235235
cursor(xB, yB)
236236
for delimiterg in ZipLists(open_delimiters, close_delimiters)
237-
found_delimiters_interval = CursorDelimitersInterval(delimiterg[0], delimiterg[1])
237+
found_delimiters_interval = IsInRange(delimiterg[0], delimiterg[1])
238238
if !empty(found_delimiters_interval)
239239
old_left_delimiter = delimiterg[0]
240240
break
@@ -419,11 +419,12 @@ enddef
419419

420420
export def GetDelimitersRanges(open_delimiter: string,
421421
close_delimiter: string,
422-
open_delimiter_length_max: number = 5,
423-
close_delimiter_length_max: number = 5
422+
open_delimiter_length_max: number = 2,
423+
close_delimiter_length_max: number = 2
424424
): list<list<list<number>>>
425425
# It returns open-intervals, i.e. the delimiters are excluded
426-
# If there is a spare delimiter, it won't be considered
426+
# If there is a spare delimiter, it won't be considered. Delimiters are
427+
# regex
427428
#
428429
# TODO: It is assumed that the ranges have no intersections. Note that this
429430
# won't happen if open_delimiter = close_delimiter, as in many languages.
@@ -447,6 +448,7 @@ export def GetDelimitersRanges(open_delimiter: string,
447448
var close_delimiter_match = ''
448449

449450
while open_delimiter_pos_short != [0, 0]
451+
echom "open_delimiter: " .. open_delimiter
450452
open_delimiter_pos_short = searchpos(open_delimiter, 'W')
451453

452454
# If you pass a regex, you don't know how long is the captured string
@@ -456,7 +458,7 @@ export def GetDelimitersRanges(open_delimiter: string,
456458
->matchstr(open_delimiter)
457459
open_delimiter_length = len(open_delimiter_match)
458460

459-
if getline(open_delimiter_pos_short[0]) =~ $'{open_delimiter}$'
461+
if open_delimiter_pos_short[1] + open_delimiter_length == col('$')
460462
# If the open delimiter is the tail of the line, then the open-interval starts from
461463
# the next line, column 1
462464
open_delimiter_pos_short_final[0] = open_delimiter_pos_short[0] + 1
@@ -468,8 +470,8 @@ export def GetDelimitersRanges(open_delimiter: string,
468470
endif
469471
open_delimiter_pos = [0] + open_delimiter_pos_short_final + [0]
470472

473+
# Close delimiter
471474
close_delimiter_pos_short = searchpos(close_delimiter, 'W')
472-
473475
# If you pass a regex, you don't know how long is the captured string
474476
close_delimiter_match = strcharpart(
475477
getline(close_delimiter_pos_short[0]),
@@ -479,7 +481,7 @@ export def GetDelimitersRanges(open_delimiter: string,
479481

480482
# If the closed delimiter is the lead of the line, then the open-interval starts from
481483
# the previous line, last column
482-
if getline(close_delimiter_pos_short[0]) =~ $'^{close_delimiter}'
484+
if close_delimiter_pos_short[1] - 1 == 0
483485
close_delimiter_pos_short_final[0] = close_delimiter_pos_short[0] - 1
484486
close_delimiter_pos_short_final[1] = len(getline(close_delimiter_pos_short_final[0]))
485487
else
@@ -512,9 +514,9 @@ export def IsBetweenMarks(A: string, B: string): bool
512514
var cursor_pos_float = str2float($'{getcharpos(".")[1]}.{getcharpos(".")[2]}')
513515

514516
# Debugging
515-
echom "cur_pos: " .. cursor_pos_float
516-
echom "a: " .. string(lower_float)
517-
echom "b: " .. string(upper_float)
517+
# echom "cur_pos: " .. cursor_pos_float
518+
# echom "a: " .. string(lower_float)
519+
# echom "b: " .. string(upper_float)
518520

519521
# In case the lower limit is larger than the higher limit, swap
520522
if upper_float < lower_float
@@ -527,31 +529,33 @@ export def IsBetweenMarks(A: string, B: string): bool
527529

528530
enddef
529531

530-
export def CursorDelimitersInterval(open_delimiter: string, close_delimiter: string): list<list<number>>
532+
export def IsInRange(open_delimiter: string, close_delimiter: string): list<list<number>>
531533
# Return the range of the delimiters if the cursor is within such a range,
532534
# otherwise return an empty list.
535+
# Arguments must be regex.
533536
var interval = []
534537

535538
# OBS! Ranges are open-intervals!
536-
var ranges = g:GetBlocksRangesNew(open_delimiter, close_delimiter)
539+
var ranges = GetDelimitersRanges(open_delimiter, close_delimiter)
537540

538541
var saved_mark_a = getcharpos("'a")
539542
var saved_mark_b = getcharpos("'b")
540543

541544
for range in ranges
542-
var A = setcharpos("'a", range[0])
543-
var B = setcharpos("'b", range[1])
545+
setcharpos("'a", range[0])
546+
setcharpos("'b", range[1])
544547
if IsBetweenMarks("'a", "'b")
545-
interval = [A, B]
548+
interval = [range[0], range[1]]
546549
break
547550
endif
548551
endfor
549552

553+
echom "interval: " .. string(interval)
550554
# Restore marks 'a and 'b
551555
setcharpos("'a", saved_mark_a)
552556
setcharpos("'b", saved_mark_b)
553557

554-
return is_inside_block
558+
return interval
555559
enddef
556560

557561
export def DeleteTextBetweenMarks(A: string, B: string): string

test/results.txt

Lines changed: 0 additions & 12 deletions
This file was deleted.

test/run_tests.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ eval $VIM_CMD " -c \"vim9cmd g:TestName = [$TESTS_STRING]\" -S runner.vim"
4747

4848
# Check that Vim started and that the runner did its job
4949
if [ $? -eq 0 ]; then
50-
echo "\nVim executed successfully."
50+
echo "Vim executed successfully.\n"
5151
else
52-
echo "\nVim execution failed with exit code $?."
52+
echo "Vim execution failed with exit code $?.\n"
5353
exit 1
5454
fi
5555

test/runner.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def RunTests(test_file: string)
2424
->sort()
2525

2626
# Check is user defined some function name erroneously
27-
var wrong_test_functions = copy(all_functions)->filter('v:val !~ "g:Test_"')
27+
var wrong_test_functions = copy(all_functions)->filter('v:val !~ "Test_"')
2828
echom "wrong_functions: " .. string(wrong_test_functions)
2929
if !empty(wrong_test_functions)
3030
writefile([$'WARNING: The following tests are skipped: {wrong_test_functions}'], 'results.txt', 'a')

test/test_utils.vim

Lines changed: 73 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import "./common.vim"
77
import "../lib/utils.vim"
88
var WaitForAssert = common.WaitForAssert
99

10+
var code_regex = '\(^\|[^`]\)\zs`\ze\([^`]\|$\)'
11+
var italic_regex = '\(^\|[^\*]\)\zs\*\ze\([^\*]\|$\)'
12+
var bold_regex = '\(^\|[^\*]\)\zs\*\*\ze\([^\*]\|$\)'
13+
var strikethrough_regex = '\(^\|[^\~]\)\zs\~\~\ze\([^\~]\|$\)'
1014

1115
var src_name = 'testfile.md'
1216

@@ -23,11 +27,17 @@ def Generate_markdown_testfile()
2327
consectetur, adipisci velit, `sed quia non numquam eius modi tempora
2428
incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut
2529
enim ad minima veniam, quis nostrum exercitationem ullam corporis
26-
suscipit laboriosam`, nisi ut aliquid ex ea commodi consequatur?
30+
suscipit laboriosam`, nisi ut ~~aliquid ex ea commodi consequatur?~~
2731

2832
Quis autem vel eum **iure reprehenderit qui in ea voluptate velit esse
2933
quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo
3034
voluptas nulla** pariatur?
35+
36+
At vero eos et accusamus et iusto odio dignissimos ducimus qui
37+
blanditiis praesentium voluptatum deleniti atque corrupti quos dolores~~
38+
et quas molestias excepturi sint~~ occaecati cupiditate non provident,
39+
similique sunt in culpa qui officia ~~deserunt mollitia animi, id est
40+
~~laborum et dolorum fuga.
3141
END
3242
writefile(lines, src_name)
3343
enddef
@@ -62,22 +72,78 @@ def g:Test_GetDelimitersRanges()
6272

6373
exe $"edit {src_name}"
6474

65-
var code_regex = '\(^\|[^`]\)\zs`\ze\([^`]\|$\)'
6675
var expected_ranges = [[[0, 9, 31, 0], [0, 12, 19, 0]]]
6776
var actual_ranges = utils.GetDelimitersRanges(code_regex, code_regex)
68-
assert_true(expected_ranges == actual_ranges)
77+
assert_equal(expected_ranges, actual_ranges)
6978

70-
var italic_regex = '\(^\|[^\*]\)\zs\*\ze\([^\*]\|$\)'
7179
expected_ranges = [[[0, 3, 20, 0], [0, 3, 28, 0]],
7280
[[0, 4, 18, 0], [0, 5, 29, 0]]]
7381
actual_ranges = utils.GetDelimitersRanges(italic_regex, italic_regex)
74-
assert_true(expected_ranges == actual_ranges)
82+
assert_equal(expected_ranges, actual_ranges)
7583

76-
var bold_regex = '\(^\|[^\*]\)\zs\*\*\ze\([^\*]\|$\)'
7784
expected_ranges = [[[0, 1, 23, 0], [0, 1, 37, 0]],
7885
[[0, 14, 22, 0], [0, 16, 14, 0]]]
7986
actual_ranges = utils.GetDelimitersRanges(bold_regex, bold_regex)
80-
assert_true(expected_ranges == actual_ranges)
87+
assert_equal(expected_ranges, actual_ranges)
88+
89+
expected_ranges = [[[0, 12, 33, 0], [0, 12, 66, 0]],
90+
[[0, 20, 1, 0], [0, 20, 32, 0]],
91+
[[0, 21, 39, 0], [0, 21, 69, 0]]]
92+
actual_ranges = utils.GetDelimitersRanges(strikethrough_regex, strikethrough_regex)
93+
assert_equal(expected_ranges, actual_ranges)
94+
95+
:%bw!
96+
Cleanup_markdown_testfile()
97+
enddef
98+
99+
def g:Test_IsBetweenMarks()
100+
Generate_markdown_testfile()
101+
exe $"edit {src_name}"
102+
103+
setpos("'A", [4, 23])
104+
setpos("'B", [9, 11])
105+
106+
cursor(2, 15)
107+
assert_false(utils.IsBetweenMarks("'A", "'B"))
108+
109+
cursor(8, 3)
110+
assert_true(utils.IsBetweenMarks("'A", "'B"))
111+
112+
:%bw!
113+
Cleanup_markdown_testfile()
114+
enddef
115+
116+
def g:Test_IsInRange()
117+
Generate_markdown_testfile()
118+
exe $"edit {src_name}"
119+
120+
cursor(5, 18)
121+
var expected_value = [[0, 4, 18, 0], [0, 5, 29, 0]]
122+
var range = utils.IsInRange(italic_regex, italic_regex)
123+
assert_equal(expected_value, range)
124+
125+
range = utils.IsInRange(bold_regex, bold_regex)
126+
assert_true(empty(range))
127+
128+
range = utils.IsInRange(code_regex, code_regex)
129+
assert_true(empty(range))
130+
131+
range = utils.IsInRange(strikethrough_regex, strikethrough_regex)
132+
assert_true(empty(range))
133+
134+
# Test singularity: cursor on a delimiter
135+
cursor(14, 21)
136+
range = utils.IsInRange(italic_regex, italic_regex)
137+
assert_true(empty(range))
138+
139+
range = utils.IsInRange(bold_regex, bold_regex)
140+
assert_true(empty(range))
141+
142+
range = utils.IsInRange(code_regex, code_regex)
143+
assert_true(empty(range))
144+
145+
range = utils.IsInRange(strikethrough_regex, strikethrough_regex)
146+
assert_true(empty(range))
81147

82148
:%bw!
83149
Cleanup_markdown_testfile()

0 commit comments

Comments
 (0)