+277
-7
lines changedFilter options
+277
-7
lines changed Original file line number Diff line number Diff line change
@@ -105,15 +105,16 @@ local function align_end_position(line, byte, offset_encoding)
105
105
char = compute_line_length(line, offset_encoding) + 1
106
106
else
107
107
-- Modifying line, find the nearest utf codepoint
108
-
local offset = str_utf_end(line, byte)
108
+
local offset = str_utf_start(line, byte)
109
109
-- If the byte does not fall on the start of the character, then
110
110
-- align to the start of the next character.
111
-
if offset > 0 then
112
-
char = byte_to_utf(line, byte, offset_encoding) + 1
113
-
byte = byte + offset
114
-
else
111
+
if offset < 0 then
112
+
byte = byte + str_utf_end(line, byte) + 1
113
+
end
114
+
if byte <= #line then
115
115
char = byte_to_utf(line, byte, offset_encoding)
116
-
byte = byte + offset
116
+
else
117
+
char = compute_line_length(line, offset_encoding) + 1
117
118
end
118
119
-- Extending line, find the nearest utf codepoint for the last valid character
119
120
end
@@ -167,7 +168,7 @@ local function compute_start_range(prev_lines, curr_lines, firstline, lastline,
167
168
char_idx = compute_line_length(prev_line, offset_encoding) + 1
168
169
else
169
170
byte_idx = start_byte_idx + str_utf_start(prev_line, start_byte_idx)
170
-
char_idx = byte_to_utf(prev_line, start_byte_idx, offset_encoding)
171
+
char_idx = byte_to_utf(prev_line, byte_idx, offset_encoding)
171
172
end
172
173
173
174
-- Return the start difference (shared for new and prev lines)
Original file line number Diff line number Diff line change
@@ -164,6 +164,201 @@ describe('incremental synchronization', function()
164
164
}
165
165
test_edit({"a"}, {"rb"}, expected_text_changes, 'utf-16', '\n')
166
166
end)
167
+
it('deleting a line', function()
168
+
local expected_text_changes = {
169
+
{
170
+
range = {
171
+
['start'] = {
172
+
character = 0,
173
+
line = 0
174
+
},
175
+
['end'] = {
176
+
character = 0,
177
+
line = 1
178
+
}
179
+
},
180
+
rangeLength = 12,
181
+
text = ''
182
+
}
183
+
}
184
+
test_edit({"hello world"}, {"dd"}, expected_text_changes, 'utf-16', '\n')
185
+
end)
186
+
it('deleting an empty line', function()
187
+
local expected_text_changes = {
188
+
{
189
+
range = {
190
+
['start'] = {
191
+
character = 0,
192
+
line = 1
193
+
},
194
+
['end'] = {
195
+
character = 0,
196
+
line = 2
197
+
}
198
+
},
199
+
rangeLength = 1,
200
+
text = ''
201
+
}
202
+
}
203
+
test_edit({"hello world", ""}, {"jdd"}, expected_text_changes, 'utf-16', '\n')
204
+
end)
205
+
it('adding a line', function()
206
+
local expected_text_changes = {
207
+
{
208
+
range = {
209
+
['start'] = {
210
+
character = 0,
211
+
line = 1
212
+
},
213
+
['end'] = {
214
+
character = 0,
215
+
line = 1
216
+
}
217
+
},
218
+
rangeLength = 0,
219
+
text = 'hello world\n'
220
+
}
221
+
}
222
+
test_edit({"hello world"}, {"yyp"}, expected_text_changes, 'utf-16', '\n')
223
+
end)
224
+
it('adding an empty line', function()
225
+
local expected_text_changes = {
226
+
{
227
+
range = {
228
+
['start'] = {
229
+
character = 0,
230
+
line = 1
231
+
},
232
+
['end'] = {
233
+
character = 0,
234
+
line = 1
235
+
}
236
+
},
237
+
rangeLength = 0,
238
+
text = '\n'
239
+
}
240
+
}
241
+
test_edit({"hello world"}, {"o"}, expected_text_changes, 'utf-16', '\n')
242
+
end)
243
+
end)
244
+
describe('multi line edit', function()
245
+
it('deletion and insertion', function()
246
+
local expected_text_changes = {
247
+
-- delete "_fsda" from end of line 1
248
+
{
249
+
range = {
250
+
['start'] = {
251
+
character = 4,
252
+
line = 1
253
+
},
254
+
['end'] = {
255
+
character = 9,
256
+
line = 1
257
+
}
258
+
},
259
+
rangeLength = 5,
260
+
text = ''
261
+
},
262
+
-- delete "hello world\n" from line 2
263
+
{
264
+
range = {
265
+
['start'] = {
266
+
character = 0,
267
+
line = 2
268
+
},
269
+
['end'] = {
270
+
character = 0,
271
+
line = 3
272
+
}
273
+
},
274
+
rangeLength = 12,
275
+
text = ''
276
+
},
277
+
-- delete "1234" from beginning of line 2
278
+
{
279
+
range = {
280
+
['start'] = {
281
+
character = 0,
282
+
line = 2
283
+
},
284
+
['end'] = {
285
+
character = 4,
286
+
line = 2
287
+
}
288
+
},
289
+
rangeLength = 4,
290
+
text = ''
291
+
},
292
+
-- add " asdf" to end of line 1
293
+
{
294
+
range = {
295
+
['start'] = {
296
+
character = 4,
297
+
line = 1
298
+
},
299
+
['end'] = {
300
+
character = 4,
301
+
line = 1
302
+
}
303
+
},
304
+
rangeLength = 0,
305
+
text = ' asdf'
306
+
},
307
+
-- delete " asdf\n" from line 2
308
+
{
309
+
range = {
310
+
['start'] = {
311
+
character = 0,
312
+
line = 2
313
+
},
314
+
['end'] = {
315
+
character = 0,
316
+
line = 3
317
+
}
318
+
},
319
+
rangeLength = 6,
320
+
text = ''
321
+
},
322
+
-- undo entire deletion
323
+
{
324
+
range = {
325
+
['start'] = {
326
+
character = 4,
327
+
line = 1
328
+
},
329
+
['end'] = {
330
+
character = 9,
331
+
line = 1
332
+
}
333
+
},
334
+
rangeLength = 5,
335
+
text = "_fdsa\nhello world\n1234 asdf"
336
+
},
337
+
-- redo entire deletion
338
+
{
339
+
range = {
340
+
['start'] = {
341
+
character = 4,
342
+
line = 1
343
+
},
344
+
['end'] = {
345
+
character = 9,
346
+
line = 3
347
+
}
348
+
},
349
+
rangeLength = 27,
350
+
text = ' asdf'
351
+
},
352
+
}
353
+
local original_lines = {
354
+
"\\begin{document}",
355
+
"test_fdsa",
356
+
"hello world",
357
+
"1234 asdf",
358
+
"\\end{document}"
359
+
}
360
+
test_edit(original_lines, {"jf_vejjbhhdu<C-R>"}, expected_text_changes, 'utf-16', '\n')
361
+
end)
167
362
end)
168
363
169
364
describe('multi-operation edits', function()
@@ -297,6 +492,80 @@ describe('incremental synchronization', function()
297
492
}
298
493
test_edit({"🔥"}, {"x"}, expected_text_changes, 'utf-16', '\n')
299
494
end)
495
+
it('replacing a multibyte character with matching prefix', function()
496
+
local expected_text_changes = {
497
+
{
498
+
range = {
499
+
['start'] = {
500
+
character = 0,
501
+
line = 1
502
+
},
503
+
['end'] = {
504
+
character = 1,
505
+
line = 1
506
+
}
507
+
},
508
+
rangeLength = 1,
509
+
text = '⟩'
510
+
}
511
+
}
512
+
-- ⟨ is e29fa8, ⟩ is e29fa9
513
+
local original_lines = {
514
+
"\\begin{document}",
515
+
"⟨",
516
+
"\\end{document}",
517
+
}
518
+
test_edit(original_lines, {"jr⟩"}, expected_text_changes, 'utf-16', '\n')
519
+
end)
520
+
it('replacing a multibyte character with matching suffix', function()
521
+
local expected_text_changes = {
522
+
{
523
+
range = {
524
+
['start'] = {
525
+
character = 0,
526
+
line = 1
527
+
},
528
+
['end'] = {
529
+
character = 1,
530
+
line = 1
531
+
}
532
+
},
533
+
rangeLength = 1,
534
+
text = 'ḟ'
535
+
}
536
+
}
537
+
-- ฟ is e0b89f, ḟ is e1b89f
538
+
local original_lines = {
539
+
"\\begin{document}",
540
+
"ฟ",
541
+
"\\end{document}",
542
+
}
543
+
test_edit(original_lines, {"jrḟ"}, expected_text_changes, 'utf-16', '\n')
544
+
end)
545
+
it('inserting before a multibyte character', function()
546
+
local expected_text_changes = {
547
+
{
548
+
range = {
549
+
['start'] = {
550
+
character = 0,
551
+
line = 1
552
+
},
553
+
['end'] = {
554
+
character = 0,
555
+
line = 1
556
+
}
557
+
},
558
+
rangeLength = 0,
559
+
text = ' '
560
+
}
561
+
}
562
+
local original_lines = {
563
+
"\\begin{document}",
564
+
"→",
565
+
"\\end{document}",
566
+
}
567
+
test_edit(original_lines, {"ji "}, expected_text_changes, 'utf-16', '\n')
568
+
end)
300
569
it('deleting a multibyte character from a long line', function()
301
570
local expected_text_changes = {
302
571
{
You can’t perform that action at this time.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4