@ftrdotdev/pi-tui 0.2.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.
Files changed (50) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +63 -0
  3. package/package.json +80 -0
  4. package/src/autocomplete-contract.test.ts +126 -0
  5. package/src/autocomplete-contract.ts +91 -0
  6. package/src/commands/git-execution.test.ts +69 -0
  7. package/src/commands/git-execution.ts +198 -0
  8. package/src/commands/git-status.test.ts +379 -0
  9. package/src/commands/git-status.ts +320 -0
  10. package/src/components/bordered-box.test.ts +136 -0
  11. package/src/components/bordered-box.ts +106 -0
  12. package/src/components/define-component.ts +33 -0
  13. package/src/components/index.ts +35 -0
  14. package/src/config.test.ts +469 -0
  15. package/src/config.ts +307 -0
  16. package/src/frame/box-frame.test.ts +44 -0
  17. package/src/frame/box-frame.ts +160 -0
  18. package/src/icons.ts +203 -0
  19. package/src/index.ts +359 -0
  20. package/src/runtime.ts +256 -0
  21. package/src/session-lifecycle.ts +41 -0
  22. package/src/state.test.ts +7 -0
  23. package/src/state.ts +90 -0
  24. package/src/telemetry.ts +301 -0
  25. package/src/ui/editor-tint.test.ts +70 -0
  26. package/src/ui/editor-tint.ts +78 -0
  27. package/src/ui/editor.ts +403 -0
  28. package/src/ui/footer.test.ts +350 -0
  29. package/src/ui/footer.ts +225 -0
  30. package/src/ui/header.test.ts +260 -0
  31. package/src/ui/header.ts +165 -0
  32. package/src/ui/preview.test.ts +639 -0
  33. package/src/ui/preview.ts +374 -0
  34. package/src/ui/render-helpers.ts +275 -0
  35. package/src/ui/settings-command.test.ts +218 -0
  36. package/src/ui/settings-command.ts +390 -0
  37. package/src/utils.ts +448 -0
  38. package/src/vim/editor-close-autocomplete.ts +26 -0
  39. package/src/vim/editor-host.test.ts +340 -0
  40. package/src/vim/editor-state.test.ts +385 -0
  41. package/src/vim/editor-state.ts +314 -0
  42. package/src/vim/mode-indicator.test.ts +27 -0
  43. package/src/vim/mode-indicator.ts +44 -0
  44. package/src/vim/selection-render.test.ts +77 -0
  45. package/src/vim/selection-render.ts +85 -0
  46. package/src/vim/text-model.test.ts +85 -0
  47. package/src/vim/text-model.ts +75 -0
  48. package/src/vim/vim-router.test.ts +50 -0
  49. package/src/vim/vim-router.ts +55 -0
  50. package/src/vim/word-motion.ts +96 -0
