@vim-villain/engine 0.0.0-stage → 0.4.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/CHANGELOG.md +66 -0
- package/DIVERGENCES.md +51 -0
- package/LICENSE +21 -0
- package/README.md +298 -2
- package/dist/buffer.d.ts +55 -0
- package/dist/buffer.d.ts.map +1 -0
- package/dist/buffer.js +191 -0
- package/dist/buffer.js.map +1 -0
- package/dist/constants.d.ts +16 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +16 -0
- package/dist/constants.js.map +1 -0
- package/dist/engine.d.ts +6 -0
- package/dist/engine.d.ts.map +1 -0
- package/dist/engine.js +315 -0
- package/dist/engine.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/insert.d.ts +25 -0
- package/dist/insert.d.ts.map +1 -0
- package/dist/insert.js +364 -0
- package/dist/insert.js.map +1 -0
- package/dist/keys.d.ts +20 -0
- package/dist/keys.d.ts.map +1 -0
- package/dist/keys.js +41 -0
- package/dist/keys.js.map +1 -0
- package/dist/motions.d.ts +32 -0
- package/dist/motions.d.ts.map +1 -0
- package/dist/motions.js +462 -0
- package/dist/motions.js.map +1 -0
- package/dist/operators.d.ts +25 -0
- package/dist/operators.d.ts.map +1 -0
- package/dist/operators.js +648 -0
- package/dist/operators.js.map +1 -0
- package/dist/state.d.ts +22 -0
- package/dist/state.d.ts.map +1 -0
- package/dist/state.js +122 -0
- package/dist/state.js.map +1 -0
- package/dist/types.d.ts +185 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +8 -0
- package/dist/types.js.map +1 -0
- package/package.json +47 -4
|
@@ -0,0 +1,648 @@
|
|
|
1
|
+
import { clampPosition, firstNonBlank, isBlankCharacter, lineLength, positionsEqual, textBetween, toChars, } from "./buffer.js";
|
|
2
|
+
import { MAX_COUNT, SHIFT_WIDTH, TAB_WIDTH } from "./constants.js";
|
|
3
|
+
import { applyMotion, executeFind, inverseFindCommand, wordEndAt, wordForwardOperatorEnd, } from "./motions.js";
|
|
4
|
+
import { insertAutoIndentFor, startChangeInsert, } from "./insert.js";
|
|
5
|
+
import { cloneState } from "./state.js";
|
|
6
|
+
const OPERATOR_START_KEYS = new Set([
|
|
7
|
+
"d",
|
|
8
|
+
"c",
|
|
9
|
+
"y",
|
|
10
|
+
">",
|
|
11
|
+
"<",
|
|
12
|
+
]);
|
|
13
|
+
export function isOperatorStartKey(key) {
|
|
14
|
+
return OPERATOR_START_KEYS.has(key);
|
|
15
|
+
}
|
|
16
|
+
export function isCaseOperator(operator) {
|
|
17
|
+
return operator === "gu" || operator === "gU";
|
|
18
|
+
}
|
|
19
|
+
/** Start an operator-pending command. `count` is 0 when no count was given. */
|
|
20
|
+
export function startOperator(state, operator, count) {
|
|
21
|
+
state.pendingOperator = operator;
|
|
22
|
+
state.pendingOperatorCount = Math.max(0, Math.min(MAX_COUNT, count));
|
|
23
|
+
state.pendingCount = 0;
|
|
24
|
+
state.pendingG = false;
|
|
25
|
+
state.pendingFind = null;
|
|
26
|
+
state.pendingTextObject = null;
|
|
27
|
+
state.preferredColumn = null;
|
|
28
|
+
}
|
|
29
|
+
function effectiveCount(operatorCount, motionCount) {
|
|
30
|
+
const op = operatorCount > 0 ? operatorCount : 1;
|
|
31
|
+
const motion = motionCount > 0 ? motionCount : 1;
|
|
32
|
+
return Math.min(MAX_COUNT, op * motion);
|
|
33
|
+
}
|
|
34
|
+
function hasCount(operatorCount, motionCount) {
|
|
35
|
+
return operatorCount > 0 || motionCount > 0;
|
|
36
|
+
}
|
|
37
|
+
function normalCursor(state, position) {
|
|
38
|
+
return clampPosition(state.buffer, position, false);
|
|
39
|
+
}
|
|
40
|
+
function charRange(start, end) {
|
|
41
|
+
const comparison = start.line === end.line
|
|
42
|
+
? start.col - end.col
|
|
43
|
+
: start.line - end.line;
|
|
44
|
+
const from = comparison <= 0 ? start : end;
|
|
45
|
+
const to = comparison <= 0 ? end : start;
|
|
46
|
+
if (from.line === to.line && from.col === to.col) {
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
return { kind: "char", start: from, end: to };
|
|
50
|
+
}
|
|
51
|
+
function lineRange(first, second) {
|
|
52
|
+
return {
|
|
53
|
+
kind: "line",
|
|
54
|
+
startLine: Math.min(first, second),
|
|
55
|
+
endLine: Math.max(first, second),
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
function rangeForMotion(state, motionKey, operatorCount, motionCount) {
|
|
59
|
+
const count = effectiveCount(operatorCount, motionCount);
|
|
60
|
+
const cursor = state.cursor;
|
|
61
|
+
const line = state.buffer[cursor.line] ?? "";
|
|
62
|
+
const length = lineLength(line);
|
|
63
|
+
switch (motionKey) {
|
|
64
|
+
case "h": {
|
|
65
|
+
const start = { line: cursor.line, col: Math.max(0, cursor.col - count) };
|
|
66
|
+
return charRange(start, cursor);
|
|
67
|
+
}
|
|
68
|
+
case "l": {
|
|
69
|
+
const end = {
|
|
70
|
+
line: cursor.line,
|
|
71
|
+
col: Math.min(length, cursor.col + count),
|
|
72
|
+
};
|
|
73
|
+
return charRange(cursor, end);
|
|
74
|
+
}
|
|
75
|
+
case "0":
|
|
76
|
+
return charRange({ line: cursor.line, col: 0 }, cursor);
|
|
77
|
+
case "^": {
|
|
78
|
+
const first = firstNonBlank(line);
|
|
79
|
+
if (first === cursor.col) {
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
return first < cursor.col
|
|
83
|
+
? charRange({ line: cursor.line, col: first }, cursor)
|
|
84
|
+
: charRange(cursor, { line: cursor.line, col: first });
|
|
85
|
+
}
|
|
86
|
+
case "$": {
|
|
87
|
+
if (hasCount(operatorCount, motionCount) && cursor.line >= state.buffer.length - 1) {
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
const offset = (hasCount(operatorCount, motionCount) ? count : 1) - 1;
|
|
91
|
+
const targetLine = Math.min(state.buffer.length - 1, cursor.line + offset);
|
|
92
|
+
return {
|
|
93
|
+
kind: "char",
|
|
94
|
+
start: cursor,
|
|
95
|
+
end: {
|
|
96
|
+
line: targetLine,
|
|
97
|
+
col: lineLength(state.buffer[targetLine] ?? ""),
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
case "j":
|
|
102
|
+
case "k": {
|
|
103
|
+
const targetLine = Math.max(0, Math.min(state.buffer.length - 1, cursor.line + (motionKey === "j" ? count : -count)));
|
|
104
|
+
if (targetLine === cursor.line) {
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
return lineRange(cursor.line, targetLine);
|
|
108
|
+
}
|
|
109
|
+
case "G": {
|
|
110
|
+
const raw = hasCount(operatorCount, motionCount) ? count : 0;
|
|
111
|
+
const target = raw > 0
|
|
112
|
+
? Math.min(raw - 1, state.buffer.length - 1)
|
|
113
|
+
: state.buffer.length - 1;
|
|
114
|
+
return lineRange(cursor.line, target);
|
|
115
|
+
}
|
|
116
|
+
case "gg": {
|
|
117
|
+
const raw = hasCount(operatorCount, motionCount) ? count : 0;
|
|
118
|
+
const target = raw > 0 ? Math.min(raw - 1, state.buffer.length - 1) : 0;
|
|
119
|
+
return lineRange(cursor.line, target);
|
|
120
|
+
}
|
|
121
|
+
case "w":
|
|
122
|
+
case "W": {
|
|
123
|
+
const end = wordForwardOperatorEnd(state.buffer, cursor, motionKey === "W", count);
|
|
124
|
+
return charRange(cursor, end);
|
|
125
|
+
}
|
|
126
|
+
case "b":
|
|
127
|
+
case "B": {
|
|
128
|
+
const probe = cloneState(state);
|
|
129
|
+
applyMotion(probe, motionKey, count);
|
|
130
|
+
return charRange(probe.cursor, cursor);
|
|
131
|
+
}
|
|
132
|
+
case "e":
|
|
133
|
+
case "E": {
|
|
134
|
+
const probe = cloneState(state);
|
|
135
|
+
applyMotion(probe, motionKey, count);
|
|
136
|
+
const end = {
|
|
137
|
+
line: probe.cursor.line,
|
|
138
|
+
col: Math.min(lineLength(state.buffer[probe.cursor.line] ?? ""), probe.cursor.col + 1),
|
|
139
|
+
};
|
|
140
|
+
return charRange(cursor, end);
|
|
141
|
+
}
|
|
142
|
+
default:
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
function rangeFromFind(original, found, command) {
|
|
147
|
+
if (command === "f" || command === "t") {
|
|
148
|
+
return charRange(original, {
|
|
149
|
+
line: original.line,
|
|
150
|
+
col: found.col + 1,
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
return charRange(found, original);
|
|
154
|
+
}
|
|
155
|
+
function setRegister(state, register) {
|
|
156
|
+
state.registers.unnamed = {
|
|
157
|
+
lines: register.lines.slice(),
|
|
158
|
+
linewise: register.linewise,
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
/** Vim's empty charwise register, e.g. `y$` or `yh` on an empty line. */
|
|
162
|
+
function setEmptyCharRegister(state) {
|
|
163
|
+
setRegister(state, { lines: [""], linewise: false });
|
|
164
|
+
}
|
|
165
|
+
function charRegister(state, start, end) {
|
|
166
|
+
return {
|
|
167
|
+
lines: textBetween(state.buffer, start, end).split("\n"),
|
|
168
|
+
linewise: false,
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
function lineRegister(state, startLine, endLine) {
|
|
172
|
+
return {
|
|
173
|
+
lines: state.buffer.slice(startLine, endLine + 1),
|
|
174
|
+
linewise: true,
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
function firstCursorInLine(buffer, line) {
|
|
178
|
+
const target = Math.max(0, Math.min(buffer.length - 1, line));
|
|
179
|
+
const text = buffer[target] ?? "";
|
|
180
|
+
return {
|
|
181
|
+
line: target,
|
|
182
|
+
col: Math.min(firstNonBlank(text), Math.max(0, lineLength(text) - 1)),
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
function deleteCharRange(state, start, end) {
|
|
186
|
+
const next = state.buffer.slice();
|
|
187
|
+
const startChars = toChars(next[start.line] ?? "");
|
|
188
|
+
const endChars = toChars(next[end.line] ?? "");
|
|
189
|
+
const merged = startChars.slice(0, start.col).join("") +
|
|
190
|
+
endChars.slice(end.col).join("");
|
|
191
|
+
next.splice(start.line, end.line - start.line + 1, merged);
|
|
192
|
+
if (next.length === 0) {
|
|
193
|
+
next.push("");
|
|
194
|
+
}
|
|
195
|
+
state.buffer = next;
|
|
196
|
+
state.cursor = normalCursor(state, start);
|
|
197
|
+
}
|
|
198
|
+
function deleteLineRange(state, startLine, endLine, originalColumn) {
|
|
199
|
+
const next = state.buffer.slice();
|
|
200
|
+
next.splice(startLine, endLine - startLine + 1);
|
|
201
|
+
if (next.length === 0) {
|
|
202
|
+
next.push("");
|
|
203
|
+
}
|
|
204
|
+
state.buffer = next;
|
|
205
|
+
const targetLine = Math.min(startLine, next.length - 1);
|
|
206
|
+
const text = next[targetLine] ?? "";
|
|
207
|
+
state.cursor = {
|
|
208
|
+
line: targetLine,
|
|
209
|
+
col: Math.min(originalColumn, Math.max(0, lineLength(text) - 1)),
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
function changeCharRange(state, start, end) {
|
|
213
|
+
deleteCharRange(state, start, end);
|
|
214
|
+
// `deleteCharRange` clamps to an existing character for normal mode. A change
|
|
215
|
+
// can start at the append position (for example `cl` on the last character),
|
|
216
|
+
// so restore the range start and allow insert mode's one-past-EOL column.
|
|
217
|
+
state.cursor = clampPosition(state.buffer, start, true);
|
|
218
|
+
startChangeInsert(state, null);
|
|
219
|
+
}
|
|
220
|
+
function changeLineRange(state, startLine, endLine) {
|
|
221
|
+
const indent = insertAutoIndentFor(state.buffer[startLine] ?? "");
|
|
222
|
+
const next = state.buffer.slice();
|
|
223
|
+
next.splice(startLine, endLine - startLine + 1, indent);
|
|
224
|
+
if (next.length === 0) {
|
|
225
|
+
next.push(indent);
|
|
226
|
+
}
|
|
227
|
+
state.buffer = next;
|
|
228
|
+
startChangeInsert(state, startLine, indent);
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* ASCII-only case conversion, matching the engine's fixed ASCII word classes.
|
|
232
|
+
* Non-ASCII code points are left unchanged; see DIVERGENCES.md.
|
|
233
|
+
*/
|
|
234
|
+
function asciiCase(char, upper) {
|
|
235
|
+
const code = char.codePointAt(0) ?? 0;
|
|
236
|
+
if (upper && code >= 0x61 && code <= 0x7a) {
|
|
237
|
+
return String.fromCodePoint(code - 0x20);
|
|
238
|
+
}
|
|
239
|
+
if (!upper && code >= 0x41 && code <= 0x5a) {
|
|
240
|
+
return String.fromCodePoint(code + 0x20);
|
|
241
|
+
}
|
|
242
|
+
return char;
|
|
243
|
+
}
|
|
244
|
+
function applyCharCase(state, start, end, upper) {
|
|
245
|
+
const next = state.buffer.slice();
|
|
246
|
+
for (let lineIndex = start.line; lineIndex <= end.line; lineIndex += 1) {
|
|
247
|
+
const text = next[lineIndex] ?? "";
|
|
248
|
+
const chars = toChars(text);
|
|
249
|
+
const from = lineIndex === start.line ? start.col : 0;
|
|
250
|
+
const to = lineIndex === end.line ? end.col : chars.length;
|
|
251
|
+
const transformed = chars
|
|
252
|
+
.map((char, index) => index < from || index >= to ? char : asciiCase(char, upper))
|
|
253
|
+
.join("");
|
|
254
|
+
next[lineIndex] = transformed;
|
|
255
|
+
}
|
|
256
|
+
state.buffer = next;
|
|
257
|
+
}
|
|
258
|
+
function applyLineCase(state, startLine, endLine, upper) {
|
|
259
|
+
const next = state.buffer.slice();
|
|
260
|
+
for (let lineIndex = startLine; lineIndex <= endLine; lineIndex += 1) {
|
|
261
|
+
const text = next[lineIndex] ?? "";
|
|
262
|
+
next[lineIndex] = toChars(text)
|
|
263
|
+
.map((char) => asciiCase(char, upper))
|
|
264
|
+
.join("");
|
|
265
|
+
}
|
|
266
|
+
state.buffer = next;
|
|
267
|
+
}
|
|
268
|
+
function applyCaseRange(state, range, upper, doubled) {
|
|
269
|
+
const original = { ...state.cursor };
|
|
270
|
+
if (range.kind === "char") {
|
|
271
|
+
applyCharCase(state, range.start, range.end, upper);
|
|
272
|
+
state.cursor = normalCursor(state, range.start);
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
applyLineCase(state, range.startLine, range.endLine, upper);
|
|
276
|
+
if (doubled) {
|
|
277
|
+
state.cursor = firstCursorInLine(state.buffer, range.startLine);
|
|
278
|
+
}
|
|
279
|
+
else {
|
|
280
|
+
const text = state.buffer[range.startLine] ?? "";
|
|
281
|
+
state.cursor = {
|
|
282
|
+
line: range.startLine,
|
|
283
|
+
col: Math.min(original.col, Math.max(0, lineLength(text) - 1)),
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
function visualWidth(text) {
|
|
288
|
+
let width = 0;
|
|
289
|
+
for (const char of toChars(text)) {
|
|
290
|
+
if (char === "\t") {
|
|
291
|
+
width += TAB_WIDTH - (width % TAB_WIDTH);
|
|
292
|
+
}
|
|
293
|
+
else {
|
|
294
|
+
width += 1;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
return width;
|
|
298
|
+
}
|
|
299
|
+
function leadingWhitespace(text) {
|
|
300
|
+
const chars = toChars(text);
|
|
301
|
+
let index = 0;
|
|
302
|
+
while (index < chars.length) {
|
|
303
|
+
const char = chars[index];
|
|
304
|
+
if (char !== " " && char !== "\t") {
|
|
305
|
+
break;
|
|
306
|
+
}
|
|
307
|
+
index += 1;
|
|
308
|
+
}
|
|
309
|
+
return {
|
|
310
|
+
width: visualWidth(chars.slice(0, index).join("")),
|
|
311
|
+
rest: chars.slice(index).join(""),
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
function shiftIndentLine(text, delta) {
|
|
315
|
+
if (text === "") {
|
|
316
|
+
return text;
|
|
317
|
+
}
|
|
318
|
+
const leading = leadingWhitespace(text);
|
|
319
|
+
const width = Math.max(0, leading.width + delta);
|
|
320
|
+
return " ".repeat(width) + leading.rest;
|
|
321
|
+
}
|
|
322
|
+
function codepointIndexForVisualColumn(text, column) {
|
|
323
|
+
const chars = toChars(text);
|
|
324
|
+
if (chars.length === 0) {
|
|
325
|
+
return 0;
|
|
326
|
+
}
|
|
327
|
+
let width = 0;
|
|
328
|
+
for (let index = 0; index < chars.length; index += 1) {
|
|
329
|
+
const char = chars[index] ?? "";
|
|
330
|
+
const charWidth = char === "\t" ? TAB_WIDTH - (width % TAB_WIDTH) : 1;
|
|
331
|
+
if (width + charWidth > column) {
|
|
332
|
+
return index;
|
|
333
|
+
}
|
|
334
|
+
width += charWidth;
|
|
335
|
+
}
|
|
336
|
+
return chars.length - 1;
|
|
337
|
+
}
|
|
338
|
+
function applyIndentRange(state, range, right) {
|
|
339
|
+
const original = { ...state.cursor };
|
|
340
|
+
let startLine;
|
|
341
|
+
let endLine;
|
|
342
|
+
if (range.kind === "line") {
|
|
343
|
+
startLine = range.startLine;
|
|
344
|
+
endLine = range.endLine;
|
|
345
|
+
}
|
|
346
|
+
else {
|
|
347
|
+
startLine = range.start.line;
|
|
348
|
+
endLine =
|
|
349
|
+
range.end.col === 0 && range.end.line > range.start.line
|
|
350
|
+
? range.end.line - 1
|
|
351
|
+
: range.end.line;
|
|
352
|
+
}
|
|
353
|
+
const originalLine = state.buffer[original.line] ?? "";
|
|
354
|
+
const originalColumn = visualWidth(toChars(originalLine).slice(0, original.col).join(""));
|
|
355
|
+
const next = state.buffer.slice();
|
|
356
|
+
const delta = right ? SHIFT_WIDTH : -SHIFT_WIDTH;
|
|
357
|
+
for (let lineIndex = startLine; lineIndex <= endLine; lineIndex += 1) {
|
|
358
|
+
next[lineIndex] = shiftIndentLine(next[lineIndex] ?? "", delta);
|
|
359
|
+
}
|
|
360
|
+
state.buffer = next;
|
|
361
|
+
const targetLine = next[startLine] ?? "";
|
|
362
|
+
state.cursor = {
|
|
363
|
+
line: startLine,
|
|
364
|
+
col: Math.min(codepointIndexForVisualColumn(targetLine, originalColumn), Math.max(0, lineLength(targetLine) - 1)),
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
function applyRange(state, operator, range, doubled) {
|
|
368
|
+
if (operator === "y") {
|
|
369
|
+
if (range.kind === "char") {
|
|
370
|
+
setRegister(state, charRegister(state, range.start, range.end));
|
|
371
|
+
state.cursor = normalCursor(state, range.start);
|
|
372
|
+
}
|
|
373
|
+
else {
|
|
374
|
+
setRegister(state, lineRegister(state, range.startLine, range.endLine));
|
|
375
|
+
const text = state.buffer[range.startLine] ?? "";
|
|
376
|
+
state.cursor = {
|
|
377
|
+
line: range.startLine,
|
|
378
|
+
col: Math.min(state.cursor.col, Math.max(0, lineLength(text) - 1)),
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
383
|
+
if (operator === "d") {
|
|
384
|
+
if (range.kind === "char") {
|
|
385
|
+
// Vim does not touch the register when the range is empty (`d$` on an
|
|
386
|
+
// empty line, `d0` at column 0).
|
|
387
|
+
if (positionsEqual(range.start, range.end)) {
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
390
|
+
setRegister(state, charRegister(state, range.start, range.end));
|
|
391
|
+
deleteCharRange(state, range.start, range.end);
|
|
392
|
+
}
|
|
393
|
+
else {
|
|
394
|
+
const originalColumn = state.cursor.col;
|
|
395
|
+
setRegister(state, lineRegister(state, range.startLine, range.endLine));
|
|
396
|
+
deleteLineRange(state, range.startLine, range.endLine, originalColumn);
|
|
397
|
+
}
|
|
398
|
+
return;
|
|
399
|
+
}
|
|
400
|
+
if (operator === "c") {
|
|
401
|
+
if (range.kind === "char") {
|
|
402
|
+
setRegister(state, charRegister(state, range.start, range.end));
|
|
403
|
+
changeCharRange(state, range.start, range.end);
|
|
404
|
+
}
|
|
405
|
+
else {
|
|
406
|
+
setRegister(state, lineRegister(state, range.startLine, range.endLine));
|
|
407
|
+
changeLineRange(state, range.startLine, range.endLine);
|
|
408
|
+
}
|
|
409
|
+
return;
|
|
410
|
+
}
|
|
411
|
+
if (operator === ">") {
|
|
412
|
+
applyIndentRange(state, range, true);
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
if (operator === "<") {
|
|
416
|
+
applyIndentRange(state, range, false);
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
if (operator === "gu") {
|
|
420
|
+
applyCaseRange(state, range, false, doubled);
|
|
421
|
+
return;
|
|
422
|
+
}
|
|
423
|
+
if (operator === "gU") {
|
|
424
|
+
applyCaseRange(state, range, true, doubled);
|
|
425
|
+
return;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
/**
|
|
429
|
+
* Motions that can be valid yet cover no characters. `c` still enters insert
|
|
430
|
+
* mode for these (`c0` at column 0, `ch` at column 0, `c$` on an empty line),
|
|
431
|
+
* while `d`/`y` are no-ops.
|
|
432
|
+
*/
|
|
433
|
+
const VALID_EMPTY_CHANGE_MOTIONS = new Set([
|
|
434
|
+
"h",
|
|
435
|
+
"l",
|
|
436
|
+
"0",
|
|
437
|
+
"^",
|
|
438
|
+
"w",
|
|
439
|
+
"W",
|
|
440
|
+
"e",
|
|
441
|
+
"E",
|
|
442
|
+
]);
|
|
443
|
+
/**
|
|
444
|
+
* Vim's `cw`/`cW` are not plain operator+motion compositions.
|
|
445
|
+
*
|
|
446
|
+
* - On a non-blank, the change runs to the end of the current word; if the
|
|
447
|
+
* cursor is already on the word's last character, only that character is
|
|
448
|
+
* changed.
|
|
449
|
+
* - With a count greater than 1, the change uses `e`/`E` semantics, so the
|
|
450
|
+
* last word does not take trailing whitespace.
|
|
451
|
+
* - On blanks it behaves like the matching `w`/`W` operator range.
|
|
452
|
+
* - On an empty line `cw` changes the line like `cc`; on an empty one-line
|
|
453
|
+
* buffer it simply enters insert mode.
|
|
454
|
+
*/
|
|
455
|
+
function completeChangeWord(state, operatorCount, motionCount, bigWord) {
|
|
456
|
+
const lineIndex = state.cursor.line;
|
|
457
|
+
const line = state.buffer[lineIndex] ?? "";
|
|
458
|
+
const length = lineLength(line);
|
|
459
|
+
const chars = toChars(line);
|
|
460
|
+
const char = chars[state.cursor.col] ?? "";
|
|
461
|
+
if (length === 0) {
|
|
462
|
+
if (state.buffer.length === 1) {
|
|
463
|
+
startChangeInsert(state, null);
|
|
464
|
+
return;
|
|
465
|
+
}
|
|
466
|
+
setRegister(state, lineRegister(state, lineIndex, lineIndex));
|
|
467
|
+
changeLineRange(state, lineIndex, lineIndex);
|
|
468
|
+
return;
|
|
469
|
+
}
|
|
470
|
+
const count = effectiveCount(operatorCount, motionCount);
|
|
471
|
+
if (isBlankCharacter(char)) {
|
|
472
|
+
const range = rangeForMotion(state, bigWord ? "W" : "w", operatorCount, motionCount);
|
|
473
|
+
if (range === null) {
|
|
474
|
+
startChangeInsert(state, null);
|
|
475
|
+
return;
|
|
476
|
+
}
|
|
477
|
+
applyRange(state, "c", range, false);
|
|
478
|
+
return;
|
|
479
|
+
}
|
|
480
|
+
if (count > 1) {
|
|
481
|
+
// `2cw` changes two words without taking the whitespace after the second
|
|
482
|
+
// word. Advance word by word rather than using `e` with a count, which
|
|
483
|
+
// would skip a single-character word such as `.` in `foo.bar`.
|
|
484
|
+
let end = wordEndAt(state.buffer, state.cursor, bigWord);
|
|
485
|
+
for (let word = 1; word < count; word += 1) {
|
|
486
|
+
const probe = cloneState(state);
|
|
487
|
+
probe.cursor = { line: end.line, col: end.col };
|
|
488
|
+
if (!applyMotion(probe, bigWord ? "W" : "w", 1)) {
|
|
489
|
+
break;
|
|
490
|
+
}
|
|
491
|
+
if (probe.cursor.line === end.line &&
|
|
492
|
+
probe.cursor.col <= end.col) {
|
|
493
|
+
break;
|
|
494
|
+
}
|
|
495
|
+
end = wordEndAt(state.buffer, probe.cursor, bigWord);
|
|
496
|
+
}
|
|
497
|
+
const range = charRange(state.cursor, {
|
|
498
|
+
line: end.line,
|
|
499
|
+
col: end.col + 1,
|
|
500
|
+
});
|
|
501
|
+
if (range === null) {
|
|
502
|
+
startChangeInsert(state, null);
|
|
503
|
+
return;
|
|
504
|
+
}
|
|
505
|
+
applyRange(state, "c", range, false);
|
|
506
|
+
return;
|
|
507
|
+
}
|
|
508
|
+
const end = wordEndAt(state.buffer, state.cursor, bigWord);
|
|
509
|
+
const endCol = positionsEqual(end, state.cursor)
|
|
510
|
+
? Math.min(length, state.cursor.col + 1)
|
|
511
|
+
: Math.min(length, end.col + 1);
|
|
512
|
+
const range = charRange(state.cursor, { line: lineIndex, col: endCol });
|
|
513
|
+
if (range === null) {
|
|
514
|
+
startChangeInsert(state, null);
|
|
515
|
+
return;
|
|
516
|
+
}
|
|
517
|
+
applyRange(state, "c", range, false);
|
|
518
|
+
}
|
|
519
|
+
/** Apply one operator over a direct motion. */
|
|
520
|
+
export function completeOperatorMotion(state, operator, motionKey, operatorCount, motionCount) {
|
|
521
|
+
if (operator === "c" && (motionKey === "w" || motionKey === "W")) {
|
|
522
|
+
completeChangeWord(state, operatorCount, motionCount, motionKey === "W");
|
|
523
|
+
return;
|
|
524
|
+
}
|
|
525
|
+
// On an empty line in a multi-line buffer, `w`/`W` is a linewise motion for
|
|
526
|
+
// every operator. On a one-line buffer it has no lines to move across.
|
|
527
|
+
if ((operator === "d" || operator === "y") &&
|
|
528
|
+
(motionKey === "w" || motionKey === "W")) {
|
|
529
|
+
const lineIndex = state.cursor.line;
|
|
530
|
+
const line = state.buffer[lineIndex] ?? "";
|
|
531
|
+
if (lineLength(line) === 0) {
|
|
532
|
+
if (state.buffer.length === 1) {
|
|
533
|
+
if (operator === "y") {
|
|
534
|
+
setEmptyCharRegister(state);
|
|
535
|
+
}
|
|
536
|
+
return;
|
|
537
|
+
}
|
|
538
|
+
setRegister(state, lineRegister(state, lineIndex, lineIndex));
|
|
539
|
+
if (operator === "d") {
|
|
540
|
+
deleteLineRange(state, lineIndex, lineIndex, state.cursor.col);
|
|
541
|
+
}
|
|
542
|
+
return;
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
const range = rangeForMotion(state, motionKey, operatorCount, motionCount);
|
|
546
|
+
if (range === null) {
|
|
547
|
+
if (operator === "y" &&
|
|
548
|
+
VALID_EMPTY_CHANGE_MOTIONS.has(motionKey)) {
|
|
549
|
+
// `y0`, `yh`, and `y^` on an empty range set Vim's empty charwise
|
|
550
|
+
// register. `yb` at column 0 is a failed motion and leaves it untouched.
|
|
551
|
+
setEmptyCharRegister(state);
|
|
552
|
+
return;
|
|
553
|
+
}
|
|
554
|
+
if (operator === "c" &&
|
|
555
|
+
VALID_EMPTY_CHANGE_MOTIONS.has(motionKey)) {
|
|
556
|
+
// `ce`/`cE` on an empty line still set the empty charwise register,
|
|
557
|
+
// unlike `c0`/`ch`, which only enter insert mode.
|
|
558
|
+
if (motionKey === "e" || motionKey === "E") {
|
|
559
|
+
setEmptyCharRegister(state);
|
|
560
|
+
}
|
|
561
|
+
startChangeInsert(state, null);
|
|
562
|
+
}
|
|
563
|
+
return;
|
|
564
|
+
}
|
|
565
|
+
applyRange(state, operator, range, false);
|
|
566
|
+
}
|
|
567
|
+
/** Apply a doubled operator over the current line(s). */
|
|
568
|
+
export function completeDoubledOperator(state, operator, operatorCount, motionCount) {
|
|
569
|
+
const count = effectiveCount(operatorCount, motionCount);
|
|
570
|
+
const startLine = state.cursor.line;
|
|
571
|
+
const endLine = Math.min(state.buffer.length - 1, startLine + count - 1);
|
|
572
|
+
// Vim fails a multi-line operator when the clamped range is still only the
|
|
573
|
+
// current line: `2dd` on a one-line buffer, or `2yy` on the last line, is a
|
|
574
|
+
// no-op rather than a one-line operation.
|
|
575
|
+
if (count > 1 && endLine === startLine) {
|
|
576
|
+
return;
|
|
577
|
+
}
|
|
578
|
+
applyRange(state, operator, lineRange(startLine, endLine), true);
|
|
579
|
+
}
|
|
580
|
+
/** Apply an operator over an explicit f/t/F/T search target. */
|
|
581
|
+
export function completeOperatorFind(state, operator, command, char, operatorCount, motionCount) {
|
|
582
|
+
const count = effectiveCount(operatorCount, motionCount);
|
|
583
|
+
const probe = cloneState(state);
|
|
584
|
+
const found = executeFind(probe, command, char, count);
|
|
585
|
+
// Vim records failed searches too.
|
|
586
|
+
state.lastFind = { command, char };
|
|
587
|
+
if (!found) {
|
|
588
|
+
return;
|
|
589
|
+
}
|
|
590
|
+
const range = rangeFromFind(state.cursor, probe.cursor, command);
|
|
591
|
+
if (range === null) {
|
|
592
|
+
return;
|
|
593
|
+
}
|
|
594
|
+
applyRange(state, operator, range, false);
|
|
595
|
+
}
|
|
596
|
+
/** Apply an operator over `;` or `,`. */
|
|
597
|
+
export function completeOperatorRepeatFind(state, operator, reverse, operatorCount, motionCount) {
|
|
598
|
+
if (state.lastFind === null) {
|
|
599
|
+
return;
|
|
600
|
+
}
|
|
601
|
+
const count = effectiveCount(operatorCount, motionCount);
|
|
602
|
+
const command = reverse
|
|
603
|
+
? inverseFindCommand(state.lastFind.command)
|
|
604
|
+
: state.lastFind.command;
|
|
605
|
+
const forceMoveFirst = count === 1 && (command === "t" || command === "T");
|
|
606
|
+
const probe = cloneState(state);
|
|
607
|
+
const found = executeFind(probe, command, state.lastFind.char, count, forceMoveFirst);
|
|
608
|
+
if (!found) {
|
|
609
|
+
return;
|
|
610
|
+
}
|
|
611
|
+
const range = rangeFromFind(state.cursor, probe.cursor, command);
|
|
612
|
+
if (range === null) {
|
|
613
|
+
return;
|
|
614
|
+
}
|
|
615
|
+
applyRange(state, operator, range, false);
|
|
616
|
+
}
|
|
617
|
+
/** Direct normal-mode `~`: toggle count characters, advance along the line. */
|
|
618
|
+
export function toggleCaseAtCursor(state, count) {
|
|
619
|
+
const lineIndex = state.cursor.line;
|
|
620
|
+
const line = state.buffer[lineIndex] ?? "";
|
|
621
|
+
const chars = toChars(line);
|
|
622
|
+
if (chars.length === 0) {
|
|
623
|
+
return;
|
|
624
|
+
}
|
|
625
|
+
const start = state.cursor.col;
|
|
626
|
+
const end = Math.min(chars.length, start + Math.max(1, count));
|
|
627
|
+
const next = chars.map((char, index) => {
|
|
628
|
+
if (index < start || index >= end) {
|
|
629
|
+
return char;
|
|
630
|
+
}
|
|
631
|
+
const code = char.codePointAt(0) ?? 0;
|
|
632
|
+
if (code >= 0x41 && code <= 0x5a) {
|
|
633
|
+
return String.fromCodePoint(code + 0x20);
|
|
634
|
+
}
|
|
635
|
+
if (code >= 0x61 && code <= 0x7a) {
|
|
636
|
+
return String.fromCodePoint(code - 0x20);
|
|
637
|
+
}
|
|
638
|
+
return char;
|
|
639
|
+
});
|
|
640
|
+
const nextBuffer = state.buffer.slice();
|
|
641
|
+
nextBuffer[lineIndex] = next.join("");
|
|
642
|
+
state.buffer = nextBuffer;
|
|
643
|
+
state.cursor = {
|
|
644
|
+
line: lineIndex,
|
|
645
|
+
col: Math.min(chars.length - 1, start + Math.max(1, count)),
|
|
646
|
+
};
|
|
647
|
+
}
|
|
648
|
+
//# sourceMappingURL=operators.js.map
|