Skip to content

Commit a36a81b

Browse files
committed
Marks '[ and '] are saved/restored
1 parent 4f15e39 commit a36a81b

File tree

3 files changed

+58
-31
lines changed

3 files changed

+58
-31
lines changed

lib/utils.vim

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,24 +42,32 @@ export def RegexList2RegexOR(regex_list: list<string>,
4242
return result
4343
enddef
4444

45-
export def GetTextObject(textobject: string): string
45+
export def GetTextObject(textobject: string): dict<any>
4646
# You pass a text object like 'iw' and it returns the text
47-
# associated to it.
47+
# associated to it along with the start and end positions
4848
#
4949
# Note that when you yank some text, the registers '[' and ']' are set, so
5050
# after call this function, you can retrieve start and end position of the
5151
# text-object by looking at such marks.
5252

53-
# Backup the content of register t (arbitrary choice, YMMV)
53+
# Backup the content of register t (arbitrary choice, YMMV) and marks
5454
var oldreg = getreg("t")
55+
var saved_A = getcharpos("'[")
56+
var saved_B = getcharpos("']")
5557
# silently yank the text covered by whatever text object
56-
# was given as argument into register t
58+
# was given as argument into register t. Yank also set marks '[ and ']
5759
noautocmd execute 'silent normal "ty' .. textobject
58-
# save the content of register t into a variable
60+
5961
var text = getreg("t")
60-
# restore register t
62+
var start_pos = getcharpos("'[")
63+
var end_pos = getcharpos("']")
64+
65+
# restore register t and marks
6166
setreg("t", oldreg)
62-
return text
67+
setcharpos("'[", saved_A)
68+
setcharpos("']", saved_B)
69+
70+
return {text: text, start: start_pos, end: end_pos}
6371
enddef
6472

6573
export def FormatWithoutMoving(a: number = 0, b: number = 0)
@@ -137,17 +145,17 @@ def SurroundSmart(open_delimiter: string,
137145
var B = getcharpos("'>")
138146
if !empty(text_object)
139147
# GetTextObject is called for setting '[ and '] marks through a yank.
140-
GetTextObject(text_object)
141-
A = getcharpos("'[")
142-
B = getcharpos("']")
148+
var text_object_dict = GetTextObject(text_object)
149+
echom text_object
150+
A = text_object_dict.start
151+
B = text_object_dict.end
143152
endif
144153

145-
# marks -> (x,y) coordinates
146-
# line and column
154+
# line and column of point A
147155
var lA = A[1]
148156
var cA = A[2]
149157

150-
# line and column
158+
# line and column of point B
151159
var lB = B[1]
152160
var cB = B[2]
153161

test/test_markdown_extras.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ def g:Test_check_uncheck_todo_keybinding()
165165
enddef
166166

167167
def g:Test_Surround_one_line()
168-
# vnew
168+
vnew
169169
Generate_markdown_testfile()
170170
exe $"edit {src_name}"
171171

test/test_utils.vim

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -204,55 +204,75 @@ def g:Test_IsInRange()
204204
enddef
205205

206206
def g:Test_GetTextObject()
207+
vnew
207208
Generate_markdown_testfile()
208209
exe $"edit {src_name}"
209210

210211
# test 'iw'
211212
cursor(1, 8)
212-
var expected_value = 'perspiciatis'
213+
var expected_value = {text: 'perspiciatis',
214+
start: [0, 1, 8, 0],
215+
end: [0, 1, 19, 0],
216+
}
217+
213218
var actual_value = utils.GetTextObject('iw')
214219
assert_equal(expected_value, actual_value)
215220

216221
# test 'iW'
217222
actual_value = utils.GetTextObject('iW')
218223
assert_equal(expected_value, actual_value)
219224

220-
# test 'aw'
221-
expected_value = 'perspiciatis '
225+
# # test 'aw'
226+
expected_value = {text: 'perspiciatis ',
227+
start: [0, 1, 8, 0],
228+
end: [0, 1, 20, 0],
229+
}
222230
actual_value = utils.GetTextObject('aw')
223231
assert_equal(expected_value, actual_value)
224232

225-
# test 'aW'
233+
# # test 'aW'
226234
actual_value = utils.GetTextObject('aW')
227235
assert_equal(expected_value, actual_value)
228236

229-
# Test 'i('
237+
# # Test 'i('
230238
cursor(25, 33)
231-
expected_value = 'eligendi optio cumque nihil'
239+
expected_value = {text: 'eligendi optio cumque nihil',
240+
start: [0, 25, 32, 0],
241+
end: [0, 25, 58, 0],
242+
}
232243
actual_value = utils.GetTextObject('i(')
233244
assert_equal(expected_value, actual_value)
234245

235-
# Test 'yib'
246+
# # Test 'yib'
236247
actual_value = utils.GetTextObject('ib')
237248
assert_equal(expected_value, actual_value)
238249

239-
# Test 'a('
240-
expected_value = '(eligendi optio cumque nihil)'
250+
# # Test 'a('
251+
expected_value = {text: '(eligendi optio cumque nihil)',
252+
start: [0, 25, 31, 0],
253+
end: [0, 25, 59, 0],
254+
}
241255
actual_value = utils.GetTextObject('a(')
242256
assert_equal(expected_value, actual_value)
243257

244-
# Test 'ab'
258+
# # Test 'ab'
245259
actual_value = utils.GetTextObject('ab')
246260
assert_equal(expected_value, actual_value)
247261

248-
# Test 'i{'
262+
# # Test 'i{'
249263
cursor(28, 25)
250-
expected_value = 'rerum necessitatibus'
264+
expected_value = {text: 'rerum necessitatibus',
265+
start: [0, 28, 23, 0],
266+
end: [0, 28, 42, 0],
267+
}
251268
actual_value = utils.GetTextObject('i{')
252269
assert_equal(expected_value, actual_value)
253270

254-
# Test 'a{'
255-
expected_value = '{rerum necessitatibus}'
271+
# # Test 'a{'
272+
expected_value = {text: '{rerum necessitatibus}',
273+
start: [0, 28, 22, 0],
274+
end: [0, 28, 43, 0],
275+
}
256276
actual_value = utils.GetTextObject('a{')
257277
assert_equal(expected_value, actual_value)
258278

@@ -266,11 +286,10 @@ def g:Test_GetTextObject()
266286
# actual_value = utils.GetTextObject('i"')
267287
# AssertGetTextObject(expected_value, actual_value)
268288

269-
:%bw!
270-
Cleanup_markdown_testfile()
289+
# :%bw!
290+
# Cleanup_markdown_testfile()
271291
enddef
272292

273-
274293
def g:Test_DeleteTextBetweenMarks()
275294
Generate_markdown_testfile()
276295
exe $"edit {src_name}"

0 commit comments

Comments
 (0)