@jadelclemens/pi-vim 0.0.0-stage → 1.8.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/operators.ts ADDED
@@ -0,0 +1,305 @@
1
+ /**
2
+ * Operator system for vim.
3
+ * Operators (d, c, y, >, <) combine with motions and text objects to act on ranges.
4
+ */
5
+
6
+ import type { Position, MotionResult } from "./motions.js";
7
+ import type { TextObjectRange } from "./text-objects.js";
8
+ import { deleteToRegister, yankToRegister } from "./registers.js";
9
+
10
+ export interface OperatorRange {
11
+ start: Position;
12
+ end: Position;
13
+ linewise: boolean;
14
+ /** Whether end position is included (inclusive motion) */
15
+ inclusive: boolean;
16
+ }
17
+
18
+ /**
19
+ * Convert a motion result (from cursor) into an operator range.
20
+ */
21
+ export function motionToRange(
22
+ cursor: Position,
23
+ motion: MotionResult,
24
+ ): OperatorRange {
25
+ const motionPos = motion.position;
26
+
27
+ // Determine start/end (motion can go backward)
28
+ let start: Position;
29
+ let end: Position;
30
+
31
+ if (
32
+ motionPos.line < cursor.line ||
33
+ (motionPos.line === cursor.line && motionPos.col < cursor.col)
34
+ ) {
35
+ start = motionPos;
36
+ end = cursor;
37
+ } else {
38
+ start = cursor;
39
+ end = motionPos;
40
+ }
41
+
42
+ return {
43
+ start,
44
+ end,
45
+ linewise: motion.linewise,
46
+ inclusive: motion.inclusive,
47
+ };
48
+ }
49
+
50
+ /**
51
+ * Convert a text object range into an operator range.
52
+ */
53
+ export function textObjectToRange(range: TextObjectRange): OperatorRange {
54
+ return {
55
+ start: range.start,
56
+ end: range.end,
57
+ linewise: range.linewise,
58
+ inclusive: true, // Text objects are always inclusive
59
+ };
60
+ }
61
+
62
+ /**
63
+ * Extract text from a range within the buffer lines.
64
+ */
65
+ export function extractText(
66
+ lines: string[],
67
+ range: OperatorRange,
68
+ ): string {
69
+ if (range.linewise) {
70
+ const startLine = range.start.line;
71
+ const endLine = range.end.line;
72
+ return lines.slice(startLine, endLine + 1).join("\n");
73
+ }
74
+
75
+ if (range.start.line === range.end.line) {
76
+ const line = lines[range.start.line] || "";
77
+ const endCol = range.inclusive ? range.end.col + 1 : range.end.col;
78
+ return line.substring(range.start.col, endCol);
79
+ }
80
+
81
+ // Multi-line, character-wise
82
+ const result: string[] = [];
83
+ const firstLine = lines[range.start.line] || "";
84
+ result.push(firstLine.substring(range.start.col));
85
+
86
+ for (let i = range.start.line + 1; i < range.end.line; i++) {
87
+ result.push(lines[i] || "");
88
+ }
89
+
90
+ const lastLine = lines[range.end.line] || "";
91
+ const endCol = range.inclusive ? range.end.col + 1 : range.end.col;
92
+ result.push(lastLine.substring(0, endCol));
93
+
94
+ return result.join("\n");
95
+ }
96
+
97
+ /**
98
+ * Delete text in a range and return the new lines + cursor position.
99
+ */
100
+ export function deleteRange(
101
+ lines: string[],
102
+ range: OperatorRange,
103
+ ): { newLines: string[]; cursor: Position } {
104
+ const newLines = [...lines];
105
+
106
+ if (range.linewise) {
107
+ const startLine = range.start.line;
108
+ const endLine = range.end.line;
109
+ const deleteCount = endLine - startLine + 1;
110
+ newLines.splice(startLine, deleteCount);
111
+
112
+ // Ensure at least one empty line
113
+ if (newLines.length === 0) {
114
+ newLines.push("");
115
+ }
116
+
117
+ const cursorLine = Math.min(startLine, newLines.length - 1);
118
+ const cursorLineText = newLines[cursorLine] || "";
119
+ const match = cursorLineText.match(/^\s*/);
120
+ const cursorCol = match ? match[0].length : 0;
121
+
122
+ return { newLines, cursor: { line: cursorLine, col: cursorCol } };
123
+ }
124
+
125
+ // Character-wise delete
126
+ if (range.start.line === range.end.line) {
127
+ const line = newLines[range.start.line] || "";
128
+ const endCol = range.inclusive ? range.end.col + 1 : range.end.col;
129
+ newLines[range.start.line] =
130
+ line.substring(0, range.start.col) + line.substring(endCol);
131
+
132
+ const resultLine = newLines[range.start.line] || "";
133
+ const cursorCol = Math.min(
134
+ range.start.col,
135
+ Math.max(0, resultLine.length - 1),
136
+ );
137
+
138
+ return {
139
+ newLines,
140
+ cursor: { line: range.start.line, col: cursorCol },
141
+ };
142
+ }
143
+
144
+ // Multi-line character-wise delete
145
+ const firstLine = newLines[range.start.line] || "";
146
+ const lastLine = newLines[range.end.line] || "";
147
+ const endCol = range.inclusive ? range.end.col + 1 : range.end.col;
148
+
149
+ const merged =
150
+ firstLine.substring(0, range.start.col) + lastLine.substring(endCol);
151
+ newLines.splice(
152
+ range.start.line,
153
+ range.end.line - range.start.line + 1,
154
+ merged,
155
+ );
156
+
157
+ if (newLines.length === 0) {
158
+ newLines.push("");
159
+ }
160
+
161
+ const cursorCol = Math.min(
162
+ range.start.col,
163
+ Math.max(0, (newLines[range.start.line] || "").length - 1),
164
+ );
165
+
166
+ return {
167
+ newLines,
168
+ cursor: { line: range.start.line, col: Math.max(0, cursorCol) },
169
+ };
170
+ }
171
+
172
+ /**
173
+ * Indent lines in a range.
174
+ */
175
+ export function indentRange(
176
+ lines: string[],
177
+ range: OperatorRange,
178
+ shiftWidth: number = 2,
179
+ ): { newLines: string[]; cursor: Position } {
180
+ const newLines = [...lines];
181
+ const indent = " ".repeat(shiftWidth);
182
+
183
+ const startLine = range.start.line;
184
+ const endLine = range.linewise
185
+ ? range.end.line
186
+ : range.end.line;
187
+
188
+ for (let i = startLine; i <= endLine; i++) {
189
+ if ((newLines[i] || "").length > 0) {
190
+ newLines[i] = indent + (newLines[i] || "");
191
+ }
192
+ }
193
+
194
+ const cursorLineText = newLines[startLine] || "";
195
+ const match = cursorLineText.match(/^\s*/);
196
+ const cursorCol = match ? match[0].length : 0;
197
+
198
+ return { newLines, cursor: { line: startLine, col: cursorCol } };
199
+ }
200
+
201
+ /**
202
+ * Dedent lines in a range.
203
+ */
204
+ export function dedentRange(
205
+ lines: string[],
206
+ range: OperatorRange,
207
+ shiftWidth: number = 2,
208
+ ): { newLines: string[]; cursor: Position } {
209
+ const newLines = [...lines];
210
+
211
+ const startLine = range.start.line;
212
+ const endLine = range.linewise
213
+ ? range.end.line
214
+ : range.end.line;
215
+
216
+ for (let i = startLine; i <= endLine; i++) {
217
+ const line = newLines[i] || "";
218
+ let removed = 0;
219
+ while (removed < shiftWidth && removed < line.length && line[removed] === " ") {
220
+ removed++;
221
+ }
222
+ // Also handle tabs
223
+ if (removed === 0 && line.length > 0 && line[0] === "\t") {
224
+ removed = 1;
225
+ }
226
+ newLines[i] = line.substring(removed);
227
+ }
228
+
229
+ const cursorLineText = newLines[startLine] || "";
230
+ const match = cursorLineText.match(/^\s*/);
231
+ const cursorCol = match ? match[0].length : 0;
232
+
233
+ return { newLines, cursor: { line: startLine, col: cursorCol } };
234
+ }
235
+
236
+ /**
237
+ * Apply an operator to a range.
238
+ * Returns the new buffer state and cursor position.
239
+ */
240
+ export function applyOperator(
241
+ operator: string,
242
+ lines: string[],
243
+ range: OperatorRange,
244
+ register: string,
245
+ ): {
246
+ newLines: string[];
247
+ cursor: Position;
248
+ enterInsert: boolean;
249
+ } {
250
+ const text = extractText(lines, range);
251
+
252
+ switch (operator) {
253
+ case "d": {
254
+ deleteToRegister(register, text, range.linewise);
255
+ const result = deleteRange(lines, range);
256
+ return { ...result, enterInsert: false };
257
+ }
258
+
259
+ case "c": {
260
+ deleteToRegister(register, text, range.linewise);
261
+ if (range.linewise) {
262
+ // For linewise change, replace lines with a single empty line and enter insert
263
+ const newLines = [...lines];
264
+ const startLine = range.start.line;
265
+ const endLine = range.end.line;
266
+ newLines.splice(startLine, endLine - startLine + 1, "");
267
+ return {
268
+ newLines,
269
+ cursor: { line: startLine, col: 0 },
270
+ enterInsert: true,
271
+ };
272
+ }
273
+ const result = deleteRange(lines, range);
274
+ // For change, cursor position is at the start of the deleted range
275
+ return {
276
+ newLines: result.newLines,
277
+ cursor: { line: range.start.line, col: range.start.col },
278
+ enterInsert: true,
279
+ };
280
+ }
281
+
282
+ case "y": {
283
+ yankToRegister(register, text, range.linewise);
284
+ // Yank doesn't modify text, cursor goes to start of range
285
+ return {
286
+ newLines: [...lines],
287
+ cursor: { line: range.start.line, col: range.start.col },
288
+ enterInsert: false,
289
+ };
290
+ }
291
+
292
+ case ">": {
293
+ const result = indentRange(lines, range);
294
+ return { ...result, enterInsert: false };
295
+ }
296
+
297
+ case "<": {
298
+ const result = dedentRange(lines, range);
299
+ return { ...result, enterInsert: false };
300
+ }
301
+
302
+ default:
303
+ return { newLines: [...lines], cursor: range.start, enterInsert: false };
304
+ }
305
+ }
package/package.json CHANGED
@@ -1,6 +1,51 @@
1
1
  {
2
2
  "name": "@jadelclemens/pi-vim",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.8.0",
4
+ "description": "Vim motions extension for pi \u2013 normal, insert, visual, and replace modes with operators, motions, text objects, registers, search, and repeat",
5
+ "license": "MIT",
6
+ "author": "burneikis",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/burneikis/pi-vim.git"
10
+ },
11
+ "keywords": [
12
+ "pi-package"
13
+ ],
14
+ "scripts": {
15
+ "test": "node --experimental-default-type=module --import tsx --test test/**/*.test.ts"
16
+ },
17
+ "files": [
18
+ "index.ts",
19
+ "keys.ts",
20
+ "motions.ts",
21
+ "operators.ts",
22
+ "registers.ts",
23
+ "repeat.ts",
24
+ "search.ts",
25
+ "state.ts",
26
+ "text-objects.ts",
27
+ "vim-editor.ts",
28
+ "modes/",
29
+ "README.md",
30
+ "config.ts"
31
+ ],
32
+ "exports": {
33
+ ".": "./index.ts"
34
+ },
35
+ "pi": {
36
+ "extensions": [
37
+ "./index.ts"
38
+ ]
39
+ },
40
+ "peerDependencies": {
41
+ "@mariozechner/pi-coding-agent": "*",
42
+ "@mariozechner/pi-tui": "*"
43
+ },
44
+ "optionalDependencies": {
45
+ "@burneikis/pi-fzfp": "^2.1.0"
46
+ },
47
+ "devDependencies": {
48
+ "@types/node": "^26.1.1",
49
+ "tsx": "^4.23.0"
50
+ }
51
+ }
package/registers.ts ADDED
@@ -0,0 +1,151 @@
1
+ /**
2
+ * Register system for yank/delete/paste.
3
+ *
4
+ * Registers:
5
+ * - `"` (unnamed/default) - last delete or yank
6
+ * - `0` - last yank only
7
+ * - `1-9` - delete history (shifted on each delete)
8
+ * - `a-z` - named registers
9
+ * - `A-Z` - append to named register (on write, reads as lowercase)
10
+ * - `_` - black hole register (discards)
11
+ * - `+` / `*` - system clipboard (aliased to each other)
12
+ */
13
+
14
+ export interface RegisterContent {
15
+ text: string;
16
+ linewise: boolean;
17
+ }
18
+
19
+ const registers: Map<string, RegisterContent> = new Map();
20
+
21
+ /**
22
+ * Check if a register name is valid.
23
+ */
24
+ export function isValidRegister(name: string): boolean {
25
+ if (name === '"' || name === "_") return true;
26
+ if (name >= "0" && name <= "9") return true;
27
+ if (name >= "a" && name <= "z") return true;
28
+ if (name >= "A" && name <= "Z") return true;
29
+ if (name === "+" || name === "*") return true;
30
+ return false;
31
+ }
32
+
33
+ /**
34
+ * Get the content of a register.
35
+ */
36
+ export function getRegister(name: string): RegisterContent | undefined {
37
+ return registers.get(name);
38
+ }
39
+
40
+ /**
41
+ * Store text into a register after a yank operation.
42
+ * Updates the unnamed register `"` and the `0` register.
43
+ */
44
+ export function yankToRegister(
45
+ name: string,
46
+ text: string,
47
+ linewise: boolean,
48
+ ): void {
49
+ if (name === "_") return; // black hole
50
+
51
+ // Handle uppercase registers (append to lowercase)
52
+ if (name >= "A" && name <= "Z") {
53
+ const lower = name.toLowerCase();
54
+ const existing = registers.get(lower);
55
+ if (existing) {
56
+ const separator = existing.linewise || linewise ? "\n" : "";
57
+ const content: RegisterContent = {
58
+ text: existing.text + separator + text,
59
+ linewise: existing.linewise || linewise,
60
+ };
61
+ registers.set(lower, content);
62
+ registers.set('"', content);
63
+ } else {
64
+ const content: RegisterContent = { text, linewise };
65
+ registers.set(lower, content);
66
+ registers.set('"', content);
67
+ }
68
+ return;
69
+ }
70
+
71
+ // Handle clipboard registers
72
+ if (name === "+" || name === "*") {
73
+ const content: RegisterContent = { text, linewise };
74
+ registers.set("+", content);
75
+ registers.set("*", content);
76
+ registers.set('"', content);
77
+ registers.set("0", content);
78
+ return;
79
+ }
80
+
81
+ const content: RegisterContent = { text, linewise };
82
+
83
+ if (name === '"') {
84
+ // Unnamed register: also update register 0
85
+ registers.set('"', content);
86
+ registers.set("0", content);
87
+ } else {
88
+ // Named register
89
+ registers.set(name, content);
90
+ registers.set('"', content);
91
+ }
92
+ }
93
+
94
+ /**
95
+ * Store text into a register after a delete/change operation.
96
+ * Updates the unnamed register `"` and shifts the numbered registers 1-9.
97
+ */
98
+ export function deleteToRegister(
99
+ name: string,
100
+ text: string,
101
+ linewise: boolean,
102
+ ): void {
103
+ if (name === "_") return; // black hole
104
+
105
+ // Handle uppercase registers (append to lowercase)
106
+ if (name >= "A" && name <= "Z") {
107
+ const lower = name.toLowerCase();
108
+ const existing = registers.get(lower);
109
+ if (existing) {
110
+ const separator = existing.linewise || linewise ? "\n" : "";
111
+ const content: RegisterContent = {
112
+ text: existing.text + separator + text,
113
+ linewise: existing.linewise || linewise,
114
+ };
115
+ registers.set(lower, content);
116
+ registers.set('"', content);
117
+ } else {
118
+ const content: RegisterContent = { text, linewise };
119
+ registers.set(lower, content);
120
+ registers.set('"', content);
121
+ }
122
+ return;
123
+ }
124
+
125
+ // Handle clipboard registers
126
+ if (name === "+" || name === "*") {
127
+ const content: RegisterContent = { text, linewise };
128
+ registers.set("+", content);
129
+ registers.set("*", content);
130
+ registers.set('"', content);
131
+ return;
132
+ }
133
+
134
+ const content: RegisterContent = { text, linewise };
135
+
136
+ if (name === '"') {
137
+ // Shift numbered registers 9 <- 8 <- ... <- 2 <- 1
138
+ for (let i = 9; i >= 2; i--) {
139
+ const prev = registers.get(String(i - 1));
140
+ if (prev) {
141
+ registers.set(String(i), prev);
142
+ }
143
+ }
144
+ registers.set("1", content);
145
+ registers.set('"', content);
146
+ } else {
147
+ // Named register
148
+ registers.set(name, content);
149
+ registers.set('"', content);
150
+ }
151
+ }
package/repeat.ts ADDED
@@ -0,0 +1,113 @@
1
+ /**
2
+ * Dot-repeat (.) system for vim.
3
+ *
4
+ * Records each "change" as a replayable sequence:
5
+ * - operator + motion/textobj keys
6
+ * - insert session entry key + inserted text
7
+ * - simple edits (x, X, r{char}, ~, J, p, P)
8
+ *
9
+ * The `.` command replays the last change, optionally with a new count.
10
+ */
11
+
12
+ export interface RecordedChange {
13
+ /** Normal mode keys that initiated this change (e.g., ["d", "2", "w"], ["c", "i", "w"]) */
14
+ keys: string[];
15
+ /** Text typed during insert mode session, if the change entered insert mode */
16
+ insertedText: string;
17
+ /** Whether insert mode was entered as part of this change */
18
+ enteredInsert: boolean;
19
+ }
20
+
21
+ let lastChange: RecordedChange | null = null;
22
+ let currentRecording: RecordedChange | null = null;
23
+ let isRecordingInsert = false;
24
+
25
+ /**
26
+ * Start recording a new change. Call this when a change-initiating key is pressed.
27
+ */
28
+ export function startRecording(): void {
29
+ currentRecording = {
30
+ keys: [],
31
+ insertedText: "",
32
+ enteredInsert: false,
33
+ };
34
+ isRecordingInsert = false;
35
+ }
36
+
37
+ /**
38
+ * Add a key to the current recording.
39
+ */
40
+ export function recordKey(key: string): void {
41
+ if (currentRecording) {
42
+ currentRecording.keys.push(key);
43
+ }
44
+ }
45
+
46
+ /**
47
+ * Mark that the current change entered insert mode.
48
+ */
49
+ export function markInsertEntry(): void {
50
+ if (currentRecording) {
51
+ currentRecording.enteredInsert = true;
52
+ isRecordingInsert = true;
53
+ }
54
+ }
55
+
56
+ /**
57
+ * Record text typed during insert mode.
58
+ */
59
+ export function recordInsertText(text: string): void {
60
+ if (currentRecording && isRecordingInsert) {
61
+ currentRecording.insertedText += text;
62
+ }
63
+ }
64
+
65
+ /**
66
+ * Record a backspace during insert mode (remove last char from insertedText).
67
+ */
68
+ export function recordInsertBackspace(): void {
69
+ if (currentRecording && isRecordingInsert && currentRecording.insertedText.length > 0) {
70
+ currentRecording.insertedText = currentRecording.insertedText.slice(0, -1);
71
+ }
72
+ }
73
+
74
+ /**
75
+ * Finalize the current recording and save it as the last change.
76
+ * Call this when a change completes (operator applied, or Escape from insert).
77
+ */
78
+ export function finalizeRecording(): void {
79
+ if (currentRecording && currentRecording.keys.length > 0) {
80
+ lastChange = currentRecording;
81
+ }
82
+ currentRecording = null;
83
+ isRecordingInsert = false;
84
+ }
85
+
86
+ /**
87
+ * Check if we're currently recording a change.
88
+ */
89
+ export function isCurrentlyRecording(): boolean {
90
+ return currentRecording !== null;
91
+ }
92
+
93
+ /**
94
+ * Check if we're in the insert-recording phase.
95
+ */
96
+ export function isRecordingInsertMode(): boolean {
97
+ return isRecordingInsert;
98
+ }
99
+
100
+ /**
101
+ * Get the last recorded change for dot-repeat.
102
+ */
103
+ export function getLastChange(): RecordedChange | null {
104
+ return lastChange;
105
+ }
106
+
107
+ /**
108
+ * Discard the current recording without saving.
109
+ */
110
+ export function discardRecording(): void {
111
+ currentRecording = null;
112
+ isRecordingInsert = false;
113
+ }