Skip to content

Commit ba8a367

Browse files
committed
All tests for lib/utils written
1 parent fc1c4df commit ba8a367

File tree

2 files changed

+26
-137
lines changed

2 files changed

+26
-137
lines changed

lib/utils.vim

Lines changed: 2 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -38,67 +38,6 @@ export def GetTextObject(textobject: string): dict<any>
3838
return text_object
3939
enddef
4040

41-
export def Surround(pre: string, post: string, text_object: string = '')
42-
# Usage:
43-
# Select text and hit <leader> + e.g. parenthesis
44-
#
45-
# Note that Visual Selections and Text Objects are cousins
46-
#
47-
var [line_start, column_start] = [-1, -1]
48-
var [line_end, column_end] = [-1, -1]
49-
if !empty(text_object)
50-
[line_start, column_start] = GetTextObject(text_object).start_pos[1 : 2]
51-
[line_end, column_end] = GetTextObject(text_object).end_pos[1 : 2]
52-
else
53-
# If no text_object is passed, then the selection is visual
54-
[line_start, column_start] = getpos("'<")[1 : 2]
55-
[line_end, column_end] = getpos("'>")[1 : 2]
56-
endif
57-
58-
var pre_len = strlen(pre)
59-
var post_len = strlen(post)
60-
if line_start > line_end
61-
var tmp = line_start
62-
line_start = line_end
63-
line_end = tmp
64-
65-
tmp = column_start
66-
column_start = column_end
67-
column_end = tmp
68-
endif
69-
if line_start == line_end && column_start > column_end
70-
var tmp = column_start
71-
column_start = column_end
72-
column_end = tmp
73-
endif
74-
var leading_chars = strcharpart(getline(line_start), column_start - 1 -
75-
pre_len, pre_len)
76-
var trailing_chars = strcharpart(getline(line_end), column_end, post_len)
77-
78-
cursor(line_start, column_start)
79-
var offset = 0
80-
if leading_chars == pre
81-
exe $"normal! {pre_len}X"
82-
offset = -pre_len
83-
else
84-
exe $"normal! i{pre}"
85-
offset = pre_len
86-
endif
87-
88-
# Some chars have been added if you are working on the same line
89-
if line_start == line_end
90-
cursor(line_end, column_end + offset)
91-
else
92-
cursor(line_end, column_end)
93-
endif
94-
95-
if trailing_chars == post
96-
exe $"normal! l{post_len}x"
97-
else
98-
exe $"normal! a{post}"
99-
endif
100-
enddef
101-
10241
export def FormatWithoutMoving(a: number = 0, b: number = 0)
10342
var view = winsaveview()
10443
if a == 0 && b == 0
@@ -123,33 +62,7 @@ export def FormatWithoutMoving(a: number = 0, b: number = 0)
12362
winrestview(view)
12463
enddef
12564

