@oh-my-pi/pi-tui 18.8.7 → 18.8.8
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 +28 -64
- package/dist/types/chat/execution-shared.d.ts +6 -5
- package/dist/types/keybinding-matchers.d.ts +3 -0
- package/dist/types/overlays/annotation-overlay.d.ts +12 -0
- package/dist/types/overlays/annotation-types.d.ts +2 -0
- package/dist/types/overlays/hook-selector.d.ts +9 -4
- package/dist/types/overlays/model-browser.d.ts +2 -2
- package/dist/types/overlays/model-hub.d.ts +21 -4
- package/dist/types/overlays/session-observer-registry.d.ts +2 -0
- package/dist/types/overlays/usage-dashboard.d.ts +3 -1
- package/dist/types/status-line/types.d.ts +1 -1
- package/dist/types/theme/symbols.d.ts +1 -1
- package/dist/types/tools/eval.d.ts +1 -1
- package/dist/types/tools/native-view.d.ts +47 -0
- package/dist/types/tools/renderer.d.ts +15 -2
- package/dist/types/tui.d.ts +20 -0
- package/dist/types/vim.d.ts +19 -5
- package/package.json +9 -9
- package/src/chat/execution-shared.ts +42 -25
- package/src/chat/tool-execution.ts +9 -2
- package/src/components/editor.ts +182 -5
- package/src/keybinding-matchers.ts +6 -2
- package/src/overlays/agent-hub.ts +7 -3
- package/src/overlays/annotation-overlay.ts +87 -6
- package/src/overlays/annotation-types.ts +2 -0
- package/src/overlays/hook-selector.ts +12 -5
- package/src/overlays/model-browser.ts +3 -3
- package/src/overlays/model-hub.ts +107 -27
- package/src/overlays/session-observer-registry.ts +10 -2
- package/src/overlays/usage-dashboard.ts +70 -23
- package/src/prompt/composer.ts +1 -0
- package/src/status-line/segments.ts +3 -0
- package/src/status-line/types.ts +1 -1
- package/src/theme/symbols.ts +5 -0
- package/src/tools/bash.ts +82 -31
- package/src/tools/eval.ts +149 -123
- package/src/tools/native-view.ts +102 -1
- package/src/tools/renderer.ts +15 -2
- package/src/tui.ts +68 -0
- package/src/vim.ts +333 -18
package/src/tools/renderer.ts
CHANGED
|
@@ -26,6 +26,14 @@ export interface RenderResultOptions {
|
|
|
26
26
|
* Streamed `xd://` previews stay queued until this is set.
|
|
27
27
|
*/
|
|
28
28
|
executionStarted?: boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Milliseconds since this call began executing (`tool_execution_start`),
|
|
31
|
+
* or its total once settled; undefined before it starts. Native describe
|
|
32
|
+
* hooks only.
|
|
33
|
+
*/
|
|
34
|
+
elapsedMs?: number;
|
|
35
|
+
/** The call was abandoned without a final result (turn aborted, sealed). Native describe hooks only. */
|
|
36
|
+
cancelled?: boolean;
|
|
29
37
|
}
|
|
30
38
|
|
|
31
39
|
/** Render options for a result, plus the tool-specific context the transcript threads through. */
|
|
@@ -90,6 +98,8 @@ export interface NativeToolHead {
|
|
|
90
98
|
readonly targetKind?: TspToolProps["targetKind"];
|
|
91
99
|
/** Language for `command` targets. */
|
|
92
100
|
readonly lang?: string;
|
|
101
|
+
/** The raw command line the head's Copy command button copies (set implicitly by a `command` target). */
|
|
102
|
+
readonly command?: string;
|
|
93
103
|
/** `file://` link for path targets. */
|
|
94
104
|
readonly href?: string;
|
|
95
105
|
/** Short facts after the target (`+8 −1`, `5 matches · 2 files`). */
|
|
@@ -117,8 +127,11 @@ export interface NativeToolView {
|
|
|
117
127
|
readonly body?: readonly NativeChild[];
|
|
118
128
|
/** Tone override; the frame otherwise derives it from the call status. */
|
|
119
129
|
readonly tone?: TspTone;
|
|
120
|
-
/**
|
|
121
|
-
|
|
130
|
+
/**
|
|
131
|
+
* Body clamp while collapsed; `{tail}` keeps the end (terminal output). Defaults to the transcript's preview size.
|
|
132
|
+
* `children`: the body stays whole while collapsed and its own `ansi`/`code` previews clamp instead.
|
|
133
|
+
*/
|
|
134
|
+
readonly preview?: TspPreview | { readonly tail: number } | "none" | "children";
|
|
122
135
|
/** Starts expanded whatever the transcript's expand state (the todo checklist); the user can still fold it. */
|
|
123
136
|
readonly open?: boolean;
|
|
124
137
|
/** Render frameless (`frame:"inline"`): a head line plus a disclosed body. */
|
package/src/tui.ts
CHANGED
|
@@ -196,6 +196,10 @@ export interface TUIStartOptions {
|
|
|
196
196
|
* Paint without owning stdin: the terminal stays in cooked mode (kernel
|
|
197
197
|
* echo + line editing at the hardware cursor) until {@link TUI.enableInput}
|
|
198
198
|
* switches to raw input and replays the kernel-buffered keystrokes.
|
|
199
|
+
*
|
|
200
|
+
* A terminal expected to speak TSP needs raw input from the start, so it
|
|
201
|
+
* gets it; its keystrokes are held instead (TSP events and the cell-size
|
|
202
|
+
* reply still apply) until {@link TUI.releaseHeldInput} replays them.
|
|
199
203
|
*/
|
|
200
204
|
deferInput?: boolean;
|
|
201
205
|
}
|
|
@@ -991,6 +995,17 @@ export class TUI extends Container {
|
|
|
991
995
|
#cancelPostmortemRestore?: () => void;
|
|
992
996
|
/** True between a `deferInput` start() and enableInput(). */
|
|
993
997
|
#inputDeferred = false;
|
|
998
|
+
/**
|
|
999
|
+
* Keystrokes held since a TSP `deferInput` start, replayed by
|
|
1000
|
+
* releaseHeldInput(); undefined when not holding.
|
|
1001
|
+
*/
|
|
1002
|
+
#heldInput: string[] | undefined;
|
|
1003
|
+
/**
|
|
1004
|
+
* The component focused when holding began. Only its keystrokes are held:
|
|
1005
|
+
* a dialog that takes focus meanwhile (a startup hook's select or confirm)
|
|
1006
|
+
* gets its input live.
|
|
1007
|
+
*/
|
|
1008
|
+
#heldFocus: Component | null = null;
|
|
994
1009
|
// Always-on event-loop lag probe. The high default threshold keeps it quiet;
|
|
995
1010
|
// it only logs `ui.loop-blocked` (with the current loop phase) when a frame
|
|
996
1011
|
// budget is genuinely starved. Armed in start(), disarmed in stop().
|
|
@@ -1435,6 +1450,12 @@ export class TUI extends Container {
|
|
|
1435
1450
|
// `hello` query must go out now to confirm the surface.
|
|
1436
1451
|
const nativeExpected = this.terminal.tspExpected === true;
|
|
1437
1452
|
this.#inputDeferred = options?.deferInput === true && !nativeExpected;
|
|
1453
|
+
// A restart (a startup dialog's external editor stops and restarts the
|
|
1454
|
+
// TUI) keeps an existing hold: those keys still belong to the editor.
|
|
1455
|
+
if (options?.deferInput === true && nativeExpected) {
|
|
1456
|
+
this.#heldInput = [];
|
|
1457
|
+
this.#heldFocus = this.#focusedComponent;
|
|
1458
|
+
}
|
|
1438
1459
|
this.#watchdog.start();
|
|
1439
1460
|
this.#ghosttyInitialImageDelayDone = false;
|
|
1440
1461
|
this.#ghosttyImageReadyAtMs = this.#renderScheduler.now() + TUI.#GHOSTTY_INITIAL_IMAGE_DELAY_MS;
|
|
@@ -2273,6 +2294,32 @@ export class TUI extends Container {
|
|
|
2273
2294
|
this.requestRender(true);
|
|
2274
2295
|
}
|
|
2275
2296
|
|
|
2297
|
+
/**
|
|
2298
|
+
* Replay the keystrokes held since a TSP `deferInput` start through the
|
|
2299
|
+
* normal input path, then deliver input live. Call once the app's key
|
|
2300
|
+
* handlers are installed so a hotkey pressed during startup still fires.
|
|
2301
|
+
* Only keys typed while the start-time focus owner had focus are held. The
|
|
2302
|
+
* hold survives a stop/start until released. Idempotent; no-op when nothing
|
|
2303
|
+
* is held.
|
|
2304
|
+
*/
|
|
2305
|
+
releaseHeldInput(): void {
|
|
2306
|
+
const held = this.#heldInput;
|
|
2307
|
+
if (held === undefined) return;
|
|
2308
|
+
this.#heldInput = undefined;
|
|
2309
|
+
if (this.#stopped) return;
|
|
2310
|
+
for (const data of held) this.#handleInput(data);
|
|
2311
|
+
}
|
|
2312
|
+
|
|
2313
|
+
/**
|
|
2314
|
+
* Hand held-key ownership from `previous` to `next` when the app replaces
|
|
2315
|
+
* the component focused at start (a swapped-in custom editor), so keys
|
|
2316
|
+
* typed into the replacement stay queued behind the held ones instead of
|
|
2317
|
+
* overtaking them. No-op unless `previous` owns the held keys.
|
|
2318
|
+
*/
|
|
2319
|
+
replaceHeldFocus(previous: Component, next: Component): void {
|
|
2320
|
+
if (this.#heldInput !== undefined && this.#heldFocus === previous) this.#heldFocus = next;
|
|
2321
|
+
}
|
|
2322
|
+
|
|
2276
2323
|
addStartListener(listener: StartListener): () => void {
|
|
2277
2324
|
this.#startListeners.add(listener);
|
|
2278
2325
|
return () => {
|
|
@@ -2296,6 +2343,10 @@ export class TUI extends Container {
|
|
|
2296
2343
|
// PI_FORCE_IMAGE_PROTOCOL choice — including its `off` kill switch — wins
|
|
2297
2344
|
// over the probe.
|
|
2298
2345
|
if (TERMINAL.imageProtocol) return;
|
|
2346
|
+
// A Tern surface (live, or about to open optimistically at start) sends
|
|
2347
|
+
// images through TSP; this also keeps held startup input free of probe
|
|
2348
|
+
// listeners.
|
|
2349
|
+
if (this.#nativeLive || this.terminal.tspExpected) return;
|
|
2299
2350
|
if (isImageProtocolForced()) return;
|
|
2300
2351
|
if (!process.stdin.isTTY || !process.stdout.isTTY) return;
|
|
2301
2352
|
|
|
@@ -2781,6 +2832,11 @@ export class TUI extends Container {
|
|
|
2781
2832
|
}
|
|
2782
2833
|
if (data.length === 0) return;
|
|
2783
2834
|
|
|
2835
|
+
if (this.#heldInput !== undefined && this.#focusedComponent === this.#heldFocus) {
|
|
2836
|
+
this.#holdInput(data);
|
|
2837
|
+
return;
|
|
2838
|
+
}
|
|
2839
|
+
|
|
2784
2840
|
// If focused component is an overlay, verify it's still visible (visibility can change due to
|
|
2785
2841
|
// terminal resize or visible() callback). Runs before the capture preflight below, which must
|
|
2786
2842
|
// target the effective focus owner, not a hidden overlay.
|
|
@@ -2860,6 +2916,18 @@ export class TUI extends Container {
|
|
|
2860
2916
|
}
|
|
2861
2917
|
}
|
|
2862
2918
|
|
|
2919
|
+
/**
|
|
2920
|
+
* Queue a keystroke typed before the app installed its key handlers; input
|
|
2921
|
+
* listeners see it once, on replay. The cell-size reply is a terminal report,
|
|
2922
|
+
* consumed now. Ctrl+C/Ctrl+D release the queue so a stalled startup stays
|
|
2923
|
+
* interruptible.
|
|
2924
|
+
*/
|
|
2925
|
+
#holdInput(data: string): void {
|
|
2926
|
+
if (this.#consumeCellSizeResponse(data)) return;
|
|
2927
|
+
this.#heldInput!.push(data);
|
|
2928
|
+
if (matchesKey(data, "ctrl+c") || matchesKey(data, "ctrl+d")) this.releaseHeldInput();
|
|
2929
|
+
}
|
|
2930
|
+
|
|
2863
2931
|
#consumeCellSizeResponse(data: string): boolean {
|
|
2864
2932
|
// Response format: ESC [ 6 ; height ; width t
|
|
2865
2933
|
const match = data.match(/^\x1b\[6;(\d+);(\d+)t$/);
|
package/src/vim.ts
CHANGED
|
@@ -11,9 +11,9 @@ import { getSegmenter, moveWordLeft, moveWordRight } from "./utils";
|
|
|
11
11
|
* modes, the common motions, and operators built from those motions.
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
|
-
export type VimMode = "insert" | "normal" | "visual" | "visual-line";
|
|
14
|
+
export type VimMode = "insert" | "normal" | "visual" | "visual-line" | "replace";
|
|
15
15
|
|
|
16
|
-
export type VimOperator = "d" | "y" | "c";
|
|
16
|
+
export type VimOperator = "d" | "y" | "c" | ">" | "<";
|
|
17
17
|
|
|
18
18
|
export interface VimPosition {
|
|
19
19
|
line: number;
|
|
@@ -38,7 +38,10 @@ export type VimCommand =
|
|
|
38
38
|
| { kind: "delete"; from: VimPosition; to: VimPosition; linewise: boolean; insert: boolean }
|
|
39
39
|
| { kind: "openLine"; below: boolean }
|
|
40
40
|
| { kind: "paste"; after: boolean; count: number }
|
|
41
|
-
| { kind: "undo" }
|
|
41
|
+
| { kind: "undo" }
|
|
42
|
+
| { kind: "replace"; from: VimPosition; to: VimPosition; text: string }
|
|
43
|
+
| { kind: "join"; fromLine: number; lines: number }
|
|
44
|
+
| { kind: "indent"; fromLine: number; toLine: number; out: boolean };
|
|
42
45
|
|
|
43
46
|
const segmenter = getSegmenter();
|
|
44
47
|
|
|
@@ -75,6 +78,35 @@ function firstNonBlank(text: string): number {
|
|
|
75
78
|
}
|
|
76
79
|
return 0;
|
|
77
80
|
}
|
|
81
|
+
function findPendingKey(pending: { forward: boolean; till: boolean } | null): string {
|
|
82
|
+
if (!pending) return "";
|
|
83
|
+
return pending.till ? (pending.forward ? "t" : "T") : pending.forward ? "f" : "F";
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Column of the `count`th grapheme equal to `target`, searching strictly after `fromCol` when
|
|
88
|
+
* `forward` and strictly before it otherwise. Line-local, like Vim's `f`/`F`.
|
|
89
|
+
*/
|
|
90
|
+
function findGrapheme(text: string, fromCol: number, target: string, forward: boolean, count: number): number | null {
|
|
91
|
+
if (count < 1 || target.length === 0) return null;
|
|
92
|
+
const segs = [...segmenter.segment(text)];
|
|
93
|
+
let here = -1;
|
|
94
|
+
for (let i = 0; i < segs.length; i++) {
|
|
95
|
+
if (segs[i]!.index <= fromCol) here = i;
|
|
96
|
+
else break;
|
|
97
|
+
}
|
|
98
|
+
let seen = 0;
|
|
99
|
+
if (forward) {
|
|
100
|
+
for (let i = here + 1; i < segs.length; i++) {
|
|
101
|
+
if (segs[i]!.segment === target && ++seen === count) return segs[i]!.index;
|
|
102
|
+
}
|
|
103
|
+
} else {
|
|
104
|
+
for (let i = here - 1; i >= 0; i--) {
|
|
105
|
+
if (segs[i]!.segment === target && ++seen === count) return segs[i]!.index;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
78
110
|
|
|
79
111
|
/** Vim's `w`: `moveWordRight` stops at the end of the current word, so skip the gap that follows. */
|
|
80
112
|
function wordForward(text: string, col: number): number {
|
|
@@ -157,11 +189,7 @@ function wordObject(text: string, col: number, around: boolean, big: boolean, co
|
|
|
157
189
|
* leading run when there is none.
|
|
158
190
|
*/
|
|
159
191
|
function quoteObject(text: string, col: number, quote: string, around: boolean): [number, number] | null {
|
|
160
|
-
const marks
|
|
161
|
-
for (let i = 0; i < text.length; i++) {
|
|
162
|
-
if (text.charAt(i) === "\\") i++;
|
|
163
|
-
else if (text.charAt(i) === quote) marks.push(i);
|
|
164
|
-
}
|
|
192
|
+
const marks = quoteMarks(text, quote);
|
|
165
193
|
for (let p = 0; p + 1 < marks.length; p += 2) {
|
|
166
194
|
const open = marks[p]!;
|
|
167
195
|
const close = marks[p + 1]!;
|
|
@@ -254,6 +282,113 @@ function paragraphObject(buf: VimBuffer, around: boolean): TextObjectRange {
|
|
|
254
282
|
return { from: { line: first, col: 0 }, to: { line: end, col: (buf.lines[end] ?? "").length }, linewise: true };
|
|
255
283
|
}
|
|
256
284
|
|
|
285
|
+
const BRACKET_MATCH: Record<string, { mate: string; forward: boolean }> = {
|
|
286
|
+
"(": { mate: ")", forward: true },
|
|
287
|
+
")": { mate: "(", forward: false },
|
|
288
|
+
"[": { mate: "]", forward: true },
|
|
289
|
+
"]": { mate: "[", forward: false },
|
|
290
|
+
"{": { mate: "}", forward: true },
|
|
291
|
+
"}": { mate: "{", forward: false },
|
|
292
|
+
};
|
|
293
|
+
|
|
294
|
+
/** Quote columns, skipping a backslash and the character after it, same as `i"`/`a"`. */
|
|
295
|
+
function quoteMarks(text: string, quote: string): number[] {
|
|
296
|
+
const marks: number[] = [];
|
|
297
|
+
for (let i = 0; i < text.length; i++) {
|
|
298
|
+
if (text.charAt(i) === "\\") i++;
|
|
299
|
+
else if (text.charAt(i) === quote) marks.push(i);
|
|
300
|
+
}
|
|
301
|
+
return marks;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
function quoteMate(marks: readonly number[], col: number): number | null {
|
|
305
|
+
for (let p = 0; p + 1 < marks.length; p += 2) {
|
|
306
|
+
const open = marks[p]!;
|
|
307
|
+
const close = marks[p + 1]!;
|
|
308
|
+
if (col === open) return close;
|
|
309
|
+
if (col === close) return open;
|
|
310
|
+
}
|
|
311
|
+
return null;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/** Column of the match for `%`: the next bracket or quote on this line, then its pair. */
|
|
315
|
+
function matchDelimiter(buf: VimBuffer): VimPosition | null {
|
|
316
|
+
const line = buf.lines[buf.cursorLine] ?? "";
|
|
317
|
+
const quotes = {
|
|
318
|
+
'"': quoteMarks(line, '"'),
|
|
319
|
+
"'": quoteMarks(line, "'"),
|
|
320
|
+
};
|
|
321
|
+
const quoteAt = (col: number): '"' | "'" | null =>
|
|
322
|
+
quotes['"'].includes(col) ? '"' : quotes["'"].includes(col) ? "'" : null;
|
|
323
|
+
let col = buf.cursorCol;
|
|
324
|
+
while (col < line.length && BRACKET_MATCH[line.charAt(col)] === undefined && quoteAt(col) === null) col++;
|
|
325
|
+
const quote = quoteAt(col);
|
|
326
|
+
if (quote) {
|
|
327
|
+
const mate = quoteMate(quotes[quote], col);
|
|
328
|
+
return mate === null ? null : { line: buf.cursorLine, col: mate };
|
|
329
|
+
}
|
|
330
|
+
const spec = BRACKET_MATCH[line.charAt(col)];
|
|
331
|
+
if (!spec) return null;
|
|
332
|
+
const { text, cursor } = flatten({ lines: buf.lines, cursorLine: buf.cursorLine, cursorCol: col });
|
|
333
|
+
const open = spec.forward ? line.charAt(col) : spec.mate;
|
|
334
|
+
const close = spec.forward ? spec.mate : line.charAt(col);
|
|
335
|
+
let depth = 0;
|
|
336
|
+
let match = -1;
|
|
337
|
+
if (spec.forward) {
|
|
338
|
+
for (let i = cursor + 1; i < text.length; i++) {
|
|
339
|
+
const ch = text.charAt(i);
|
|
340
|
+
if (ch === open) depth++;
|
|
341
|
+
else if (ch === close) {
|
|
342
|
+
if (depth === 0) {
|
|
343
|
+
match = i;
|
|
344
|
+
break;
|
|
345
|
+
}
|
|
346
|
+
depth--;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
} else {
|
|
350
|
+
for (let i = cursor - 1; i >= 0; i--) {
|
|
351
|
+
const ch = text.charAt(i);
|
|
352
|
+
if (ch === close) depth++;
|
|
353
|
+
else if (ch === open) {
|
|
354
|
+
if (depth === 0) {
|
|
355
|
+
match = i;
|
|
356
|
+
break;
|
|
357
|
+
}
|
|
358
|
+
depth--;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
return match < 0 ? null : offsetToPos(buf.lines, match);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
function isParagraphBlank(lines: readonly string[], line: number): boolean {
|
|
366
|
+
return (lines[line] ?? "").trim() === "";
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/** Line `{`/`}` land on. A missing boundary stays on the first or last line. */
|
|
370
|
+
function paragraphBoundary(lines: readonly string[], line: number, forward: boolean): number {
|
|
371
|
+
const last = lines.length - 1;
|
|
372
|
+
if (last < 0) return 0;
|
|
373
|
+
let i = Math.max(0, Math.min(line, last));
|
|
374
|
+
if (forward) {
|
|
375
|
+
if (i < last && !isParagraphBlank(lines, i)) {
|
|
376
|
+
while (i < last && !isParagraphBlank(lines, i + 1)) i++;
|
|
377
|
+
return i < last ? i + 1 : last;
|
|
378
|
+
}
|
|
379
|
+
while (i < last && isParagraphBlank(lines, i)) i++;
|
|
380
|
+
while (i < last && !isParagraphBlank(lines, i)) i++;
|
|
381
|
+
return i;
|
|
382
|
+
}
|
|
383
|
+
if (i > 0 && !isParagraphBlank(lines, i)) {
|
|
384
|
+
while (i > 0 && !isParagraphBlank(lines, i - 1)) i--;
|
|
385
|
+
return i > 0 ? i - 1 : 0;
|
|
386
|
+
}
|
|
387
|
+
while (i > 0 && isParagraphBlank(lines, i)) i--;
|
|
388
|
+
while (i > 0 && !isParagraphBlank(lines, i - 1)) i--;
|
|
389
|
+
return i > 0 ? i - 1 : 0;
|
|
390
|
+
}
|
|
391
|
+
|
|
257
392
|
interface Motion {
|
|
258
393
|
to: VimPosition;
|
|
259
394
|
/** Inclusive motions cover the grapheme under `to` when used with an operator. */
|
|
@@ -271,6 +406,13 @@ export class VimState {
|
|
|
271
406
|
#pendingG = false;
|
|
272
407
|
/** `i` or `a` typed after an operator or in Visual mode — waiting for the object key. */
|
|
273
408
|
#textObject: "i" | "a" | null = null;
|
|
409
|
+
/** `f`/`F`/`t`/`T` waiting for the target grapheme. */
|
|
410
|
+
#findPending: { forward: boolean; till: boolean } | null = null;
|
|
411
|
+
/** Last find attempt, so `;` repeats it and `,` reverses it. */
|
|
412
|
+
#lastFind: { forward: boolean; till: boolean; target: string } | null = null;
|
|
413
|
+
/** `r` waiting for the replacement grapheme. */
|
|
414
|
+
#replaceChar = false;
|
|
415
|
+
|
|
274
416
|
/**
|
|
275
417
|
* Vim's "desired column": `j`/`k` remember the column you started from, so descending through a
|
|
276
418
|
* short line and back out returns to it instead of collapsing permanently. `null` means the
|
|
@@ -279,18 +421,25 @@ export class VimState {
|
|
|
279
421
|
*/
|
|
280
422
|
#desiredCol: number | null = null;
|
|
281
423
|
|
|
282
|
-
/** True while a count, operator, `g`,
|
|
424
|
+
/** True while a count, operator, `g`, text-object, find, or `r` prefix is half-typed — Escape cancels it. */
|
|
283
425
|
get pending(): boolean {
|
|
284
|
-
return
|
|
426
|
+
return (
|
|
427
|
+
this.#count.length > 0 ||
|
|
428
|
+
this.#operator !== null ||
|
|
429
|
+
this.#pendingG ||
|
|
430
|
+
this.#textObject !== null ||
|
|
431
|
+
this.#findPending !== null ||
|
|
432
|
+
this.#replaceChar
|
|
433
|
+
);
|
|
285
434
|
}
|
|
286
435
|
|
|
287
436
|
/**
|
|
288
|
-
* The half-typed command as Vim would echo it (`"2"`, `"d"`, `"2d"`, `"di"`) — empty
|
|
289
|
-
* nothing is pending. Hosts render this next to the mode so a partially entered operator is
|
|
437
|
+
* The half-typed command as Vim would echo it (`"2"`, `"d"`, `"2d"`, `"di"`, `"df"`, `"r"`) — empty
|
|
438
|
+
* when nothing is pending. Hosts render this next to the mode so a partially entered operator is
|
|
290
439
|
* visible instead of silently swallowing the next keystroke.
|
|
291
440
|
*/
|
|
292
441
|
get pendingText(): string {
|
|
293
|
-
return `${this.#count}${this.#operator ?? ""}${this.#pendingG ? "g" : ""}${this.#textObject ?? ""}`;
|
|
442
|
+
return `${this.#count}${this.#operator ?? ""}${this.#pendingG ? "g" : ""}${this.#textObject ?? ""}${findPendingKey(this.#findPending)}${this.#replaceChar ? "r" : ""}`;
|
|
294
443
|
}
|
|
295
444
|
|
|
296
445
|
get visual(): boolean {
|
|
@@ -302,6 +451,7 @@ export class VimState {
|
|
|
302
451
|
this.anchor = null;
|
|
303
452
|
this.#desiredCol = null;
|
|
304
453
|
this.#clearPending();
|
|
454
|
+
this.#lastFind = null;
|
|
305
455
|
}
|
|
306
456
|
|
|
307
457
|
#clearPending(): void {
|
|
@@ -309,6 +459,8 @@ export class VimState {
|
|
|
309
459
|
this.#operator = null;
|
|
310
460
|
this.#pendingG = false;
|
|
311
461
|
this.#textObject = null;
|
|
462
|
+
this.#findPending = null;
|
|
463
|
+
this.#replaceChar = false;
|
|
312
464
|
}
|
|
313
465
|
|
|
314
466
|
#takeCount(): number {
|
|
@@ -334,6 +486,46 @@ export class VimState {
|
|
|
334
486
|
handleKey(key: string, buf: VimBuffer): VimCommand[] | null {
|
|
335
487
|
if (key === "escape") return this.#handleEscape(buf);
|
|
336
488
|
if (this.mode === "insert") return null;
|
|
489
|
+
// The character after `f`/`t` is the target, including digits. Resolve it before the count prefix.
|
|
490
|
+
if (this.#findPending) {
|
|
491
|
+
const spec = this.#findPending;
|
|
492
|
+
this.#findPending = null;
|
|
493
|
+
this.#desiredCol = null;
|
|
494
|
+
this.#lastFind = { ...spec, target: key };
|
|
495
|
+
const count = this.#count.length > 0 ? Math.max(1, Number.parseInt(this.#count, 10)) : 1;
|
|
496
|
+
const motion = this.#seek(key, buf, spec, count);
|
|
497
|
+
if (!motion) {
|
|
498
|
+
this.#operator = null;
|
|
499
|
+
this.#count = "";
|
|
500
|
+
return [];
|
|
501
|
+
}
|
|
502
|
+
return this.#applyMotion(buf, motion);
|
|
503
|
+
}
|
|
504
|
+
if (this.#replaceChar) {
|
|
505
|
+
this.#replaceChar = false;
|
|
506
|
+
const count = this.#count.length > 0 ? Math.max(1, Number.parseInt(this.#count, 10)) : 1;
|
|
507
|
+
const line = buf.lines[buf.cursorLine] ?? "";
|
|
508
|
+
// One segmenter pass: stepping with `nextGraphemeStart` would rescan the line per grapheme.
|
|
509
|
+
let end = -1;
|
|
510
|
+
let taken = 0;
|
|
511
|
+
for (const seg of segmenter.segment(line)) {
|
|
512
|
+
if (seg.index < buf.cursorCol) continue;
|
|
513
|
+
if (++taken === count) {
|
|
514
|
+
end = seg.index + seg.segment.length;
|
|
515
|
+
break;
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
this.#count = "";
|
|
519
|
+
if (end < 0) return [];
|
|
520
|
+
return [
|
|
521
|
+
{
|
|
522
|
+
kind: "replace",
|
|
523
|
+
from: { line: buf.cursorLine, col: buf.cursorCol },
|
|
524
|
+
to: { line: buf.cursorLine, col: end },
|
|
525
|
+
text: key.repeat(count),
|
|
526
|
+
},
|
|
527
|
+
];
|
|
528
|
+
}
|
|
337
529
|
|
|
338
530
|
// Count prefix. `0` is the line-start motion unless it extends a count already being typed.
|
|
339
531
|
if ((key >= "1" && key <= "9") || (key === "0" && this.#count.length > 0)) {
|
|
@@ -364,6 +556,21 @@ export class VimState {
|
|
|
364
556
|
this.#textObject = key;
|
|
365
557
|
return [];
|
|
366
558
|
}
|
|
559
|
+
if (key === "f" || key === "F" || key === "t" || key === "T") {
|
|
560
|
+
this.#desiredCol = null;
|
|
561
|
+
this.#findPending = { forward: key === "f" || key === "t", till: key === "t" || key === "T" };
|
|
562
|
+
return [];
|
|
563
|
+
}
|
|
564
|
+
if ((key === "r" || key === "R") && this.#operator === null && !this.visual) {
|
|
565
|
+
this.#desiredCol = null;
|
|
566
|
+
if (key === "R") {
|
|
567
|
+
this.#takeCount();
|
|
568
|
+
this.mode = "replace";
|
|
569
|
+
return [{ kind: "mode", mode: "replace" }];
|
|
570
|
+
}
|
|
571
|
+
this.#replaceChar = true;
|
|
572
|
+
return [];
|
|
573
|
+
}
|
|
367
574
|
|
|
368
575
|
// Only consecutive `j`/`k` carry the desired column; anything else re-anchors it. Counts and
|
|
369
576
|
// the `g` prefix returned above, so `2j` still continues an established column.
|
|
@@ -388,7 +595,7 @@ export class VimState {
|
|
|
388
595
|
{ kind: "move", to: this.#clampNormal(buf, cursorOf(buf)) },
|
|
389
596
|
];
|
|
390
597
|
}
|
|
391
|
-
if (this.mode === "insert") {
|
|
598
|
+
if (this.mode === "insert" || this.mode === "replace") {
|
|
392
599
|
this.mode = "normal";
|
|
393
600
|
const text = buf.lines[buf.cursorLine] ?? "";
|
|
394
601
|
return [
|
|
@@ -454,6 +661,31 @@ export class VimState {
|
|
|
454
661
|
for (let i = 0; i < count; i++) col = wordEnd(line, col);
|
|
455
662
|
return { to: at(col), inclusive: true, linewise: false };
|
|
456
663
|
}
|
|
664
|
+
case "%": {
|
|
665
|
+
// `{count}%` jumps to that percentage of the buffer, linewise, like Vim.
|
|
666
|
+
if (this.#count.length > 0) {
|
|
667
|
+
if (count > 100) return null;
|
|
668
|
+
const target = Math.max(0, Math.floor((count * buf.lines.length + 99) / 100) - 1);
|
|
669
|
+
const text = buf.lines[target] ?? "";
|
|
670
|
+
return { to: { line: target, col: firstNonBlank(text) }, inclusive: false, linewise: true };
|
|
671
|
+
}
|
|
672
|
+
const to = matchDelimiter(buf);
|
|
673
|
+
return to === null ? null : { to, inclusive: true, linewise: false };
|
|
674
|
+
}
|
|
675
|
+
case "{":
|
|
676
|
+
case "}": {
|
|
677
|
+
let line = buf.cursorLine;
|
|
678
|
+
for (let n = 0; n < count; n++) {
|
|
679
|
+
const next = paragraphBoundary(buf.lines, line, key === "}");
|
|
680
|
+
if (next === line) break;
|
|
681
|
+
line = next;
|
|
682
|
+
}
|
|
683
|
+
const text = buf.lines[line] ?? "";
|
|
684
|
+
// No blank line ahead: Vim parks at the end of the buffer, not column 0. A bare move
|
|
685
|
+
// clamps back onto the last grapheme; an operator still covers the rest of the line.
|
|
686
|
+
const atBufferEnd = key === "}" && line === buf.lines.length - 1 && text.trim() !== "";
|
|
687
|
+
return { to: { line, col: atBufferEnd ? text.length : 0 }, inclusive: false, linewise: false };
|
|
688
|
+
}
|
|
457
689
|
case "G": {
|
|
458
690
|
const target = this.#count.length > 0 ? count - 1 : buf.lines.length - 1;
|
|
459
691
|
return {
|
|
@@ -462,23 +694,66 @@ export class VimState {
|
|
|
462
694
|
linewise: true,
|
|
463
695
|
};
|
|
464
696
|
}
|
|
697
|
+
case ";":
|
|
698
|
+
case ",":
|
|
699
|
+
return this.#repeatFind(key === ";", buf, count);
|
|
465
700
|
default:
|
|
466
701
|
return null;
|
|
467
702
|
}
|
|
468
703
|
}
|
|
469
704
|
|
|
705
|
+
#repeatFind(sameDirection: boolean, buf: VimBuffer, count: number): Motion | null {
|
|
706
|
+
const last = this.#lastFind;
|
|
707
|
+
if (!last) return null;
|
|
708
|
+
const forward = sameDirection ? last.forward : !last.forward;
|
|
709
|
+
const text = buf.lines[buf.cursorLine] ?? "";
|
|
710
|
+
// A till lands beside its target, so repeating from the cursor rediscovers that same target.
|
|
711
|
+
const fromCol = last.till
|
|
712
|
+
? forward
|
|
713
|
+
? nextGraphemeStart(text, buf.cursorCol)
|
|
714
|
+
: prevGraphemeStart(text, buf.cursorCol)
|
|
715
|
+
: buf.cursorCol;
|
|
716
|
+
return this.#seek(last.target, buf, { forward, till: last.till }, count, fromCol);
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
/**
|
|
720
|
+
* `f`/`F` land on the match and include it. `t`/`T` land on the adjacent grapheme and include
|
|
721
|
+
* that grapheme, so the operator stops short of the target. A till that would not move fails.
|
|
722
|
+
*/
|
|
723
|
+
#seek(
|
|
724
|
+
target: string,
|
|
725
|
+
buf: VimBuffer,
|
|
726
|
+
spec: { forward: boolean; till: boolean },
|
|
727
|
+
count: number,
|
|
728
|
+
fromCol = buf.cursorCol,
|
|
729
|
+
): Motion | null {
|
|
730
|
+
const text = buf.lines[buf.cursorLine] ?? "";
|
|
731
|
+
const match = findGrapheme(text, fromCol, target, spec.forward, count);
|
|
732
|
+
if (match === null) return null;
|
|
733
|
+
const land = spec.till ? (spec.forward ? prevGraphemeStart(text, match) : nextGraphemeStart(text, match)) : match;
|
|
734
|
+
if (land === buf.cursorCol) return null;
|
|
735
|
+
return { to: { line: buf.cursorLine, col: land }, inclusive: true, linewise: false };
|
|
736
|
+
}
|
|
737
|
+
|
|
470
738
|
/** Turn a resolved motion into either a cursor move, a selection extension, or an operator range. */
|
|
471
739
|
#applyMotion(buf: VimBuffer, motion: Motion): VimCommand[] {
|
|
472
740
|
const operator = this.#operator;
|
|
473
741
|
this.#operator = null;
|
|
474
742
|
this.#takeCount();
|
|
743
|
+
if (operator === ">" || operator === "<") {
|
|
744
|
+
const fromLine = Math.min(buf.cursorLine, motion.to.line);
|
|
745
|
+
const toLine = Math.max(buf.cursorLine, motion.to.line);
|
|
746
|
+
return [{ kind: "indent", fromLine, toLine, out: operator === "<" }];
|
|
747
|
+
}
|
|
475
748
|
|
|
476
749
|
if (operator === null) {
|
|
477
750
|
// `$` parks on the last grapheme in Normal mode but must still be able to select the
|
|
478
|
-
// final character in Visual mode, where the cursor is allowed one past it.
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
751
|
+
// final character in Visual mode, where the cursor is allowed one past it. Replace mode
|
|
752
|
+
// may also sit past the end, so typing there appends instead of overwriting the last grapheme.
|
|
753
|
+
const to =
|
|
754
|
+
this.visual || this.mode === "replace"
|
|
755
|
+
? { line: motion.to.line, col: Math.min(motion.to.col, (buf.lines[motion.to.line] ?? "").length) }
|
|
756
|
+
: this.#clampNormal(buf, motion.to);
|
|
482
757
|
return [{ kind: "move", to }];
|
|
483
758
|
}
|
|
484
759
|
|
|
@@ -494,6 +769,11 @@ export class VimState {
|
|
|
494
769
|
}
|
|
495
770
|
|
|
496
771
|
#operate(operator: VimOperator, from: VimPosition, to: VimPosition, linewise: boolean): VimCommand[] {
|
|
772
|
+
if (operator === ">" || operator === "<") {
|
|
773
|
+
const fromLine = Math.min(from.line, to.line);
|
|
774
|
+
const toLine = Math.max(from.line, to.line);
|
|
775
|
+
return [{ kind: "indent", fromLine, toLine, out: operator === "<" }];
|
|
776
|
+
}
|
|
497
777
|
if (operator === "y") {
|
|
498
778
|
return [
|
|
499
779
|
{ kind: "yank", from, to, linewise },
|
|
@@ -683,6 +963,28 @@ export class VimState {
|
|
|
683
963
|
case "u":
|
|
684
964
|
this.#takeCount();
|
|
685
965
|
return [{ kind: "undo" }];
|
|
966
|
+
case "J": {
|
|
967
|
+
if (this.#operator !== null) {
|
|
968
|
+
this.#clearPending();
|
|
969
|
+
return [];
|
|
970
|
+
}
|
|
971
|
+
return [{ kind: "join", fromLine: buf.cursorLine, lines: Math.max(2, this.#takeCount()) }];
|
|
972
|
+
}
|
|
973
|
+
case ">":
|
|
974
|
+
case "<": {
|
|
975
|
+
if (this.#operator === key) {
|
|
976
|
+
const span = this.#takeCount();
|
|
977
|
+
const last = Math.min(buf.cursorLine + span - 1, buf.lines.length - 1);
|
|
978
|
+
this.#operator = null;
|
|
979
|
+
return [{ kind: "indent", fromLine: buf.cursorLine, toLine: last, out: key === "<" }];
|
|
980
|
+
}
|
|
981
|
+
if (this.#operator !== null) {
|
|
982
|
+
this.#clearPending();
|
|
983
|
+
return [];
|
|
984
|
+
}
|
|
985
|
+
this.#operator = key;
|
|
986
|
+
return [];
|
|
987
|
+
}
|
|
686
988
|
default:
|
|
687
989
|
// Normal mode swallows unknown printable keys rather than typing them into the buffer.
|
|
688
990
|
this.#clearPending();
|
|
@@ -727,6 +1029,19 @@ export class VimState {
|
|
|
727
1029
|
const commands = this.#operate(operator, from, to, linewise);
|
|
728
1030
|
return operator === "c" ? commands : [...commands, { kind: "mode", mode: "normal" }];
|
|
729
1031
|
}
|
|
1032
|
+
case ">":
|
|
1033
|
+
case "<":
|
|
1034
|
+
case "J": {
|
|
1035
|
+
const { from, to } = visualRange(buf, anchor, linewise);
|
|
1036
|
+
this.#clearPending();
|
|
1037
|
+
this.anchor = null;
|
|
1038
|
+
this.mode = "normal";
|
|
1039
|
+
const edit: VimCommand =
|
|
1040
|
+
key === "J"
|
|
1041
|
+
? { kind: "join", fromLine: from.line, lines: Math.max(2, to.line - from.line + 1) }
|
|
1042
|
+
: { kind: "indent", fromLine: from.line, toLine: to.line, out: key === "<" };
|
|
1043
|
+
return [edit, { kind: "mode", mode: "normal" }];
|
|
1044
|
+
}
|
|
730
1045
|
default:
|
|
731
1046
|
this.#clearPending();
|
|
732
1047
|
return [];
|