@@ -0,0 +1,385 @@
1
+ import { assert, it } from "@effect/vitest";
2
+ import { VimEditorState } from "./editor-state.ts";
3
+ import { makeRange, type Cursor, type Range, type VimTextModelShape } from "./text-model.ts";
4
+ import { wordBackward, wordEnd, wordForward } from "./word-motion.ts";
5
+
6
+ /**
7
+ * In-memory text model with real line/word geometry (London-school fake:
8
+ * stateful behavior, not a stub). It is provided to the pure state machine
9
+ * directly; the `VimTextModel` service tag wraps the same shape in layers.
10
+ */
11
+ export class FakeVimTextModel implements VimTextModelShape {
12
+ lines: string[];
13
+ cursor: Cursor;
14
+
15
+ constructor(lines: string[], cursor: Cursor = { line: 0, col: 0 }) {
16
+ this.lines = [...lines];
17
+ this.cursor = { ...cursor };
18
+ }
19
+
20
+ getText(): string {
21
+ return this.lines.join("\n");
22
+ }
23
+
24
+ getCursor(): Cursor {
25
+ return { ...this.cursor };
26
+ }
27
+
28
+ moveCursorTo(c: Cursor): void {
29
+ const line = Math.max(0, Math.min(c.line, this.lines.length - 1));
30
+ this.cursor = { line, col: Math.max(0, Math.min(c.col, this.lineLength(line))) };
31
+ }
32
+
33
+ getLineCount(): number {
34
+ return this.lines.length;
35
+ }
36
+
37
+ lineLength(line: number): number {
38
+ return this.lines[line]?.length ?? 0;
39
+ }
40
+
41
+ moveWordForward(from: Cursor): Cursor {
42
+ return wordForward(this.lines, from);
43
+ }
44
+
45
+ moveWordBackward(from: Cursor): Cursor {
46
+ return wordBackward(this.lines, from);
47
+ }
48
+
49
+ moveWordEnd(from: Cursor): Cursor {
50
+ return wordEnd(this.lines, from);
51
+ }
52
+
53
+ moveToLineStart(c: Cursor): Cursor {
54
+ return { line: c.line, col: 0 };
55
+ }
56
+
57
+ moveToLineEnd(c: Cursor): Cursor {
58
+ return { line: c.line, col: this.lineLength(c.line) };
59
+ }
60
+
61
+ private absoluteIndex(c: Cursor): number {
62
+ let idx = 0;
63
+ for (let i = 0; i < c.line; i++) idx += (this.lines[i]?.length ?? 0) + 1;
64
+ return idx + c.col;
65
+ }
66
+
67
+ deleteRange(range: Range): void {
68
+ const flat = this.getText();
69
+ const startIdx = this.absoluteIndex(range.start);
70
+ const endIdx = this.absoluteIndex(range.end);
71
+ this.lines = (flat.slice(0, startIdx) + flat.slice(endIdx)).split("\n");
72
+ this.moveCursorTo(range.start);
73
+ }
74
+
75
+ yankRange(range: Range): string {
76
+ const flat = this.getText();
77
+ return flat.slice(this.absoluteIndex(range.start), this.absoluteIndex(range.end));
78
+ }
79
+
80
+ replaceRange(range: Range, text: string): void {
81
+ const flat = this.getText();
82
+ const startIdx = this.absoluteIndex(range.start);
83
+ const endIdx = this.absoluteIndex(range.end);
84
+ this.lines = (flat.slice(0, startIdx) + text + flat.slice(endIdx)).split("\n");
85
+ this.moveCursorTo(range.start);
86
+ }
87
+
88
+ insertTextAtCursor(text: string): void {
89
+ const flat = this.getText();
90
+ const idx = this.absoluteIndex(this.cursor);
91
+ this.lines = (flat.slice(0, idx) + text + flat.slice(idx)).split("\n");
92
+ if (!text.includes("\n")) {
93
+ this.cursor = { line: this.cursor.line, col: this.cursor.col + text.length };
94
+ } else {
95
+ const parts = text.split("\n");
96
+ this.cursor = {
97
+ line: this.cursor.line + parts.length - 1,
98
+ col: parts[parts.length - 1]!.length,
99
+ };
100
+ }
101
+ }
102
+ }
103
+
104
+ const makeState = (lines: string[], cursor?: Cursor) => {
105
+ const model = new FakeVimTextModel(lines, cursor);
106
+ const state = new VimEditorState(model, () => true);
107
+ return { model, state };
108
+ };
109
+
110
+ /** Enter Normal mode from the initial Insert mode. */
111
+ const toNormal = (state: VimEditorState): void => {
112
+ const decision = state.step("\x1b");
113
+ assert.deepStrictEqual(decision, { kind: "consumed", enteredNormal: true });
114
+ };
115
+
116
+ // ---------------------------------------------------------------------------
117
+ // Gating
118
+ // ---------------------------------------------------------------------------
119
+
120
+ it("passes every input through when vim is off", () => {
121
+ const model = new FakeVimTextModel(["foo"]);
122
+ const state = new VimEditorState(model, () => false);
123
+ assert.deepStrictEqual(state.step("a"), { kind: "pass-through" });
124
+ assert.deepStrictEqual(state.step("\x1b"), { kind: "pass-through" });
125
+ assert.deepStrictEqual(state.step("\x1b[B"), { kind: "pass-through" });
126
+ assert.strictEqual(state.mode, "insert");
127
+ assert.strictEqual(model.getText(), "foo");
128
+ });
129
+
130
+ // ---------------------------------------------------------------------------
131
+ // Insert mode
132
+ // ---------------------------------------------------------------------------
133
+
134
+ it("insert mode passes printable keys through and stays in insert", () => {
135
+ const { model, state } = makeState(["foo"], { line: 0, col: 1 });
136
+ const decision = state.step("h");
137
+ assert.deepStrictEqual(decision, { kind: "pass-through" });
138
+ assert.strictEqual(state.mode, "insert");
139
+ assert.deepStrictEqual(model.getCursor(), { line: 0, col: 1 }); // untouched
140
+ });
141
+
142
+ it("escape enters normal mode from insert and signals the host", () => {
143
+ const { state } = makeState(["hello"]);
144
+ const decision = state.step("\x1b");
145
+ assert.deepStrictEqual(decision, { kind: "consumed", enteredNormal: true });
146
+ assert.strictEqual(state.mode, "normal");
147
+ });
148
+
149
+ // ---------------------------------------------------------------------------
150
+ // Normal mode: motions
151
+ // ---------------------------------------------------------------------------
152
+
153
+ it("moves the cursor with h/l/j/k in normal mode", () => {
154
+ const { model, state } = makeState(["foo", "bar"], { line: 0, col: 1 });
155
+ toNormal(state);
156
+ state.step("h");
157
+ assert.deepStrictEqual(model.getCursor(), { line: 0, col: 0 });
158
+ state.step("l");
159
+ assert.deepStrictEqual(model.getCursor(), { line: 0, col: 1 });
160
+ state.step("j");
161
+ assert.deepStrictEqual(model.getCursor(), { line: 1, col: 1 });
162
+ state.step("k");
163
+ assert.deepStrictEqual(model.getCursor(), { line: 0, col: 1 });
164
+ });
165
+
166
+ it("clamps h/l at the line edges and j/k at the last line", () => {
167
+ const { model, state } = makeState(["foo"], { line: 0, col: 0 });
168
+ toNormal(state);
169
+ state.step("h");
170
+ assert.deepStrictEqual(model.getCursor(), { line: 0, col: 0 });
171
+ state.step("k");
172
+ assert.deepStrictEqual(model.getCursor(), { line: 0, col: 0 });
173
+ state.step("$");
174
+ state.step("l");
175
+ assert.deepStrictEqual(model.getCursor(), { line: 0, col: 3 });
176
+ state.step("j");
177
+ assert.deepStrictEqual(model.getCursor(), { line: 0, col: 3 });
178
+ });
179
+
180
+ it("word motions w/b/e and line motions 0/$ target the right positions", () => {
181
+ const { model, state } = makeState(["foo bar baz"], { line: 0, col: 0 });
182
+ toNormal(state);
183
+ state.step("w");
184
+ assert.deepStrictEqual(model.getCursor(), { line: 0, col: 4 });
185
+ state.step("b");
186
+ assert.deepStrictEqual(model.getCursor(), { line: 0, col: 0 });
187
+ state.step("e");
188
+ assert.deepStrictEqual(model.getCursor(), { line: 0, col: 2 });
189
+ state.step("0");
190
+ assert.deepStrictEqual(model.getCursor(), { line: 0, col: 0 });
191
+ state.step("$");
192
+ assert.deepStrictEqual(model.getCursor(), { line: 0, col: 11 });
193
+ });
194
+
195
+ it("word motions move across lines", () => {
196
+ const { model, state } = makeState(["foo", "bar"], { line: 0, col: 3 });
197
+ toNormal(state);
198
+ state.step("w");
199
+ assert.deepStrictEqual(model.getCursor(), { line: 1, col: 0 });
200
+ state.step("b"); // no word before on line 1 → start of the last word on line 0
201
+ assert.deepStrictEqual(model.getCursor(), { line: 0, col: 0 });
202
+ state.step("e");
203
+ assert.deepStrictEqual(model.getCursor(), { line: 0, col: 2 });
204
+ });
205
+
206
+ // ---------------------------------------------------------------------------
207
+ // Normal mode: entering Insert
208
+ // ---------------------------------------------------------------------------
209
+
210
+ it("i returns to insert with the cursor unchanged", () => {
211
+ const { model, state } = makeState(["foo"], { line: 0, col: 1 });
212
+ toNormal(state);
213
+ state.step("i");
214
+ assert.strictEqual(state.mode, "insert");
215
+ assert.deepStrictEqual(model.getCursor(), { line: 0, col: 1 });
216
+ });
217
+
218
+ it("a returns to insert with the cursor one column right", () => {
219
+ const { model, state } = makeState(["foo"], { line: 0, col: 1 });
220
+ toNormal(state);
221
+ state.step("a");
222
+ assert.strictEqual(state.mode, "insert");
223
+ assert.deepStrictEqual(model.getCursor(), { line: 0, col: 2 });
224
+ });
225
+
226
+ it("o opens a new line below and enters insert on it", () => {
227
+ const { model, state } = makeState(["foo", "bar"], { line: 0, col: 1 });
228
+ toNormal(state);
229
+ state.step("o");
230
+ assert.strictEqual(state.mode, "insert");
231
+ assert.deepStrictEqual(model.lines, ["foo", "", "bar"]);
232
+ assert.deepStrictEqual(model.getCursor(), { line: 1, col: 0 });
233
+ });
234
+
235
+ // ---------------------------------------------------------------------------
236
+ // Normal mode: delegation rules
237
+ // ---------------------------------------------------------------------------
238
+
239
+ it("delegates arrows, escape, tab, and control chords in normal mode", () => {
240
+ const { state } = makeState(["foo"]);
241
+ toNormal(state);
242
+ for (const data of ["\x1b[B", "\x1b", "\t", "\x1b[P", "\x1b[Z"]) {
243
+ assert.deepStrictEqual(state.step(data), { kind: "pass-through" });
244
+ }
245
+ assert.strictEqual(state.mode, "normal");
246
+ });
247
+
248
+ it("consumes bare letters that are not vim commands in normal mode", () => {
249
+ const { model, state } = makeState(["foo"], { line: 0, col: 0 });
250
+ toNormal(state);
251
+ const decision = state.step("q");
252
+ assert.deepStrictEqual(decision, { kind: "consumed" });
253
+ assert.strictEqual(model.getText(), "foo"); // not inserted
254
+ assert.strictEqual(state.mode, "normal");
255
+ });
256
+
257
+ // ---------------------------------------------------------------------------
258
+ // Visual mode
259
+ // ---------------------------------------------------------------------------
260
+
261
+ it("v enters visual mode and v again returns to normal", () => {
262
+ const { state } = makeState(["foo"], { line: 0, col: 1 });
263
+ toNormal(state);
264
+ state.step("v");
265
+ assert.strictEqual(state.mode, "visual");
266
+ assert.deepStrictEqual(state.selection, makeRange({ line: 0, col: 1 }, { line: 0, col: 1 }));
267
+ state.step("v");
268
+ assert.strictEqual(state.mode, "normal");
269
+ assert.strictEqual(state.selection, undefined);
270
+ });
271
+
272
+ it("motions in visual mode extend the selection while the anchor stays fixed", () => {
273
+ const { model, state } = makeState(["foo bar"], { line: 0, col: 1 });
274
+ toNormal(state);
275
+ state.step("v");
276
+ state.step("l");
277
+ assert.deepStrictEqual(state.selection, makeRange({ line: 0, col: 1 }, { line: 0, col: 2 }));
278
+ state.step("w");
279
+ assert.deepStrictEqual(state.selection, makeRange({ line: 0, col: 1 }, { line: 0, col: 4 }));
280
+ assert.deepStrictEqual(state.selection?.anchor, { line: 0, col: 1 });
281
+ assert.deepStrictEqual(model.getCursor(), { line: 0, col: 4 });
282
+ });
283
+
284
+ it("escape in visual mode returns to normal without deleting", () => {
285
+ const { model, state } = makeState(["foo bar"], { line: 0, col: 1 });
286
+ toNormal(state);
287
+ state.step("v");
288
+ state.step("l");
289
+ const decision = state.step("\x1b");
290
+ assert.deepStrictEqual(decision, { kind: "pass-through" }); // delegated to app.interrupt
291
+ assert.strictEqual(state.mode, "normal");
292
+ assert.strictEqual(state.selection, undefined);
293
+ assert.strictEqual(model.getText(), "foo bar");
294
+ });
295
+
296
+ // ---------------------------------------------------------------------------
297
+ // Operators (d/c/y/p) + unnamed yank buffer
298
+ // ---------------------------------------------------------------------------
299
+
300
+ it("d w deletes a word-delineated range", () => {
301
+ const { model, state } = makeState(["foo bar"], { line: 0, col: 0 });
302
+ toNormal(state);
303
+ state.step("d");
304
+ state.step("w");
305
+ assert.strictEqual(model.getText(), "bar");
306
+ assert.strictEqual(state.yankBuffer, "foo ");
307
+ assert.deepStrictEqual(model.getCursor(), { line: 0, col: 0 });
308
+ });
309
+
310
+ it("c w changes a word (yank + replace with empty)", () => {
311
+ const { model, state } = makeState(["foo bar"], { line: 0, col: 0 });
312
+ toNormal(state);
313
+ state.step("c");
314
+ state.step("w");
315
+ assert.strictEqual(model.getText(), "bar");
316
+ assert.strictEqual(state.yankBuffer, "foo ");
317
+ assert.strictEqual(state.mode, "normal");
318
+ });
319
+
320
+ it("y w yanks a word and p puts it at the cursor", () => {
321
+ const { model, state } = makeState(["foo bar"], { line: 0, col: 0 });
322
+ toNormal(state);
323
+ state.step("y");
324
+ state.step("w");
325
+ state.step("p");
326
+ assert.strictEqual(state.yankBuffer, "foo ");
327
+ assert.strictEqual(model.getText(), "foo foo bar");
328
+ });
329
+
330
+ it("visual mode d deletes the selection and clears visual", () => {
331
+ const { model, state } = makeState(["foo bar"], { line: 0, col: 1 });
332
+ toNormal(state);
333
+ state.step("v");
334
+ state.step("l");
335
+ state.step("l"); // selection {0,1} → {0,3}
336
+ state.step("d");
337
+ assert.strictEqual(model.getText(), "f bar");
338
+ assert.strictEqual(state.yankBuffer, "oo");
339
+ assert.strictEqual(state.mode, "normal");
340
+ assert.strictEqual(state.selection, undefined);
341
+ });
342
+
343
+ it("visual mode c changes the selection and pushes the old text to the buffer", () => {
344
+ const { model, state } = makeState(["foo bar"], { line: 0, col: 1 });
345
+ toNormal(state);
346
+ state.step("v");
347
+ state.step("l");
348
+ state.step("l");
349
+ state.step("c");
350
+ assert.strictEqual(model.getText(), "f bar");
351
+ assert.strictEqual(state.yankBuffer, "oo");
352
+ assert.strictEqual(state.mode, "normal");
353
+ });
354
+
355
+ it("visual mode y yanks the selection into the unnamed buffer without deleting", () => {
356
+ const { model, state } = makeState(["foo bar"], { line: 0, col: 1 });
357
+ toNormal(state);
358
+ state.step("v");
359
+ state.step("l");
360
+ state.step("l");
361
+ state.step("y");
362
+ assert.strictEqual(model.getText(), "foo bar");
363
+ assert.strictEqual(state.yankBuffer, "oo");
364
+ assert.strictEqual(state.mode, "normal");
365
+ });
366
+
367
+ it("operator with no following motion is a no-op", () => {
368
+ const { model, state } = makeState(["foo bar"], { line: 0, col: 0 });
369
+ toNormal(state);
370
+ state.step("d");
371
+ const decision = state.step("\x1b[B"); // non-motion delegates
372
+ assert.deepStrictEqual(decision, { kind: "pass-through" });
373
+ assert.strictEqual(model.getText(), "foo bar");
374
+ assert.strictEqual(state.yankBuffer, undefined);
375
+ // the pending operator was aborted: the next motion is a plain move
376
+ state.step("w");
377
+ assert.deepStrictEqual(model.getCursor(), { line: 0, col: 4 });
378
+ });
379
+
380
+ it("put with an empty unnamed buffer is a no-op", () => {
381
+ const { model, state } = makeState(["foo"], { line: 0, col: 0 });
382
+ toNormal(state);
383
+ state.step("p");
384
+ assert.strictEqual(model.getText(), "foo");
385
+ });
@@ -0,0 +1,314 @@
1
+ import { decodeKittyPrintable, Key, matchesKey } from "@earendil-works/pi-tui";
2
+ import { makeRange, type Cursor, type Range, type VimTextModelShape } from "./text-model.ts";
3
+
4
+ /**
5
+ * Modal state machine for the vim editor: Normal / Visual / Insert.
6
+ *
7
+ * Pure transition core: no Effect imports and no pi-tui internals beyond the
8
+ * pure key-decoding helpers. All text access goes through the
9
+ * {@link VimTextModelShape} interface (the editor implements it; tests use a
10
+ * fake with real geometry). The host editor calls `step` per raw input and
11
+ * delegates to `super.handleInput` only when the decision says pass-through.
12
+ */
13
+ export type EditorMode = "insert" | "normal" | "visual";
14
+
15
+ /**
16
+ * What the host editor should do after a `step`:
17
+ * - `consumed`: vim handled the input; do NOT call `super.handleInput`.
18
+ * `enteredNormal` is set when the step transitioned from Insert into
19
+ * Normal (Escape), signalling the host to close any open autocomplete.
20
+ * - `pass-through`: vim did not handle the input; call `super.handleInput`.
21
+ */
22
+ export type VimDecision =
23
+ | { readonly kind: "consumed"; readonly enteredNormal?: boolean }
24
+ | { readonly kind: "pass-through" };
25
+
26
+ const CONSUMED: VimDecision = { kind: "consumed" };
27
+ const PASS_THROUGH: VimDecision = { kind: "pass-through" };
28
+
29
+ type Operator = "d" | "c" | "y";
30
+
31
+ const MOTIONS = new Set(["h", "j", "k", "l", "w", "b", "e", "0", "^", "$"]);
32
+
33
+ /**
34
+ * The printable character a raw input represents, or undefined when the
35
+ * input is not a plain (or Shift-modified) text key. Covers Kitty CSI-u
36
+ * sequences via the public `decodeKittyPrintable`, plus the legacy raw
37
+ * single-char case pi's editor itself accepts.
38
+ */
39
+ function printableOf(data: string): string | undefined {
40
+ return (
41
+ decodeKittyPrintable(data) ?? (data.length === 1 && data.charCodeAt(0) >= 32 ? data : undefined)
42
+ );
43
+ }
44
+
45
+ export class VimEditorState {
46
+ mode: EditorMode = "insert";
47
+ selection: Range | undefined;
48
+ yankBuffer: string | undefined;
49
+ private pendingOperator: Operator | undefined;
50
+
51
+ constructor(
52
+ private readonly model: VimTextModelShape,
53
+ private readonly getVimEnabled: () => boolean,
54
+ ) {}
55
+
56
+ /** Whether the vim gate is currently on (drives the cursor style too). */
57
+ get enabled(): boolean {
58
+ return this.getVimEnabled();
59
+ }
60
+
61
+ /** Step the state machine with one raw input chunk. */
62
+ step(data: string): VimDecision {
63
+ if (!this.getVimEnabled()) return PASS_THROUGH; // gated; inert
64
+ switch (this.mode) {
65
+ case "insert":
66
+ return this.stepInsert(data);
67
+ case "normal":
68
+ return this.stepNormal(data);
69
+ case "visual":
70
+ return this.stepVisual(data);
71
+ }
72
+ }
73
+
74
+ // -------------------------------------------------------------------------
75
+ // Insert
76
+ // -------------------------------------------------------------------------
77
+
78
+ private stepInsert(data: string): VimDecision {
79
+ if (matchesKey(data, Key.escape)) {
80
+ // Escape enters Normal (classic vim, assumption 1). Entering Normal
81
+ // closes any open autocomplete popup (binding decision 2).
82
+ this.mode = "normal";
83
+ this.selection = undefined;
84
+ this.pendingOperator = undefined;
85
+ return { kind: "consumed", enteredNormal: true };
86
+ }
87
+ return PASS_THROUGH; // plain always-insert editor in Insert mode
88
+ }
89
+
90
+ // -------------------------------------------------------------------------
91
+ // Normal
92
+ // -------------------------------------------------------------------------
93
+
94
+ private stepNormal(data: string): VimDecision {
95
+ // Escape in Normal delegates to the app (app.interrupt), unchanged.
96
+ if (matchesKey(data, Key.escape)) return PASS_THROUGH;
97
+ const printable = printableOf(data);
98
+ if (printable === undefined) {
99
+ // Arrows, Tab/autocomplete keys, control chords, shift chords: abort a
100
+ // pending operator and delegate to the existing editor/app handling.
101
+ this.pendingOperator = undefined;
102
+ return PASS_THROUGH;
103
+ }
104
+
105
+ if (this.pendingOperator !== undefined) {
106
+ const target = this.resolveMotionCursor(printable);
107
+ if (target !== undefined) {
108
+ this.applyOperator(this.pendingOperator, makeRange(this.model.getCursor(), target));
109
+ this.pendingOperator = undefined;
110
+ return CONSUMED;
111
+ }
112
+ // A non-motion printable aborts the pending operator (no-op).
113
+ this.pendingOperator = undefined;
114
+ return CONSUMED;
115
+ }
116
+
117
+ if (MOTIONS.has(printable)) {
118
+ this.moveCursorByMotion(printable);
119
+ return CONSUMED;
120
+ }
121
+ if (printable === "i") {
122
+ this.mode = "insert";
123
+ return CONSUMED;
124
+ }
125
+ if (printable === "a") {
126
+ const cur = this.model.getCursor();
127
+ this.model.moveCursorTo({
128
+ line: cur.line,
129
+ col: Math.min(this.model.lineLength(cur.line), cur.col + 1),
130
+ });
131
+ this.mode = "insert";
132
+ return CONSUMED;
133
+ }
134
+ if (printable === "o") {
135
+ // Open a new line below the cursor: jump to the line end and insert
136
+ // a newline; the model positions the cursor on the fresh line.
137
+ this.model.moveCursorTo(this.model.moveToLineEnd(this.model.getCursor()));
138
+ this.model.insertTextAtCursor("\n");
139
+ this.mode = "insert";
140
+ return CONSUMED;
141
+ }
142
+ if (printable === "v") {
143
+ const cur = this.model.getCursor();
144
+ this.selection = makeRange(cur, cur);
145
+ this.mode = "visual";
146
+ return CONSUMED;
147
+ }
148
+ if (printable === "d" || printable === "c" || printable === "y") {
149
+ this.pendingOperator = printable;
150
+ return CONSUMED;
151
+ }
152
+ if (printable === "p") {
153
+ if (this.yankBuffer !== undefined) this.model.insertTextAtCursor(this.yankBuffer);
154
+ return CONSUMED;
155
+ }
156
+ // Bare letter that is not a vim command: consumed, never inserted.
157
+ return CONSUMED;
158
+ }
159
+
160
+ // -------------------------------------------------------------------------
161
+ // Visual
162
+ // -------------------------------------------------------------------------
163
+
164
+ private stepVisual(data: string): VimDecision {
165
+ // Escape leaves Visual without deleting and is then passed through so the
166
+ // app interrupt still fires (like Normal-mode Escape).
167
+ if (matchesKey(data, Key.escape)) {
168
+ this.mode = "normal";
169
+ this.selection = undefined;
170
+ return PASS_THROUGH;
171
+ }
172
+ const printable = printableOf(data);
173
+ if (printable === undefined) return PASS_THROUGH; // arrows/ctrl/tab delegate
174
+
175
+ if (printable === "v") {
176
+ this.mode = "normal";
177
+ this.selection = undefined;
178
+ return CONSUMED;
179
+ }
180
+ if (printable === "d" || printable === "c" || printable === "y") {
181
+ const sel = this.selection;
182
+ if (sel) {
183
+ if (printable === "d") {
184
+ this.yankBuffer = this.model.yankRange(sel);
185
+ this.model.deleteRange(sel);
186
+ } else if (printable === "c") {
187
+ this.yankBuffer = this.model.yankRange(sel);
188
+ this.model.replaceRange(sel, "");
189
+ } else {
190
+ this.yankBuffer = this.model.yankRange(sel);
191
+ }
192
+ }
193
+ this.mode = "normal";
194
+ this.selection = undefined;
195
+ return CONSUMED;
196
+ }
197
+ if (MOTIONS.has(printable)) {
198
+ this.moveCursorByMotion(printable);
199
+ if (this.selection) {
200
+ // Extend the selection: the anchor stays fixed, the active end moves.
201
+ this.selection = makeRange(this.selection.anchor, this.model.getCursor());
202
+ }
203
+ return CONSUMED;
204
+ }
205
+ // Other printable keys are consumed (nothing is inserted in Visual mode).
206
+ return CONSUMED;
207
+ }
208
+
209
+ // -------------------------------------------------------------------------
210
+ // Shared pieces
211
+ // -------------------------------------------------------------------------
212
+
213
+ /** True when `c` is one of the motion keys; moves the cursor to its target. */
214
+ private moveCursorByMotion(c: string): boolean {
215
+ const cur = this.model.getCursor();
216
+ switch (c) {
217
+ case "h":
218
+ this.model.moveCursorTo({ line: cur.line, col: Math.max(0, cur.col - 1) });
219
+ return true;
220
+ case "l": {
221
+ const maxCol = this.model.lineLength(cur.line);
222
+ this.model.moveCursorTo({ line: cur.line, col: Math.min(maxCol, cur.col + 1) });
223
+ return true;
224
+ }
225
+ case "j": {
226
+ const next = Math.min(cur.line + 1, this.model.getLineCount() - 1);
227
+ this.model.moveCursorTo({
228
+ line: next,
229
+ col: Math.min(cur.col, this.model.lineLength(next)),
230
+ });
231
+ return true;
232
+ }
233
+ case "k": {
234
+ const prev = Math.max(0, cur.line - 1);
235
+ this.model.moveCursorTo({
236
+ line: prev,
237
+ col: Math.min(cur.col, this.model.lineLength(prev)),
238
+ });
239
+ return true;
240
+ }
241
+ case "w":
242
+ this.model.moveCursorTo(this.model.moveWordForward(cur));
243
+ return true;
244
+ case "b":
245
+ this.model.moveCursorTo(this.model.moveWordBackward(cur));
246
+ return true;
247
+ case "e":
248
+ this.model.moveCursorTo(this.model.moveWordEnd(cur));
249
+ return true;
250
+ case "^":
251
+ case "0":
252
+ this.model.moveCursorTo(this.model.moveToLineStart(cur));
253
+ return true;
254
+ case "$":
255
+ this.model.moveCursorTo(this.model.moveToLineEnd(cur));
256
+ return true;
257
+ default:
258
+ return false;
259
+ }
260
+ }
261
+
262
+ /** The motion target for a printable key, or undefined when not a motion. */
263
+ private resolveMotionCursor(c: string): Cursor | undefined {
264
+ const cur = this.model.getCursor();
265
+ switch (c) {
266
+ case "h":
267
+ return { line: cur.line, col: Math.max(0, cur.col - 1) };
268
+ case "l":
269
+ return { line: cur.line, col: Math.min(this.model.lineLength(cur.line), cur.col + 1) };
270
+ case "j": {
271
+ const next = Math.min(cur.line + 1, this.model.getLineCount() - 1);
272
+ return { line: next, col: Math.min(cur.col, this.model.lineLength(next)) };
273
+ }
274
+ case "k": {
275
+ const prev = Math.max(0, cur.line - 1);
276
+ return { line: prev, col: Math.min(cur.col, this.model.lineLength(prev)) };
277
+ }
278
+ case "w":
279
+ return this.model.moveWordForward(cur);
280
+ case "b":
281
+ return this.model.moveWordBackward(cur);
282
+ case "e":
283
+ return this.model.moveWordEnd(cur);
284
+ case "^":
285
+ case "0":
286
+ return this.model.moveToLineStart(cur);
287
+ case "$":
288
+ return this.model.moveToLineEnd(cur);
289
+ default:
290
+ return undefined;
291
+ }
292
+ }
293
+
294
+ /**
295
+ * Apply a Normal-mode operator over the motion-delineated range. Deletes and
296
+ * changes push the removed text into the unnamed yank buffer (single put
297
+ * source); the cursor lands at the range start.
298
+ */
299
+ private applyOperator(op: Operator, range: Range): void {
300
+ switch (op) {
301
+ case "d":
302
+ this.yankBuffer = this.model.yankRange(range);
303
+ this.model.deleteRange(range);
304
+ break;
305
+ case "c":
306
+ this.yankBuffer = this.model.yankRange(range);
307
+ this.model.replaceRange(range, "");
308
+ break;
309
+ case "y":
310
+ this.yankBuffer = this.model.yankRange(range);
311
+ break;
312
+ }
313
+ }
314
+ }