@jadelclemens/pi-vim 0.0.0-stage → 1.8.1
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/LICENSE +21 -0
- package/README.md +94 -2
- package/config.ts +33 -0
- package/index.ts +43 -0
- package/keys.ts +26 -0
- package/modes/insert.ts +86 -0
- package/modes/normal.ts +1141 -0
- package/modes/replace.ts +136 -0
- package/modes/visual.ts +651 -0
- package/motions.ts +653 -0
- package/operators.ts +305 -0
- package/package.json +49 -4
- package/registers.ts +151 -0
- package/repeat.ts +113 -0
- package/search.ts +295 -0
- package/state.ts +85 -0
- package/text-objects.ts +365 -0
- package/vim-editor.ts +662 -0
package/text-objects.ts
ADDED
|
@@ -0,0 +1,365 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Text object definitions for vim operators.
|
|
3
|
+
* Each text object returns a range { start, end } within the buffer.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { Position } from "./motions.js";
|
|
7
|
+
|
|
8
|
+
export interface TextObjectRange {
|
|
9
|
+
start: Position;
|
|
10
|
+
end: Position;
|
|
11
|
+
linewise: boolean;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export type TextObjectFn = (
|
|
15
|
+
lines: string[],
|
|
16
|
+
cursor: Position,
|
|
17
|
+
) => TextObjectRange | null;
|
|
18
|
+
|
|
19
|
+
// --- Character classification helpers ---
|
|
20
|
+
|
|
21
|
+
function isWordChar(ch: string): boolean {
|
|
22
|
+
return /[a-zA-Z0-9_]/.test(ch);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function isWhitespace(ch: string): boolean {
|
|
26
|
+
return ch === " " || ch === "\t";
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// --- Word text objects ---
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* `iw` - Inner word: the word under the cursor (no surrounding whitespace).
|
|
33
|
+
*/
|
|
34
|
+
export const innerWord: TextObjectFn = (lines, cursor) => {
|
|
35
|
+
const line = lines[cursor.line] || "";
|
|
36
|
+
if (line.length === 0) return null;
|
|
37
|
+
|
|
38
|
+
const col = Math.min(cursor.col, line.length - 1);
|
|
39
|
+
const ch = line[col]!;
|
|
40
|
+
|
|
41
|
+
let start = col;
|
|
42
|
+
let end = col;
|
|
43
|
+
|
|
44
|
+
if (isWordChar(ch)) {
|
|
45
|
+
while (start > 0 && isWordChar(line[start - 1]!)) start--;
|
|
46
|
+
while (end < line.length - 1 && isWordChar(line[end + 1]!)) end++;
|
|
47
|
+
} else if (isWhitespace(ch)) {
|
|
48
|
+
while (start > 0 && isWhitespace(line[start - 1]!)) start--;
|
|
49
|
+
while (end < line.length - 1 && isWhitespace(line[end + 1]!)) end++;
|
|
50
|
+
} else {
|
|
51
|
+
// Punctuation
|
|
52
|
+
while (start > 0 && !isWordChar(line[start - 1]!) && !isWhitespace(line[start - 1]!)) start--;
|
|
53
|
+
while (end < line.length - 1 && !isWordChar(line[end + 1]!) && !isWhitespace(line[end + 1]!)) end++;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return {
|
|
57
|
+
start: { line: cursor.line, col: start },
|
|
58
|
+
end: { line: cursor.line, col: end },
|
|
59
|
+
linewise: false,
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* `aw` - A word: the word under the cursor plus trailing (or leading) whitespace.
|
|
65
|
+
*/
|
|
66
|
+
export const aWord: TextObjectFn = (lines, cursor) => {
|
|
67
|
+
const inner = innerWord(lines, cursor);
|
|
68
|
+
if (!inner) return null;
|
|
69
|
+
|
|
70
|
+
const line = lines[cursor.line] || "";
|
|
71
|
+
let { start, end } = { start: inner.start.col, end: inner.end.col };
|
|
72
|
+
|
|
73
|
+
// Try to include trailing whitespace first
|
|
74
|
+
if (end + 1 < line.length && isWhitespace(line[end + 1]!)) {
|
|
75
|
+
end++;
|
|
76
|
+
while (end + 1 < line.length && isWhitespace(line[end + 1]!)) end++;
|
|
77
|
+
} else if (start > 0 && isWhitespace(line[start - 1]!)) {
|
|
78
|
+
// No trailing whitespace, include leading whitespace
|
|
79
|
+
start--;
|
|
80
|
+
while (start > 0 && isWhitespace(line[start - 1]!)) start--;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return {
|
|
84
|
+
start: { line: cursor.line, col: start },
|
|
85
|
+
end: { line: cursor.line, col: end },
|
|
86
|
+
linewise: false,
|
|
87
|
+
};
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* `iW` - Inner WORD (whitespace-delimited).
|
|
92
|
+
*/
|
|
93
|
+
export const innerWORD: TextObjectFn = (lines, cursor) => {
|
|
94
|
+
const line = lines[cursor.line] || "";
|
|
95
|
+
if (line.length === 0) return null;
|
|
96
|
+
|
|
97
|
+
const col = Math.min(cursor.col, line.length - 1);
|
|
98
|
+
const ch = line[col]!;
|
|
99
|
+
|
|
100
|
+
let start = col;
|
|
101
|
+
let end = col;
|
|
102
|
+
|
|
103
|
+
if (isWhitespace(ch)) {
|
|
104
|
+
while (start > 0 && isWhitespace(line[start - 1]!)) start--;
|
|
105
|
+
while (end < line.length - 1 && isWhitespace(line[end + 1]!)) end++;
|
|
106
|
+
} else {
|
|
107
|
+
while (start > 0 && !isWhitespace(line[start - 1]!)) start--;
|
|
108
|
+
while (end < line.length - 1 && !isWhitespace(line[end + 1]!)) end++;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return {
|
|
112
|
+
start: { line: cursor.line, col: start },
|
|
113
|
+
end: { line: cursor.line, col: end },
|
|
114
|
+
linewise: false,
|
|
115
|
+
};
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* `aW` - A WORD plus trailing/leading whitespace.
|
|
120
|
+
*/
|
|
121
|
+
export const aWORD: TextObjectFn = (lines, cursor) => {
|
|
122
|
+
const inner = innerWORD(lines, cursor);
|
|
123
|
+
if (!inner) return null;
|
|
124
|
+
|
|
125
|
+
const line = lines[cursor.line] || "";
|
|
126
|
+
let { start, end } = { start: inner.start.col, end: inner.end.col };
|
|
127
|
+
|
|
128
|
+
if (end + 1 < line.length && isWhitespace(line[end + 1]!)) {
|
|
129
|
+
end++;
|
|
130
|
+
while (end + 1 < line.length && isWhitespace(line[end + 1]!)) end++;
|
|
131
|
+
} else if (start > 0 && isWhitespace(line[start - 1]!)) {
|
|
132
|
+
start--;
|
|
133
|
+
while (start > 0 && isWhitespace(line[start - 1]!)) start--;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return {
|
|
137
|
+
start: { line: cursor.line, col: start },
|
|
138
|
+
end: { line: cursor.line, col: end },
|
|
139
|
+
linewise: false,
|
|
140
|
+
};
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
// --- Quote text objects ---
|
|
144
|
+
|
|
145
|
+
function makeQuoteTextObject(quoteChar: string, inner: boolean): TextObjectFn {
|
|
146
|
+
return (lines, cursor) => {
|
|
147
|
+
const line = lines[cursor.line] || "";
|
|
148
|
+
const col = cursor.col;
|
|
149
|
+
|
|
150
|
+
// Find the opening and closing quotes on the current line
|
|
151
|
+
// Strategy: find all quote positions, pair them up, find pair containing cursor
|
|
152
|
+
const quotePositions: number[] = [];
|
|
153
|
+
for (let i = 0; i < line.length; i++) {
|
|
154
|
+
if (line[i] === quoteChar && (i === 0 || line[i - 1] !== "\\")) {
|
|
155
|
+
quotePositions.push(i);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Try to find a pair that contains the cursor
|
|
160
|
+
for (let i = 0; i < quotePositions.length - 1; i += 2) {
|
|
161
|
+
const open = quotePositions[i]!;
|
|
162
|
+
const close = quotePositions[i + 1]!;
|
|
163
|
+
|
|
164
|
+
if (col >= open && col <= close) {
|
|
165
|
+
if (inner) {
|
|
166
|
+
// Inner: between quotes (exclusive)
|
|
167
|
+
if (close - open <= 1) {
|
|
168
|
+
// Empty quotes - return a zero-width range at the position after open quote
|
|
169
|
+
return {
|
|
170
|
+
start: { line: cursor.line, col: open + 1 },
|
|
171
|
+
end: { line: cursor.line, col: open },
|
|
172
|
+
linewise: false,
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
return {
|
|
176
|
+
start: { line: cursor.line, col: open + 1 },
|
|
177
|
+
end: { line: cursor.line, col: close - 1 },
|
|
178
|
+
linewise: false,
|
|
179
|
+
};
|
|
180
|
+
} else {
|
|
181
|
+
// Around: including quotes
|
|
182
|
+
return {
|
|
183
|
+
start: { line: cursor.line, col: open },
|
|
184
|
+
end: { line: cursor.line, col: close },
|
|
185
|
+
linewise: false,
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// If cursor is before the first pair, use first pair
|
|
192
|
+
if (quotePositions.length >= 2 && col < quotePositions[0]!) {
|
|
193
|
+
const open = quotePositions[0]!;
|
|
194
|
+
const close = quotePositions[1]!;
|
|
195
|
+
if (inner) {
|
|
196
|
+
if (close - open <= 1) {
|
|
197
|
+
return {
|
|
198
|
+
start: { line: cursor.line, col: open + 1 },
|
|
199
|
+
end: { line: cursor.line, col: open },
|
|
200
|
+
linewise: false,
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
return {
|
|
204
|
+
start: { line: cursor.line, col: open + 1 },
|
|
205
|
+
end: { line: cursor.line, col: close - 1 },
|
|
206
|
+
linewise: false,
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
return {
|
|
210
|
+
start: { line: cursor.line, col: open },
|
|
211
|
+
end: { line: cursor.line, col: close },
|
|
212
|
+
linewise: false,
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
return null; // No matching quotes found
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
export const innerDoubleQuote = makeQuoteTextObject('"', true);
|
|
221
|
+
export const aDoubleQuote = makeQuoteTextObject('"', false);
|
|
222
|
+
export const innerSingleQuote = makeQuoteTextObject("'", true);
|
|
223
|
+
export const aSingleQuote = makeQuoteTextObject("'", false);
|
|
224
|
+
export const innerBacktick = makeQuoteTextObject("`", true);
|
|
225
|
+
export const aBacktick = makeQuoteTextObject("`", false);
|
|
226
|
+
|
|
227
|
+
// --- Bracket/delimiter text objects ---
|
|
228
|
+
|
|
229
|
+
function makeBracketTextObject(
|
|
230
|
+
openChar: string,
|
|
231
|
+
closeChar: string,
|
|
232
|
+
inner: boolean,
|
|
233
|
+
): TextObjectFn {
|
|
234
|
+
return (lines, cursor) => {
|
|
235
|
+
// Search backward for the opening bracket (handling nesting)
|
|
236
|
+
let depth = 0;
|
|
237
|
+
let openLine = cursor.line;
|
|
238
|
+
let openCol = cursor.col;
|
|
239
|
+
let found = false;
|
|
240
|
+
|
|
241
|
+
// Search backward from cursor
|
|
242
|
+
outer_back: for (let ln = cursor.line; ln >= 0; ln--) {
|
|
243
|
+
const lineText = lines[ln] || "";
|
|
244
|
+
const startCol = ln === cursor.line ? cursor.col : lineText.length - 1;
|
|
245
|
+
|
|
246
|
+
for (let c = startCol; c >= 0; c--) {
|
|
247
|
+
const ch = lineText[c];
|
|
248
|
+
if (ch === closeChar && !(ln === cursor.line && c === cursor.col)) {
|
|
249
|
+
depth++;
|
|
250
|
+
} else if (ch === openChar) {
|
|
251
|
+
if (depth === 0) {
|
|
252
|
+
openLine = ln;
|
|
253
|
+
openCol = c;
|
|
254
|
+
found = true;
|
|
255
|
+
break outer_back;
|
|
256
|
+
}
|
|
257
|
+
depth--;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
if (!found) return null;
|
|
263
|
+
|
|
264
|
+
// Search forward for the matching closing bracket
|
|
265
|
+
depth = 0;
|
|
266
|
+
let closeLine = cursor.line;
|
|
267
|
+
let closeCol = cursor.col;
|
|
268
|
+
found = false;
|
|
269
|
+
|
|
270
|
+
outer_fwd: for (let ln = openLine; ln < lines.length; ln++) {
|
|
271
|
+
const lineText = lines[ln] || "";
|
|
272
|
+
const startCol = ln === openLine ? openCol + 1 : 0;
|
|
273
|
+
|
|
274
|
+
for (let c = startCol; c < lineText.length; c++) {
|
|
275
|
+
const ch = lineText[c];
|
|
276
|
+
if (ch === openChar) {
|
|
277
|
+
depth++;
|
|
278
|
+
} else if (ch === closeChar) {
|
|
279
|
+
if (depth === 0) {
|
|
280
|
+
closeLine = ln;
|
|
281
|
+
closeCol = c;
|
|
282
|
+
found = true;
|
|
283
|
+
break outer_fwd;
|
|
284
|
+
}
|
|
285
|
+
depth--;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
if (!found) return null;
|
|
291
|
+
|
|
292
|
+
if (inner) {
|
|
293
|
+
// Inner: between the brackets (exclusive of brackets themselves)
|
|
294
|
+
// If open and close are on the same line and adjacent, empty range
|
|
295
|
+
if (openLine === closeLine && closeCol - openCol <= 1) {
|
|
296
|
+
return {
|
|
297
|
+
start: { line: openLine, col: openCol + 1 },
|
|
298
|
+
end: { line: closeLine, col: openCol },
|
|
299
|
+
linewise: false,
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
return {
|
|
303
|
+
start: { line: openLine, col: openCol + 1 },
|
|
304
|
+
end: { line: closeLine, col: closeCol - 1 },
|
|
305
|
+
linewise: false,
|
|
306
|
+
};
|
|
307
|
+
} else {
|
|
308
|
+
// Around: including the brackets
|
|
309
|
+
return {
|
|
310
|
+
start: { line: openLine, col: openCol },
|
|
311
|
+
end: { line: closeLine, col: closeCol },
|
|
312
|
+
linewise: false,
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
export const innerParen = makeBracketTextObject("(", ")", true);
|
|
319
|
+
export const aParen = makeBracketTextObject("(", ")", false);
|
|
320
|
+
export const innerBrace = makeBracketTextObject("{", "}", true);
|
|
321
|
+
export const aBrace = makeBracketTextObject("{", "}", false);
|
|
322
|
+
export const innerBracket = makeBracketTextObject("[", "]", true);
|
|
323
|
+
export const aBracket = makeBracketTextObject("[", "]", false);
|
|
324
|
+
export const innerAngle = makeBracketTextObject("<", ">", true);
|
|
325
|
+
export const aAngle = makeBracketTextObject("<", ">", false);
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* Resolve a text object key sequence like "iw", "a(", etc.
|
|
329
|
+
* `prefix` is 'i' or 'a', `key` is the object identifier.
|
|
330
|
+
*/
|
|
331
|
+
export function resolveTextObject(
|
|
332
|
+
prefix: string,
|
|
333
|
+
key: string,
|
|
334
|
+
): TextObjectFn | null {
|
|
335
|
+
const isInner = prefix === "i";
|
|
336
|
+
|
|
337
|
+
switch (key) {
|
|
338
|
+
case "w":
|
|
339
|
+
return isInner ? innerWord : aWord;
|
|
340
|
+
case "W":
|
|
341
|
+
return isInner ? innerWORD : aWORD;
|
|
342
|
+
case '"':
|
|
343
|
+
return isInner ? innerDoubleQuote : aDoubleQuote;
|
|
344
|
+
case "'":
|
|
345
|
+
return isInner ? innerSingleQuote : aSingleQuote;
|
|
346
|
+
case "`":
|
|
347
|
+
return isInner ? innerBacktick : aBacktick;
|
|
348
|
+
case "(":
|
|
349
|
+
case ")":
|
|
350
|
+
case "b":
|
|
351
|
+
return isInner ? innerParen : aParen;
|
|
352
|
+
case "{":
|
|
353
|
+
case "}":
|
|
354
|
+
case "B":
|
|
355
|
+
return isInner ? innerBrace : aBrace;
|
|
356
|
+
case "[":
|
|
357
|
+
case "]":
|
|
358
|
+
return isInner ? innerBracket : aBracket;
|
|
359
|
+
case "<":
|
|
360
|
+
case ">":
|
|
361
|
+
return isInner ? innerAngle : aAngle;
|
|
362
|
+
default:
|
|
363
|
+
return null;
|
|
364
|
+
}
|
|
365
|
+
}
|