@jadelclemens/pi-vim 0.0.0-stage → 1.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,136 @@
1
+ /**
2
+ * Replace mode handler (R command).
3
+ * Overtypes characters at cursor position instead of inserting.
4
+ * Backspace restores the original character.
5
+ * Escape returns to normal mode.
6
+ */
7
+
8
+ import type { VimState } from "../state.js";
9
+ import { matchesKey } from "@mariozechner/pi-tui";
10
+ import {
11
+ isCurrentlyRecording,
12
+ isRecordingInsertMode,
13
+ recordInsertText,
14
+ recordInsertBackspace,
15
+ finalizeRecording,
16
+ } from "../repeat.js";
17
+
18
+ export interface ReplaceModeContext {
19
+ state: VimState;
20
+ getCursor: () => { line: number; col: number };
21
+ getText: () => string;
22
+ setText: (text: string) => void;
23
+ moveCursorTo: (line: number, col: number) => void;
24
+ superHandleInput: (data: string) => void;
25
+ }
26
+
27
+ /** Stack of original characters replaced during this replace session, for backspace undo */
28
+ let replacedChars: string[] = [];
29
+
30
+ /**
31
+ * Reset replace mode state (call when entering replace mode).
32
+ */
33
+ export function resetReplaceState(): void {
34
+ replacedChars = [];
35
+ }
36
+
37
+ /**
38
+ * Handle input in replace mode.
39
+ */
40
+ export function handleReplaceMode(
41
+ data: string,
42
+ ctx: ReplaceModeContext,
43
+ ): boolean {
44
+ // Escape or Ctrl+[ → switch to normal mode
45
+ if (matchesKey(data, "escape") || data === "\x1b") {
46
+ ctx.state.mode = "normal";
47
+ // Move cursor left one position (vim behavior)
48
+ if (ctx.getCursor().col > 0) {
49
+ ctx.superHandleInput("\x1b[D");
50
+ }
51
+ if (isCurrentlyRecording()) {
52
+ finalizeRecording();
53
+ }
54
+ replacedChars = [];
55
+ return true;
56
+ }
57
+
58
+ // Ctrl+C → switch to normal mode
59
+ if (data === "\x03") {
60
+ ctx.state.mode = "normal";
61
+ if (isCurrentlyRecording()) {
62
+ finalizeRecording();
63
+ }
64
+ replacedChars = [];
65
+ return true;
66
+ }
67
+
68
+ // Backspace → restore the original character and move back
69
+ if (data === "\x7f") {
70
+ const cursor = ctx.getCursor();
71
+ if (cursor.col > 0 && replacedChars.length > 0) {
72
+ const originalChar = replacedChars.pop()!;
73
+ const lines = ctx.getText().split("\n");
74
+ const line = lines[cursor.line] || "";
75
+ // Restore original character at previous position
76
+ const col = cursor.col - 1;
77
+ lines[cursor.line] = line.substring(0, col) + originalChar + line.substring(col + 1);
78
+ ctx.setText(lines.join("\n"));
79
+ ctx.moveCursorTo(cursor.line, col);
80
+
81
+ if (isCurrentlyRecording() && isRecordingInsertMode()) {
82
+ recordInsertBackspace();
83
+ }
84
+ }
85
+ return true;
86
+ }
87
+
88
+ // Enter/Return → move to start of next line (like vim)
89
+ if (data === "\n" || data === "\r") {
90
+ const lines = ctx.getText().split("\n");
91
+ const cursor = ctx.getCursor();
92
+ // Insert a newline (splitting the line)
93
+ const line = lines[cursor.line] || "";
94
+ const before = line.substring(0, cursor.col);
95
+ const after = line.substring(cursor.col);
96
+ lines.splice(cursor.line, 1, before, after);
97
+ ctx.setText(lines.join("\n"));
98
+ ctx.moveCursorTo(cursor.line + 1, 0);
99
+ replacedChars.push(""); // marker for newline (can't undo back past this simply)
100
+
101
+ if (isCurrentlyRecording() && isRecordingInsertMode()) {
102
+ recordInsertText("\n");
103
+ }
104
+ return true;
105
+ }
106
+
107
+ // Printable character → overtype
108
+ if (data.length === 1 && data.charCodeAt(0) >= 32) {
109
+ const lines = ctx.getText().split("\n");
110
+ const cursor = ctx.getCursor();
111
+ const line = lines[cursor.line] || "";
112
+
113
+ if (cursor.col < line.length) {
114
+ // Replace character at cursor
115
+ replacedChars.push(line[cursor.col]!);
116
+ lines[cursor.line] = line.substring(0, cursor.col) + data + line.substring(cursor.col + 1);
117
+ ctx.setText(lines.join("\n"));
118
+ ctx.moveCursorTo(cursor.line, cursor.col + 1);
119
+ } else {
120
+ // At end of line: append (like insert)
121
+ replacedChars.push(""); // nothing to restore
122
+ lines[cursor.line] = line + data;
123
+ ctx.setText(lines.join("\n"));
124
+ ctx.moveCursorTo(cursor.line, cursor.col + 1);
125
+ }
126
+
127
+ if (isCurrentlyRecording() && isRecordingInsertMode()) {
128
+ recordInsertText(data);
129
+ }
130
+ return true;
131
+ }
132
+
133
+ // Pass other control sequences through
134
+ ctx.superHandleInput(data);
135
+ return true;
136
+ }