forked from leohenon/opencode-vim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvim-motions.ts
More file actions
873 lines (764 loc) · 29.6 KB
/
vim-motions.ts
File metadata and controls
873 lines (764 loc) · 29.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
import type { TextareaRenderable } from "@opentui/core"
import type { VimRegister } from "./vim-state"
export type VimSpan = { start: number; end: number }
export type VimCopyRow = { col: number }
export type VimWantedColumn = number | "end"
function lineStart(text: string, offset: number) {
if (offset <= 0) return 0
const index = text.lastIndexOf("\n", offset - 1)
if (index === -1) return 0
return index + 1
}
function lineEnd(text: string, offset: number) {
const index = text.indexOf("\n", offset)
if (index === -1) return text.length
return index
}
function lineLast(text: string, offset: number) {
const start = lineStart(text, offset)
const end = lineEnd(text, offset)
if (end > start) return end - 1
return start
}
function prevLineStart(text: string, offset: number) {
const start = lineStart(text, offset)
if (start === 0) return undefined
return lineStart(text, start - 1)
}
function nextLineStart(text: string, offset: number) {
const end = lineEnd(text, offset)
if (end >= text.length) return undefined
return end + 1
}
function isBlankLine(text: string, lineStartOffset: number) {
return lineEnd(text, lineStartOffset) === lineStartOffset
}
// vim treats a trailing \n as EOL of the last line, not a new empty line.
// Differs from nextLineStart on "abc\n": this returns null, that returns 4.
function paragraphAdvance(text: string, lineOffset: number): number | null {
const end = lineEnd(text, lineOffset)
if (end >= text.length - 1) return null
return end + 1
}
function paragraphRetreat(text: string, lineOffset: number): number | null {
if (lineOffset <= 0) return null
return lineStart(text, lineOffset - 1)
}
function nextParagraphTarget(text: string, cursor: number): number {
if (text.length === 0) return 0
let probe = lineStart(text, cursor)
while (isBlankLine(text, probe)) {
const next = paragraphAdvance(text, probe)
if (next === null) return probe
probe = next
}
while (!isBlankLine(text, probe)) {
const next = paragraphAdvance(text, probe)
if (next === null) return lineLast(text, text.length - 1)
probe = next
}
return probe
}
function previousParagraphTarget(text: string, cursor: number): number {
if (text.length === 0 || cursor === 0) return 0
let probe = lineStart(text, cursor)
while (isBlankLine(text, probe)) {
const previous = paragraphRetreat(text, probe)
if (previous === null) return 0
probe = previous
}
while (probe > 0) {
const previous = paragraphRetreat(text, probe)
if (previous === null) return 0
if (isBlankLine(text, previous)) return previous
probe = previous
}
return 0
}
function lineColumn(text: string, offset: number) {
return offset - lineStart(text, offset)
}
function moveUp(text: string, offset: number, column: VimWantedColumn = lineColumn(text, offset)) {
const targetStart = prevLineStart(text, offset)
if (targetStart === undefined) return offset
const targetLast = lineLast(text, targetStart)
const col = column === "end" ? targetLast - targetStart : column
return Math.min(targetStart + col, targetLast)
}
function moveDown(text: string, offset: number, column: VimWantedColumn = lineColumn(text, offset)) {
const targetStart = nextLineStart(text, offset)
if (targetStart === undefined) return offset
const targetLast = lineLast(text, targetStart)
const col = column === "end" ? targetLast - targetStart : column
return Math.min(targetStart + col, targetLast)
}
export function getLineColumn(textarea: TextareaRenderable) {
return lineColumn(textarea.plainText, textarea.cursorOffset)
}
export function moveLeft(textarea: TextareaRenderable) {
const text = textarea.plainText
const start = lineStart(text, textarea.cursorOffset)
textarea.cursorOffset = Math.max(start, textarea.cursorOffset - 1)
}
export function moveLineBeginning(textarea: TextareaRenderable) {
const text = textarea.plainText
textarea.cursorOffset = lineStart(text, textarea.cursorOffset)
}
export function moveFirstNonWhitespace(textarea: TextareaRenderable) {
const text = textarea.plainText
textarea.cursorOffset = firstNonWhitespace(text, textarea.cursorOffset)
}
export function moveLineEnd(textarea: TextareaRenderable) {
const text = textarea.plainText
textarea.cursorOffset = lineLast(text, textarea.cursorOffset)
}
export function clampCursorToLine(textarea: TextareaRenderable) {
const text = textarea.plainText
const last = lineLast(text, textarea.cursorOffset)
if (textarea.cursorOffset > last) textarea.cursorOffset = last
}
export function moveRight(textarea: TextareaRenderable) {
const text = textarea.plainText
const last = lineLast(text, textarea.cursorOffset)
textarea.cursorOffset = Math.min(last, textarea.cursorOffset + 1)
}
export function moveLineUp(textarea: TextareaRenderable, column?: VimWantedColumn) {
const text = textarea.plainText
textarea.cursorOffset = moveUp(text, textarea.cursorOffset, column)
}
export function moveLineDown(textarea: TextareaRenderable, column?: VimWantedColumn) {
const text = textarea.plainText
textarea.cursorOffset = moveDown(text, textarea.cursorOffset, column)
}
export function movePreviousParagraph(textarea: TextareaRenderable) {
textarea.cursorOffset = previousParagraphTarget(textarea.plainText, textarea.cursorOffset)
}
export function moveNextParagraph(textarea: TextareaRenderable) {
textarea.cursorOffset = nextParagraphTarget(textarea.plainText, textarea.cursorOffset)
}
export type ParagraphOperation = "d" | "c" | "y"
export type ParagraphResult = {
span: VimSpan | null
register: VimRegister
}
// vim linewise register convention: content ends with \n per line terminator.
function asLinewise(slice: string): string {
return slice.endsWith("\n") ? slice : slice + "\n"
}
function buildParagraphResult(
text: string,
span: VimSpan | null,
registerSpan: VimSpan | null,
linewise: boolean,
): ParagraphResult {
if (!span) return { span: null, register: null }
const register = registerSpan ?? span
const slice = text.slice(register.start, register.end)
return { span, register: { text: linewise ? asLinewise(slice) : slice, linewise } }
}
type NextClassification = {
lineStartOffset: number
onBlank: boolean
lineAligned: boolean
target: number
targetIsBlank: boolean
multiLine: boolean
}
function classifyNextParagraph(text: string, cursor: number): NextClassification {
const lineStartOffset = lineStart(text, cursor)
const onBlank = isBlankLine(text, lineStartOffset)
const target = nextParagraphTarget(text, cursor)
const targetLineStart = lineStart(text, target)
return {
lineStartOffset,
onBlank,
lineAligned: onBlank || cursor === lineStartOffset,
target,
targetIsBlank: target === targetLineStart && target < text.length && isBlankLine(text, target),
multiLine: lineStartOffset !== targetLineStart,
}
}
// linewise rules derived empirically from nvim:
// d: line-aligned cursor + (blank target OR motion crosses lines)
// y/c: line-aligned cursor AND blank target
function isLinewiseNext(c: NextClassification, op: ParagraphOperation): boolean {
if (!c.lineAligned) return false
return op === "d" ? c.targetIsBlank || c.multiLine : c.targetIsBlank
}
// d at EOF with no trailing \n extends the delete span back to swallow the
// preceding \n separator, but keeps the register range tight.
// c strips the trailing \n that d/y keep (preserves line structure).
function nextLinewiseSpan(
text: string,
c: NextClassification,
op: ParagraphOperation,
): { span: VimSpan | null; registerSpan: VimSpan | null } {
if (op === "d" && !c.targetIsBlank) {
const extendBack = text[text.length - 1] !== "\n" && c.lineStartOffset > 0
return {
span: { start: extendBack ? c.lineStartOffset - 1 : c.lineStartOffset, end: text.length },
registerSpan: { start: c.lineStartOffset, end: text.length },
}
}
const end = op === "c" ? c.target - 1 : c.target
if (end <= c.lineStartOffset) return { span: null, registerSpan: null }
return { span: { start: c.lineStartOffset, end }, registerSpan: null }
}
// onBlank branch is only reached by y/c from a blank line with a non-blank
// target (d-from-blank is always linewise via the multi-line rule).
function nextCharwiseSpan(text: string, cursor: number, c: NextClassification): VimSpan | null {
const end = c.targetIsBlank ? c.target - 1 : c.onBlank ? text.length : c.target + 1
if (end <= cursor) return null
return { start: cursor, end }
}
export function nextParagraphOperation(textarea: TextareaRenderable, operation: ParagraphOperation): ParagraphResult {
const text = textarea.plainText
const cursor = textarea.cursorOffset
if (text.length === 0) return { span: null, register: null }
const c = classifyNextParagraph(text, cursor)
if (!isLinewiseNext(c, operation)) return buildParagraphResult(text, nextCharwiseSpan(text, cursor, c), null, false)
const { span, registerSpan } = nextLinewiseSpan(text, c, operation)
return buildParagraphResult(text, span, registerSpan, true)
}
// vim `{` operator. linewise for all of y/d/c when cursor is line-aligned.
// c strips the trailing \n at cursor-1; d/y keep it.
export function previousParagraphOperation(
textarea: TextareaRenderable,
operation: ParagraphOperation,
): ParagraphResult {
const text = textarea.plainText
const cursor = textarea.cursorOffset
if (text.length === 0 || cursor === 0) return { span: null, register: null }
const lineStartOffset = lineStart(text, cursor)
const linewise = isBlankLine(text, lineStartOffset) || cursor === lineStartOffset
const target = previousParagraphTarget(text, cursor)
if (target >= cursor) return { span: null, register: null }
if (!linewise || operation !== "c") return buildParagraphResult(text, { start: target, end: cursor }, null, linewise)
const end = text[cursor - 1] === "\n" ? cursor - 1 : cursor
return buildParagraphResult(text, end > target ? { start: target, end } : null, null, true)
}
export function isWord(char: string) {
return /[A-Za-z0-9_]/.test(char)
}
export function isBigWord(char: string) {
return !/\s/.test(char)
}
export function nextWordStart(text: string, offset: number, big: boolean) {
let pos = offset
if (pos >= text.length) return text.length
const startClass = wordClass(text[pos], big)
if (startClass !== "blank") {
while (pos < text.length && wordClass(text[pos], big) === startClass) pos++
}
while (pos < text.length && wordClass(text[pos], big) === "blank") pos++
return pos
}
export function prevWordStart(text: string, offset: number, big: boolean) {
let pos = Math.min(offset, text.length)
if (pos <= 0) return 0
pos--
while (pos > 0 && wordClass(text[pos], big) === "blank") pos--
const target = wordClass(text[pos], big)
while (pos > 0 && wordClass(text[pos - 1], big) === target) pos--
return pos
}
function wordClass(char: string, big: boolean): "blank" | "word" | "punct" {
if (!isBigWord(char)) return "blank"
if (big || isWord(char)) return "word"
return "punct"
}
function wordRunEnd(text: string, offset: number, big: boolean) {
const target = wordClass(text[offset], big)
let pos = offset
while (pos + 1 < text.length && wordClass(text[pos + 1], big) === target) pos++
return pos
}
export function wordEnd(text: string, offset: number, big: boolean) {
if (text.length === 0) return 0
let pos = offset
if (pos >= text.length) pos = text.length - 1
const startClass = wordClass(text[pos], big)
const atRunEnd = startClass === "blank" || pos + 1 >= text.length || wordClass(text[pos + 1], big) !== startClass
if (atRunEnd) {
pos++
while (pos < text.length && wordClass(text[pos], big) === "blank") pos++
if (pos >= text.length) return text.length - 1
}
return wordRunEnd(text, pos, big)
}
function deleteOffsets(textarea: TextareaRenderable, startOffset: number, endOffset: number) {
if (endOffset <= startOffset) return
const end = Math.min(endOffset, textarea.plainText.length)
if (end <= startOffset) return
const start = textarea.editBuffer.offsetToPosition(startOffset)
const pos = textarea.editBuffer.offsetToPosition(end)
if (!start || !pos) return
textarea.deleteRange(start.row, start.col, pos.row, pos.col)
textarea.cursorOffset = startOffset
}
function swap(char: string) {
const low = char.toLowerCase()
const up = char.toUpperCase()
if (char === low && char !== up) return up
if (char === up && char !== low) return low
return char
}
export function moveWordNext(textarea: TextareaRenderable) {
const text = textarea.plainText
textarea.cursorOffset = nextWordStart(text, textarea.cursorOffset, false)
}
export function moveWordPrev(textarea: TextareaRenderable) {
const text = textarea.plainText
textarea.cursorOffset = prevWordStart(text, textarea.cursorOffset, false)
}
export function moveWordEnd(textarea: TextareaRenderable) {
const text = textarea.plainText
textarea.cursorOffset = wordEnd(text, textarea.cursorOffset, false)
}
export function moveBigWordNext(textarea: TextareaRenderable) {
const text = textarea.plainText
textarea.cursorOffset = nextWordStart(text, textarea.cursorOffset, true)
}
export function moveBigWordPrev(textarea: TextareaRenderable) {
const text = textarea.plainText
textarea.cursorOffset = prevWordStart(text, textarea.cursorOffset, true)
}
export function moveBigWordEnd(textarea: TextareaRenderable) {
const text = textarea.plainText
textarea.cursorOffset = wordEnd(text, textarea.cursorOffset, true)
}
export function firstNonWhitespace(text: string, offset: number) {
const start = lineStart(text, offset)
const end = lineEnd(text, offset)
let pos = start
while (pos < end && /\s/.test(text[pos])) pos++
return pos
}
export function findCharInLine(
text: string,
offset: number,
char: string,
forward: boolean,
till = false,
repeat = false,
) {
const skip = till && repeat ? 2 : 1
if (forward) {
for (let i = offset + skip; i < text.length; i++) {
if (text[i] === char) return till ? i - 1 : i
}
} else {
for (let i = offset - skip; i >= 0; i--) {
if (text[i] === char) return till ? i + 1 : i
}
}
return offset
}
export function copyWordNext(rows: VimCopyRow[], get: (idx: number) => string, idx: number, col: number, big: boolean) {
const row = rows[idx]
if (!row) return { idx, col }
const min = row.col
const text = get(idx)
const pos = Math.max(0, col - min)
const next = nextWordStart(text, pos, big)
if (next < text.length) return { idx, col: min + next }
for (let i = idx + 1; i < rows.length; i++) {
const nextRow = rows[i]
if (!nextRow) continue
const nextText = get(i)
if (!nextText.length) return { idx: i, col: nextRow.col }
const nextCol = nextWordStart(nextText, 0, big)
if (nextCol < nextText.length) return { idx: i, col: nextRow.col + nextCol }
if (nextText.length > 0) return { idx: i, col: nextRow.col + nextText.length - 1 }
}
return { idx, col: min + Math.max(0, text.length - 1) }
}
export function copyWordPrev(rows: VimCopyRow[], get: (idx: number) => string, idx: number, col: number, big: boolean) {
const row = rows[idx]
if (!row) return { idx, col }
const min = row.col
const text = get(idx)
const pos = Math.max(0, col - min)
const prev = prevWordStart(text, pos, big)
if (prev < pos) return { idx, col: min + prev }
for (let i = idx - 1; i >= 0; i--) {
const prevRow = rows[i]
if (!prevRow) continue
const prevText = get(i)
if (!prevText.length) return { idx: i, col: prevRow.col }
const prevCol = prevWordStart(prevText, prevText.length, big)
return { idx: i, col: prevRow.col + prevCol }
}
return { idx, col: min }
}
export function copyWordEnd(rows: VimCopyRow[], get: (idx: number) => string, idx: number, col: number, big: boolean) {
const row = rows[idx]
if (!row) return { idx, col }
const min = row.col
const text = get(idx)
const pos = Math.max(0, col - min)
const end = wordEnd(text, pos, big)
if (end > pos && wordClass(text[end], big) !== "blank") return { idx, col: min + end }
for (let i = idx + 1; i < rows.length; i++) {
const nextRow = rows[i]
if (!nextRow) continue
const nextText = get(i)
const start = nextText.split("").findIndex((char) => wordClass(char, big) !== "blank")
if (start === -1) continue
return { idx: i, col: nextRow.col + wordRunEnd(nextText, start, big) }
}
return { idx, col: min + Math.max(0, text.length - 1) }
}
export type CopyParagraphResult = { index: number; atEnd: boolean }
// `atEnd` is true only when content runs to EOF without a trailing blank line,
// the only case where vim `}` lands on end-of-line instead of column 0.
export function copyNextParagraph(
rows: VimCopyRow[],
get: (index: number) => string,
index: number,
): CopyParagraphResult {
if (!rows.length) return { index: 0, atEnd: false }
let cursor = index
while (cursor < rows.length && get(cursor) === "") cursor++
if (cursor === rows.length) return { index: rows.length - 1, atEnd: false }
while (cursor < rows.length && get(cursor) !== "") cursor++
if (cursor === rows.length) return { index: rows.length - 1, atEnd: true }
return { index: cursor, atEnd: false }
}
// no `atEnd` counterpart: vim `{` always lands on column 0 of the target row.
export function copyPreviousParagraph(
rows: VimCopyRow[],
get: (index: number) => string,
index: number,
): CopyParagraphResult {
if (!rows.length) return { index: 0, atEnd: false }
let cursor = index
while (cursor > 0 && get(cursor) === "") cursor--
if (get(cursor) === "") return { index: 0, atEnd: false }
while (cursor > 0) {
cursor--
if (get(cursor) === "") return { index: cursor, atEnd: false }
}
return { index: 0, atEnd: false }
}
export function appendAfterCursor(textarea: TextareaRenderable) {
const text = textarea.plainText
const end = lineEnd(text, textarea.cursorOffset)
textarea.cursorOffset = Math.min(textarea.cursorOffset + 1, end)
}
export function appendLineEnd(textarea: TextareaRenderable) {
const text = textarea.plainText
textarea.cursorOffset = lineEnd(text, textarea.cursorOffset)
}
export function insertLineStart(textarea: TextareaRenderable) {
const text = textarea.plainText
textarea.cursorOffset = firstNonWhitespace(text, textarea.cursorOffset)
}
export function openLineBelow(textarea: TextareaRenderable) {
const text = textarea.plainText
const end = lineEnd(text, textarea.cursorOffset)
textarea.cursorOffset = end
textarea.insertText("\n")
}
export function openLineAbove(textarea: TextareaRenderable) {
const text = textarea.plainText
const start = lineStart(text, textarea.cursorOffset)
textarea.cursorOffset = start
textarea.insertText("\n")
textarea.cursorOffset = start
}
export function deleteUnderCursor(textarea: TextareaRenderable): VimRegister {
const text = textarea.plainText
const startOffset = textarea.cursorOffset
const end = lineEnd(text, startOffset)
if (startOffset >= end) return null
const yanked = text[startOffset]
deleteOffsets(textarea, startOffset, startOffset + 1)
return { text: yanked, linewise: false }
}
export function deleteWord(textarea: TextareaRenderable, big = false): VimRegister {
const text = textarea.plainText
const startOffset = textarea.cursorOffset
const endOffset = nextWordStart(text, startOffset, big)
if (endOffset <= startOffset) return null
const yanked = text.slice(startOffset, endOffset)
deleteOffsets(textarea, startOffset, endOffset)
return { text: yanked, linewise: false }
}
export function deleteWordBackward(textarea: TextareaRenderable): VimRegister {
const text = textarea.plainText
const startOffset = textarea.cursorOffset
const endOffset = prevWordStart(text, startOffset, false)
if (endOffset >= startOffset) return null
const yanked = text.slice(endOffset, startOffset)
deleteOffsets(textarea, endOffset, startOffset)
return { text: yanked, linewise: false }
}
export function deleteWordEnd(textarea: TextareaRenderable, big = false): VimRegister {
const text = textarea.plainText
const startOffset = textarea.cursorOffset
if (startOffset >= text.length) return null
const endOffset = wordEnd(text, startOffset, big) + 1
if (endOffset <= startOffset) return null
const yanked = text.slice(startOffset, endOffset)
deleteOffsets(textarea, startOffset, endOffset)
return { text: yanked, linewise: false }
}
export function deleteLine(textarea: TextareaRenderable, anchor?: number): VimRegister {
const text = textarea.plainText
if (!text.length) return null
const offset = textarea.cursorOffset
const lo = anchor !== undefined ? Math.min(anchor, offset) : offset
const hi = anchor !== undefined ? Math.max(anchor, offset) : offset
const start = lineStart(text, lo)
const end = lineEnd(text, hi)
const yanked = text.slice(start, end)
if (end < text.length) {
deleteOffsets(textarea, start, end + 1)
return { text: yanked, linewise: true }
}
if (start > 0) {
deleteOffsets(textarea, start - 1, end)
textarea.cursorOffset = lineStart(textarea.plainText, textarea.cursorOffset)
return { text: yanked, linewise: true }
}
deleteOffsets(textarea, start, end)
return { text: yanked, linewise: true }
}
export function deleteLineEnd(textarea: TextareaRenderable): VimRegister {
const text = textarea.plainText
const start = textarea.cursorOffset
const end = lineEnd(text, start)
if (end <= start) return null
const yanked = text.slice(start, end)
deleteOffsets(textarea, start, end)
textarea.cursorOffset = lineLast(textarea.plainText, start)
return { text: yanked, linewise: false }
}
export function deleteSpan(textarea: TextareaRenderable, span: VimSpan | null): void {
if (!span || span.end <= span.start) return
deleteOffsets(textarea, span.start, span.end)
}
export function findChar(textarea: TextareaRenderable, char: string, forward: boolean, till = false, repeat = false) {
const text = textarea.plainText
const offset = textarea.cursorOffset
const skip = till && repeat ? 2 : 1
if (forward) {
const end = lineEnd(text, offset)
for (let i = offset + skip; i < end; i++) {
if (text[i] === char) {
textarea.cursorOffset = till ? i - 1 : i
return
}
}
} else {
const start = lineStart(text, offset)
for (let i = offset - skip; i >= start; i--) {
if (text[i] === char) {
textarea.cursorOffset = till ? i + 1 : i
return
}
}
}
}
export function joinLines(textarea: TextareaRenderable) {
const text = textarea.plainText
const end = lineEnd(text, textarea.cursorOffset)
if (end >= text.length) return
let next = end + 1
while (next < text.length && (text[next] === " " || text[next] === "\t")) next++
const trailing = end > 0 && /[ \t]/.test(text[end - 1])
const paren = next < text.length && text[next] === ")"
deleteOffsets(textarea, end, next)
if (!trailing && !paren) textarea.insertText(" ")
textarea.cursorOffset = end
}
export function substituteLine(textarea: TextareaRenderable, anchor?: number): VimRegister {
const text = textarea.plainText
const offset = textarea.cursorOffset
const lo = anchor !== undefined ? Math.min(anchor, offset) : offset
const hi = anchor !== undefined ? Math.max(anchor, offset) : offset
const start = lineStart(text, lo)
const end = lineEnd(text, hi)
if (end <= start) return null
const yanked = text.slice(start, end)
deleteOffsets(textarea, start, end)
return { text: yanked, linewise: true }
}
export function substituteLineEnd(textarea: TextareaRenderable): VimRegister {
const text = textarea.plainText
const start = textarea.cursorOffset
const end = lineEnd(text, start)
if (end <= start) return null
const yanked = text.slice(start, end)
deleteOffsets(textarea, start, end)
return { text: yanked, linewise: false }
}
export function replaceUnderCursor(textarea: TextareaRenderable, value: string) {
const text = textarea.plainText
const offset = textarea.cursorOffset
if (offset >= text.length || text[offset] === "\n") {
textarea.insertText(value)
return
}
deleteOffsets(textarea, offset, offset + 1)
textarea.insertText(value)
}
export function toggleCase(textarea: TextareaRenderable) {
const text = textarea.plainText
const start = textarea.cursorOffset
const end = lineEnd(text, start)
if (start >= end) return
const char = text[start]
const next = swap(char)
if (next !== char) {
deleteOffsets(textarea, start, start + 1)
textarea.insertText(next)
textarea.cursorOffset = start
}
moveRight(textarea)
}
export function yankLine(textarea: TextareaRenderable): VimRegister {
const span = yankLineSpan(textarea)
return { text: textarea.plainText.slice(span.start, span.end), linewise: true }
}
export function yankLineSpan(textarea: TextareaRenderable): VimSpan {
const text = textarea.plainText
const start = lineStart(text, textarea.cursorOffset)
const end = lineEnd(text, textarea.cursorOffset)
return { start, end }
}
export function yankWord(textarea: TextareaRenderable, big = false): VimRegister {
const span = yankWordSpan(textarea, big)
if (!span) return null
return { text: textarea.plainText.slice(span.start, span.end), linewise: false }
}
export function yankWordSpan(textarea: TextareaRenderable, big = false): VimSpan | null {
const text = textarea.plainText
const start = textarea.cursorOffset
const end = nextWordStart(text, start, big)
if (end <= start) return null
return { start, end }
}
export function yankWordEnd(textarea: TextareaRenderable, big = false): VimRegister {
const span = yankWordEndSpan(textarea, big)
if (!span) return null
return { text: textarea.plainText.slice(span.start, span.end), linewise: false }
}
export function yankWordEndSpan(textarea: TextareaRenderable, big = false): VimSpan | null {
const text = textarea.plainText
const start = textarea.cursorOffset
if (start >= text.length) return null
const end = wordEnd(text, start, big) + 1
if (end <= start) return null
return { start, end }
}
export function pasteAfter(textarea: TextareaRenderable, reg: VimRegister) {
if (!reg) return
if (reg.linewise) {
const text = textarea.plainText
const end = lineEnd(text, textarea.cursorOffset)
textarea.cursorOffset = end
textarea.insertText("\n" + reg.text)
textarea.cursorOffset = end + 1
return
}
textarea.cursorOffset = Math.min(textarea.cursorOffset + 1, textarea.plainText.length)
textarea.insertText(reg.text)
textarea.cursorOffset = textarea.cursorOffset - 1
}
export function pasteBefore(textarea: TextareaRenderable, reg: VimRegister) {
if (!reg) return
if (reg.linewise) {
const text = textarea.plainText
const start = lineStart(text, textarea.cursorOffset)
textarea.cursorOffset = start
textarea.insertText(reg.text + "\n")
textarea.cursorOffset = start
return
}
textarea.insertText(reg.text)
textarea.cursorOffset = textarea.cursorOffset - 1
}
export function syncSelection(textarea: TextareaRenderable, anchor: number, linewise = false) {
const text = textarea.plainText
const cursor = textarea.cursorOffset
let lo = Math.min(anchor, cursor)
let hi = Math.max(anchor + 1, cursor + 1)
if (linewise) {
lo = lineStart(text, lo)
hi = lineEnd(text, hi - 1)
if (hi < text.length) hi++
}
const ta = textarea as any
const forward = cursor >= anchor
textarea.cursorOffset = forward ? lo : hi
ta.updateSelectionForMovement(true, true)
textarea.cursorOffset = forward ? hi : lo
ta.updateSelectionForMovement(true, false)
textarea.cursorOffset = cursor
textarea.editorView.setSelection(lo, hi, textarea.selectionBg, textarea.selectionFg)
}
export function clearSelection(textarea: TextareaRenderable) {
const ta = textarea as any
ta.updateSelectionForMovement(false, true)
textarea.editorView.resetSelection()
}
function selectionRange(textarea: TextareaRenderable, anchor?: number, linewise = false) {
if (anchor === undefined) return null
let start = Math.min(anchor, textarea.cursorOffset)
let end = Math.max(anchor + 1, textarea.cursorOffset + 1)
if (linewise) {
const text = textarea.plainText
start = lineStart(text, start)
end = lineEnd(text, end - 1)
if (end < text.length) end++
}
return { start, end }
}
export function toggleSelectionCase(textarea: TextareaRenderable, linewise = false, anchor?: number) {
const sel = selectionRange(textarea, anchor, linewise)
if (!sel) return
const text = textarea.plainText.slice(sel.start, sel.end)
const next = text.split("").map(swap).join("")
if (next !== text) {
deleteOffsets(textarea, sel.start, sel.end)
textarea.insertText(next)
}
textarea.cursorOffset = sel.start
}
export function deleteSelection(textarea: TextareaRenderable, linewise = false, anchor?: number): VimRegister {
const sel = selectionRange(textarea, anchor, linewise)
if (!sel) return null
const text = textarea.plainText
const yanked = text.slice(sel.start, sel.end)
let start = sel.start
let end = sel.end
// ensure delete complete lines to avoid leaving empty lines
if (linewise) {
const hasTrailingNl = end < text.length && text[end - 1] === "\n"
const hasLeadingNl = start > 0 && text[start - 1] === "\n"
if (!hasTrailingNl && end < text.length && text[end] === "\n") {
end++
} else if (!hasTrailingNl && hasLeadingNl) {
start--
}
}
deleteOffsets(textarea, start, end)
const after = textarea.plainText
if (linewise) {
if (start >= after.length && start > 0) {
textarea.cursorOffset = lineStart(after, after.length - 1)
} else {
textarea.cursorOffset = lineStart(after, Math.min(start, Math.max(after.length - 1, 0)))
}
} else {
textarea.cursorOffset = Math.min(start, Math.max(after.length - 1, 0))
}
return { text: yanked, linewise }
}
export function yankSelection(textarea: TextareaRenderable, linewise = false, anchor?: number): VimRegister {
const sel = selectionRange(textarea, anchor, linewise)
if (!sel) return null
return { text: textarea.plainText.slice(sel.start, sel.end), linewise }
}