@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/LICENSE +21 -0
- package/README.md +94 -2
- package/config.ts +33 -0
- package/index.ts +43 -0
- package/keys.ts +26 -0
- package/modes/insert.ts +86 -0
- package/modes/normal.ts +1141 -0
- package/modes/replace.ts +136 -0
- package/modes/visual.ts +651 -0
- package/motions.ts +653 -0
- package/operators.ts +305 -0
- package/package.json +49 -4
- package/registers.ts +151 -0
- package/repeat.ts +113 -0
- package/search.ts +295 -0
- package/state.ts +85 -0
- package/text-objects.ts +365 -0
- package/vim-editor.ts +662 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 burneikis
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,95 @@
|
|
|
1
|
-
#
|
|
1
|
+
# pi-vim
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Vim motions extension for [pi-coding-agent](https://github.com/badlogic/pi-mono/tree/main/packages/coding-agent). Replaces the default input editor with a vim-modal editor supporting normal, insert, visual, and replace modes.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pi install npm:@burneikis/pi-vim
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
### With Fuzzy File Picker
|
|
12
|
+
|
|
13
|
+
Install [pi-fzfp](https://github.com/burneikis/pi-fzfp) as a separate pi package alongside pi-vim:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pi install npm:@burneikis/pi-fzfp
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
That's it. When both are installed, pi-vim detects pi-fzfp at startup and integrates its fuzzy autocomplete automatically. pi-fzfp will not install its own editor — pi-vim handles the editor and wraps its autocomplete provider with fzfp's fuzzy matching.
|
|
20
|
+
|
|
21
|
+
> **Requires** [`fd`](https://github.com/sharkdp/fd) and [`fzf`](https://github.com/junegunn/fzf) on your `PATH`.
|
|
22
|
+
|
|
23
|
+
## Configuration
|
|
24
|
+
|
|
25
|
+
`pi-vim` reads optional JSON configuration, with project settings overriding global settings:
|
|
26
|
+
|
|
27
|
+
- Global: `~/.pi/agent/pi-vim/config.json`
|
|
28
|
+
- Project: `<project>/.pi/pi-vim/config.json`
|
|
29
|
+
|
|
30
|
+
```json
|
|
31
|
+
{
|
|
32
|
+
"disableModeIndicator": true
|
|
33
|
+
}
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
`disableModeIndicator` defaults to `false`; set it to `true` to hide the right-border `NORMAL`/`INSERT`/`VISUAL` label and rely on cursor color instead. Cursor colors use OSC 12, which Sakura/VTE supports; terminals that do not support it retain their configured cursor color.
|
|
37
|
+
|
|
38
|
+
## Features
|
|
39
|
+
|
|
40
|
+
### Vim Motions
|
|
41
|
+
- Normal, Insert, Visual, and Replace modes
|
|
42
|
+
- Mode-colored terminal cursor (Normal = yellow-orange, Insert = green, Visual = light purple), with Insert as a bar and other modes as a block. In tmux, focus hooks provide a dim, mode-colored software fallback on blur (insert = underline; other modes = outlined block)
|
|
43
|
+
- Motions (`h`, `j`, `k`, `l`, `w`, `b`, `e`, `0`, `$`, `gg`, `G`, etc.)
|
|
44
|
+
- Operators (`d`, `c`, `y`, `p`, etc.)
|
|
45
|
+
- Text objects (`iw`, `aw`, `i"`, `a(`, etc.)
|
|
46
|
+
- Search (`/`, `?`, `n`, `N`)
|
|
47
|
+
- Registers and yank/paste
|
|
48
|
+
- Dot repeat
|
|
49
|
+
|
|
50
|
+
### Fuzzy File Picker (optional, via pi-fzfp)
|
|
51
|
+
- Replaces `@file` autocomplete with weighted dual-key fuzzy matching
|
|
52
|
+
- Basename matches scored 2× higher than path matches
|
|
53
|
+
- Suffix alignment bonus for extension-aware matching (`@acts` → `abct.ts` over `abct.scss`)
|
|
54
|
+
- Path prefix pre-filtering when query contains `/`
|
|
55
|
+
- Test file penalty as a tiebreaker
|
|
56
|
+
|
|
57
|
+
## Integration Protocol
|
|
58
|
+
|
|
59
|
+
pi-vim and pi-fzfp coordinate via `pi.events` so they work regardless of which extension loads first. Other custom editors can use the same protocol to integrate pi-fzfp.
|
|
60
|
+
|
|
61
|
+
### As an editor extension that wants pi-fzfp integration
|
|
62
|
+
|
|
63
|
+
Register listeners during your extension factory (before `session_start`), so they are in place regardless of load order:
|
|
64
|
+
|
|
65
|
+
```typescript
|
|
66
|
+
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
|
|
67
|
+
import type { AutocompleteProvider } from "@mariozechner/pi-tui";
|
|
68
|
+
|
|
69
|
+
export default function (pi: ExtensionAPI) {
|
|
70
|
+
let wrapAutocomplete: ((provider: AutocompleteProvider) => AutocompleteProvider) | undefined;
|
|
71
|
+
|
|
72
|
+
// Tell pi-fzfp not to set its own editor component.
|
|
73
|
+
pi.events.on("pi-fzfp:check-editor", (ack: () => void) => { ack(); });
|
|
74
|
+
|
|
75
|
+
// Receive the provider (pi-fzfp emits this from both its factory and
|
|
76
|
+
// session_start to cover both load orderings).
|
|
77
|
+
pi.events.on("pi-fzfp:provider", (fn: (provider: AutocompleteProvider) => AutocompleteProvider) => {
|
|
78
|
+
wrapAutocomplete = fn;
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
pi.on("session_start", (_event, ctx) => {
|
|
82
|
+
ctx.ui.setEditorComponent((tui, theme, keybindings) =>
|
|
83
|
+
new MyEditor(tui, theme, keybindings, wrapAutocomplete)
|
|
84
|
+
);
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Then apply `wrapAutocomplete` inside your editor's `setAutocompleteProvider`:
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
override setAutocompleteProvider(provider: AutocompleteProvider): void {
|
|
93
|
+
super.setAutocompleteProvider(this.wrapAutocomplete ? this.wrapAutocomplete(provider) : provider);
|
|
94
|
+
}
|
|
95
|
+
```
|
package/config.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
|
|
5
|
+
export interface VimConfig {
|
|
6
|
+
/** Hide the NORMAL/INSERT/etc. label from the editor border. */
|
|
7
|
+
disableModeIndicator: boolean;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const defaults: VimConfig = { disableModeIndicator: false };
|
|
11
|
+
|
|
12
|
+
function readConfig(path: string): Partial<VimConfig> {
|
|
13
|
+
try {
|
|
14
|
+
if (!existsSync(path)) return {};
|
|
15
|
+
const value: unknown = JSON.parse(readFileSync(path, "utf8"));
|
|
16
|
+
if (!value || typeof value !== "object") return {};
|
|
17
|
+
const config = value as Record<string, unknown>;
|
|
18
|
+
return typeof config.disableModeIndicator === "boolean"
|
|
19
|
+
? { disableModeIndicator: config.disableModeIndicator }
|
|
20
|
+
: {};
|
|
21
|
+
} catch {
|
|
22
|
+
return {};
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Global settings are overridden by a project-local configuration. */
|
|
27
|
+
export function loadConfig(cwd: string): VimConfig {
|
|
28
|
+
return {
|
|
29
|
+
...defaults,
|
|
30
|
+
...readConfig(join(homedir(), ".pi", "agent", "pi-vim", "config.json")),
|
|
31
|
+
...readConfig(join(cwd, ".pi", "pi-vim", "config.json")),
|
|
32
|
+
};
|
|
33
|
+
}
|
package/index.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* pi-vim: Vim motions extension for pi-coding-agent.
|
|
3
|
+
* Replaces the default input editor with a vim-modal editor.
|
|
4
|
+
*
|
|
5
|
+
* Integrates with @burneikis/pi-fzfp if it is also installed:
|
|
6
|
+
* - Responds to "pi-fzfp:check-editor" so fzfp skips its own setEditorComponent.
|
|
7
|
+
* - Receives wrapWithFuzzyFiles via "pi-fzfp:provider" and passes it to VimEditor.
|
|
8
|
+
*
|
|
9
|
+
* Both listeners are registered during the factory (before session_start), so
|
|
10
|
+
* they are in place regardless of which extension's session_start fires first.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
|
|
14
|
+
import type { AutocompleteProvider } from "@mariozechner/pi-tui";
|
|
15
|
+
import { VimEditor } from "./vim-editor.js";
|
|
16
|
+
import { loadConfig } from "./config.js";
|
|
17
|
+
|
|
18
|
+
export default function (pi: ExtensionAPI) {
|
|
19
|
+
let wrapAutocomplete: ((provider: AutocompleteProvider) => AutocompleteProvider) | undefined;
|
|
20
|
+
let editor: VimEditor | undefined;
|
|
21
|
+
|
|
22
|
+
// Ack fzfp's editor check — registered at factory time so it's always ready.
|
|
23
|
+
pi.events.on("pi-fzfp:check-editor", (ack: () => void) => { ack(); });
|
|
24
|
+
|
|
25
|
+
// Capture the provider whenever fzfp announces it (emitted from both fzfp's
|
|
26
|
+
// factory and its session_start to cover both load orderings).
|
|
27
|
+
pi.events.on("pi-fzfp:provider", (fn: (provider: AutocompleteProvider) => AutocompleteProvider) => {
|
|
28
|
+
wrapAutocomplete = fn;
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
pi.on("session_start", (_event, ctx) => {
|
|
32
|
+
const config = loadConfig(ctx.cwd);
|
|
33
|
+
ctx.ui.setEditorComponent((tui, theme, keybindings) => {
|
|
34
|
+
editor = new VimEditor(tui, theme, keybindings, undefined, wrapAutocomplete, config);
|
|
35
|
+
return editor;
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
pi.on("session_shutdown", () => {
|
|
40
|
+
editor?.dispose();
|
|
41
|
+
editor = undefined;
|
|
42
|
+
});
|
|
43
|
+
}
|
package/keys.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Key parsing utilities for vim keybindings.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/** Check if a key is a printable character */
|
|
6
|
+
export function isPrintable(data: string): boolean {
|
|
7
|
+
return data.length === 1 && data.charCodeAt(0) >= 32;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** Check if key is a digit (for count prefix) */
|
|
11
|
+
export function isDigit(data: string): boolean {
|
|
12
|
+
return data.length === 1 && data >= "0" && data <= "9";
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** Escape sequences for cursor movement */
|
|
16
|
+
export const ESCAPE_SEQS = {
|
|
17
|
+
up: "\x1b[A",
|
|
18
|
+
down: "\x1b[B",
|
|
19
|
+
right: "\x1b[C",
|
|
20
|
+
left: "\x1b[D",
|
|
21
|
+
home: "\x01", // Ctrl+A
|
|
22
|
+
end: "\x05", // Ctrl+E
|
|
23
|
+
delete: "\x1b[3~",
|
|
24
|
+
backspace: "\x7f",
|
|
25
|
+
newline: "\n",
|
|
26
|
+
} as const;
|
package/modes/insert.ts
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Insert mode handler.
|
|
3
|
+
* Passes all keys through to the base editor except Escape/Ctrl+[.
|
|
4
|
+
* Tracks inserted text for dot-repeat.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { VimState } from "../state.js";
|
|
8
|
+
import { matchesKey } from "@mariozechner/pi-tui";
|
|
9
|
+
import {
|
|
10
|
+
isCurrentlyRecording,
|
|
11
|
+
isRecordingInsertMode,
|
|
12
|
+
recordInsertText,
|
|
13
|
+
recordInsertBackspace,
|
|
14
|
+
finalizeRecording,
|
|
15
|
+
} from "../repeat.js";
|
|
16
|
+
|
|
17
|
+
export interface InsertModeContext {
|
|
18
|
+
state: VimState;
|
|
19
|
+
getCursor: () => { line: number; col: number };
|
|
20
|
+
getText: () => string;
|
|
21
|
+
setText: (text: string) => void;
|
|
22
|
+
moveCursorTo: (line: number, col: number) => void;
|
|
23
|
+
superHandleInput: (data: string) => void;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Handle input in insert mode.
|
|
28
|
+
* Returns true if the key was handled, false if it should be passed to super.
|
|
29
|
+
*/
|
|
30
|
+
export function handleInsertMode(
|
|
31
|
+
data: string,
|
|
32
|
+
ctx: InsertModeContext,
|
|
33
|
+
): boolean {
|
|
34
|
+
// Escape or Ctrl+[ → switch to normal mode
|
|
35
|
+
if (matchesKey(data, "escape") || data === "\x1b") {
|
|
36
|
+
ctx.state.mode = "normal";
|
|
37
|
+
const cursor = ctx.getCursor();
|
|
38
|
+
if (ctx.state.openLineRepeatCount > 1) {
|
|
39
|
+
const lines = ctx.getText().split("\n");
|
|
40
|
+
const insertedLine = lines[cursor.line] || "";
|
|
41
|
+
const copies = Array(ctx.state.openLineRepeatCount - 1).fill(insertedLine);
|
|
42
|
+
lines.splice(cursor.line + 1, 0, ...copies);
|
|
43
|
+
ctx.setText(lines.join("\n"));
|
|
44
|
+
const targetLine = cursor.line + copies.length;
|
|
45
|
+
ctx.moveCursorTo(targetLine, Math.max(0, cursor.col - 1));
|
|
46
|
+
ctx.state.openLineRepeatCount = 1;
|
|
47
|
+
} else if (cursor.col > 0) {
|
|
48
|
+
// Move cursor left one position (vim behavior: cursor moves back on Escape)
|
|
49
|
+
// but only if not already at column 0 (to prevent moving up a line)
|
|
50
|
+
ctx.superHandleInput("\x1b[D");
|
|
51
|
+
}
|
|
52
|
+
// Finalize dot-repeat recording when leaving insert mode
|
|
53
|
+
if (isCurrentlyRecording()) {
|
|
54
|
+
finalizeRecording();
|
|
55
|
+
}
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Ctrl+C → switch to normal mode (without pi's clear behavior)
|
|
60
|
+
// We handle this ourselves to prevent the default ctrl+c behavior
|
|
61
|
+
if (data === "\x03") {
|
|
62
|
+
ctx.state.mode = "normal";
|
|
63
|
+
if (isCurrentlyRecording()) {
|
|
64
|
+
finalizeRecording();
|
|
65
|
+
}
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Track inserted text for dot-repeat
|
|
70
|
+
if (isCurrentlyRecording() && isRecordingInsertMode()) {
|
|
71
|
+
if (data === "\x7f") {
|
|
72
|
+
// Backspace
|
|
73
|
+
recordInsertBackspace();
|
|
74
|
+
} else if (data.length === 1 && data.charCodeAt(0) >= 32) {
|
|
75
|
+
// Printable character
|
|
76
|
+
recordInsertText(data);
|
|
77
|
+
} else if (data === "\n" || data === "\r") {
|
|
78
|
+
// Newline
|
|
79
|
+
recordInsertText("\n");
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Everything else passes through to base editor
|
|
84
|
+
ctx.superHandleInput(data);
|
|
85
|
+
return true;
|
|
86
|
+
}
|