Skip to content

Commit 324690d

Browse files
committed
Added SurroundSimple
1 parent a0104ba commit 324690d

File tree

3 files changed

+109
-3
lines changed

3 files changed

+109
-3
lines changed

lib/utils.vim

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,64 @@ export def RemoveSurrounding(
127127
endif
128128
enddef
129129

130+
export def SurroundSimple(open_delimiter: string,
131+
close_delimiter: string,
132+
open_delimiters_dict: dict<string>,
133+
close_delimiters_dict: dict<string>,
134+
text_object: string = '')
135+
136+
var open_string = open_delimiter
137+
var open_regex = open_delimiters_dict[open_string]
138+
var open_delimiter_dict = {open_string: open_regex}
139+
140+
var close_string = close_delimiter
141+
var close_regex = close_delimiters_dict[close_string]
142+
var close_delimiter_dict = {close_string: close_regex}
143+
# Set marks
144+
var A = getcharpos("'<")
145+
var B = getcharpos("'>")
146+
if !empty(text_object)
147+
# GetTextObject is called for setting '[ and '] marks through a yank.
148+
var text_object_dict = GetTextObject(text_object)
149+
echom text_object
150+
A = text_object_dict.start
151+
B = text_object_dict.end
152+
endif
153+
154+
# line and column of point A
155+
var lA = A[1]
156+
var cA = A[2]
157+
158+
# line and column of point B
159+
var lB = B[1]
160+
var cB = B[2]
161+
162+
if A == B
163+
return
164+
endif
165+
166+
var toA = strcharpart(getline(lA), 0, cA - 1) .. open_string
167+
var fromB = close_string .. strcharpart(getline(lB), cB)
168+
169+
# If on the same line
170+
if lA == lB
171+
# Overwrite everything that is in the middle
172+
var A_to_B = strcharpart(getline(lA), cA - 1, cB - cA + 1)
173+
setline(lA, toA .. A_to_B .. fromB)
174+
else
175+
var lineA = toA .. strcharpart(getline(lA), cA - 1)
176+
setline(lA, lineA)
177+
var lineB = strcharpart(getline(lB), 0, cB - 1) .. fromB
178+
setline(lB, lineB)
179+
var l = 1
180+
# Fix intermediate lines
181+
while lA + l < lB
182+
setline(lA + l, getline(lA + l))
183+
l += 1
184+
endwhile
185+
endif
186+
enddef
187+
130188
export def SurroundSmart(open_delimiter: string,
131189
close_delimiter: string,
132190
open_delimiters_dict: dict<string>,

test/test_markdown_extras.vim

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ def g:Test_markdown_lists()
148148
Cleanup_testfile(src_name_1)
149149
enddef
150150

151-
152151
def g:Test_check_uncheck_todo_keybinding()
153152

154153
Generate_testfile(lines_1, src_name_1)

test/test_utils.vim

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ def g:Test_Dict2ListOfDicts()
398398

399399
enddef
400400

401-
def g:Test_Surround_one_line()
401+
def g:Test_SurroundSmart_one_line()
402402
vnew
403403
Generate_testfile(lines_2, src_name_2)
404404
exe $"edit {src_name_2}"
@@ -430,7 +430,56 @@ def g:Test_Surround_one_line()
430430
Cleanup_testfile(src_name_2)
431431
enddef
432432

433-
def g:Test_Surround_one_line_smart_delimiters()
433+
def g:Test_SurroundSimple_one_line_smart_delimiters()
434+
vnew
435+
Generate_testfile(lines_2, src_name_2)
436+
exe $"edit {src_name_2}"
437+
setlocal conceallevel=0
438+
439+
# Smart delimiters
440+
var expected_value = [
441+
'incidunt ut (labore et ~~dolore magnam) aliquam quaerat~~ voluptatem. Ut',
442+
'enim ad `minima *[veniam`, quis no~~strum]* exercitationem~~ ullam corporis',
443+
'suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur?'
444+
]
445+
cursor(11, 29)
446+
exe "norm! va[\<esc>"
447+
utils.SurroundSimple('*', '*', text_style_dict, text_style_dict)
448+
var actual_value = getline(10, 12)
449+
echom assert_equal(expected_value, actual_value)
450+
451+
# # Test with junk between A and B. Overwrite everything and avoid consecutive
452+
# # delimiters of same type, like ** **
453+
# cursor(21, 41)
454+
# exe "norm! va(\<esc>"
455+
# expected_value = [
456+
# 'dolores et quas molestias excepturi sint, obcaecati cupiditate non',
457+
# 'pro**vident, (similique sunt in culpa, qui officia deserunt)**',
458+
# 'mollitia) animi, id est laborum et dolorum fuga.'
459+
# ]
460+
# utils.SurroundSimple('**', '**', text_style_dict, text_style_dict)
461+
# actual_value = getline(20, 22)
462+
# assert_equal(expected_value, actual_value)
463+
464+
# # Test with junk between A and B. Overwrite everything and avoid consecutive
465+
# # delimiters of same type, like ** **
466+
# cursor(19, 20)
467+
# exe "norm! va(\<esc>"
468+
# expected_value = [
469+
# 'At vero eos et accusamus et iusto odio dignissimos ducimus, qui',
470+
# 'blandit*iis pra(esentium voluptatum deleniti atque) corrupti*, quos',
471+
# 'dolores et quas molestias excepturi sint, obcaecati cupiditate non',
472+
# ]
473+
# utils.SurroundSimple('*', '*', text_style_dict, text_style_dict)
474+
# actual_value = getline(18, 20)
475+
# assert_equal(expected_value, actual_value)
476+
477+
# :%bw!
478+
# Cleanup_testfile(src_name_2)
479+
enddef
480+
481+
482+
def g:Test_SurroundSmart_one_line_smart_delimiters()
434483
# vnew
435484
Generate_testfile(lines_2, src_name_2)
436485
exe $"edit {src_name_2}"

0 commit comments

Comments
 (0)