@jadelclemens/pi-vim 0.0.0-stage → 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/motions.ts ADDED
@@ -0,0 +1,653 @@
1
+ /**
2
+ * Motion definitions for vim navigation.
3
+ * Each motion computes a target cursor position given text, cursor, and count.
4
+ */
5
+
6
+ export interface Position {
7
+ line: number;
8
+ col: number;
9
+ }
10
+
11
+ export interface MotionResult {
12
+ position: Position;
13
+ /** Whether this motion operates on whole lines (for operators) */
14
+ linewise: boolean;
15
+ /** Whether the end position is included in operator ranges */
16
+ inclusive: boolean;
17
+ }
18
+
19
+ export type MotionFn = (
20
+ lines: string[],
21
+ cursor: Position,
22
+ count: number,
23
+ ) => MotionResult;
24
+
25
+ // --- Character classification helpers ---
26
+
27
+ function isWordChar(ch: string): boolean {
28
+ return /[\p{L}\p{N}_]/u.test(ch);
29
+ }
30
+
31
+ function isPunctuation(ch: string): boolean {
32
+ return !isWordChar(ch) && ch !== " " && ch !== "\t" && ch !== "";
33
+ }
34
+
35
+ function isWhitespace(ch: string): boolean {
36
+ return ch === " " || ch === "\t";
37
+ }
38
+
39
+ function isBlankLine(line: string): boolean {
40
+ return /^\s*$/.test(line);
41
+ }
42
+
43
+ function clampCol(line: string, col: number): number {
44
+ if (!line || line.length === 0) return 0;
45
+ return Math.max(0, Math.min(col, line.length - 1));
46
+ }
47
+
48
+ function clampLine(lines: string[], line: number): number {
49
+ return Math.max(0, Math.min(line, lines.length - 1));
50
+ }
51
+
52
+ // --- Word motions (small word: alphanumeric/_ vs punctuation vs whitespace) ---
53
+
54
+ /**
55
+ * `w` - Move to start of next word.
56
+ * A word boundary is a transition between character classes, or moving past whitespace.
57
+ */
58
+ export const wordForward: MotionFn = (lines, cursor, count) => {
59
+ let { line, col } = cursor;
60
+
61
+ for (let n = 0; n < count; n++) {
62
+ const result = nextWordStart(lines, line, col);
63
+ line = result.line;
64
+ col = result.col;
65
+ }
66
+
67
+ return { position: { line, col }, linewise: false, inclusive: false };
68
+ };
69
+
70
+ function nextWordStart(
71
+ lines: string[],
72
+ line: number,
73
+ col: number,
74
+ ): Position {
75
+ if (lines.length === 0) return { line: 0, col: 0 };
76
+
77
+ let text = lines[line] || "";
78
+
79
+ // If at end of file, stay
80
+ if (line >= lines.length - 1 && col >= text.length - 1) {
81
+ return { line, col: Math.max(0, text.length - 1) };
82
+ }
83
+
84
+ const ch = text[col] || "";
85
+
86
+ // Skip current word characters of same class
87
+ if (isWordChar(ch)) {
88
+ while (col < text.length && isWordChar(text[col]!)) col++;
89
+ } else if (isPunctuation(ch)) {
90
+ while (col < text.length && isPunctuation(text[col]!)) col++;
91
+ }
92
+
93
+ // Skip whitespace (possibly across lines)
94
+ while (true) {
95
+ while (col < text.length && isWhitespace(text[col]!)) col++;
96
+ if (col < text.length) break;
97
+ // Move to next line
98
+ line++;
99
+ if (line >= lines.length) {
100
+ line = lines.length - 1;
101
+ return { line, col: Math.max(0, (lines[line] || "").length - 1) };
102
+ }
103
+ text = lines[line] || "";
104
+ col = 0;
105
+ // Empty lines are word boundaries in Vim. On non-empty lines, continue
106
+ // through indentation rather than stopping on whitespace at column zero.
107
+ if (text.length === 0) break;
108
+ }
109
+
110
+ return { line, col };
111
+ }
112
+
113
+ /**
114
+ * `b` - Move to start of previous word.
115
+ */
116
+ export const wordBackward: MotionFn = (lines, cursor, count) => {
117
+ let { line, col } = cursor;
118
+
119
+ for (let n = 0; n < count; n++) {
120
+ const result = prevWordStart(lines, line, col);
121
+ line = result.line;
122
+ col = result.col;
123
+ }
124
+
125
+ return { position: { line, col }, linewise: false, inclusive: false };
126
+ };
127
+
128
+ function prevWordStart(
129
+ lines: string[],
130
+ line: number,
131
+ col: number,
132
+ ): Position {
133
+ if (lines.length === 0) return { line: 0, col: 0 };
134
+
135
+ let text = lines[line] || "";
136
+
137
+ // Move back one character to start searching
138
+ col--;
139
+
140
+ // Skip whitespace backwards (possibly across lines)
141
+ while (true) {
142
+ while (col >= 0 && isWhitespace(text[col]!)) col--;
143
+ if (col >= 0) break;
144
+ // Move to previous line
145
+ line--;
146
+ if (line < 0) return { line: 0, col: 0 };
147
+ text = lines[line] || "";
148
+ col = text.length - 1;
149
+ }
150
+
151
+ // Now skip backwards through the word
152
+ if (col >= 0 && isWordChar(text[col]!)) {
153
+ while (col > 0 && isWordChar(text[col - 1]!)) col--;
154
+ } else if (col >= 0 && isPunctuation(text[col]!)) {
155
+ while (col > 0 && isPunctuation(text[col - 1]!)) col--;
156
+ }
157
+
158
+ return { line, col: Math.max(0, col) };
159
+ }
160
+
161
+ /**
162
+ * `e` - Move to end of current/next word.
163
+ */
164
+ export const wordEnd: MotionFn = (lines, cursor, count) => {
165
+ let { line, col } = cursor;
166
+
167
+ for (let n = 0; n < count; n++) {
168
+ const result = nextWordEnd(lines, line, col);
169
+ line = result.line;
170
+ col = result.col;
171
+ }
172
+
173
+ return { position: { line, col }, linewise: false, inclusive: true };
174
+ };
175
+
176
+ function nextWordEnd(
177
+ lines: string[],
178
+ line: number,
179
+ col: number,
180
+ ): Position {
181
+ if (lines.length === 0) return { line: 0, col: 0 };
182
+
183
+ let text = lines[line] || "";
184
+
185
+ // Move forward one character to start searching
186
+ col++;
187
+
188
+ // Skip whitespace (possibly across lines)
189
+ while (true) {
190
+ while (col < text.length && isWhitespace(text[col]!)) col++;
191
+ if (col < text.length) break;
192
+ line++;
193
+ if (line >= lines.length) {
194
+ line = lines.length - 1;
195
+ return { line, col: Math.max(0, (lines[line] || "").length - 1) };
196
+ }
197
+ text = lines[line] || "";
198
+ col = 0;
199
+ }
200
+
201
+ // Move forward through word characters of same class
202
+ if (isWordChar(text[col]!)) {
203
+ while (col < text.length - 1 && isWordChar(text[col + 1]!)) col++;
204
+ } else if (isPunctuation(text[col]!)) {
205
+ while (col < text.length - 1 && isPunctuation(text[col + 1]!)) col++;
206
+ }
207
+
208
+ return { line, col };
209
+ }
210
+
211
+ // --- WORD motions (whitespace-delimited) ---
212
+
213
+ /**
214
+ * `W` - Move to start of next WORD (whitespace-delimited).
215
+ */
216
+ export const WORDForward: MotionFn = (lines, cursor, count) => {
217
+ let { line, col } = cursor;
218
+
219
+ for (let n = 0; n < count; n++) {
220
+ const result = nextWORDStart(lines, line, col);
221
+ line = result.line;
222
+ col = result.col;
223
+ }
224
+
225
+ return { position: { line, col }, linewise: false, inclusive: false };
226
+ };
227
+
228
+ function nextWORDStart(
229
+ lines: string[],
230
+ line: number,
231
+ col: number,
232
+ ): Position {
233
+ if (lines.length === 0) return { line: 0, col: 0 };
234
+
235
+ let text = lines[line] || "";
236
+
237
+ // Skip non-whitespace
238
+ while (col < text.length && !isWhitespace(text[col]!)) col++;
239
+
240
+ // Skip whitespace (possibly across lines)
241
+ while (true) {
242
+ while (col < text.length && isWhitespace(text[col]!)) col++;
243
+ if (col < text.length) break;
244
+ line++;
245
+ if (line >= lines.length) {
246
+ line = lines.length - 1;
247
+ return { line, col: Math.max(0, (lines[line] || "").length - 1) };
248
+ }
249
+ text = lines[line] || "";
250
+ col = 0;
251
+ // Empty lines are WORD boundaries; leading whitespace on a non-empty
252
+ // line is not.
253
+ if (text.length === 0) break;
254
+ }
255
+
256
+ return { line, col };
257
+ }
258
+
259
+ /**
260
+ * `B` - Move to start of previous WORD.
261
+ */
262
+ export const WORDBackward: MotionFn = (lines, cursor, count) => {
263
+ let { line, col } = cursor;
264
+
265
+ for (let n = 0; n < count; n++) {
266
+ const result = prevWORDStart(lines, line, col);
267
+ line = result.line;
268
+ col = result.col;
269
+ }
270
+
271
+ return { position: { line, col }, linewise: false, inclusive: false };
272
+ };
273
+
274
+ function prevWORDStart(
275
+ lines: string[],
276
+ line: number,
277
+ col: number,
278
+ ): Position {
279
+ if (lines.length === 0) return { line: 0, col: 0 };
280
+
281
+ let text = lines[line] || "";
282
+ col--;
283
+
284
+ // Skip whitespace backwards
285
+ while (true) {
286
+ while (col >= 0 && isWhitespace(text[col]!)) col--;
287
+ if (col >= 0) break;
288
+ line--;
289
+ if (line < 0) return { line: 0, col: 0 };
290
+ text = lines[line] || "";
291
+ col = text.length - 1;
292
+ }
293
+
294
+ // Skip non-whitespace backwards
295
+ while (col > 0 && !isWhitespace(text[col - 1]!)) col--;
296
+
297
+ return { line, col: Math.max(0, col) };
298
+ }
299
+
300
+ /**
301
+ * `E` - Move to end of current/next WORD.
302
+ */
303
+ export const WORDEnd: MotionFn = (lines, cursor, count) => {
304
+ let { line, col } = cursor;
305
+
306
+ for (let n = 0; n < count; n++) {
307
+ const result = nextWORDEnd(lines, line, col);
308
+ line = result.line;
309
+ col = result.col;
310
+ }
311
+
312
+ return { position: { line, col }, linewise: false, inclusive: true };
313
+ };
314
+
315
+ function nextWORDEnd(
316
+ lines: string[],
317
+ line: number,
318
+ col: number,
319
+ ): Position {
320
+ if (lines.length === 0) return { line: 0, col: 0 };
321
+
322
+ let text = lines[line] || "";
323
+ col++;
324
+
325
+ // Skip whitespace
326
+ while (true) {
327
+ while (col < text.length && isWhitespace(text[col]!)) col++;
328
+ if (col < text.length) break;
329
+ line++;
330
+ if (line >= lines.length) {
331
+ line = lines.length - 1;
332
+ return { line, col: Math.max(0, (lines[line] || "").length - 1) };
333
+ }
334
+ text = lines[line] || "";
335
+ col = 0;
336
+ }
337
+
338
+ // Skip non-whitespace forward
339
+ while (col < text.length - 1 && !isWhitespace(text[col + 1]!)) col++;
340
+
341
+ return { line, col };
342
+ }
343
+
344
+ // --- Line-level motions ---
345
+
346
+ /**
347
+ * `gg` - Go to first line (or line N with count).
348
+ */
349
+ export const goToFirstLine: MotionFn = (lines, _cursor, count) => {
350
+ // count is the line number (1-indexed) if explicitly given, otherwise line 1
351
+ const targetLine = clampLine(lines, count - 1);
352
+ const text = lines[targetLine] || "";
353
+ const match = text.match(/^\s*/);
354
+ const col = match ? match[0].length : 0;
355
+ return { position: { line: targetLine, col }, linewise: true, inclusive: false };
356
+ };
357
+
358
+ /**
359
+ * `G` - Go to last line (or line N with count).
360
+ */
361
+ export const goToLastLine: MotionFn = (lines, _cursor, count) => {
362
+ // If count was explicitly provided it's passed as count, otherwise go to last line
363
+ const targetLine = clampLine(lines, count - 1);
364
+ const text = lines[targetLine] || "";
365
+ const match = text.match(/^\s*/);
366
+ const col = match ? match[0].length : 0;
367
+ return { position: { line: targetLine, col }, linewise: true, inclusive: false };
368
+ };
369
+
370
+ /**
371
+ * `^` - First non-whitespace character on line.
372
+ */
373
+ export const firstNonBlank: MotionFn = (lines, cursor, _count) => {
374
+ const text = lines[cursor.line] || "";
375
+ const match = text.match(/^\s*/);
376
+ const col = match ? match[0].length : 0;
377
+ return { position: { line: cursor.line, col }, linewise: false, inclusive: false };
378
+ };
379
+
380
+ /**
381
+ * `0` - Start of line.
382
+ */
383
+ export const lineStart: MotionFn = (_lines, cursor, _count) => {
384
+ return { position: { line: cursor.line, col: 0 }, linewise: false, inclusive: false };
385
+ };
386
+
387
+ /**
388
+ * `$` - End of line.
389
+ */
390
+ export const lineEnd: MotionFn = (lines, cursor, count) => {
391
+ // With count > 1, go down count-1 lines then to end
392
+ const targetLine = clampLine(lines, cursor.line + count - 1);
393
+ const text = lines[targetLine] || "";
394
+ const col = Math.max(0, text.length - 1);
395
+ return { position: { line: targetLine, col }, linewise: false, inclusive: true };
396
+ };
397
+
398
+ // --- Find character motions ---
399
+
400
+ /** Last f/F/t/T search for `;` and `,` repeat */
401
+ let lastCharSearch: {
402
+ char: string;
403
+ direction: "forward" | "backward";
404
+ type: "f" | "t";
405
+ } | null = null;
406
+
407
+ export function getLastCharSearch() {
408
+ return lastCharSearch;
409
+ }
410
+
411
+ /**
412
+ * `f{char}` - Find char forward on current line (inclusive).
413
+ */
414
+ export function findCharForward(char: string): MotionFn {
415
+ return (lines, cursor, count) => {
416
+ lastCharSearch = { char, direction: "forward", type: "f" };
417
+ const text = lines[cursor.line] || "";
418
+ let col = cursor.col;
419
+
420
+ for (let n = 0; n < count; n++) {
421
+ col++;
422
+ while (col < text.length && text[col] !== char) col++;
423
+ if (col >= text.length) {
424
+ // Not found - stay at current position
425
+ return { position: { line: cursor.line, col: cursor.col }, linewise: false, inclusive: true };
426
+ }
427
+ }
428
+
429
+ return { position: { line: cursor.line, col }, linewise: false, inclusive: true };
430
+ };
431
+ }
432
+
433
+ /**
434
+ * `F{char}` - Find char backward on current line (inclusive).
435
+ */
436
+ export function findCharBackward(char: string): MotionFn {
437
+ return (lines, cursor, count) => {
438
+ lastCharSearch = { char, direction: "backward", type: "f" };
439
+ const text = lines[cursor.line] || "";
440
+ let col = cursor.col;
441
+
442
+ for (let n = 0; n < count; n++) {
443
+ col--;
444
+ while (col >= 0 && text[col] !== char) col--;
445
+ if (col < 0) {
446
+ return { position: { line: cursor.line, col: cursor.col }, linewise: false, inclusive: true };
447
+ }
448
+ }
449
+
450
+ return { position: { line: cursor.line, col }, linewise: false, inclusive: true };
451
+ };
452
+ }
453
+
454
+ /**
455
+ * `t{char}` - Till char forward (one before char, inclusive).
456
+ */
457
+ export function tillCharForward(char: string): MotionFn {
458
+ return (lines, cursor, count) => {
459
+ lastCharSearch = { char, direction: "forward", type: "t" };
460
+ const text = lines[cursor.line] || "";
461
+ let col = cursor.col;
462
+
463
+ for (let n = 0; n < count; n++) {
464
+ const searchStart = col + 1;
465
+ col = searchStart;
466
+ while (col < text.length && text[col] !== char) col++;
467
+ if (col >= text.length) {
468
+ return { position: { line: cursor.line, col: cursor.col }, linewise: false, inclusive: true };
469
+ }
470
+ }
471
+
472
+ // Stop one before the found character
473
+ return { position: { line: cursor.line, col: col - 1 }, linewise: false, inclusive: true };
474
+ };
475
+ }
476
+
477
+ /**
478
+ * `T{char}` - Till char backward (one after char, inclusive).
479
+ */
480
+ export function tillCharBackward(char: string): MotionFn {
481
+ return (lines, cursor, count) => {
482
+ lastCharSearch = { char, direction: "backward", type: "t" };
483
+ const text = lines[cursor.line] || "";
484
+ let col = cursor.col;
485
+
486
+ for (let n = 0; n < count; n++) {
487
+ col--;
488
+ while (col >= 0 && text[col] !== char) col--;
489
+ if (col < 0) {
490
+ return { position: { line: cursor.line, col: cursor.col }, linewise: false, inclusive: true };
491
+ }
492
+ }
493
+
494
+ // Stop one after the found character
495
+ return { position: { line: cursor.line, col: col + 1 }, linewise: false, inclusive: true };
496
+ };
497
+ }
498
+
499
+ /**
500
+ * `;` - Repeat last f/F/t/T in same direction.
501
+ */
502
+ export const repeatCharSearch: MotionFn = (lines, cursor, count) => {
503
+ if (!lastCharSearch) return { position: cursor, linewise: false, inclusive: true };
504
+
505
+ const { char, direction, type } = lastCharSearch;
506
+ if (direction === "forward") {
507
+ const fn = type === "f" ? findCharForward(char) : tillCharForward(char);
508
+ // Restore lastCharSearch since the helper overwrites it
509
+ const saved = { ...lastCharSearch };
510
+ const result = fn(lines, cursor, count);
511
+ lastCharSearch = saved;
512
+ return result;
513
+ } else {
514
+ const fn = type === "f" ? findCharBackward(char) : tillCharBackward(char);
515
+ const saved = { ...lastCharSearch };
516
+ const result = fn(lines, cursor, count);
517
+ lastCharSearch = saved;
518
+ return result;
519
+ }
520
+ };
521
+
522
+ /**
523
+ * `,` - Repeat last f/F/t/T in opposite direction.
524
+ */
525
+ export const reverseCharSearch: MotionFn = (lines, cursor, count) => {
526
+ if (!lastCharSearch) return { position: cursor, linewise: false, inclusive: true };
527
+
528
+ const { char, direction, type } = lastCharSearch;
529
+ // Reverse direction
530
+ if (direction === "forward") {
531
+ const fn = type === "f" ? findCharBackward(char) : tillCharBackward(char);
532
+ const saved = { ...lastCharSearch };
533
+ const result = fn(lines, cursor, count);
534
+ lastCharSearch = saved;
535
+ return result;
536
+ } else {
537
+ const fn = type === "f" ? findCharForward(char) : tillCharForward(char);
538
+ const saved = { ...lastCharSearch };
539
+ const result = fn(lines, cursor, count);
540
+ lastCharSearch = saved;
541
+ return result;
542
+ }
543
+ };
544
+
545
+ // --- Paragraph motions ---
546
+
547
+ /**
548
+ * `{` - Move to previous blank line (paragraph boundary).
549
+ */
550
+ export const paragraphBackward: MotionFn = (lines, cursor, count) => {
551
+ let line = cursor.line;
552
+
553
+ for (let n = 0; n < count; n++) {
554
+ // Skip current blank lines
555
+ while (line > 0 && isBlankLine(lines[line]!)) line--;
556
+ // Find next blank line
557
+ while (line > 0 && !isBlankLine(lines[line]!)) line--;
558
+ }
559
+
560
+ return { position: { line, col: 0 }, linewise: true, inclusive: false };
561
+ };
562
+
563
+ /**
564
+ * `}` - Move to next blank line (paragraph boundary).
565
+ */
566
+ export const paragraphForward: MotionFn = (lines, cursor, count) => {
567
+ let line = cursor.line;
568
+ const lastLine = lines.length - 1;
569
+
570
+ for (let n = 0; n < count; n++) {
571
+ // Skip current blank lines
572
+ while (line < lastLine && isBlankLine(lines[line]!)) line++;
573
+ // Find next blank line
574
+ while (line < lastLine && !isBlankLine(lines[line]!)) line++;
575
+ }
576
+
577
+ return { position: { line, col: 0 }, linewise: true, inclusive: false };
578
+ };
579
+
580
+ // --- Matching bracket ---
581
+
582
+ const BRACKET_PAIRS: Record<string, string> = {
583
+ "(": ")",
584
+ ")": "(",
585
+ "[": "]",
586
+ "]": "[",
587
+ "{": "}",
588
+ "}": "{",
589
+ };
590
+
591
+ const OPEN_BRACKETS = new Set(["(", "[", "{"]);
592
+
593
+ /**
594
+ * `%` - Move to matching bracket.
595
+ */
596
+ export const matchingBracket: MotionFn = (lines, cursor, _count) => {
597
+ const text = lines[cursor.line] || "";
598
+
599
+ // Find the first bracket at or after cursor on current line
600
+ let bracketCol = cursor.col;
601
+ while (bracketCol < text.length && !BRACKET_PAIRS[text[bracketCol]!]) {
602
+ bracketCol++;
603
+ }
604
+
605
+ if (bracketCol >= text.length) {
606
+ // No bracket found on line from cursor
607
+ return { position: cursor, linewise: false, inclusive: true };
608
+ }
609
+
610
+ const bracket = text[bracketCol]!;
611
+ const match = BRACKET_PAIRS[bracket]!;
612
+ const isOpen = OPEN_BRACKETS.has(bracket);
613
+ let depth = 1;
614
+
615
+ if (isOpen) {
616
+ // Search forward
617
+ let line = cursor.line;
618
+ let col = bracketCol + 1;
619
+ while (line < lines.length) {
620
+ const lineText = lines[line]!;
621
+ while (col < lineText.length) {
622
+ if (lineText[col] === bracket) depth++;
623
+ else if (lineText[col] === match) depth--;
624
+ if (depth === 0) {
625
+ return { position: { line, col }, linewise: false, inclusive: true };
626
+ }
627
+ col++;
628
+ }
629
+ line++;
630
+ col = 0;
631
+ }
632
+ } else {
633
+ // Search backward
634
+ let line = cursor.line;
635
+ let col = bracketCol - 1;
636
+ while (line >= 0) {
637
+ const lineText = lines[line]!;
638
+ while (col >= 0) {
639
+ if (lineText[col] === bracket) depth++;
640
+ else if (lineText[col] === match) depth--;
641
+ if (depth === 0) {
642
+ return { position: { line, col }, linewise: false, inclusive: true };
643
+ }
644
+ col--;
645
+ }
646
+ line--;
647
+ if (line >= 0) col = lines[line]!.length - 1;
648
+ }
649
+ }
650
+
651
+ // No match found
652
+ return { position: cursor, linewise: false, inclusive: true };
653
+ };