126-
export def RemoveSurrounding(A: string, B: string, lead: number, trail: number)
127-
# Remove 'lead' chars from before mark 'A and 'trail' chars after mark 'B
128-
if line(A) == line(B)
129-
var part1 = strcharpart(getline(A), 0, col(A) - lead - 1)
130-
var part2 = strcharpart(getline(A), col(A) - 1, col(B) - col(A) + 1)
131-
var part3 = strcharpart(getline(A), col(B) + trail)
132-
133-
echom part1
134-
echom part2
135-
echom part3
136-
137-
var new_line = part1 .. part2 .. part3
138-
setline(line(A), new_line)
139-
else
140-
var first_line = strcharpart(getline(A), 0, col(A) - lead - 1)
141-
.. strcharpart(getline(A), col(A) - 1)
142-
echom first_line
143-
setline(line(A), first_line)
144-
145-
var last_line = strcharpart(getline(B), 0, col(B) - trail - 1)
146-
.. strcharpart(getline(B), col(B) - 1)
147-
echom last_line
148-
setline(line(B), last_line)
149-
endif
150-
enddef
151-
152-
export def SurroundNew(open_delimiter: string,
65+
export def Surround(open_delimiter: string,
15366
close_delimiter: string,
15467
text_object: string = '',
15568
keep_even: bool = false)
@@ -345,49 +258,6 @@ export def GetTextBetweenMarks(A: string, B: string): list<string>
345258
endif
346259
enddef
347260

348-
export def InsertLinesAtMark(marker: string, lines: list<string>)
349-
# ----- NOT WORKING ----------------------
350-
var pos = getpos(marker) # Get (line, column) position of the marker
351-
var line_num = pos[1] # Line number
352-
var col = pos[2] # Column number
353-
354-
# Get the existing line at the marker
355-
var current_line = getline(line_num)
356-
357-
# If the input list is empty, do nothing
358-
if empty(lines)
359-
return
360-
endif
361-
362-
# If there's only one line in the list, insert it inline
363-
if len(lines) == 1
364-
var new_line = strcharpart(current_line, 0, col - 1) .. lines[0]
365-
.. strcharpart(current_line, col - 1)
366-
setline(line_num, new_line)
367-
else
368-
# Modify the first line (before the marker)
369-
var first_part = strcharpart(current_line, 0, col - 1)
370-
var last_part = strcharpart(current_line, col - 1)
371-
372-
# Construct the final text to insert
373-
var new_lines = [first_part .. lines[0]] + lines[1 : ] + [last_part]
374-
375-
# Insert the lines into the buffer
376-
# setline(line_num - 1, new_lines)
377-
append(line_num - 1, new_lines)
378-
endif
379-
enddef
380-
381-
# export def InsertInLine(marker: string, text: list<string>)
382-
# # Insert text in the given column
383-
# var line = getline(line(marker)) # Get the current line
384-
# var lnum = line(marker)
385-
# var column = col(marker)
386-
# var new_line = strcharpart(line, 0, column) .. text .. strcharpart(line, column)
387-
# setline(lnum, new_line) # Set the modified line back
388-
# enddef
389-
390-
391261
export def GetDelimitersRanges(open_delimiter: string,
392262
close_delimiter: string,
393263
open_delimiter_length_max: number = 2,
@@ -419,7 +289,6 @@ export def GetDelimitersRanges(open_delimiter: string,
419289
var close_delimiter_match = ''
420290

421291
while open_delimiter_pos_short != [0, 0]
422-
echom "open_delimiter: " .. open_delimiter
423292
open_delimiter_pos_short = searchpos(open_delimiter, 'W')
424293

425294
# If you pass a regex, you don't know how long is the captured string
@@ -524,7 +393,7 @@ export def IsInRange(open_delimiter: string,
524393
endif
525394
endfor
526395

527-
echom "interval: " .. string(interval)
396+
# echom "interval: " .. string(interval)
528397
# Restore marks 'a and 'b
529398
setcharpos("'a", saved_mark_a)
530399
setcharpos("'b", saved_mark_b)

test/test_utils.vim

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,6 @@ def g:Test_IsInRange()
165165
Cleanup_markdown_testfile()
166166
enddef
167167

168-
169-
170168
def g:Test_GetTextObject()
171169
Generate_markdown_testfile()
172170
exe $"edit {src_name}"
@@ -249,6 +247,28 @@ def g:Test_GetTextObject()
249247
# actual_value = utils.GetTextObject('i"')
250248
# AssertGetTextObject(expected_value, actual_value)
251249

252-
# :%bw!
253-
# Cleanup_markdown_testfile()
250+
:%bw!
251+
Cleanup_markdown_testfile()
252+
enddef
253+
254+
255+
def g:Test_DeleteTextBetweenMarks()
256+
Generate_markdown_testfile()
257+
exe $"edit {src_name}"
258+
259+
setcharpos("'A", [0, 9, 23, 0])
260+
setcharpos("'B", [0, 18, 39, 0])
261+
262+
var expected_value =
263+
['Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet,',
264+
'consectetur, adipisci dignissimos ducimus qui',
265+
'blanditiis praesentium voluptatum deleniti atque corrupti quos dolores~~']
266+
267+
utils.DeleteTextBetweenMarks("'A", "'B")
268+
var actual_value = getline(8, 10)
269+
270+
assert_equal(expected_value, actual_value)
271+
272+
:%bw!
273+
Cleanup_markdown_testfile()
254274
enddef

0 commit comments

Comments
 (0)