@oh-my-pi/pi-coding-agent 15.3.1 → 15.4.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.
- package/CHANGELOG.md +119 -0
- package/dist/types/cli/auth-gateway-cli.d.ts +1 -1
- package/dist/types/cli/file-processor.d.ts +1 -1
- package/dist/types/config/settings-schema.d.ts +45 -3
- package/dist/types/config/settings.d.ts +1 -1
- package/dist/types/debug/raw-sse.d.ts +2 -0
- package/dist/types/edit/file-read-cache.d.ts +15 -4
- package/dist/types/edit/index.d.ts +3 -8
- package/dist/types/edit/renderer.d.ts +1 -2
- package/dist/types/eval/__tests__/shared-executors.test.d.ts +1 -0
- package/dist/types/eval/js/shared/local-module-loader.d.ts +16 -0
- package/dist/types/eval/js/shared/rewrite-imports.d.ts +4 -0
- package/dist/types/eval/js/shared/runtime.d.ts +14 -8
- package/dist/types/eval/py/executor.d.ts +1 -2
- package/dist/types/eval/py/kernel.d.ts +6 -0
- package/dist/types/eval/py/tool-bridge.d.ts +1 -5
- package/dist/types/eval/session-id.d.ts +3 -0
- package/dist/types/extensibility/extensions/types.d.ts +1 -3
- package/dist/types/hashline/anchors.d.ts +15 -9
- package/dist/types/hashline/constants.d.ts +0 -2
- package/dist/types/hashline/diff.d.ts +1 -2
- package/dist/types/hashline/executor.d.ts +52 -0
- package/dist/types/hashline/hash.d.ts +44 -93
- package/dist/types/hashline/index.d.ts +2 -1
- package/dist/types/hashline/input.d.ts +2 -9
- package/dist/types/hashline/recovery.d.ts +3 -9
- package/dist/types/hashline/tokenizer.d.ts +91 -0
- package/dist/types/hashline/types.d.ts +5 -7
- package/dist/types/modes/components/extensions/types.d.ts +0 -4
- package/dist/types/modes/types.d.ts +1 -0
- package/dist/types/modes/utils/ui-helpers.d.ts +1 -0
- package/dist/types/sdk.d.ts +2 -0
- package/dist/types/session/agent-session.d.ts +11 -15
- package/dist/types/session/agent-storage.d.ts +11 -10
- package/dist/types/slash-commands/acp-builtins.d.ts +3 -3
- package/dist/types/slash-commands/types.d.ts +0 -5
- package/dist/types/task/executor.d.ts +2 -0
- package/dist/types/task/types.d.ts +8 -0
- package/dist/types/tool-discovery/tool-index.d.ts +0 -50
- package/dist/types/tools/index.d.ts +2 -8
- package/dist/types/tools/match-line-format.d.ts +4 -4
- package/dist/types/tools/output-schema-validator.d.ts +64 -0
- package/dist/types/tools/review.d.ts +13 -0
- package/dist/types/tools/search-tool-bm25.d.ts +1 -1
- package/dist/types/tools/search.d.ts +4 -3
- package/dist/types/utils/edit-mode.d.ts +1 -1
- package/dist/types/web/kagi.d.ts +4 -2
- package/dist/types/web/parallel.d.ts +4 -3
- package/dist/types/web/scrapers/types.d.ts +2 -1
- package/dist/types/web/search/index.d.ts +12 -4
- package/dist/types/web/search/provider.d.ts +2 -1
- package/dist/types/web/search/providers/anthropic.d.ts +9 -4
- package/dist/types/web/search/providers/base.d.ts +34 -2
- package/dist/types/web/search/providers/brave.d.ts +8 -1
- package/dist/types/web/search/providers/codex.d.ts +13 -9
- package/dist/types/web/search/providers/exa.d.ts +10 -1
- package/dist/types/web/search/providers/gemini.d.ts +20 -23
- package/dist/types/web/search/providers/jina.d.ts +2 -1
- package/dist/types/web/search/providers/kagi.d.ts +4 -1
- package/dist/types/web/search/providers/kimi.d.ts +10 -1
- package/dist/types/web/search/providers/parallel.d.ts +3 -2
- package/dist/types/web/search/providers/perplexity.d.ts +5 -2
- package/dist/types/web/search/providers/searxng.d.ts +2 -1
- package/dist/types/web/search/providers/synthetic.d.ts +5 -8
- package/dist/types/web/search/providers/tavily.d.ts +11 -4
- package/dist/types/web/search/providers/utils.d.ts +8 -6
- package/dist/types/web/search/providers/zai.d.ts +12 -3
- package/package.json +7 -7
- package/src/cli/auth-gateway-cli.ts +71 -2
- package/src/cli/file-processor.ts +12 -2
- package/src/cli.ts +0 -8
- package/src/commands/auth-gateway.ts +2 -0
- package/src/commands/commit.ts +8 -8
- package/src/config/prompt-templates.ts +6 -6
- package/src/config/settings-schema.ts +47 -3
- package/src/config/settings.ts +5 -5
- package/src/debug/raw-sse.ts +68 -3
- package/src/edit/file-read-cache.ts +68 -25
- package/src/edit/index.ts +6 -37
- package/src/edit/renderer.ts +9 -47
- package/src/edit/streaming.ts +43 -56
- package/src/eval/__tests__/shared-executors.test.ts +520 -0
- package/src/eval/js/context-manager.ts +64 -53
- package/src/eval/js/shared/local-module-loader.ts +265 -0
- package/src/eval/js/shared/prelude.txt +4 -0
- package/src/eval/js/shared/rewrite-imports.ts +85 -0
- package/src/eval/js/shared/runtime.ts +129 -86
- package/src/eval/js/worker-core.ts +23 -38
- package/src/eval/py/executor.ts +155 -84
- package/src/eval/py/kernel.ts +10 -1
- package/src/eval/py/prelude.py +22 -24
- package/src/eval/py/runner.py +203 -85
- package/src/eval/py/tool-bridge.ts +17 -10
- package/src/eval/session-id.ts +8 -0
- package/src/exec/bash-executor.ts +27 -16
- package/src/extensibility/extensions/runner.ts +0 -1
- package/src/extensibility/extensions/types.ts +1 -3
- package/src/extensibility/plugins/marketplace/manager.ts +20 -1
- package/src/hashline/anchors.ts +56 -65
- package/src/hashline/apply.ts +29 -31
- package/src/hashline/constants.ts +0 -3
- package/src/hashline/diff-preview.ts +4 -5
- package/src/hashline/diff.ts +30 -4
- package/src/hashline/execute.ts +91 -26
- package/src/hashline/executor.ts +239 -0
- package/src/hashline/grammar.lark +12 -10
- package/src/hashline/hash.ts +69 -114
- package/src/hashline/index.ts +2 -1
- package/src/hashline/input.ts +48 -41
- package/src/hashline/prefixes.ts +21 -11
- package/src/hashline/recovery.ts +63 -71
- package/src/hashline/stream.ts +2 -2
- package/src/hashline/tokenizer.ts +467 -0
- package/src/hashline/types.ts +6 -8
- package/src/internal-urls/docs-index.generated.ts +9 -8
- package/src/lsp/config.ts +87 -22
- package/src/modes/components/extensions/types.ts +0 -5
- package/src/modes/components/session-observer-overlay.ts +11 -2
- package/src/modes/components/tree-selector.ts +10 -2
- package/src/modes/controllers/command-controller.ts +1 -3
- package/src/modes/controllers/extension-ui-controller.ts +10 -11
- package/src/modes/controllers/selector-controller.ts +5 -5
- package/src/modes/types.ts +4 -1
- package/src/modes/utils/ui-helpers.ts +4 -0
- package/src/prompts/agents/explore.md +1 -1
- package/src/prompts/tools/ast-edit.md +1 -1
- package/src/prompts/tools/ast-grep.md +1 -1
- package/src/prompts/tools/eval.md +1 -1
- package/src/prompts/tools/hashline.md +73 -94
- package/src/prompts/tools/read.md +4 -4
- package/src/prompts/tools/search.md +3 -3
- package/src/sdk.ts +21 -24
- package/src/session/agent-session.ts +59 -66
- package/src/session/agent-storage.ts +13 -14
- package/src/slash-commands/acp-builtins.ts +3 -3
- package/src/slash-commands/types.ts +0 -6
- package/src/task/executor.ts +55 -57
- package/src/task/index.ts +8 -4
- package/src/task/render.ts +53 -1
- package/src/task/types.ts +8 -0
- package/src/tool-discovery/tool-index.ts +0 -134
- package/src/tools/ast-edit.ts +36 -13
- package/src/tools/ast-grep.ts +45 -4
- package/src/tools/browser/tab-worker.ts +3 -2
- package/src/tools/eval.ts +2 -1
- package/src/tools/fetch.ts +23 -14
- package/src/tools/index.ts +2 -8
- package/src/tools/irc.ts +59 -5
- package/src/tools/jtd-to-json-schema.ts +5 -1
- package/src/tools/match-line-format.ts +5 -7
- package/src/tools/output-schema-validator.ts +132 -0
- package/src/tools/read.ts +142 -63
- package/src/tools/review.ts +23 -0
- package/src/tools/search-tool-bm25.ts +3 -30
- package/src/tools/search.ts +48 -16
- package/src/tools/write.ts +3 -3
- package/src/tools/yield.ts +32 -41
- package/src/utils/edit-mode.ts +1 -2
- package/src/utils/file-mentions.ts +2 -2
- package/src/web/kagi.ts +15 -6
- package/src/web/parallel.ts +9 -6
- package/src/web/scrapers/types.ts +7 -1
- package/src/web/scrapers/youtube.ts +13 -7
- package/src/web/search/index.ts +37 -11
- package/src/web/search/provider.ts +5 -3
- package/src/web/search/providers/anthropic.ts +30 -21
- package/src/web/search/providers/base.ts +35 -2
- package/src/web/search/providers/brave.ts +4 -4
- package/src/web/search/providers/codex.ts +118 -89
- package/src/web/search/providers/exa.ts +3 -2
- package/src/web/search/providers/gemini.ts +58 -155
- package/src/web/search/providers/jina.ts +4 -4
- package/src/web/search/providers/kagi.ts +17 -11
- package/src/web/search/providers/kimi.ts +29 -13
- package/src/web/search/providers/parallel.ts +171 -23
- package/src/web/search/providers/perplexity.ts +38 -37
- package/src/web/search/providers/searxng.ts +3 -1
- package/src/web/search/providers/synthetic.ts +16 -19
- package/src/web/search/providers/tavily.ts +23 -18
- package/src/web/search/providers/utils.ts +11 -17
- package/src/web/search/providers/zai.ts +16 -8
- package/dist/types/hashline/parser.d.ts +0 -7
- package/dist/types/mcp/discoverable-tool-metadata.d.ts +0 -7
- package/dist/types/tools/vim.d.ts +0 -58
- package/dist/types/vim/buffer.d.ts +0 -41
- package/dist/types/vim/commands.d.ts +0 -6
- package/dist/types/vim/engine.d.ts +0 -47
- package/dist/types/vim/parser.d.ts +0 -3
- package/dist/types/vim/render.d.ts +0 -25
- package/dist/types/vim/types.d.ts +0 -182
- package/src/hashline/parser.ts +0 -212
- package/src/mcp/discoverable-tool-metadata.ts +0 -24
- package/src/prompts/tools/vim.md +0 -98
- package/src/tools/vim.ts +0 -949
- package/src/vim/buffer.ts +0 -309
- package/src/vim/commands.ts +0 -382
- package/src/vim/engine.ts +0 -2409
- package/src/vim/parser.ts +0 -134
- package/src/vim/render.ts +0 -252
- package/src/vim/types.ts +0 -197
package/src/vim/engine.ts
DELETED
|
@@ -1,2409 +0,0 @@
|
|
|
1
|
-
import type { FileDiagnosticsResult } from "../lsp";
|
|
2
|
-
import { snapshotEqual, type VimBuffer } from "./buffer";
|
|
3
|
-
import { parseExCommand } from "./commands";
|
|
4
|
-
import { replayTokens } from "./parser";
|
|
5
|
-
import type {
|
|
6
|
-
Position,
|
|
7
|
-
VimBufferSnapshot,
|
|
8
|
-
VimInputMode,
|
|
9
|
-
VimKeyToken,
|
|
10
|
-
VimLineRange,
|
|
11
|
-
VimLoadedFile,
|
|
12
|
-
VimPendingInput,
|
|
13
|
-
VimRegister,
|
|
14
|
-
VimSearchState,
|
|
15
|
-
VimSelection,
|
|
16
|
-
VimUndoEntry,
|
|
17
|
-
} from "./types";
|
|
18
|
-
import { clonePosition, maxPosition, minPosition, toPublicMode, VimInputError as VimError } from "./types";
|
|
19
|
-
|
|
20
|
-
export interface VimSaveResult {
|
|
21
|
-
loaded: VimLoadedFile;
|
|
22
|
-
diagnostics?: FileDiagnosticsResult;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export interface VimEngineCallbacks {
|
|
26
|
-
beforeMutate: (buffer: VimBuffer) => Promise<void>;
|
|
27
|
-
loadBuffer: (path: string) => Promise<VimLoadedFile>;
|
|
28
|
-
saveBuffer: (buffer: VimBuffer, options?: { force?: boolean }) => Promise<VimSaveResult>;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
interface PendingChange {
|
|
32
|
-
before: VimBufferSnapshot;
|
|
33
|
-
tokens: string[];
|
|
34
|
-
moveCursorLeftOnEscape: boolean;
|
|
35
|
-
inserted: boolean;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
interface MotionResult {
|
|
39
|
-
nextIndex: number;
|
|
40
|
-
target: Position;
|
|
41
|
-
inclusive?: boolean;
|
|
42
|
-
linewise?: boolean;
|
|
43
|
-
range?: { start: number; end: number; linewise?: boolean };
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const WORD_CHAR = /[A-Za-z0-9_]/;
|
|
47
|
-
const DEFAULT_VIEWPORT_HEIGHT = 10;
|
|
48
|
-
const BRACKET_PAIRS = new Map<string, string>([
|
|
49
|
-
["(", ")"],
|
|
50
|
-
["[", "]"],
|
|
51
|
-
["{", "}"],
|
|
52
|
-
["<", ">"],
|
|
53
|
-
]);
|
|
54
|
-
const CLOSING_BRACKETS = new Map<string, string>(
|
|
55
|
-
Array.from(BRACKET_PAIRS.entries()).map(([open, close]) => [close, open]),
|
|
56
|
-
);
|
|
57
|
-
const NOOP_Z_COMMANDS = new Set(["a", "A", "c", "C", "m", "M", "o", "O", "r", "R", "v", "x", "X"]);
|
|
58
|
-
|
|
59
|
-
function escapeRegex(value: string): string {
|
|
60
|
-
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
function isWhitespace(char: string): boolean {
|
|
64
|
-
return /\s/.test(char);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
function isWordChar(char: string): boolean {
|
|
68
|
-
return WORD_CHAR.test(char);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
function wordCategory(char: string, bigWord: boolean): "space" | "word" | "punct" {
|
|
72
|
-
if (char.length === 0 || isWhitespace(char)) {
|
|
73
|
-
return "space";
|
|
74
|
-
}
|
|
75
|
-
if (bigWord) {
|
|
76
|
-
return "word";
|
|
77
|
-
}
|
|
78
|
-
return isWordChar(char) ? "word" : "punct";
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
function decodeReplacement(replacement: string): string {
|
|
82
|
-
return replacement.replace(/\\\//g, "/").replace(/\\\\/g, "\\");
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
function literalTextToReplayTokens(text: string): string[] {
|
|
86
|
-
const tokens: string[] = [];
|
|
87
|
-
for (const char of text) {
|
|
88
|
-
if (char === "\n") {
|
|
89
|
-
tokens.push("CR");
|
|
90
|
-
continue;
|
|
91
|
-
}
|
|
92
|
-
if (char === "\t") {
|
|
93
|
-
tokens.push("Tab");
|
|
94
|
-
continue;
|
|
95
|
-
}
|
|
96
|
-
tokens.push(char);
|
|
97
|
-
}
|
|
98
|
-
return tokens;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
// Convert a vim-style search pattern to a JavaScript RegExp.
|
|
102
|
-
// In vim's default ("magic") mode, (, ), {, }, |, + are literal unless backslash-escaped.
|
|
103
|
-
// In JS regex these are metacharacters. Swap the escaping so bare chars are literal
|
|
104
|
-
// and \( etc. become regex groups.
|
|
105
|
-
function vimPatternToJsRegex(pattern: string): string {
|
|
106
|
-
return pattern.replace(/\\([(){}|+])|([(){}|+])/g, (_match, escaped, bare) => {
|
|
107
|
-
if (escaped) return escaped; // \( -> ( (regex group)
|
|
108
|
-
return `\\${bare}`; // ( -> \( (literal paren)
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
function createSearchRegex(pattern: string, flags = "g"): RegExp {
|
|
113
|
-
try {
|
|
114
|
-
return new RegExp(vimPatternToJsRegex(pattern), flags);
|
|
115
|
-
} catch {
|
|
116
|
-
return new RegExp(escapeRegex(pattern), flags);
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
function detectIndentUnit(lines: string[]): string {
|
|
121
|
-
for (const line of lines) {
|
|
122
|
-
if (line.startsWith("\t")) {
|
|
123
|
-
return "\t";
|
|
124
|
-
}
|
|
125
|
-
if (line.startsWith(" ")) {
|
|
126
|
-
return " ";
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
return "\t";
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
function normalizeRange(start: number, end: number): { start: number; end: number } {
|
|
133
|
-
return {
|
|
134
|
-
start: Math.min(start, end),
|
|
135
|
-
end: Math.max(start, end),
|
|
136
|
-
};
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
function selectionFromAnchor(buffer: VimBuffer, anchor: Position, linewise: boolean): VimSelection {
|
|
140
|
-
if (linewise) {
|
|
141
|
-
const startLine = Math.min(anchor.line, buffer.cursor.line);
|
|
142
|
-
const endLine = Math.max(anchor.line, buffer.cursor.line);
|
|
143
|
-
return {
|
|
144
|
-
kind: "line",
|
|
145
|
-
start: { line: startLine + 1, col: 1 },
|
|
146
|
-
end: { line: endLine + 1, col: buffer.getLine(endLine).length + 1 },
|
|
147
|
-
};
|
|
148
|
-
}
|
|
149
|
-
const start = minPosition(anchor, buffer.cursor);
|
|
150
|
-
const end = maxPosition(anchor, buffer.cursor);
|
|
151
|
-
return {
|
|
152
|
-
kind: "char",
|
|
153
|
-
start: { line: start.line + 1, col: start.col + 1 },
|
|
154
|
-
end: { line: end.line + 1, col: end.col + 1 },
|
|
155
|
-
};
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
function expandVisualOffsets(
|
|
159
|
-
buffer: VimBuffer,
|
|
160
|
-
anchor: Position,
|
|
161
|
-
linewise: boolean,
|
|
162
|
-
): { start: number; end: number; linewise: boolean } {
|
|
163
|
-
if (linewise) {
|
|
164
|
-
const startLine = Math.min(anchor.line, buffer.cursor.line);
|
|
165
|
-
const endLine = Math.max(anchor.line, buffer.cursor.line);
|
|
166
|
-
const startOffset = buffer.positionToOffset({ line: startLine, col: 0 });
|
|
167
|
-
const endOffset =
|
|
168
|
-
endLine >= buffer.lastLineIndex()
|
|
169
|
-
? buffer.getText().length
|
|
170
|
-
: buffer.positionToOffset({ line: endLine + 1, col: 0 });
|
|
171
|
-
return { start: startOffset, end: endOffset, linewise: true };
|
|
172
|
-
}
|
|
173
|
-
const anchorOffset = buffer.positionToOffset(anchor);
|
|
174
|
-
const cursorOffset = buffer.positionToOffset(buffer.cursor);
|
|
175
|
-
const { start, end } = normalizeRange(anchorOffset, cursorOffset);
|
|
176
|
-
return { start, end: end + 1, linewise: false };
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
function nextWordStart(text: string, offset: number, bigWord: boolean): number {
|
|
180
|
-
let index = Math.min(Math.max(offset, 0), text.length);
|
|
181
|
-
if (index >= text.length) {
|
|
182
|
-
return text.length;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
const currentCategory = wordCategory(text[index] ?? "", bigWord);
|
|
186
|
-
if (currentCategory === "space") {
|
|
187
|
-
while (index < text.length && wordCategory(text[index] ?? "", bigWord) === "space") {
|
|
188
|
-
index += 1;
|
|
189
|
-
}
|
|
190
|
-
return index;
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
while (index < text.length && wordCategory(text[index] ?? "", bigWord) === currentCategory) {
|
|
194
|
-
index += 1;
|
|
195
|
-
}
|
|
196
|
-
while (index < text.length && wordCategory(text[index] ?? "", bigWord) === "space") {
|
|
197
|
-
index += 1;
|
|
198
|
-
}
|
|
199
|
-
return index;
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
function previousWordStart(text: string, offset: number, bigWord: boolean): number {
|
|
203
|
-
let index = Math.min(Math.max(offset - 1, 0), text.length);
|
|
204
|
-
while (index > 0 && wordCategory(text[index] ?? "", bigWord) === "space") {
|
|
205
|
-
index -= 1;
|
|
206
|
-
}
|
|
207
|
-
const category = wordCategory(text[index] ?? "", bigWord);
|
|
208
|
-
while (index > 0 && wordCategory(text[index - 1] ?? "", bigWord) === category) {
|
|
209
|
-
index -= 1;
|
|
210
|
-
}
|
|
211
|
-
return index;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
function endOfWord(text: string, offset: number, bigWord: boolean): number {
|
|
215
|
-
let index = Math.min(Math.max(offset, 0), text.length);
|
|
216
|
-
while (index < text.length && wordCategory(text[index] ?? "", bigWord) === "space") {
|
|
217
|
-
index += 1;
|
|
218
|
-
}
|
|
219
|
-
const category = wordCategory(text[index] ?? "", bigWord);
|
|
220
|
-
while (index < text.length && wordCategory(text[index] ?? "", bigWord) === category) {
|
|
221
|
-
index += 1;
|
|
222
|
-
}
|
|
223
|
-
return Math.max(0, index - 1);
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
function endOfPreviousWord(text: string, offset: number, bigWord: boolean): number {
|
|
227
|
-
if (text.length === 0) {
|
|
228
|
-
return 0;
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
let index = Math.min(Math.max(offset - 1, 0), text.length - 1);
|
|
232
|
-
while (index >= 0 && wordCategory(text[index] ?? "", bigWord) === "space") {
|
|
233
|
-
index -= 1;
|
|
234
|
-
}
|
|
235
|
-
if (index < 0) {
|
|
236
|
-
return 0;
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
const currentCategory = wordCategory(text[index] ?? "", bigWord);
|
|
240
|
-
while (index >= 0 && wordCategory(text[index] ?? "", bigWord) === currentCategory) {
|
|
241
|
-
index -= 1;
|
|
242
|
-
}
|
|
243
|
-
while (index >= 0 && wordCategory(text[index] ?? "", bigWord) === "space") {
|
|
244
|
-
index -= 1;
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
return Math.max(0, index);
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
function toggleCase(text: string): string {
|
|
251
|
-
let toggled = "";
|
|
252
|
-
for (const char of text) {
|
|
253
|
-
if (char >= "a" && char <= "z") {
|
|
254
|
-
toggled += char.toUpperCase();
|
|
255
|
-
continue;
|
|
256
|
-
}
|
|
257
|
-
if (char >= "A" && char <= "Z") {
|
|
258
|
-
toggled += char.toLowerCase();
|
|
259
|
-
continue;
|
|
260
|
-
}
|
|
261
|
-
toggled += char;
|
|
262
|
-
}
|
|
263
|
-
return toggled;
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
function lastNonBlankColumn(line: string): number {
|
|
267
|
-
for (let index = line.length - 1; index >= 0; index -= 1) {
|
|
268
|
-
if (!isWhitespace(line[index] ?? "")) {
|
|
269
|
-
return index;
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
return 0;
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
function findParagraphStart(lines: string[], line: number): number {
|
|
276
|
-
let index = Math.max(0, line - 1);
|
|
277
|
-
while (index > 0 && lines[index]!.trim().length > 0) {
|
|
278
|
-
index -= 1;
|
|
279
|
-
}
|
|
280
|
-
while (index > 0 && lines[index - 1]!.trim().length === 0) {
|
|
281
|
-
index -= 1;
|
|
282
|
-
}
|
|
283
|
-
return index;
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
function findParagraphEnd(lines: string[], line: number): number {
|
|
287
|
-
let index = Math.min(lines.length - 1, line + 1);
|
|
288
|
-
while (index < lines.length - 1 && lines[index]!.trim().length > 0) {
|
|
289
|
-
index += 1;
|
|
290
|
-
}
|
|
291
|
-
while (index < lines.length - 1 && lines[index + 1]!.trim().length === 0) {
|
|
292
|
-
index += 1;
|
|
293
|
-
}
|
|
294
|
-
return index;
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
function cloneUndoStack(stack: VimUndoEntry[]): VimUndoEntry[] {
|
|
298
|
-
return stack.map(entry => ({
|
|
299
|
-
before: {
|
|
300
|
-
...entry.before,
|
|
301
|
-
lines: [...entry.before.lines],
|
|
302
|
-
cursor: clonePosition(entry.before.cursor),
|
|
303
|
-
baseFingerprint: entry.before.baseFingerprint ? { ...entry.before.baseFingerprint } : null,
|
|
304
|
-
},
|
|
305
|
-
after: {
|
|
306
|
-
...entry.after,
|
|
307
|
-
lines: [...entry.after.lines],
|
|
308
|
-
cursor: clonePosition(entry.after.cursor),
|
|
309
|
-
baseFingerprint: entry.after.baseFingerprint ? { ...entry.after.baseFingerprint } : null,
|
|
310
|
-
},
|
|
311
|
-
}));
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
export class VimEngine {
|
|
315
|
-
buffer: VimBuffer;
|
|
316
|
-
inputMode: VimInputMode = "normal";
|
|
317
|
-
selectionAnchor: Position | null = null;
|
|
318
|
-
register: VimRegister = { kind: "char", text: "" };
|
|
319
|
-
lastSearch: VimSearchState | null = null;
|
|
320
|
-
lastCharFind: { char: string; mode: "f" | "F" | "t" | "T" } | null = null;
|
|
321
|
-
lastVisual: { anchor: Position; cursor: Position; mode: VimInputMode } | null = null;
|
|
322
|
-
lastCommand?: string;
|
|
323
|
-
statusMessage?: string;
|
|
324
|
-
diagnostics?: FileDiagnosticsResult;
|
|
325
|
-
viewportStart = 1;
|
|
326
|
-
closed = false;
|
|
327
|
-
|
|
328
|
-
#callbacks: VimEngineCallbacks;
|
|
329
|
-
#undoStack: VimUndoEntry[] = [];
|
|
330
|
-
#redoStack: VimUndoEntry[] = [];
|
|
331
|
-
#pendingInput = "";
|
|
332
|
-
#lastChangeTokens: string[] | null = null;
|
|
333
|
-
#pendingChange: PendingChange | null = null;
|
|
334
|
-
#stepCallback?: () => Promise<void>;
|
|
335
|
-
|
|
336
|
-
constructor(buffer: VimBuffer, callbacks: VimEngineCallbacks) {
|
|
337
|
-
this.buffer = buffer;
|
|
338
|
-
this.#callbacks = callbacks;
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
clone(callbacks?: Partial<VimEngineCallbacks>): VimEngine {
|
|
342
|
-
const next = new VimEngine(this.buffer.clone(), {
|
|
343
|
-
beforeMutate: callbacks?.beforeMutate ?? this.#callbacks.beforeMutate,
|
|
344
|
-
loadBuffer: callbacks?.loadBuffer ?? this.#callbacks.loadBuffer,
|
|
345
|
-
saveBuffer: callbacks?.saveBuffer ?? this.#callbacks.saveBuffer,
|
|
346
|
-
});
|
|
347
|
-
next.inputMode = this.inputMode;
|
|
348
|
-
next.selectionAnchor = this.selectionAnchor ? clonePosition(this.selectionAnchor) : null;
|
|
349
|
-
next.register = { ...this.register };
|
|
350
|
-
next.lastSearch = this.lastSearch ? { ...this.lastSearch } : null;
|
|
351
|
-
next.lastCharFind = this.lastCharFind ? { ...this.lastCharFind } : null;
|
|
352
|
-
next.lastVisual = this.lastVisual
|
|
353
|
-
? {
|
|
354
|
-
anchor: clonePosition(this.lastVisual.anchor),
|
|
355
|
-
cursor: clonePosition(this.lastVisual.cursor),
|
|
356
|
-
mode: this.lastVisual.mode,
|
|
357
|
-
}
|
|
358
|
-
: null;
|
|
359
|
-
next.lastCommand = this.lastCommand;
|
|
360
|
-
next.statusMessage = this.statusMessage;
|
|
361
|
-
next.diagnostics = this.diagnostics;
|
|
362
|
-
next.viewportStart = this.viewportStart;
|
|
363
|
-
next.closed = this.closed;
|
|
364
|
-
next.#pendingInput = this.#pendingInput;
|
|
365
|
-
next.#lastChangeTokens = this.#lastChangeTokens ? [...this.#lastChangeTokens] : null;
|
|
366
|
-
next.#pendingChange = this.#pendingChange
|
|
367
|
-
? {
|
|
368
|
-
before: {
|
|
369
|
-
...this.#pendingChange.before,
|
|
370
|
-
lines: [...this.#pendingChange.before.lines],
|
|
371
|
-
cursor: clonePosition(this.#pendingChange.before.cursor),
|
|
372
|
-
baseFingerprint: this.#pendingChange.before.baseFingerprint
|
|
373
|
-
? { ...this.#pendingChange.before.baseFingerprint }
|
|
374
|
-
: null,
|
|
375
|
-
},
|
|
376
|
-
tokens: [...this.#pendingChange.tokens],
|
|
377
|
-
moveCursorLeftOnEscape: this.#pendingChange.moveCursorLeftOnEscape,
|
|
378
|
-
inserted: this.#pendingChange.inserted,
|
|
379
|
-
}
|
|
380
|
-
: null;
|
|
381
|
-
next.#undoStack = cloneUndoStack(this.#undoStack);
|
|
382
|
-
next.#redoStack = cloneUndoStack(this.#redoStack);
|
|
383
|
-
return next;
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
getPublicMode() {
|
|
387
|
-
return toPublicMode(this.inputMode);
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
getSelection(): VimSelection | undefined {
|
|
391
|
-
if (this.selectionAnchor === null) {
|
|
392
|
-
return undefined;
|
|
393
|
-
}
|
|
394
|
-
return selectionFromAnchor(this.buffer, this.selectionAnchor, this.inputMode === "visual-line");
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
getPendingInput(): VimPendingInput | undefined {
|
|
398
|
-
switch (this.inputMode) {
|
|
399
|
-
case "insert":
|
|
400
|
-
return { kind: "insert", text: "" };
|
|
401
|
-
case "command":
|
|
402
|
-
case "search-forward":
|
|
403
|
-
case "search-backward":
|
|
404
|
-
return { kind: this.inputMode, text: this.#pendingInput };
|
|
405
|
-
default:
|
|
406
|
-
return undefined;
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
rollbackPendingInsert(): void {
|
|
411
|
-
if (this.#pendingChange) {
|
|
412
|
-
this.buffer.restore(this.#pendingChange.before);
|
|
413
|
-
this.#pendingChange = null;
|
|
414
|
-
}
|
|
415
|
-
this.inputMode = "normal";
|
|
416
|
-
this.selectionAnchor = null;
|
|
417
|
-
this.#pendingInput = "";
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
setCursor(line: number, col: number): void {
|
|
421
|
-
this.buffer.setCursor({ line, col });
|
|
422
|
-
}
|
|
423
|
-
|
|
424
|
-
async executeTokens(
|
|
425
|
-
tokens: readonly VimKeyToken[],
|
|
426
|
-
lastCommand?: string,
|
|
427
|
-
onStep?: () => Promise<void>,
|
|
428
|
-
): Promise<void> {
|
|
429
|
-
const previousStepCallback = this.#stepCallback;
|
|
430
|
-
this.#stepCallback = onStep ?? previousStepCallback;
|
|
431
|
-
this.lastCommand = lastCommand;
|
|
432
|
-
this.statusMessage = undefined;
|
|
433
|
-
this.diagnostics = undefined;
|
|
434
|
-
|
|
435
|
-
try {
|
|
436
|
-
for (let index = 0; index < tokens.length; ) {
|
|
437
|
-
switch (this.inputMode) {
|
|
438
|
-
case "insert":
|
|
439
|
-
index = await this.#executeInsert(tokens, index);
|
|
440
|
-
break;
|
|
441
|
-
case "command":
|
|
442
|
-
case "search-forward":
|
|
443
|
-
case "search-backward":
|
|
444
|
-
index = await this.#executePrompt(tokens, index);
|
|
445
|
-
break;
|
|
446
|
-
case "visual":
|
|
447
|
-
case "visual-line":
|
|
448
|
-
index = await this.#executeVisual(tokens, index);
|
|
449
|
-
break;
|
|
450
|
-
default:
|
|
451
|
-
index = await this.#executeNormal(tokens, index);
|
|
452
|
-
break;
|
|
453
|
-
}
|
|
454
|
-
if (this.closed) {
|
|
455
|
-
break;
|
|
456
|
-
}
|
|
457
|
-
this.#ensureCursorVisible();
|
|
458
|
-
await this.#stepCallback?.();
|
|
459
|
-
}
|
|
460
|
-
} finally {
|
|
461
|
-
this.#stepCallback = previousStepCallback;
|
|
462
|
-
}
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
async close(force: boolean): Promise<void> {
|
|
466
|
-
if (this.buffer.modified && !force) {
|
|
467
|
-
throw new VimError("Unsaved changes; use force to discard");
|
|
468
|
-
}
|
|
469
|
-
this.closed = true;
|
|
470
|
-
this.statusMessage = `Closed ${this.buffer.displayPath}`;
|
|
471
|
-
}
|
|
472
|
-
|
|
473
|
-
#ensureCursorVisible(): void {
|
|
474
|
-
const line = this.buffer.cursor.line + 1;
|
|
475
|
-
if (line < this.viewportStart) {
|
|
476
|
-
this.viewportStart = line;
|
|
477
|
-
return;
|
|
478
|
-
}
|
|
479
|
-
const viewportEnd = this.viewportStart + DEFAULT_VIEWPORT_HEIGHT - 1;
|
|
480
|
-
if (line > viewportEnd) {
|
|
481
|
-
this.viewportStart = Math.max(1, line - DEFAULT_VIEWPORT_HEIGHT + 1);
|
|
482
|
-
}
|
|
483
|
-
}
|
|
484
|
-
|
|
485
|
-
centerViewportOnCursor(size = DEFAULT_VIEWPORT_HEIGHT): void {
|
|
486
|
-
const lineCount = Math.max(this.buffer.lineCount(), 1);
|
|
487
|
-
const clampedSize = Math.max(1, Math.min(size, lineCount));
|
|
488
|
-
const maxStart = Math.max(1, lineCount - clampedSize + 1);
|
|
489
|
-
this.viewportStart = Math.max(1, Math.min(this.buffer.cursor.line + 1 - Math.floor(clampedSize / 2), maxStart));
|
|
490
|
-
}
|
|
491
|
-
|
|
492
|
-
#clearSelection(): void {
|
|
493
|
-
if (this.selectionAnchor && (this.inputMode === "visual" || this.inputMode === "visual-line")) {
|
|
494
|
-
this.lastVisual = {
|
|
495
|
-
anchor: clonePosition(this.selectionAnchor),
|
|
496
|
-
cursor: clonePosition(this.buffer.cursor),
|
|
497
|
-
mode: this.inputMode,
|
|
498
|
-
};
|
|
499
|
-
}
|
|
500
|
-
this.selectionAnchor = null;
|
|
501
|
-
if (this.inputMode === "visual" || this.inputMode === "visual-line") {
|
|
502
|
-
this.inputMode = "normal";
|
|
503
|
-
}
|
|
504
|
-
}
|
|
505
|
-
|
|
506
|
-
async #ensureEditable(): Promise<void> {
|
|
507
|
-
await this.#callbacks.beforeMutate(this.buffer);
|
|
508
|
-
this.diagnostics = undefined;
|
|
509
|
-
this.statusMessage = undefined;
|
|
510
|
-
}
|
|
511
|
-
|
|
512
|
-
#pushUndo(entry: VimUndoEntry, changeTokens?: readonly string[]): void {
|
|
513
|
-
if (snapshotEqual(entry.before, entry.after)) {
|
|
514
|
-
return;
|
|
515
|
-
}
|
|
516
|
-
this.#undoStack.push(entry);
|
|
517
|
-
this.#redoStack = [];
|
|
518
|
-
if (changeTokens && changeTokens.length > 0) {
|
|
519
|
-
this.#lastChangeTokens = [...changeTokens];
|
|
520
|
-
}
|
|
521
|
-
}
|
|
522
|
-
|
|
523
|
-
#beginPendingChange(prefixTokens: readonly string[], moveCursorLeftOnEscape: boolean): void {
|
|
524
|
-
this.#pendingChange = {
|
|
525
|
-
before: this.buffer.createSnapshot(),
|
|
526
|
-
tokens: [...prefixTokens],
|
|
527
|
-
moveCursorLeftOnEscape,
|
|
528
|
-
inserted: false,
|
|
529
|
-
};
|
|
530
|
-
}
|
|
531
|
-
|
|
532
|
-
#markPendingInserted(): void {
|
|
533
|
-
if (this.#pendingChange) {
|
|
534
|
-
this.#pendingChange.inserted = true;
|
|
535
|
-
}
|
|
536
|
-
}
|
|
537
|
-
|
|
538
|
-
#commitPendingChange(): void {
|
|
539
|
-
if (!this.#pendingChange) {
|
|
540
|
-
return;
|
|
541
|
-
}
|
|
542
|
-
const entry: VimUndoEntry = {
|
|
543
|
-
before: this.#pendingChange.before,
|
|
544
|
-
after: this.buffer.createSnapshot(),
|
|
545
|
-
};
|
|
546
|
-
this.#pushUndo(entry, this.#pendingChange.tokens);
|
|
547
|
-
this.#pendingChange = null;
|
|
548
|
-
}
|
|
549
|
-
|
|
550
|
-
async #applyAtomicChange(tokens: readonly string[], mutator: () => void): Promise<void> {
|
|
551
|
-
await this.#ensureEditable();
|
|
552
|
-
const before = this.buffer.createSnapshot();
|
|
553
|
-
mutator();
|
|
554
|
-
this.buffer.modified = true;
|
|
555
|
-
this.#pushUndo({ before, after: this.buffer.createSnapshot() }, tokens);
|
|
556
|
-
}
|
|
557
|
-
|
|
558
|
-
async #startInsertChange(
|
|
559
|
-
tokens: readonly string[],
|
|
560
|
-
mutator?: () => void,
|
|
561
|
-
moveCursorLeftOnEscape = true,
|
|
562
|
-
): Promise<void> {
|
|
563
|
-
await this.#ensureEditable();
|
|
564
|
-
this.#beginPendingChange(tokens, moveCursorLeftOnEscape);
|
|
565
|
-
mutator?.();
|
|
566
|
-
this.buffer.modified = true;
|
|
567
|
-
this.inputMode = "insert";
|
|
568
|
-
}
|
|
569
|
-
|
|
570
|
-
async #executePrompt(tokens: readonly VimKeyToken[], index: number): Promise<number> {
|
|
571
|
-
const token = tokens[index]!;
|
|
572
|
-
if (token.value === "Esc") {
|
|
573
|
-
this.#pendingInput = "";
|
|
574
|
-
this.inputMode = "normal";
|
|
575
|
-
return index + 1;
|
|
576
|
-
}
|
|
577
|
-
if (token.value === "BS") {
|
|
578
|
-
this.#pendingInput = this.#pendingInput.slice(0, -1);
|
|
579
|
-
return index + 1;
|
|
580
|
-
}
|
|
581
|
-
if (token.value !== "CR") {
|
|
582
|
-
this.#pendingInput += token.value === "Tab" ? "\t" : token.value;
|
|
583
|
-
return index + 1;
|
|
584
|
-
}
|
|
585
|
-
|
|
586
|
-
const input = this.#pendingInput;
|
|
587
|
-
this.#pendingInput = "";
|
|
588
|
-
const mode = this.inputMode;
|
|
589
|
-
this.inputMode = "normal";
|
|
590
|
-
if (mode === "command") {
|
|
591
|
-
await this.#executeEx(input);
|
|
592
|
-
} else {
|
|
593
|
-
await this.#runSearch(input, mode === "search-forward" ? 1 : -1, true);
|
|
594
|
-
}
|
|
595
|
-
return index + 1;
|
|
596
|
-
}
|
|
597
|
-
|
|
598
|
-
#exitInsertMode(): void {
|
|
599
|
-
if (this.#pendingChange) {
|
|
600
|
-
this.#pendingChange.tokens.push("Esc");
|
|
601
|
-
if (this.#pendingChange.moveCursorLeftOnEscape && this.#pendingChange.inserted && this.buffer.cursor.col > 0) {
|
|
602
|
-
this.buffer.setCursor({ line: this.buffer.cursor.line, col: this.buffer.cursor.col - 1 });
|
|
603
|
-
}
|
|
604
|
-
}
|
|
605
|
-
this.inputMode = "normal";
|
|
606
|
-
this.#commitPendingChange();
|
|
607
|
-
}
|
|
608
|
-
|
|
609
|
-
async applyLiteralInsert(text: string, exitInsertMode: boolean): Promise<void> {
|
|
610
|
-
if (this.inputMode !== "insert" || !this.#pendingChange) {
|
|
611
|
-
throw new VimError("Insert payload requires INSERT mode.");
|
|
612
|
-
}
|
|
613
|
-
|
|
614
|
-
if (text.length > 0) {
|
|
615
|
-
const offset = this.buffer.currentOffset();
|
|
616
|
-
this.buffer.replaceOffsets(offset, offset, text, offset + text.length);
|
|
617
|
-
this.buffer.modified = true;
|
|
618
|
-
if (text.includes("\n")) {
|
|
619
|
-
this.buffer.trailingNewline = this.buffer.trailingNewline || text.endsWith("\n");
|
|
620
|
-
}
|
|
621
|
-
this.#pendingChange.tokens.push(...literalTextToReplayTokens(text));
|
|
622
|
-
this.#markPendingInserted();
|
|
623
|
-
}
|
|
624
|
-
|
|
625
|
-
if (exitInsertMode) {
|
|
626
|
-
this.#exitInsertMode();
|
|
627
|
-
}
|
|
628
|
-
}
|
|
629
|
-
|
|
630
|
-
async #executeInsert(tokens: readonly VimKeyToken[], index: number): Promise<number> {
|
|
631
|
-
const token = tokens[index]!;
|
|
632
|
-
if (token.value === "Esc") {
|
|
633
|
-
this.#exitInsertMode();
|
|
634
|
-
return index + 1;
|
|
635
|
-
}
|
|
636
|
-
if (token.value === "CR") {
|
|
637
|
-
const offset = this.buffer.currentOffset();
|
|
638
|
-
this.buffer.replaceOffsets(offset, offset, "\n", offset + 1);
|
|
639
|
-
this.buffer.modified = true;
|
|
640
|
-
this.buffer.trailingNewline = true;
|
|
641
|
-
this.#pendingChange?.tokens.push(token.value);
|
|
642
|
-
this.#markPendingInserted();
|
|
643
|
-
return index + 1;
|
|
644
|
-
}
|
|
645
|
-
if (token.value === "BS") {
|
|
646
|
-
const offset = this.buffer.currentOffset();
|
|
647
|
-
if (offset > 0) {
|
|
648
|
-
this.buffer.deleteOffsets(offset - 1, offset);
|
|
649
|
-
this.buffer.modified = true;
|
|
650
|
-
this.#pendingChange?.tokens.push(token.value);
|
|
651
|
-
this.#markPendingInserted();
|
|
652
|
-
}
|
|
653
|
-
return index + 1;
|
|
654
|
-
}
|
|
655
|
-
if (token.value === "Tab") {
|
|
656
|
-
const offset = this.buffer.currentOffset();
|
|
657
|
-
this.buffer.replaceOffsets(offset, offset, "\t", offset + 1);
|
|
658
|
-
this.buffer.modified = true;
|
|
659
|
-
this.#pendingChange?.tokens.push(token.value);
|
|
660
|
-
this.#markPendingInserted();
|
|
661
|
-
return index + 1;
|
|
662
|
-
}
|
|
663
|
-
if (token.value === "C-w") {
|
|
664
|
-
const offset = this.buffer.currentOffset();
|
|
665
|
-
const text = this.buffer.getText();
|
|
666
|
-
let start = previousWordStart(text, offset, false);
|
|
667
|
-
if (start === offset && start > 0) {
|
|
668
|
-
start -= 1;
|
|
669
|
-
}
|
|
670
|
-
this.buffer.deleteOffsets(start, offset);
|
|
671
|
-
this.buffer.modified = true;
|
|
672
|
-
this.#pendingChange?.tokens.push(token.value);
|
|
673
|
-
this.#markPendingInserted();
|
|
674
|
-
return index + 1;
|
|
675
|
-
}
|
|
676
|
-
if (token.value === "C-u") {
|
|
677
|
-
const offset = this.buffer.currentOffset();
|
|
678
|
-
const lineStart = this.buffer.positionToOffset({ line: this.buffer.cursor.line, col: 0 });
|
|
679
|
-
if (offset > lineStart) {
|
|
680
|
-
this.buffer.deleteOffsets(lineStart, offset);
|
|
681
|
-
this.buffer.modified = true;
|
|
682
|
-
this.#pendingChange?.tokens.push(token.value);
|
|
683
|
-
this.#markPendingInserted();
|
|
684
|
-
}
|
|
685
|
-
return index + 1;
|
|
686
|
-
}
|
|
687
|
-
if (token.value === "C-o") {
|
|
688
|
-
// Execute one normal-mode command, then return to insert
|
|
689
|
-
const nextToken = tokens[index + 1];
|
|
690
|
-
if (!nextToken) {
|
|
691
|
-
return index + 1;
|
|
692
|
-
}
|
|
693
|
-
const savedMode = this.inputMode;
|
|
694
|
-
this.inputMode = "normal";
|
|
695
|
-
const nextIdx = await this.#executeNormal(tokens, index + 1);
|
|
696
|
-
this.inputMode = savedMode;
|
|
697
|
-
return nextIdx;
|
|
698
|
-
}
|
|
699
|
-
|
|
700
|
-
const insertText = token.value;
|
|
701
|
-
const offset = this.buffer.currentOffset();
|
|
702
|
-
this.buffer.replaceOffsets(offset, offset, insertText, offset + insertText.length);
|
|
703
|
-
this.buffer.modified = true;
|
|
704
|
-
this.#pendingChange?.tokens.push(token.value);
|
|
705
|
-
this.#markPendingInserted();
|
|
706
|
-
return index + 1;
|
|
707
|
-
}
|
|
708
|
-
|
|
709
|
-
async #executeVisual(tokens: readonly VimKeyToken[], index: number): Promise<number> {
|
|
710
|
-
const token = tokens[index]!;
|
|
711
|
-
if (token.value === "Esc") {
|
|
712
|
-
this.#clearSelection();
|
|
713
|
-
return index + 1;
|
|
714
|
-
}
|
|
715
|
-
if (token.value === "v") {
|
|
716
|
-
if (this.inputMode === "visual") {
|
|
717
|
-
this.#clearSelection();
|
|
718
|
-
}
|
|
719
|
-
return index + 1;
|
|
720
|
-
}
|
|
721
|
-
if (token.value === "V") {
|
|
722
|
-
this.inputMode = this.inputMode === "visual-line" ? "visual" : "visual-line";
|
|
723
|
-
return index + 1;
|
|
724
|
-
}
|
|
725
|
-
if (token.value === "o") {
|
|
726
|
-
if (this.selectionAnchor) {
|
|
727
|
-
const tmp = clonePosition(this.buffer.cursor);
|
|
728
|
-
this.buffer.setCursor(this.selectionAnchor);
|
|
729
|
-
this.selectionAnchor = tmp;
|
|
730
|
-
}
|
|
731
|
-
return index + 1;
|
|
732
|
-
}
|
|
733
|
-
if (token.value === "J") {
|
|
734
|
-
const visual = expandVisualOffsets(
|
|
735
|
-
this.buffer,
|
|
736
|
-
this.selectionAnchor ?? this.buffer.cursor,
|
|
737
|
-
this.inputMode === "visual-line",
|
|
738
|
-
);
|
|
739
|
-
const startLine = this.buffer.offsetToPosition(visual.start).line;
|
|
740
|
-
const endLine = this.buffer.offsetToPosition(Math.max(visual.start, visual.end - 1)).line;
|
|
741
|
-
await this.#applyAtomicChange(["J"], () => {
|
|
742
|
-
this.buffer.joinLines(startLine, endLine - startLine);
|
|
743
|
-
});
|
|
744
|
-
this.#clearSelection();
|
|
745
|
-
return index + 1;
|
|
746
|
-
}
|
|
747
|
-
if (token.value === "u" || token.value === "U") {
|
|
748
|
-
const visual = expandVisualOffsets(
|
|
749
|
-
this.buffer,
|
|
750
|
-
this.selectionAnchor ?? this.buffer.cursor,
|
|
751
|
-
this.inputMode === "visual-line",
|
|
752
|
-
);
|
|
753
|
-
await this.#applyAtomicChange([token.value], () => {
|
|
754
|
-
const original = this.buffer.getText().slice(visual.start, visual.end);
|
|
755
|
-
const transformed = token.value === "U" ? original.toUpperCase() : original.toLowerCase();
|
|
756
|
-
this.buffer.replaceOffsets(visual.start, visual.end, transformed, visual.start);
|
|
757
|
-
});
|
|
758
|
-
this.#clearSelection();
|
|
759
|
-
return index + 1;
|
|
760
|
-
}
|
|
761
|
-
if (token.value === "p" || token.value === "P") {
|
|
762
|
-
const visual = expandVisualOffsets(
|
|
763
|
-
this.buffer,
|
|
764
|
-
this.selectionAnchor ?? this.buffer.cursor,
|
|
765
|
-
this.inputMode === "visual-line",
|
|
766
|
-
);
|
|
767
|
-
await this.#applyAtomicChange([token.value], () => {
|
|
768
|
-
const removed = this.buffer.getText().slice(visual.start, visual.end);
|
|
769
|
-
const pasteText = this.register.text;
|
|
770
|
-
this.buffer.replaceOffsets(visual.start, visual.end, pasteText, visual.start + pasteText.length);
|
|
771
|
-
this.register = { kind: visual.linewise ? "line" : "char", text: removed };
|
|
772
|
-
});
|
|
773
|
-
this.#clearSelection();
|
|
774
|
-
return index + 1;
|
|
775
|
-
}
|
|
776
|
-
if (token.value === "g") {
|
|
777
|
-
const next = tokens[index + 1];
|
|
778
|
-
if (!next) {
|
|
779
|
-
throw new VimError("g requires a second key", token);
|
|
780
|
-
}
|
|
781
|
-
if (next.value === "J") {
|
|
782
|
-
const visual = expandVisualOffsets(
|
|
783
|
-
this.buffer,
|
|
784
|
-
this.selectionAnchor ?? this.buffer.cursor,
|
|
785
|
-
this.inputMode === "visual-line",
|
|
786
|
-
);
|
|
787
|
-
const startLine = this.buffer.offsetToPosition(visual.start).line;
|
|
788
|
-
const endLine = this.buffer.offsetToPosition(Math.max(visual.start, visual.end - 1)).line;
|
|
789
|
-
await this.#applyAtomicChange(["g", "J"], () => {
|
|
790
|
-
const start = this.buffer.clampLine(startLine);
|
|
791
|
-
const end = this.buffer.clampLine(endLine);
|
|
792
|
-
if (start < end) {
|
|
793
|
-
const joined = this.buffer.lines.slice(start, end + 1).join("");
|
|
794
|
-
this.buffer.lines.splice(start, end - start + 1, joined);
|
|
795
|
-
this.buffer.setCursor({ line: start, col: Math.max(0, joined.length - 1) });
|
|
796
|
-
}
|
|
797
|
-
});
|
|
798
|
-
this.#clearSelection();
|
|
799
|
-
return index + 2;
|
|
800
|
-
}
|
|
801
|
-
if (next.value === "u" || next.value === "U" || next.value === "~") {
|
|
802
|
-
const visual = expandVisualOffsets(
|
|
803
|
-
this.buffer,
|
|
804
|
-
this.selectionAnchor ?? this.buffer.cursor,
|
|
805
|
-
this.inputMode === "visual-line",
|
|
806
|
-
);
|
|
807
|
-
await this.#applyAtomicChange(["g", next.value], () => {
|
|
808
|
-
const original = this.buffer.getText().slice(visual.start, visual.end);
|
|
809
|
-
const transformed =
|
|
810
|
-
next.value === "u"
|
|
811
|
-
? original.toLowerCase()
|
|
812
|
-
: next.value === "U"
|
|
813
|
-
? original.toUpperCase()
|
|
814
|
-
: toggleCase(original);
|
|
815
|
-
this.buffer.replaceOffsets(visual.start, visual.end, transformed, visual.start);
|
|
816
|
-
});
|
|
817
|
-
this.#clearSelection();
|
|
818
|
-
return index + 2;
|
|
819
|
-
}
|
|
820
|
-
throw new VimError(`Unsupported g command: g${next.display}`, next);
|
|
821
|
-
}
|
|
822
|
-
|
|
823
|
-
const { count, hasCount, nextIndex } = this.#readCount(tokens, index);
|
|
824
|
-
const opToken = tokens[nextIndex];
|
|
825
|
-
if (!opToken) {
|
|
826
|
-
return nextIndex;
|
|
827
|
-
}
|
|
828
|
-
|
|
829
|
-
switch (opToken.value) {
|
|
830
|
-
case "d":
|
|
831
|
-
case "x":
|
|
832
|
-
case "X":
|
|
833
|
-
case "D":
|
|
834
|
-
case "y":
|
|
835
|
-
case "c":
|
|
836
|
-
case "s":
|
|
837
|
-
case "S":
|
|
838
|
-
case "C":
|
|
839
|
-
case ">":
|
|
840
|
-
case "<":
|
|
841
|
-
case "~": {
|
|
842
|
-
const visual = expandVisualOffsets(
|
|
843
|
-
this.buffer,
|
|
844
|
-
this.selectionAnchor ?? this.buffer.cursor,
|
|
845
|
-
this.inputMode === "visual-line",
|
|
846
|
-
);
|
|
847
|
-
const consumeExtraIndent =
|
|
848
|
-
(opToken.value === ">" || opToken.value === "<") && tokens[nextIndex + 1]?.value === opToken.value;
|
|
849
|
-
const operatorValue =
|
|
850
|
-
opToken.value === "x" || opToken.value === "X" || opToken.value === "D"
|
|
851
|
-
? "d"
|
|
852
|
-
: opToken.value === "s" || opToken.value === "S" || opToken.value === "C"
|
|
853
|
-
? "c"
|
|
854
|
-
: opToken.value;
|
|
855
|
-
const visualTokens = consumeExtraIndent ? [opToken.value, opToken.value] : [opToken.value];
|
|
856
|
-
await this.#applyVisualOperator(operatorValue, visual, count, visualTokens);
|
|
857
|
-
return nextIndex + visualTokens.length;
|
|
858
|
-
}
|
|
859
|
-
case "r": {
|
|
860
|
-
const replacement = tokens[nextIndex + 1];
|
|
861
|
-
if (!replacement || replacement.value.length !== 1) {
|
|
862
|
-
throw new VimError("Visual replace requires a literal character", opToken);
|
|
863
|
-
}
|
|
864
|
-
const visual = expandVisualOffsets(
|
|
865
|
-
this.buffer,
|
|
866
|
-
this.selectionAnchor ?? this.buffer.cursor,
|
|
867
|
-
this.inputMode === "visual-line",
|
|
868
|
-
);
|
|
869
|
-
await this.#applyAtomicChange(["r", replacement.value], () => {
|
|
870
|
-
const original = this.buffer.getText().slice(visual.start, visual.end);
|
|
871
|
-
let replaced = "";
|
|
872
|
-
for (const char of original) {
|
|
873
|
-
replaced += char === "\n" ? "\n" : replacement.value;
|
|
874
|
-
}
|
|
875
|
-
this.buffer.replaceOffsets(visual.start, visual.end, replaced, visual.start);
|
|
876
|
-
});
|
|
877
|
-
this.#clearSelection();
|
|
878
|
-
return nextIndex + 2;
|
|
879
|
-
}
|
|
880
|
-
default:
|
|
881
|
-
break;
|
|
882
|
-
}
|
|
883
|
-
|
|
884
|
-
const motion = this.#resolveMotion(tokens, nextIndex, count, hasCount);
|
|
885
|
-
this.buffer.setCursor(motion.target);
|
|
886
|
-
return motion.nextIndex;
|
|
887
|
-
}
|
|
888
|
-
|
|
889
|
-
async #applyVisualOperator(
|
|
890
|
-
operator: string,
|
|
891
|
-
visual: { start: number; end: number; linewise: boolean },
|
|
892
|
-
count: number,
|
|
893
|
-
tokens: readonly string[],
|
|
894
|
-
): Promise<void> {
|
|
895
|
-
switch (operator) {
|
|
896
|
-
case "y": {
|
|
897
|
-
this.register = {
|
|
898
|
-
kind: visual.linewise ? "line" : "char",
|
|
899
|
-
text: this.buffer.getText().slice(visual.start, visual.end),
|
|
900
|
-
};
|
|
901
|
-
this.#clearSelection();
|
|
902
|
-
this.statusMessage = `Yanked ${count} selection${count === 1 ? "" : "s"}`;
|
|
903
|
-
return;
|
|
904
|
-
}
|
|
905
|
-
case "d": {
|
|
906
|
-
await this.#applyAtomicChange(tokens, () => {
|
|
907
|
-
this.#yankAndDeleteRange(visual);
|
|
908
|
-
});
|
|
909
|
-
this.#clearSelection();
|
|
910
|
-
return;
|
|
911
|
-
}
|
|
912
|
-
case "c": {
|
|
913
|
-
await this.#startInsertChange(tokens, () => {
|
|
914
|
-
this.#yankAndDeleteRange(visual);
|
|
915
|
-
});
|
|
916
|
-
this.#clearSelection();
|
|
917
|
-
return;
|
|
918
|
-
}
|
|
919
|
-
case ">":
|
|
920
|
-
case "<": {
|
|
921
|
-
const startLine = this.buffer.offsetToPosition(visual.start).line;
|
|
922
|
-
const endLine = this.buffer.offsetToPosition(Math.max(visual.start, visual.end - 1)).line;
|
|
923
|
-
await this.#applyAtomicChange(tokens, () => {
|
|
924
|
-
this.buffer.indentLines(
|
|
925
|
-
startLine,
|
|
926
|
-
endLine,
|
|
927
|
-
detectIndentUnit(this.buffer.lines),
|
|
928
|
-
operator === ">" ? 1 : -1,
|
|
929
|
-
);
|
|
930
|
-
});
|
|
931
|
-
this.#clearSelection();
|
|
932
|
-
return;
|
|
933
|
-
}
|
|
934
|
-
case "~": {
|
|
935
|
-
await this.#applyAtomicChange(tokens, () => {
|
|
936
|
-
const original = this.buffer.getText().slice(visual.start, visual.end);
|
|
937
|
-
this.buffer.replaceOffsets(visual.start, visual.end, toggleCase(original), visual.start);
|
|
938
|
-
});
|
|
939
|
-
this.#clearSelection();
|
|
940
|
-
return;
|
|
941
|
-
}
|
|
942
|
-
default:
|
|
943
|
-
throw new VimError(`Unsupported visual operator: ${operator}`);
|
|
944
|
-
}
|
|
945
|
-
}
|
|
946
|
-
|
|
947
|
-
async #executeNormal(tokens: readonly VimKeyToken[], index: number): Promise<number> {
|
|
948
|
-
const { count, hasCount, nextIndex } = this.#readCount(tokens, index);
|
|
949
|
-
const token = tokens[nextIndex];
|
|
950
|
-
if (!token) {
|
|
951
|
-
return nextIndex;
|
|
952
|
-
}
|
|
953
|
-
|
|
954
|
-
switch (token.value) {
|
|
955
|
-
case "h":
|
|
956
|
-
this.buffer.setCursor({ line: this.buffer.cursor.line, col: this.buffer.cursor.col - count });
|
|
957
|
-
return nextIndex + 1;
|
|
958
|
-
case "j":
|
|
959
|
-
this.buffer.setCursor({ line: this.buffer.cursor.line + count, col: this.buffer.cursor.col });
|
|
960
|
-
return nextIndex + 1;
|
|
961
|
-
case "k":
|
|
962
|
-
this.buffer.setCursor({ line: this.buffer.cursor.line - count, col: this.buffer.cursor.col });
|
|
963
|
-
return nextIndex + 1;
|
|
964
|
-
case "l":
|
|
965
|
-
case " ":
|
|
966
|
-
this.buffer.setCursor({ line: this.buffer.cursor.line, col: this.buffer.cursor.col + count });
|
|
967
|
-
return nextIndex + 1;
|
|
968
|
-
case "w":
|
|
969
|
-
case "W":
|
|
970
|
-
case "b":
|
|
971
|
-
case "B":
|
|
972
|
-
case "e":
|
|
973
|
-
case "E":
|
|
974
|
-
case "0":
|
|
975
|
-
case "$":
|
|
976
|
-
case "^":
|
|
977
|
-
case "|":
|
|
978
|
-
case ";":
|
|
979
|
-
case ",":
|
|
980
|
-
case "G":
|
|
981
|
-
case "f":
|
|
982
|
-
case "F":
|
|
983
|
-
case "t":
|
|
984
|
-
case "T":
|
|
985
|
-
case "{":
|
|
986
|
-
case "}":
|
|
987
|
-
case "%":
|
|
988
|
-
case "H":
|
|
989
|
-
case "M":
|
|
990
|
-
case "+":
|
|
991
|
-
case "-":
|
|
992
|
-
case "_":
|
|
993
|
-
case "L": {
|
|
994
|
-
const motion = this.#resolveMotion(tokens, nextIndex, count, hasCount);
|
|
995
|
-
this.buffer.setCursor(motion.target);
|
|
996
|
-
return motion.nextIndex;
|
|
997
|
-
}
|
|
998
|
-
case "*":
|
|
999
|
-
case "#": {
|
|
1000
|
-
const text = this.buffer.getText();
|
|
1001
|
-
const offset = this.buffer.currentOffset();
|
|
1002
|
-
const cat = wordCategory(text[offset] ?? "", false);
|
|
1003
|
-
if (cat === "space") {
|
|
1004
|
-
throw new VimError("No word under cursor", token);
|
|
1005
|
-
}
|
|
1006
|
-
let start = offset;
|
|
1007
|
-
while (start > 0 && wordCategory(text[start - 1] ?? "", false) === cat) start -= 1;
|
|
1008
|
-
let end = offset;
|
|
1009
|
-
while (end < text.length && wordCategory(text[end] ?? "", false) === cat) end += 1;
|
|
1010
|
-
const word = text.slice(start, end);
|
|
1011
|
-
const pattern = `\\b${escapeRegex(word)}\\b`;
|
|
1012
|
-
const direction = token.value === "*" ? 1 : -1;
|
|
1013
|
-
for (let step = 0; step < count; step += 1) {
|
|
1014
|
-
await this.#runSearch(pattern, direction, true);
|
|
1015
|
-
}
|
|
1016
|
-
return nextIndex + 1;
|
|
1017
|
-
}
|
|
1018
|
-
case "n":
|
|
1019
|
-
await this.#repeatSearch(this.lastSearch?.direction ?? 1, count);
|
|
1020
|
-
return nextIndex + 1;
|
|
1021
|
-
case "N":
|
|
1022
|
-
await this.#repeatSearch(((this.lastSearch?.direction ?? 1) * -1) as 1 | -1, count);
|
|
1023
|
-
return nextIndex + 1;
|
|
1024
|
-
case "/":
|
|
1025
|
-
this.inputMode = "search-forward";
|
|
1026
|
-
this.#pendingInput = "";
|
|
1027
|
-
return nextIndex + 1;
|
|
1028
|
-
case "?":
|
|
1029
|
-
this.inputMode = "search-backward";
|
|
1030
|
-
this.#pendingInput = "";
|
|
1031
|
-
return nextIndex + 1;
|
|
1032
|
-
case ":":
|
|
1033
|
-
this.inputMode = "command";
|
|
1034
|
-
this.#pendingInput = "";
|
|
1035
|
-
return nextIndex + 1;
|
|
1036
|
-
case "v":
|
|
1037
|
-
this.inputMode = "visual";
|
|
1038
|
-
this.selectionAnchor = clonePosition(this.buffer.cursor);
|
|
1039
|
-
return nextIndex + 1;
|
|
1040
|
-
case "V":
|
|
1041
|
-
this.inputMode = "visual-line";
|
|
1042
|
-
this.selectionAnchor = clonePosition(this.buffer.cursor);
|
|
1043
|
-
return nextIndex + 1;
|
|
1044
|
-
case "i":
|
|
1045
|
-
// When count > 1 (e.g. `2i`), interpret as `2Gi` — go to line N then insert.
|
|
1046
|
-
// Models confuse `Ni` with `NGi`; bare `i` with a high count is almost never intended.
|
|
1047
|
-
if (hasCount) {
|
|
1048
|
-
this.buffer.setCursor({ line: Math.min(count, this.buffer.lineCount()) - 1, col: 0 });
|
|
1049
|
-
}
|
|
1050
|
-
await this.#startInsertChange(["i"]);
|
|
1051
|
-
return nextIndex + 1;
|
|
1052
|
-
case "a":
|
|
1053
|
-
this.buffer.setCursor({ line: this.buffer.cursor.line, col: this.buffer.cursor.col + 1 });
|
|
1054
|
-
await this.#startInsertChange(["a"]);
|
|
1055
|
-
return nextIndex + 1;
|
|
1056
|
-
case "I":
|
|
1057
|
-
this.buffer.setCursor({
|
|
1058
|
-
line: this.buffer.cursor.line,
|
|
1059
|
-
col: this.buffer.firstNonBlank(this.buffer.cursor.line),
|
|
1060
|
-
});
|
|
1061
|
-
await this.#startInsertChange(["I"]);
|
|
1062
|
-
return nextIndex + 1;
|
|
1063
|
-
case "A":
|
|
1064
|
-
this.buffer.setCursor({
|
|
1065
|
-
line: this.buffer.cursor.line,
|
|
1066
|
-
col: this.buffer.getLine(this.buffer.cursor.line).length,
|
|
1067
|
-
});
|
|
1068
|
-
await this.#startInsertChange(["A"]);
|
|
1069
|
-
return nextIndex + 1;
|
|
1070
|
-
case "o":
|
|
1071
|
-
// When count > 1 (e.g. `13o`), interpret as `13Go` — go to line N then open below.
|
|
1072
|
-
// Models confuse `No` with `NGo`; bare `o` with a high count is almost never intended.
|
|
1073
|
-
if (hasCount) {
|
|
1074
|
-
this.buffer.setCursor({ line: Math.min(count, this.buffer.lineCount()) - 1, col: 0 });
|
|
1075
|
-
}
|
|
1076
|
-
await this.#startInsertChange(["o"], () => {
|
|
1077
|
-
const line = this.buffer.cursor.line + 1;
|
|
1078
|
-
this.buffer.insertLines(line, [""]);
|
|
1079
|
-
});
|
|
1080
|
-
return nextIndex + 1;
|
|
1081
|
-
case "O":
|
|
1082
|
-
if (hasCount) {
|
|
1083
|
-
this.buffer.setCursor({ line: Math.min(count, this.buffer.lineCount()) - 1, col: 0 });
|
|
1084
|
-
}
|
|
1085
|
-
await this.#startInsertChange(["O"], () => {
|
|
1086
|
-
const line = this.buffer.cursor.line;
|
|
1087
|
-
this.buffer.insertLines(line, [""]);
|
|
1088
|
-
});
|
|
1089
|
-
return nextIndex + 1;
|
|
1090
|
-
case "s":
|
|
1091
|
-
await this.#startInsertChange(["s"], () => {
|
|
1092
|
-
this.#deleteCharsForward(count);
|
|
1093
|
-
});
|
|
1094
|
-
return nextIndex + 1;
|
|
1095
|
-
case "S":
|
|
1096
|
-
await this.#changeWholeLines(count, ["S"]);
|
|
1097
|
-
return nextIndex + 1;
|
|
1098
|
-
case "x":
|
|
1099
|
-
await this.#applyAtomicChange(["x"], () => {
|
|
1100
|
-
this.#deleteCharsForward(count);
|
|
1101
|
-
});
|
|
1102
|
-
return nextIndex + 1;
|
|
1103
|
-
case "X":
|
|
1104
|
-
await this.#applyAtomicChange(["X"], () => {
|
|
1105
|
-
const end = this.buffer.currentOffset();
|
|
1106
|
-
const start = Math.max(0, end - count);
|
|
1107
|
-
this.register = { kind: "char", text: this.buffer.deleteOffsets(start, end) };
|
|
1108
|
-
});
|
|
1109
|
-
return nextIndex + 1;
|
|
1110
|
-
case "r": {
|
|
1111
|
-
const replacement = tokens[nextIndex + 1];
|
|
1112
|
-
if (!replacement || replacement.value.length !== 1) {
|
|
1113
|
-
throw new VimError("r requires a replacement character", token);
|
|
1114
|
-
}
|
|
1115
|
-
await this.#applyAtomicChange(["r", replacement.value], () => {
|
|
1116
|
-
const start = this.buffer.currentOffset();
|
|
1117
|
-
this.buffer.replaceOffsets(
|
|
1118
|
-
start,
|
|
1119
|
-
Math.min(this.buffer.getText().length, start + count),
|
|
1120
|
-
replacement.value.repeat(count),
|
|
1121
|
-
start,
|
|
1122
|
-
);
|
|
1123
|
-
});
|
|
1124
|
-
return nextIndex + 2;
|
|
1125
|
-
}
|
|
1126
|
-
case "~":
|
|
1127
|
-
await this.#applyAtomicChange(["~"], () => {
|
|
1128
|
-
const start = this.buffer.currentOffset();
|
|
1129
|
-
const end = Math.min(this.buffer.getText().length, start + count);
|
|
1130
|
-
const text = this.buffer.getText().slice(start, end);
|
|
1131
|
-
this.buffer.replaceOffsets(start, end, toggleCase(text), end);
|
|
1132
|
-
});
|
|
1133
|
-
return nextIndex + 1;
|
|
1134
|
-
case "J":
|
|
1135
|
-
await this.#applyAtomicChange(["J"], () => {
|
|
1136
|
-
this.buffer.joinLines(this.buffer.cursor.line, count);
|
|
1137
|
-
});
|
|
1138
|
-
return nextIndex + 1;
|
|
1139
|
-
case "p":
|
|
1140
|
-
case "P":
|
|
1141
|
-
await this.#applyAtomicChange([token.value], () => {
|
|
1142
|
-
this.#paste(token.value === "p", count);
|
|
1143
|
-
});
|
|
1144
|
-
return nextIndex + 1;
|
|
1145
|
-
case "u":
|
|
1146
|
-
await this.#undo(count);
|
|
1147
|
-
return nextIndex + 1;
|
|
1148
|
-
case "C-r":
|
|
1149
|
-
await this.#redo(count);
|
|
1150
|
-
return nextIndex + 1;
|
|
1151
|
-
case ".":
|
|
1152
|
-
await this.#repeatLastChange(count, token);
|
|
1153
|
-
return nextIndex + 1;
|
|
1154
|
-
case "d":
|
|
1155
|
-
case "c":
|
|
1156
|
-
case "y":
|
|
1157
|
-
case ">":
|
|
1158
|
-
case "<":
|
|
1159
|
-
return this.#executeOperator(tokens, nextIndex, count, hasCount, token.value);
|
|
1160
|
-
case "D":
|
|
1161
|
-
await this.#applyAtomicChange(["D"], () => {
|
|
1162
|
-
const start = this.buffer.currentOffset();
|
|
1163
|
-
const line = this.buffer.getLine(this.buffer.cursor.line);
|
|
1164
|
-
const end = start + (line.length - this.buffer.cursor.col);
|
|
1165
|
-
this.register = { kind: "char", text: this.buffer.deleteOffsets(start, end) };
|
|
1166
|
-
});
|
|
1167
|
-
return nextIndex + 1;
|
|
1168
|
-
case "C":
|
|
1169
|
-
await this.#startInsertChange(["C"], () => {
|
|
1170
|
-
const start = this.buffer.currentOffset();
|
|
1171
|
-
const line = this.buffer.getLine(this.buffer.cursor.line);
|
|
1172
|
-
const end = start + (line.length - this.buffer.cursor.col);
|
|
1173
|
-
this.register = { kind: "char", text: this.buffer.deleteOffsets(start, end) };
|
|
1174
|
-
});
|
|
1175
|
-
return nextIndex + 1;
|
|
1176
|
-
case "z": {
|
|
1177
|
-
const zTarget = tokens[nextIndex + 1];
|
|
1178
|
-
if (!zTarget) {
|
|
1179
|
-
throw new VimError("z requires a second key", token);
|
|
1180
|
-
}
|
|
1181
|
-
if (zTarget.value === "z") {
|
|
1182
|
-
this.centerViewportOnCursor();
|
|
1183
|
-
} else if (zTarget.value === "t" || zTarget.value === "CR") {
|
|
1184
|
-
this.viewportStart = this.buffer.cursor.line + 1;
|
|
1185
|
-
this.buffer.setCursor({
|
|
1186
|
-
line: this.buffer.cursor.line,
|
|
1187
|
-
col: this.buffer.firstNonBlank(this.buffer.cursor.line),
|
|
1188
|
-
});
|
|
1189
|
-
} else if (zTarget.value === "b" || zTarget.value === "-") {
|
|
1190
|
-
this.viewportStart = Math.max(1, this.buffer.cursor.line + 1 - (DEFAULT_VIEWPORT_HEIGHT - 1));
|
|
1191
|
-
this.buffer.setCursor({
|
|
1192
|
-
line: this.buffer.cursor.line,
|
|
1193
|
-
col: this.buffer.firstNonBlank(this.buffer.cursor.line),
|
|
1194
|
-
});
|
|
1195
|
-
} else if (zTarget.value === ".") {
|
|
1196
|
-
this.centerViewportOnCursor();
|
|
1197
|
-
this.buffer.setCursor({
|
|
1198
|
-
line: this.buffer.cursor.line,
|
|
1199
|
-
col: this.buffer.firstNonBlank(this.buffer.cursor.line),
|
|
1200
|
-
});
|
|
1201
|
-
} else if (NOOP_Z_COMMANDS.has(zTarget.value)) {
|
|
1202
|
-
this.statusMessage = `Ignored z${zTarget.display} (folds unsupported)`;
|
|
1203
|
-
} else {
|
|
1204
|
-
throw new VimError(`Unsupported z command: z${zTarget.display}`, zTarget);
|
|
1205
|
-
}
|
|
1206
|
-
return nextIndex + 2;
|
|
1207
|
-
}
|
|
1208
|
-
case "C-f":
|
|
1209
|
-
this.buffer.setCursor({
|
|
1210
|
-
line: this.buffer.cursor.line + Math.max(1, (DEFAULT_VIEWPORT_HEIGHT - 2) * count),
|
|
1211
|
-
col: this.buffer.cursor.col,
|
|
1212
|
-
});
|
|
1213
|
-
return nextIndex + 1;
|
|
1214
|
-
case "C-b":
|
|
1215
|
-
this.buffer.setCursor({
|
|
1216
|
-
line: this.buffer.cursor.line - Math.max(1, (DEFAULT_VIEWPORT_HEIGHT - 2) * count),
|
|
1217
|
-
col: this.buffer.cursor.col,
|
|
1218
|
-
});
|
|
1219
|
-
return nextIndex + 1;
|
|
1220
|
-
case "C-d":
|
|
1221
|
-
this.buffer.setCursor({
|
|
1222
|
-
line: this.buffer.cursor.line + Math.max(1, Math.floor(DEFAULT_VIEWPORT_HEIGHT / 2) * count),
|
|
1223
|
-
col: this.buffer.cursor.col,
|
|
1224
|
-
});
|
|
1225
|
-
return nextIndex + 1;
|
|
1226
|
-
case "C-u":
|
|
1227
|
-
this.buffer.setCursor({
|
|
1228
|
-
line: this.buffer.cursor.line - Math.max(1, Math.floor(DEFAULT_VIEWPORT_HEIGHT / 2) * count),
|
|
1229
|
-
col: this.buffer.cursor.col,
|
|
1230
|
-
});
|
|
1231
|
-
return nextIndex + 1;
|
|
1232
|
-
case "Esc":
|
|
1233
|
-
return nextIndex + 1;
|
|
1234
|
-
case "Y": {
|
|
1235
|
-
const start = this.buffer.cursor.line;
|
|
1236
|
-
const end = this.buffer.clampLine(start + count - 1);
|
|
1237
|
-
this.register = { kind: "line", text: this.buffer.lines.slice(start, end + 1).join("\n") };
|
|
1238
|
-
this.statusMessage = `Yanked ${end - start + 1} line${end === start ? "" : "s"}`;
|
|
1239
|
-
return nextIndex + 1;
|
|
1240
|
-
}
|
|
1241
|
-
case "R":
|
|
1242
|
-
await this.#startInsertChange(["R"], undefined, false);
|
|
1243
|
-
return nextIndex + 1;
|
|
1244
|
-
case "g": {
|
|
1245
|
-
const gNext = tokens[nextIndex + 1];
|
|
1246
|
-
if (!gNext) {
|
|
1247
|
-
throw new VimError("g requires a second key", token);
|
|
1248
|
-
}
|
|
1249
|
-
if (gNext.value === "g") {
|
|
1250
|
-
this.buffer.setCursor({ line: hasCount ? Math.max(0, count - 1) : 0, col: 0 });
|
|
1251
|
-
return nextIndex + 2;
|
|
1252
|
-
}
|
|
1253
|
-
if (gNext.value === "v") {
|
|
1254
|
-
if (this.lastVisual) {
|
|
1255
|
-
this.selectionAnchor = clonePosition(this.lastVisual.anchor);
|
|
1256
|
-
this.buffer.setCursor(this.lastVisual.cursor);
|
|
1257
|
-
this.inputMode = this.lastVisual.mode;
|
|
1258
|
-
}
|
|
1259
|
-
return nextIndex + 2;
|
|
1260
|
-
}
|
|
1261
|
-
if (gNext.value === "*" || gNext.value === "#") {
|
|
1262
|
-
const text = this.buffer.getText();
|
|
1263
|
-
const offset = this.buffer.currentOffset();
|
|
1264
|
-
const cat = wordCategory(text[offset] ?? "", false);
|
|
1265
|
-
if (cat === "space") {
|
|
1266
|
-
throw new VimError("No word under cursor", gNext);
|
|
1267
|
-
}
|
|
1268
|
-
let start = offset;
|
|
1269
|
-
while (start > 0 && wordCategory(text[start - 1] ?? "", false) === cat) start -= 1;
|
|
1270
|
-
let end = offset;
|
|
1271
|
-
while (end < text.length && wordCategory(text[end] ?? "", false) === cat) end += 1;
|
|
1272
|
-
const word = text.slice(start, end);
|
|
1273
|
-
const direction = gNext.value === "*" ? 1 : -1;
|
|
1274
|
-
for (let step = 0; step < count; step += 1) {
|
|
1275
|
-
await this.#runSearch(escapeRegex(word), direction, true);
|
|
1276
|
-
}
|
|
1277
|
-
return nextIndex + 2;
|
|
1278
|
-
}
|
|
1279
|
-
if (gNext.value === "U" || gNext.value === "u") {
|
|
1280
|
-
const caseOp = gNext.value;
|
|
1281
|
-
const {
|
|
1282
|
-
count: motionCount,
|
|
1283
|
-
hasCount: hasMotionCount,
|
|
1284
|
-
nextIndex: motionStart,
|
|
1285
|
-
} = this.#readCount(tokens, nextIndex + 2);
|
|
1286
|
-
const motionToken = tokens[motionStart];
|
|
1287
|
-
if (!motionToken) {
|
|
1288
|
-
throw new VimError(`g${caseOp} requires a motion`, gNext);
|
|
1289
|
-
}
|
|
1290
|
-
if ((motionToken.value === "U" && caseOp === "U") || (motionToken.value === "u" && caseOp === "u")) {
|
|
1291
|
-
const effectiveCount = hasMotionCount ? count * motionCount : count;
|
|
1292
|
-
await this.#applyAtomicChange(["g", caseOp, motionToken.value], () => {
|
|
1293
|
-
const start = this.buffer.cursor.line;
|
|
1294
|
-
const end = this.buffer.clampLine(start + effectiveCount - 1);
|
|
1295
|
-
for (let line = start; line <= end; line++) {
|
|
1296
|
-
const content = this.buffer.getLine(line);
|
|
1297
|
-
this.buffer.replaceLine(line, caseOp === "U" ? content.toUpperCase() : content.toLowerCase());
|
|
1298
|
-
}
|
|
1299
|
-
});
|
|
1300
|
-
return motionStart + 1;
|
|
1301
|
-
}
|
|
1302
|
-
const effectiveCount = hasMotionCount ? count * motionCount : count;
|
|
1303
|
-
const motion = this.#resolveMotion(tokens, motionStart, effectiveCount, hasCount || hasMotionCount);
|
|
1304
|
-
const range = this.#resolveMotionRange(motion);
|
|
1305
|
-
await this.#applyAtomicChange(
|
|
1306
|
-
tokens.slice(nextIndex, motion.nextIndex).map(tokenEntry => tokenEntry.value),
|
|
1307
|
-
() => {
|
|
1308
|
-
const text = this.buffer.getText();
|
|
1309
|
-
const slice = text.slice(range.start, range.end);
|
|
1310
|
-
const transformed = caseOp === "U" ? slice.toUpperCase() : slice.toLowerCase();
|
|
1311
|
-
this.buffer.replaceOffsets(range.start, range.end, transformed, range.start);
|
|
1312
|
-
},
|
|
1313
|
-
);
|
|
1314
|
-
return motion.nextIndex;
|
|
1315
|
-
}
|
|
1316
|
-
if (gNext.value === "~") {
|
|
1317
|
-
const {
|
|
1318
|
-
count: motionCount,
|
|
1319
|
-
hasCount: hasMotionCount,
|
|
1320
|
-
nextIndex: motionStart,
|
|
1321
|
-
} = this.#readCount(tokens, nextIndex + 2);
|
|
1322
|
-
const motionToken = tokens[motionStart];
|
|
1323
|
-
if (!motionToken) {
|
|
1324
|
-
throw new VimError("g~ requires a motion", gNext);
|
|
1325
|
-
}
|
|
1326
|
-
if (motionToken.value === "~") {
|
|
1327
|
-
const effectiveCount = hasMotionCount ? count * motionCount : count;
|
|
1328
|
-
await this.#applyAtomicChange(["g", "~", motionToken.value], () => {
|
|
1329
|
-
const start = this.buffer.cursor.line;
|
|
1330
|
-
const end = this.buffer.clampLine(start + effectiveCount - 1);
|
|
1331
|
-
for (let line = start; line <= end; line += 1) {
|
|
1332
|
-
this.buffer.replaceLine(line, toggleCase(this.buffer.getLine(line)));
|
|
1333
|
-
}
|
|
1334
|
-
});
|
|
1335
|
-
return motionStart + 1;
|
|
1336
|
-
}
|
|
1337
|
-
const effectiveCount = hasMotionCount ? count * motionCount : count;
|
|
1338
|
-
const motion = this.#resolveMotion(tokens, motionStart, effectiveCount, hasCount || hasMotionCount);
|
|
1339
|
-
const range = this.#resolveMotionRange(motion);
|
|
1340
|
-
await this.#applyAtomicChange(
|
|
1341
|
-
tokens.slice(nextIndex, motion.nextIndex).map(tokenEntry => tokenEntry.value),
|
|
1342
|
-
() => {
|
|
1343
|
-
const text = this.buffer.getText();
|
|
1344
|
-
const slice = text.slice(range.start, range.end);
|
|
1345
|
-
this.buffer.replaceOffsets(range.start, range.end, toggleCase(slice), range.start);
|
|
1346
|
-
},
|
|
1347
|
-
);
|
|
1348
|
-
return motion.nextIndex;
|
|
1349
|
-
}
|
|
1350
|
-
if (gNext.value === "J") {
|
|
1351
|
-
await this.#applyAtomicChange(["g", "J"], () => {
|
|
1352
|
-
const start = this.buffer.clampLine(this.buffer.cursor.line);
|
|
1353
|
-
const end = this.buffer.clampLine(start + Math.max(count, 1));
|
|
1354
|
-
if (start < end) {
|
|
1355
|
-
const joined = this.buffer.lines.slice(start, end + 1).join("");
|
|
1356
|
-
this.buffer.lines.splice(start, end - start + 1, joined);
|
|
1357
|
-
this.buffer.setCursor({ line: start, col: Math.max(0, joined.length - 1) });
|
|
1358
|
-
}
|
|
1359
|
-
});
|
|
1360
|
-
return nextIndex + 2;
|
|
1361
|
-
}
|
|
1362
|
-
throw new VimError(`Unsupported g command: g${gNext.display}`, gNext);
|
|
1363
|
-
}
|
|
1364
|
-
case "Z": {
|
|
1365
|
-
const zNext = tokens[nextIndex + 1];
|
|
1366
|
-
if (!zNext) {
|
|
1367
|
-
throw new VimError("Z requires a second key", token);
|
|
1368
|
-
}
|
|
1369
|
-
if (zNext.value === "Z") {
|
|
1370
|
-
await this.#executeEx("wq");
|
|
1371
|
-
return nextIndex + 2;
|
|
1372
|
-
}
|
|
1373
|
-
if (zNext.value === "Q") {
|
|
1374
|
-
await this.#executeEx("q!");
|
|
1375
|
-
return nextIndex + 2;
|
|
1376
|
-
}
|
|
1377
|
-
throw new VimError(`Unsupported Z command: Z${zNext.display}`, zNext);
|
|
1378
|
-
}
|
|
1379
|
-
default:
|
|
1380
|
-
throw new VimError(`Unsupported command: ${token.display}`, token);
|
|
1381
|
-
}
|
|
1382
|
-
}
|
|
1383
|
-
|
|
1384
|
-
async #repeatLastChange(count: number, token: VimKeyToken): Promise<void> {
|
|
1385
|
-
if (!this.#lastChangeTokens || this.#lastChangeTokens.length === 0) {
|
|
1386
|
-
throw new VimError("No previous change to repeat", token);
|
|
1387
|
-
}
|
|
1388
|
-
for (let index = 0; index < count; index += 1) {
|
|
1389
|
-
await this.executeTokens(replayTokens(this.#lastChangeTokens), ".");
|
|
1390
|
-
}
|
|
1391
|
-
}
|
|
1392
|
-
|
|
1393
|
-
async #undo(count: number): Promise<void> {
|
|
1394
|
-
await this.#ensureEditable();
|
|
1395
|
-
let applied = 0;
|
|
1396
|
-
for (let index = 0; index < count; index += 1) {
|
|
1397
|
-
const entry = this.#undoStack.pop();
|
|
1398
|
-
if (!entry) {
|
|
1399
|
-
break;
|
|
1400
|
-
}
|
|
1401
|
-
this.#redoStack.push(entry);
|
|
1402
|
-
this.buffer.restore(entry.before);
|
|
1403
|
-
applied += 1;
|
|
1404
|
-
}
|
|
1405
|
-
this.inputMode = "normal";
|
|
1406
|
-
this.selectionAnchor = null;
|
|
1407
|
-
this.#pendingChange = null;
|
|
1408
|
-
this.statusMessage = `Undid ${applied} change${applied === 1 ? "" : "s"}`;
|
|
1409
|
-
}
|
|
1410
|
-
|
|
1411
|
-
async #redo(count: number): Promise<void> {
|
|
1412
|
-
await this.#ensureEditable();
|
|
1413
|
-
let applied = 0;
|
|
1414
|
-
for (let index = 0; index < count; index += 1) {
|
|
1415
|
-
const entry = this.#redoStack.pop();
|
|
1416
|
-
if (!entry) {
|
|
1417
|
-
break;
|
|
1418
|
-
}
|
|
1419
|
-
this.#undoStack.push(entry);
|
|
1420
|
-
this.buffer.restore(entry.after);
|
|
1421
|
-
applied += 1;
|
|
1422
|
-
}
|
|
1423
|
-
this.inputMode = "normal";
|
|
1424
|
-
this.selectionAnchor = null;
|
|
1425
|
-
this.#pendingChange = null;
|
|
1426
|
-
this.statusMessage = `Redid ${applied} change${applied === 1 ? "" : "s"}`;
|
|
1427
|
-
}
|
|
1428
|
-
|
|
1429
|
-
async #executeOperator(
|
|
1430
|
-
tokens: readonly VimKeyToken[],
|
|
1431
|
-
operatorIndex: number,
|
|
1432
|
-
operatorCount: number,
|
|
1433
|
-
hasOperatorCount: boolean,
|
|
1434
|
-
operator: string,
|
|
1435
|
-
): Promise<number> {
|
|
1436
|
-
const { count: motionCount, hasCount: hasMotionCount, nextIndex } = this.#readCount(tokens, operatorIndex + 1);
|
|
1437
|
-
const token = tokens[nextIndex];
|
|
1438
|
-
if (!token) {
|
|
1439
|
-
throw new VimError(`Operator ${operator} requires a motion`, tokens[operatorIndex]);
|
|
1440
|
-
}
|
|
1441
|
-
const hasAnyCount = hasOperatorCount || hasMotionCount;
|
|
1442
|
-
const effectiveCount = hasMotionCount ? operatorCount * motionCount : operatorCount;
|
|
1443
|
-
|
|
1444
|
-
if (token.value === operator) {
|
|
1445
|
-
if (operator === "d") {
|
|
1446
|
-
await this.#applyAtomicChange([operator, operator], () => {
|
|
1447
|
-
const start = this.buffer.cursor.line;
|
|
1448
|
-
const removed = this.buffer.deleteLines(start, start + Math.max(1, effectiveCount) - 1);
|
|
1449
|
-
this.register = { kind: "line", text: removed.join("\n") };
|
|
1450
|
-
});
|
|
1451
|
-
return nextIndex + 1;
|
|
1452
|
-
}
|
|
1453
|
-
if (operator === "y") {
|
|
1454
|
-
const start = this.buffer.cursor.line;
|
|
1455
|
-
const end = this.buffer.clampLine(start + Math.max(1, effectiveCount) - 1);
|
|
1456
|
-
this.register = { kind: "line", text: this.buffer.lines.slice(start, end + 1).join("\n") };
|
|
1457
|
-
this.statusMessage = `Yanked ${end - start + 1} line${end === start ? "" : "s"}`;
|
|
1458
|
-
return nextIndex + 1;
|
|
1459
|
-
}
|
|
1460
|
-
if (operator === "c") {
|
|
1461
|
-
await this.#changeWholeLines(Math.max(1, effectiveCount), [operator, operator]);
|
|
1462
|
-
return nextIndex + 1;
|
|
1463
|
-
}
|
|
1464
|
-
if (operator === ">" || operator === "<") {
|
|
1465
|
-
await this.#applyAtomicChange([operator, operator], () => {
|
|
1466
|
-
this.buffer.indentLines(
|
|
1467
|
-
this.buffer.cursor.line,
|
|
1468
|
-
this.buffer.cursor.line + Math.max(1, effectiveCount) - 1,
|
|
1469
|
-
detectIndentUnit(this.buffer.lines),
|
|
1470
|
-
operator === ">" ? 1 : -1,
|
|
1471
|
-
);
|
|
1472
|
-
});
|
|
1473
|
-
return nextIndex + 1;
|
|
1474
|
-
}
|
|
1475
|
-
}
|
|
1476
|
-
|
|
1477
|
-
if (token.value === "i" || token.value === "a") {
|
|
1478
|
-
const object = tokens[nextIndex + 1];
|
|
1479
|
-
if (!object) {
|
|
1480
|
-
throw new VimError(`Missing text object after ${operator}${token.value}`, token);
|
|
1481
|
-
}
|
|
1482
|
-
const textObject = this.#resolveTextObject(token.value === "i", object.value, object);
|
|
1483
|
-
await this.#applyOperatorToMotion(
|
|
1484
|
-
operator,
|
|
1485
|
-
{ nextIndex: nextIndex + 2, target: this.buffer.cursor, range: textObject },
|
|
1486
|
-
[operator, token.value, object.value],
|
|
1487
|
-
);
|
|
1488
|
-
return nextIndex + 2;
|
|
1489
|
-
}
|
|
1490
|
-
|
|
1491
|
-
// In vim, `cw` and `cW` act like `ce` and `cE` (don't include trailing whitespace)
|
|
1492
|
-
const motionToken = tokens[nextIndex];
|
|
1493
|
-
let motion: MotionResult;
|
|
1494
|
-
if (operator === "c" && motionToken && (motionToken.value === "w" || motionToken.value === "W")) {
|
|
1495
|
-
const eMotionValue = motionToken.value === "w" ? "e" : "E";
|
|
1496
|
-
const syntheticTokens: readonly VimKeyToken[] = [
|
|
1497
|
-
...tokens.slice(0, nextIndex),
|
|
1498
|
-
{ ...motionToken, value: eMotionValue },
|
|
1499
|
-
...tokens.slice(nextIndex + 1),
|
|
1500
|
-
];
|
|
1501
|
-
motion = this.#resolveMotion(syntheticTokens, nextIndex, effectiveCount, hasAnyCount);
|
|
1502
|
-
} else {
|
|
1503
|
-
motion = this.#resolveMotion(tokens, nextIndex, effectiveCount, hasAnyCount);
|
|
1504
|
-
}
|
|
1505
|
-
await this.#applyOperatorToMotion(
|
|
1506
|
-
operator,
|
|
1507
|
-
motion,
|
|
1508
|
-
tokens.slice(operatorIndex, motion.nextIndex).map(tokenEntry => tokenEntry.value),
|
|
1509
|
-
);
|
|
1510
|
-
return motion.nextIndex;
|
|
1511
|
-
}
|
|
1512
|
-
|
|
1513
|
-
async #applyOperatorToMotion(operator: string, motion: MotionResult, tokens: readonly string[]): Promise<void> {
|
|
1514
|
-
if (operator === "y") {
|
|
1515
|
-
const range = this.#resolveMotionRange(motion);
|
|
1516
|
-
this.register = {
|
|
1517
|
-
kind: range.linewise ? "line" : "char",
|
|
1518
|
-
text: this.buffer.getText().slice(range.start, range.end),
|
|
1519
|
-
};
|
|
1520
|
-
this.statusMessage = `Yanked ${range.linewise ? "line" : "selection"}`;
|
|
1521
|
-
return;
|
|
1522
|
-
}
|
|
1523
|
-
|
|
1524
|
-
if (operator === ">" || operator === "<") {
|
|
1525
|
-
const range = this.#resolveMotionRange(motion);
|
|
1526
|
-
const startLine = this.buffer.offsetToPosition(range.start).line;
|
|
1527
|
-
const endLine = this.buffer.offsetToPosition(Math.max(range.start, range.end - 1)).line;
|
|
1528
|
-
await this.#applyAtomicChange(tokens, () => {
|
|
1529
|
-
this.buffer.indentLines(startLine, endLine, detectIndentUnit(this.buffer.lines), operator === ">" ? 1 : -1);
|
|
1530
|
-
});
|
|
1531
|
-
return;
|
|
1532
|
-
}
|
|
1533
|
-
|
|
1534
|
-
if (operator === "d") {
|
|
1535
|
-
const range = this.#resolveMotionRange(motion);
|
|
1536
|
-
await this.#applyAtomicChange(tokens, () => {
|
|
1537
|
-
this.#yankAndDeleteRange(range);
|
|
1538
|
-
});
|
|
1539
|
-
return;
|
|
1540
|
-
}
|
|
1541
|
-
|
|
1542
|
-
if (operator === "c") {
|
|
1543
|
-
const range = this.#resolveMotionRange(motion);
|
|
1544
|
-
await this.#startInsertChange(tokens, () => {
|
|
1545
|
-
this.#yankAndDeleteRange(range);
|
|
1546
|
-
});
|
|
1547
|
-
return;
|
|
1548
|
-
}
|
|
1549
|
-
}
|
|
1550
|
-
|
|
1551
|
-
#yankAndDeleteRange(range: { start: number; end: number; linewise: boolean }): void {
|
|
1552
|
-
this.register = {
|
|
1553
|
-
kind: range.linewise ? "line" : "char",
|
|
1554
|
-
text: this.buffer.getText().slice(range.start, range.end),
|
|
1555
|
-
};
|
|
1556
|
-
this.buffer.deleteOffsets(range.start, range.end);
|
|
1557
|
-
}
|
|
1558
|
-
|
|
1559
|
-
#deleteCharsForward(count: number): void {
|
|
1560
|
-
const start = this.buffer.currentOffset();
|
|
1561
|
-
this.register = {
|
|
1562
|
-
kind: "char",
|
|
1563
|
-
text: this.buffer.deleteOffsets(start, Math.min(this.buffer.getText().length, start + count)),
|
|
1564
|
-
};
|
|
1565
|
-
}
|
|
1566
|
-
|
|
1567
|
-
async #changeWholeLines(count: number, tokens: readonly string[]): Promise<void> {
|
|
1568
|
-
await this.#startInsertChange(tokens, () => {
|
|
1569
|
-
const start = this.buffer.cursor.line;
|
|
1570
|
-
const end = this.buffer.clampLine(start + count - 1);
|
|
1571
|
-
const removed = this.buffer.lines.slice(start, end + 1);
|
|
1572
|
-
this.register = { kind: "line", text: removed.join("\n") };
|
|
1573
|
-
this.buffer.lines.splice(start, end - start + 1, "");
|
|
1574
|
-
if (this.buffer.lines.length === 0) {
|
|
1575
|
-
this.buffer.lines = [""];
|
|
1576
|
-
}
|
|
1577
|
-
this.buffer.setCursor({ line: Math.min(start, this.buffer.lastLineIndex()), col: 0 });
|
|
1578
|
-
});
|
|
1579
|
-
}
|
|
1580
|
-
|
|
1581
|
-
#resolveMotionRange(motion: MotionResult): { start: number; end: number; linewise: boolean } {
|
|
1582
|
-
if (motion.range) {
|
|
1583
|
-
return {
|
|
1584
|
-
start: motion.range.start,
|
|
1585
|
-
end: motion.range.end,
|
|
1586
|
-
linewise: motion.range.linewise ?? false,
|
|
1587
|
-
};
|
|
1588
|
-
}
|
|
1589
|
-
|
|
1590
|
-
if (motion.linewise) {
|
|
1591
|
-
const startLine = Math.min(this.buffer.cursor.line, motion.target.line);
|
|
1592
|
-
const endLine = Math.max(this.buffer.cursor.line, motion.target.line);
|
|
1593
|
-
const start = this.buffer.positionToOffset({ line: startLine, col: 0 });
|
|
1594
|
-
const end =
|
|
1595
|
-
endLine >= this.buffer.lastLineIndex()
|
|
1596
|
-
? this.buffer.getText().length
|
|
1597
|
-
: this.buffer.positionToOffset({ line: endLine + 1, col: 0 });
|
|
1598
|
-
return { start, end, linewise: true };
|
|
1599
|
-
}
|
|
1600
|
-
|
|
1601
|
-
const from = this.buffer.positionToOffset(this.buffer.cursor);
|
|
1602
|
-
const to = this.buffer.positionToOffset(motion.target);
|
|
1603
|
-
const normalized = normalizeRange(from, to);
|
|
1604
|
-
return {
|
|
1605
|
-
start: normalized.start,
|
|
1606
|
-
end: normalized.end + (motion.inclusive === false ? 0 : 1),
|
|
1607
|
-
linewise: false,
|
|
1608
|
-
};
|
|
1609
|
-
}
|
|
1610
|
-
|
|
1611
|
-
#resolveMotion(tokens: readonly VimKeyToken[], index: number, count: number, hasCount = true): MotionResult {
|
|
1612
|
-
const token = tokens[index];
|
|
1613
|
-
if (!token) {
|
|
1614
|
-
throw new VimError("Missing motion");
|
|
1615
|
-
}
|
|
1616
|
-
|
|
1617
|
-
const text = this.buffer.getText();
|
|
1618
|
-
switch (token.value) {
|
|
1619
|
-
case "h":
|
|
1620
|
-
return {
|
|
1621
|
-
nextIndex: index + 1,
|
|
1622
|
-
target: { line: this.buffer.cursor.line, col: this.buffer.cursor.col - count },
|
|
1623
|
-
};
|
|
1624
|
-
case "j":
|
|
1625
|
-
return {
|
|
1626
|
-
nextIndex: index + 1,
|
|
1627
|
-
target: { line: this.buffer.cursor.line + count, col: this.buffer.cursor.col },
|
|
1628
|
-
linewise: true,
|
|
1629
|
-
};
|
|
1630
|
-
case "k":
|
|
1631
|
-
return {
|
|
1632
|
-
nextIndex: index + 1,
|
|
1633
|
-
target: { line: this.buffer.cursor.line - count, col: this.buffer.cursor.col },
|
|
1634
|
-
linewise: true,
|
|
1635
|
-
};
|
|
1636
|
-
case "l":
|
|
1637
|
-
case " ":
|
|
1638
|
-
return {
|
|
1639
|
-
nextIndex: index + 1,
|
|
1640
|
-
target: { line: this.buffer.cursor.line, col: this.buffer.cursor.col + count },
|
|
1641
|
-
};
|
|
1642
|
-
case "w":
|
|
1643
|
-
case "W": {
|
|
1644
|
-
let offset = this.buffer.currentOffset();
|
|
1645
|
-
for (let step = 0; step < count; step += 1) {
|
|
1646
|
-
offset = nextWordStart(text, step === 0 ? offset + 1 : offset, token.value === "W");
|
|
1647
|
-
}
|
|
1648
|
-
return { nextIndex: index + 1, target: this.buffer.offsetToPosition(offset), inclusive: false };
|
|
1649
|
-
}
|
|
1650
|
-
case "b":
|
|
1651
|
-
case "B": {
|
|
1652
|
-
let offset = this.buffer.currentOffset();
|
|
1653
|
-
for (let step = 0; step < count; step += 1) {
|
|
1654
|
-
offset = previousWordStart(text, offset, token.value === "B");
|
|
1655
|
-
}
|
|
1656
|
-
return { nextIndex: index + 1, target: this.buffer.offsetToPosition(offset) };
|
|
1657
|
-
}
|
|
1658
|
-
case "e":
|
|
1659
|
-
case "E": {
|
|
1660
|
-
let offset = this.buffer.currentOffset();
|
|
1661
|
-
for (let step = 0; step < count; step += 1) {
|
|
1662
|
-
offset = endOfWord(text, step === 0 ? offset : offset + 1, token.value === "E");
|
|
1663
|
-
}
|
|
1664
|
-
return { nextIndex: index + 1, target: this.buffer.offsetToPosition(offset) };
|
|
1665
|
-
}
|
|
1666
|
-
case "0":
|
|
1667
|
-
return { nextIndex: index + 1, target: { line: this.buffer.cursor.line, col: 0 } };
|
|
1668
|
-
case "^":
|
|
1669
|
-
return {
|
|
1670
|
-
nextIndex: index + 1,
|
|
1671
|
-
target: { line: this.buffer.cursor.line, col: this.buffer.firstNonBlank(this.buffer.cursor.line) },
|
|
1672
|
-
};
|
|
1673
|
-
case "|":
|
|
1674
|
-
return {
|
|
1675
|
-
nextIndex: index + 1,
|
|
1676
|
-
target: { line: this.buffer.cursor.line, col: Math.max(0, count - 1) },
|
|
1677
|
-
};
|
|
1678
|
-
case "$":
|
|
1679
|
-
return {
|
|
1680
|
-
nextIndex: index + 1,
|
|
1681
|
-
target: {
|
|
1682
|
-
line: this.buffer.cursor.line,
|
|
1683
|
-
col: Math.max(0, this.buffer.getLine(this.buffer.cursor.line).length - 1),
|
|
1684
|
-
},
|
|
1685
|
-
};
|
|
1686
|
-
case "+": {
|
|
1687
|
-
const targetLine = this.buffer.clampLine(this.buffer.cursor.line + count);
|
|
1688
|
-
return {
|
|
1689
|
-
nextIndex: index + 1,
|
|
1690
|
-
target: { line: targetLine, col: this.buffer.firstNonBlank(targetLine) },
|
|
1691
|
-
linewise: true,
|
|
1692
|
-
};
|
|
1693
|
-
}
|
|
1694
|
-
case "-": {
|
|
1695
|
-
const targetLine = this.buffer.clampLine(this.buffer.cursor.line - count);
|
|
1696
|
-
return {
|
|
1697
|
-
nextIndex: index + 1,
|
|
1698
|
-
target: { line: targetLine, col: this.buffer.firstNonBlank(targetLine) },
|
|
1699
|
-
linewise: true,
|
|
1700
|
-
};
|
|
1701
|
-
}
|
|
1702
|
-
case "_": {
|
|
1703
|
-
const targetLine = this.buffer.clampLine(this.buffer.cursor.line + (count - 1));
|
|
1704
|
-
return {
|
|
1705
|
-
nextIndex: index + 1,
|
|
1706
|
-
target: { line: targetLine, col: this.buffer.firstNonBlank(targetLine) },
|
|
1707
|
-
linewise: true,
|
|
1708
|
-
};
|
|
1709
|
-
}
|
|
1710
|
-
case "g": {
|
|
1711
|
-
const next = tokens[index + 1];
|
|
1712
|
-
if (!next) {
|
|
1713
|
-
throw new VimError("Unsupported g motion", token);
|
|
1714
|
-
}
|
|
1715
|
-
if (next.value === "g") {
|
|
1716
|
-
return {
|
|
1717
|
-
nextIndex: index + 2,
|
|
1718
|
-
target: { line: hasCount ? Math.max(0, count - 1) : 0, col: 0 },
|
|
1719
|
-
linewise: true,
|
|
1720
|
-
};
|
|
1721
|
-
}
|
|
1722
|
-
if (next.value === "e" || next.value === "E") {
|
|
1723
|
-
let offset = this.buffer.currentOffset();
|
|
1724
|
-
for (let step = 0; step < count; step += 1) {
|
|
1725
|
-
offset = endOfPreviousWord(text, offset, next.value === "E");
|
|
1726
|
-
}
|
|
1727
|
-
return { nextIndex: index + 2, target: this.buffer.offsetToPosition(offset) };
|
|
1728
|
-
}
|
|
1729
|
-
if (next.value === "_") {
|
|
1730
|
-
const targetLine = this.buffer.clampLine(this.buffer.cursor.line + (count - 1));
|
|
1731
|
-
return {
|
|
1732
|
-
nextIndex: index + 2,
|
|
1733
|
-
target: { line: targetLine, col: lastNonBlankColumn(this.buffer.getLine(targetLine)) },
|
|
1734
|
-
};
|
|
1735
|
-
}
|
|
1736
|
-
throw new VimError("Unsupported g motion", token);
|
|
1737
|
-
}
|
|
1738
|
-
case "G":
|
|
1739
|
-
return {
|
|
1740
|
-
nextIndex: index + 1,
|
|
1741
|
-
target: { line: hasCount ? count - 1 : this.buffer.lastLineIndex(), col: 0 },
|
|
1742
|
-
linewise: true,
|
|
1743
|
-
};
|
|
1744
|
-
case "f":
|
|
1745
|
-
case "F":
|
|
1746
|
-
case "t":
|
|
1747
|
-
case "T": {
|
|
1748
|
-
const searchToken = tokens[index + 1];
|
|
1749
|
-
if (!searchToken || searchToken.value.length !== 1) {
|
|
1750
|
-
throw new VimError(`${token.value} requires a literal character`, token);
|
|
1751
|
-
}
|
|
1752
|
-
this.lastCharFind = { char: searchToken.value, mode: token.value as "f" | "F" | "t" | "T" };
|
|
1753
|
-
const line = this.buffer.getLine(this.buffer.cursor.line);
|
|
1754
|
-
const cursorCol = this.buffer.cursor.col;
|
|
1755
|
-
let matchIndex = -1;
|
|
1756
|
-
if (token.value === "f" || token.value === "t") {
|
|
1757
|
-
let start = cursorCol + 1;
|
|
1758
|
-
for (let step = 0; step < count; step += 1) {
|
|
1759
|
-
matchIndex = line.indexOf(searchToken.value, start);
|
|
1760
|
-
if (matchIndex === -1) break;
|
|
1761
|
-
start = matchIndex + 1;
|
|
1762
|
-
}
|
|
1763
|
-
if (matchIndex === -1) {
|
|
1764
|
-
throw new VimError(`Character not found: ${searchToken.value}`, searchToken);
|
|
1765
|
-
}
|
|
1766
|
-
if (token.value === "t") {
|
|
1767
|
-
matchIndex -= 1;
|
|
1768
|
-
}
|
|
1769
|
-
} else {
|
|
1770
|
-
let start = Math.max(0, cursorCol - 1);
|
|
1771
|
-
for (let step = 0; step < count; step += 1) {
|
|
1772
|
-
matchIndex = line.lastIndexOf(searchToken.value, start);
|
|
1773
|
-
if (matchIndex === -1) break;
|
|
1774
|
-
start = matchIndex - 1;
|
|
1775
|
-
}
|
|
1776
|
-
if (matchIndex === -1) {
|
|
1777
|
-
throw new VimError(`Character not found: ${searchToken.value}`, searchToken);
|
|
1778
|
-
}
|
|
1779
|
-
if (token.value === "T") {
|
|
1780
|
-
matchIndex += 1;
|
|
1781
|
-
}
|
|
1782
|
-
}
|
|
1783
|
-
return {
|
|
1784
|
-
nextIndex: index + 2,
|
|
1785
|
-
target: { line: this.buffer.cursor.line, col: Math.max(0, matchIndex) },
|
|
1786
|
-
};
|
|
1787
|
-
}
|
|
1788
|
-
case "{":
|
|
1789
|
-
return {
|
|
1790
|
-
nextIndex: index + 1,
|
|
1791
|
-
target: { line: findParagraphStart(this.buffer.lines, this.buffer.cursor.line), col: 0 },
|
|
1792
|
-
linewise: true,
|
|
1793
|
-
};
|
|
1794
|
-
case "}":
|
|
1795
|
-
return {
|
|
1796
|
-
nextIndex: index + 1,
|
|
1797
|
-
target: { line: findParagraphEnd(this.buffer.lines, this.buffer.cursor.line), col: 0 },
|
|
1798
|
-
linewise: true,
|
|
1799
|
-
};
|
|
1800
|
-
case "%": {
|
|
1801
|
-
const match = this.#findMatchingBracket();
|
|
1802
|
-
return { nextIndex: index + 1, target: match };
|
|
1803
|
-
}
|
|
1804
|
-
case "H":
|
|
1805
|
-
return {
|
|
1806
|
-
nextIndex: index + 1,
|
|
1807
|
-
target: { line: Math.max(0, this.viewportStart - 1), col: 0 },
|
|
1808
|
-
linewise: true,
|
|
1809
|
-
};
|
|
1810
|
-
case "M":
|
|
1811
|
-
return {
|
|
1812
|
-
nextIndex: index + 1,
|
|
1813
|
-
target: { line: Math.max(0, this.viewportStart - 1 + 20), col: 0 },
|
|
1814
|
-
linewise: true,
|
|
1815
|
-
};
|
|
1816
|
-
case "L":
|
|
1817
|
-
return {
|
|
1818
|
-
nextIndex: index + 1,
|
|
1819
|
-
target: { line: Math.max(0, this.viewportStart - 1 + 39), col: 0 },
|
|
1820
|
-
linewise: true,
|
|
1821
|
-
};
|
|
1822
|
-
case ";":
|
|
1823
|
-
case ",": {
|
|
1824
|
-
if (!this.lastCharFind) {
|
|
1825
|
-
throw new VimError(
|
|
1826
|
-
"No previous character search. If you meant an ex-command range like `:4,5d`, add the `:` prefix and `<CR>` suffix.",
|
|
1827
|
-
token,
|
|
1828
|
-
);
|
|
1829
|
-
}
|
|
1830
|
-
let mode = this.lastCharFind.mode;
|
|
1831
|
-
if (token.value === ",") {
|
|
1832
|
-
const reverseMap: Record<string, "f" | "F" | "t" | "T"> = { f: "F", F: "f", t: "T", T: "t" };
|
|
1833
|
-
mode = reverseMap[mode]!;
|
|
1834
|
-
}
|
|
1835
|
-
const line = this.buffer.getLine(this.buffer.cursor.line);
|
|
1836
|
-
const cursorCol = this.buffer.cursor.col;
|
|
1837
|
-
let matchIndex = -1;
|
|
1838
|
-
if (mode === "f" || mode === "t") {
|
|
1839
|
-
let start = cursorCol + 1;
|
|
1840
|
-
for (let step = 0; step < count; step += 1) {
|
|
1841
|
-
matchIndex = line.indexOf(this.lastCharFind.char, start);
|
|
1842
|
-
if (matchIndex === -1) break;
|
|
1843
|
-
start = matchIndex + 1;
|
|
1844
|
-
}
|
|
1845
|
-
if (matchIndex !== -1 && mode === "t") matchIndex -= 1;
|
|
1846
|
-
} else {
|
|
1847
|
-
let start = Math.max(0, cursorCol - 1);
|
|
1848
|
-
for (let step = 0; step < count; step += 1) {
|
|
1849
|
-
matchIndex = line.lastIndexOf(this.lastCharFind.char, start);
|
|
1850
|
-
if (matchIndex === -1) break;
|
|
1851
|
-
start = matchIndex - 1;
|
|
1852
|
-
}
|
|
1853
|
-
if (matchIndex !== -1 && mode === "T") matchIndex += 1;
|
|
1854
|
-
}
|
|
1855
|
-
if (matchIndex === -1) {
|
|
1856
|
-
throw new VimError(`Character not found: ${this.lastCharFind.char}`, token);
|
|
1857
|
-
}
|
|
1858
|
-
return {
|
|
1859
|
-
nextIndex: index + 1,
|
|
1860
|
-
target: { line: this.buffer.cursor.line, col: Math.max(0, matchIndex) },
|
|
1861
|
-
};
|
|
1862
|
-
}
|
|
1863
|
-
default:
|
|
1864
|
-
throw new VimError(`Unsupported motion: ${token.display}`, token);
|
|
1865
|
-
}
|
|
1866
|
-
}
|
|
1867
|
-
|
|
1868
|
-
#resolveTextObject(
|
|
1869
|
-
inner: boolean,
|
|
1870
|
-
objectToken: string,
|
|
1871
|
-
sourceToken: VimKeyToken,
|
|
1872
|
-
): { start: number; end: number; linewise?: boolean } {
|
|
1873
|
-
if (objectToken === "w" || objectToken === "W") {
|
|
1874
|
-
return this.#resolveWordTextObject(inner, objectToken === "W");
|
|
1875
|
-
}
|
|
1876
|
-
if (objectToken === '"' || objectToken === "'" || objectToken === "`") {
|
|
1877
|
-
return this.#resolveQuoteTextObject(inner, objectToken, sourceToken);
|
|
1878
|
-
}
|
|
1879
|
-
|
|
1880
|
-
if (objectToken === "p") {
|
|
1881
|
-
return this.#resolveParagraphTextObject(inner);
|
|
1882
|
-
}
|
|
1883
|
-
|
|
1884
|
-
const normalized =
|
|
1885
|
-
objectToken === ")"
|
|
1886
|
-
? "("
|
|
1887
|
-
: objectToken === "}"
|
|
1888
|
-
? "{"
|
|
1889
|
-
: objectToken === "]"
|
|
1890
|
-
? "["
|
|
1891
|
-
: objectToken === ">"
|
|
1892
|
-
? "<"
|
|
1893
|
-
: objectToken;
|
|
1894
|
-
if (!BRACKET_PAIRS.has(normalized)) {
|
|
1895
|
-
throw new VimError(`Unsupported text object: ${objectToken}`, sourceToken);
|
|
1896
|
-
}
|
|
1897
|
-
return this.#resolveBracketTextObject(inner, normalized, sourceToken);
|
|
1898
|
-
}
|
|
1899
|
-
|
|
1900
|
-
#resolveWordTextObject(inner: boolean, bigWord: boolean): { start: number; end: number } {
|
|
1901
|
-
const text = this.buffer.getText();
|
|
1902
|
-
const cursor = this.buffer.currentOffset();
|
|
1903
|
-
let start = cursor;
|
|
1904
|
-
if (wordCategory(text[start] ?? "", bigWord) === "space") {
|
|
1905
|
-
start = nextWordStart(text, start, bigWord);
|
|
1906
|
-
}
|
|
1907
|
-
const category = wordCategory(text[start] ?? "", bigWord);
|
|
1908
|
-
while (start > 0 && wordCategory(text[start - 1] ?? "", bigWord) === category) {
|
|
1909
|
-
start -= 1;
|
|
1910
|
-
}
|
|
1911
|
-
let end = start;
|
|
1912
|
-
while (end < text.length && wordCategory(text[end] ?? "", bigWord) === category) {
|
|
1913
|
-
end += 1;
|
|
1914
|
-
}
|
|
1915
|
-
if (!inner) {
|
|
1916
|
-
while (end < text.length && wordCategory(text[end] ?? "", bigWord) === "space") {
|
|
1917
|
-
end += 1;
|
|
1918
|
-
}
|
|
1919
|
-
while (start > 0 && wordCategory(text[start - 1] ?? "", bigWord) === "space") {
|
|
1920
|
-
start -= 1;
|
|
1921
|
-
}
|
|
1922
|
-
}
|
|
1923
|
-
return { start, end };
|
|
1924
|
-
}
|
|
1925
|
-
|
|
1926
|
-
#resolveParagraphTextObject(inner: boolean): { start: number; end: number; linewise: boolean } {
|
|
1927
|
-
const lines = this.buffer.lines;
|
|
1928
|
-
const cursorLine = this.buffer.cursor.line;
|
|
1929
|
-
let start = cursorLine;
|
|
1930
|
-
let end = cursorLine;
|
|
1931
|
-
// Find paragraph boundaries (delimited by blank lines)
|
|
1932
|
-
if (lines[cursorLine]?.trim().length === 0) {
|
|
1933
|
-
// On a blank line: select contiguous blank lines
|
|
1934
|
-
while (start > 0 && lines[start - 1]!.trim().length === 0) start -= 1;
|
|
1935
|
-
while (end < lines.length - 1 && lines[end + 1]!.trim().length === 0) end += 1;
|
|
1936
|
-
if (!inner) {
|
|
1937
|
-
// Include following non-blank paragraph
|
|
1938
|
-
while (end < lines.length - 1 && lines[end + 1]!.trim().length > 0) end += 1;
|
|
1939
|
-
}
|
|
1940
|
-
} else {
|
|
1941
|
-
// On a non-blank line: select contiguous non-blank lines
|
|
1942
|
-
while (start > 0 && lines[start - 1]!.trim().length > 0) start -= 1;
|
|
1943
|
-
while (end < lines.length - 1 && lines[end + 1]!.trim().length > 0) end += 1;
|
|
1944
|
-
if (!inner) {
|
|
1945
|
-
// Include trailing blank lines
|
|
1946
|
-
while (end < lines.length - 1 && lines[end + 1]!.trim().length === 0) end += 1;
|
|
1947
|
-
}
|
|
1948
|
-
}
|
|
1949
|
-
const startOffset = this.buffer.positionToOffset({ line: start, col: 0 });
|
|
1950
|
-
const endOffset =
|
|
1951
|
-
end >= this.buffer.lastLineIndex()
|
|
1952
|
-
? this.buffer.getText().length
|
|
1953
|
-
: this.buffer.positionToOffset({ line: end + 1, col: 0 });
|
|
1954
|
-
return { start: startOffset, end: endOffset, linewise: true };
|
|
1955
|
-
}
|
|
1956
|
-
|
|
1957
|
-
#resolveQuoteTextObject(inner: boolean, quote: string, sourceToken: VimKeyToken): { start: number; end: number } {
|
|
1958
|
-
const line = this.buffer.getLine(this.buffer.cursor.line);
|
|
1959
|
-
const col = this.buffer.cursor.col;
|
|
1960
|
-
const before = line.lastIndexOf(quote, col);
|
|
1961
|
-
const after = line.indexOf(quote, col + (line[col] === quote ? 1 : 0));
|
|
1962
|
-
if (before === -1 || after === -1 || before === after) {
|
|
1963
|
-
throw new VimError(`Quote text object not found for ${quote}`, sourceToken);
|
|
1964
|
-
}
|
|
1965
|
-
const startCol = inner ? before + 1 : before;
|
|
1966
|
-
const endCol = inner ? after : after + 1;
|
|
1967
|
-
return {
|
|
1968
|
-
start: this.buffer.positionToOffset({ line: this.buffer.cursor.line, col: startCol }),
|
|
1969
|
-
end: this.buffer.positionToOffset({ line: this.buffer.cursor.line, col: endCol }),
|
|
1970
|
-
};
|
|
1971
|
-
}
|
|
1972
|
-
|
|
1973
|
-
#resolveBracketTextObject(inner: boolean, open: string, sourceToken: VimKeyToken): { start: number; end: number } {
|
|
1974
|
-
const close = BRACKET_PAIRS.get(open)!;
|
|
1975
|
-
const text = this.buffer.getText();
|
|
1976
|
-
const cursor = this.buffer.currentOffset();
|
|
1977
|
-
let start = -1;
|
|
1978
|
-
let depth = 0;
|
|
1979
|
-
for (let index = cursor; index >= 0; index -= 1) {
|
|
1980
|
-
const char = text[index] ?? "";
|
|
1981
|
-
if (char === close) {
|
|
1982
|
-
depth += 1;
|
|
1983
|
-
} else if (char === open) {
|
|
1984
|
-
if (depth === 0) {
|
|
1985
|
-
start = index;
|
|
1986
|
-
break;
|
|
1987
|
-
}
|
|
1988
|
-
depth -= 1;
|
|
1989
|
-
}
|
|
1990
|
-
}
|
|
1991
|
-
if (start === -1) {
|
|
1992
|
-
throw new VimError(`Text object ${open}${close} not found`, sourceToken);
|
|
1993
|
-
}
|
|
1994
|
-
let end = -1;
|
|
1995
|
-
depth = 0;
|
|
1996
|
-
for (let index = start; index < text.length; index += 1) {
|
|
1997
|
-
const char = text[index] ?? "";
|
|
1998
|
-
if (char === open) {
|
|
1999
|
-
depth += 1;
|
|
2000
|
-
} else if (char === close) {
|
|
2001
|
-
depth -= 1;
|
|
2002
|
-
if (depth === 0) {
|
|
2003
|
-
end = index;
|
|
2004
|
-
break;
|
|
2005
|
-
}
|
|
2006
|
-
}
|
|
2007
|
-
}
|
|
2008
|
-
if (end === -1) {
|
|
2009
|
-
throw new VimError(`Text object ${open}${close} not found`, sourceToken);
|
|
2010
|
-
}
|
|
2011
|
-
return {
|
|
2012
|
-
start: inner ? start + 1 : start,
|
|
2013
|
-
end: inner ? end : end + 1,
|
|
2014
|
-
};
|
|
2015
|
-
}
|
|
2016
|
-
|
|
2017
|
-
#findMatchingBracket(): Position {
|
|
2018
|
-
const text = this.buffer.getText();
|
|
2019
|
-
const cursor = this.buffer.currentOffset();
|
|
2020
|
-
let offset = cursor;
|
|
2021
|
-
let char = text[offset] ?? "";
|
|
2022
|
-
if (!BRACKET_PAIRS.has(char) && !CLOSING_BRACKETS.has(char)) {
|
|
2023
|
-
offset += 1;
|
|
2024
|
-
char = text[offset] ?? "";
|
|
2025
|
-
}
|
|
2026
|
-
if (BRACKET_PAIRS.has(char)) {
|
|
2027
|
-
const close = BRACKET_PAIRS.get(char)!;
|
|
2028
|
-
let depth = 0;
|
|
2029
|
-
for (let index = offset; index < text.length; index += 1) {
|
|
2030
|
-
const current = text[index] ?? "";
|
|
2031
|
-
if (current === char) depth += 1;
|
|
2032
|
-
if (current === close) {
|
|
2033
|
-
depth -= 1;
|
|
2034
|
-
if (depth === 0) {
|
|
2035
|
-
return this.buffer.offsetToPosition(index);
|
|
2036
|
-
}
|
|
2037
|
-
}
|
|
2038
|
-
}
|
|
2039
|
-
}
|
|
2040
|
-
if (CLOSING_BRACKETS.has(char)) {
|
|
2041
|
-
const open = CLOSING_BRACKETS.get(char)!;
|
|
2042
|
-
let depth = 0;
|
|
2043
|
-
for (let index = offset; index >= 0; index -= 1) {
|
|
2044
|
-
const current = text[index] ?? "";
|
|
2045
|
-
if (current === char) depth += 1;
|
|
2046
|
-
if (current === open) {
|
|
2047
|
-
depth -= 1;
|
|
2048
|
-
if (depth === 0) {
|
|
2049
|
-
return this.buffer.offsetToPosition(index);
|
|
2050
|
-
}
|
|
2051
|
-
}
|
|
2052
|
-
}
|
|
2053
|
-
}
|
|
2054
|
-
throw new VimError("Matching bracket not found");
|
|
2055
|
-
}
|
|
2056
|
-
|
|
2057
|
-
async #runSearch(pattern: string, direction: 1 | -1, updateState: boolean): Promise<void> {
|
|
2058
|
-
const text = this.buffer.getText();
|
|
2059
|
-
const regex = createSearchRegex(pattern, "g");
|
|
2060
|
-
const cursor = this.buffer.currentOffset();
|
|
2061
|
-
let matchOffset = -1;
|
|
2062
|
-
|
|
2063
|
-
if (direction > 0) {
|
|
2064
|
-
regex.lastIndex = Math.min(text.length, cursor + 1);
|
|
2065
|
-
const match = regex.exec(text);
|
|
2066
|
-
if (match && match.index >= 0) {
|
|
2067
|
-
matchOffset = match.index;
|
|
2068
|
-
} else {
|
|
2069
|
-
regex.lastIndex = 0;
|
|
2070
|
-
const wrapMatch = regex.exec(text);
|
|
2071
|
-
if (wrapMatch && wrapMatch.index >= 0) {
|
|
2072
|
-
matchOffset = wrapMatch.index;
|
|
2073
|
-
}
|
|
2074
|
-
}
|
|
2075
|
-
} else {
|
|
2076
|
-
const matches = Array.from(text.matchAll(regex));
|
|
2077
|
-
for (let index = matches.length - 1; index >= 0; index -= 1) {
|
|
2078
|
-
const match = matches[index];
|
|
2079
|
-
if ((match.index ?? -1) < cursor) {
|
|
2080
|
-
matchOffset = match.index ?? -1;
|
|
2081
|
-
break;
|
|
2082
|
-
}
|
|
2083
|
-
}
|
|
2084
|
-
if (matchOffset === -1 && matches.length > 0) {
|
|
2085
|
-
matchOffset = matches[matches.length - 1]?.index ?? -1;
|
|
2086
|
-
}
|
|
2087
|
-
}
|
|
2088
|
-
|
|
2089
|
-
if (matchOffset === -1) {
|
|
2090
|
-
throw new VimError(`Pattern not found: ${pattern}`);
|
|
2091
|
-
}
|
|
2092
|
-
|
|
2093
|
-
this.buffer.setCursor(this.buffer.offsetToPosition(matchOffset));
|
|
2094
|
-
this.statusMessage = `${direction > 0 ? "/" : "?"}${pattern}`;
|
|
2095
|
-
if (updateState) {
|
|
2096
|
-
this.lastSearch = { pattern, direction };
|
|
2097
|
-
}
|
|
2098
|
-
}
|
|
2099
|
-
|
|
2100
|
-
async #repeatSearch(direction: 1 | -1, count: number): Promise<void> {
|
|
2101
|
-
if (!this.lastSearch) {
|
|
2102
|
-
throw new VimError("No previous search");
|
|
2103
|
-
}
|
|
2104
|
-
for (let index = 0; index < count; index += 1) {
|
|
2105
|
-
await this.#runSearch(this.lastSearch.pattern, direction, false);
|
|
2106
|
-
}
|
|
2107
|
-
this.lastSearch = { pattern: this.lastSearch.pattern, direction };
|
|
2108
|
-
}
|
|
2109
|
-
|
|
2110
|
-
#resolveExRange(
|
|
2111
|
-
range: VimLineRange | "all" | undefined,
|
|
2112
|
-
defaultStart: number,
|
|
2113
|
-
defaultEnd = defaultStart,
|
|
2114
|
-
): VimLineRange {
|
|
2115
|
-
const totalLines = Math.max(1, this.buffer.lineCount());
|
|
2116
|
-
if (range === "all") {
|
|
2117
|
-
return { start: 1, end: totalLines };
|
|
2118
|
-
}
|
|
2119
|
-
const next = range ?? { start: defaultStart, end: defaultEnd };
|
|
2120
|
-
const start = Math.max(1, Math.min(next.start, totalLines));
|
|
2121
|
-
const end = Math.max(start, Math.min(next.end, totalLines));
|
|
2122
|
-
return { start, end };
|
|
2123
|
-
}
|
|
2124
|
-
|
|
2125
|
-
async #executeEx(input: string): Promise<void> {
|
|
2126
|
-
const command = parseExCommand(input, {
|
|
2127
|
-
currentLine: this.buffer.cursor.line + 1,
|
|
2128
|
-
lastLine: this.buffer.lineCount(),
|
|
2129
|
-
});
|
|
2130
|
-
switch (command.kind) {
|
|
2131
|
-
case "goto-line":
|
|
2132
|
-
this.buffer.setCursor({ line: Math.max(0, command.line - 1), col: 0 });
|
|
2133
|
-
this.statusMessage = `Line ${command.line}`;
|
|
2134
|
-
return;
|
|
2135
|
-
case "write": {
|
|
2136
|
-
const result = await this.#callbacks.saveBuffer(this.buffer, { force: command.force });
|
|
2137
|
-
this.buffer.markSaved(result.loaded);
|
|
2138
|
-
this.diagnostics = result.diagnostics;
|
|
2139
|
-
this.statusMessage = result.diagnostics
|
|
2140
|
-
? `Wrote ${this.buffer.displayPath} (${result.diagnostics.summary})`
|
|
2141
|
-
: `Wrote ${this.buffer.displayPath}`;
|
|
2142
|
-
this.#undoStack = [];
|
|
2143
|
-
this.#redoStack = [];
|
|
2144
|
-
return;
|
|
2145
|
-
}
|
|
2146
|
-
case "update":
|
|
2147
|
-
if (!this.buffer.modified) {
|
|
2148
|
-
this.statusMessage = `${this.buffer.displayPath} unchanged`;
|
|
2149
|
-
return;
|
|
2150
|
-
}
|
|
2151
|
-
await this.#executeEx(command.force ? "w!" : "w");
|
|
2152
|
-
return;
|
|
2153
|
-
case "write-quit":
|
|
2154
|
-
await this.#executeEx(command.force ? "w!" : "w");
|
|
2155
|
-
this.closed = true;
|
|
2156
|
-
this.statusMessage = `Wrote and closed ${this.buffer.displayPath}`;
|
|
2157
|
-
return;
|
|
2158
|
-
case "quit":
|
|
2159
|
-
if (this.buffer.modified && !command.force) {
|
|
2160
|
-
throw new VimError("Unsaved changes; use :q! to discard");
|
|
2161
|
-
}
|
|
2162
|
-
this.closed = true;
|
|
2163
|
-
this.statusMessage = `Closed ${this.buffer.displayPath}`;
|
|
2164
|
-
return;
|
|
2165
|
-
case "edit": {
|
|
2166
|
-
if (this.buffer.modified && !command.force) {
|
|
2167
|
-
throw new VimError("Unsaved changes; use :e! to reload or force open");
|
|
2168
|
-
}
|
|
2169
|
-
const next = await this.#callbacks.loadBuffer(command.path ?? this.buffer.displayPath);
|
|
2170
|
-
this.buffer.replaceLoadedFile(next);
|
|
2171
|
-
this.inputMode = "normal";
|
|
2172
|
-
this.selectionAnchor = null;
|
|
2173
|
-
this.#pendingInput = "";
|
|
2174
|
-
this.#pendingChange = null;
|
|
2175
|
-
this.#undoStack = [];
|
|
2176
|
-
this.#redoStack = [];
|
|
2177
|
-
this.statusMessage = command.path
|
|
2178
|
-
? `Opened ${this.buffer.displayPath}`
|
|
2179
|
-
: `Reloaded ${this.buffer.displayPath}`;
|
|
2180
|
-
return;
|
|
2181
|
-
}
|
|
2182
|
-
case "substitute": {
|
|
2183
|
-
const range = this.#resolveExRange(command.range, this.buffer.cursor.line + 1);
|
|
2184
|
-
const startLine = range.start;
|
|
2185
|
-
const endLine = range.end;
|
|
2186
|
-
const regexFlags = command.flags.includes("i") ? "gi" : "g";
|
|
2187
|
-
const regex = createSearchRegex(command.pattern, regexFlags);
|
|
2188
|
-
let replacements = 0;
|
|
2189
|
-
await this.#applyAtomicChange([":substitute"], () => {
|
|
2190
|
-
for (let lineIndex = startLine - 1; lineIndex <= endLine - 1; lineIndex += 1) {
|
|
2191
|
-
const line = this.buffer.getLine(lineIndex);
|
|
2192
|
-
let lineReplacements = 0;
|
|
2193
|
-
const nextLine = line.replace(regex, match => {
|
|
2194
|
-
if (!command.flags.includes("g") && lineReplacements > 0) {
|
|
2195
|
-
return match;
|
|
2196
|
-
}
|
|
2197
|
-
lineReplacements += 1;
|
|
2198
|
-
replacements += 1;
|
|
2199
|
-
return decodeReplacement(command.replacement).replace(/&/g, match);
|
|
2200
|
-
});
|
|
2201
|
-
this.buffer.replaceLine(lineIndex, nextLine);
|
|
2202
|
-
regex.lastIndex = 0;
|
|
2203
|
-
}
|
|
2204
|
-
});
|
|
2205
|
-
if (replacements === 0) {
|
|
2206
|
-
throw new VimError(`Pattern not found: ${command.pattern}`);
|
|
2207
|
-
}
|
|
2208
|
-
this.statusMessage = `${replacements} substitution${replacements === 1 ? "" : "s"}`;
|
|
2209
|
-
return;
|
|
2210
|
-
}
|
|
2211
|
-
case "delete": {
|
|
2212
|
-
const range = this.#resolveExRange(command.range, this.buffer.cursor.line + 1);
|
|
2213
|
-
await this.#applyAtomicChange([":delete"], () => {
|
|
2214
|
-
const removed = this.buffer.deleteLines(range.start - 1, range.end - 1);
|
|
2215
|
-
this.register = { kind: "line", text: removed.join("\n") };
|
|
2216
|
-
});
|
|
2217
|
-
this.statusMessage = `Deleted ${range.end - range.start + 1} line${range.end === range.start ? "" : "s"}`;
|
|
2218
|
-
return;
|
|
2219
|
-
}
|
|
2220
|
-
case "yank": {
|
|
2221
|
-
const range = this.#resolveExRange(command.range, this.buffer.cursor.line + 1);
|
|
2222
|
-
this.register = {
|
|
2223
|
-
kind: "line",
|
|
2224
|
-
text: this.buffer.lines.slice(range.start - 1, range.end).join("\n"),
|
|
2225
|
-
};
|
|
2226
|
-
this.statusMessage = `Yanked ${range.end - range.start + 1} line${range.end === range.start ? "" : "s"}`;
|
|
2227
|
-
return;
|
|
2228
|
-
}
|
|
2229
|
-
case "put": {
|
|
2230
|
-
if (!this.register.text) {
|
|
2231
|
-
this.statusMessage = "Register empty";
|
|
2232
|
-
return;
|
|
2233
|
-
}
|
|
2234
|
-
const anchorRange = this.#resolveExRange(command.range, this.buffer.cursor.line + 1);
|
|
2235
|
-
const anchorLine = command.before ? anchorRange.start : anchorRange.end;
|
|
2236
|
-
const lines = this.register.text.split("\n");
|
|
2237
|
-
await this.#applyAtomicChange([":put"], () => {
|
|
2238
|
-
const insertAt = command.before
|
|
2239
|
-
? Math.max(0, anchorLine - 1)
|
|
2240
|
-
: Math.min(anchorLine, this.buffer.lineCount());
|
|
2241
|
-
this.buffer.insertLines(insertAt, lines);
|
|
2242
|
-
});
|
|
2243
|
-
this.statusMessage = `Put ${lines.length} line${lines.length === 1 ? "" : "s"}`;
|
|
2244
|
-
return;
|
|
2245
|
-
}
|
|
2246
|
-
case "copy": {
|
|
2247
|
-
const totalLines = this.buffer.lineCount();
|
|
2248
|
-
const range = this.#resolveExRange(command.range, this.buffer.cursor.line + 1);
|
|
2249
|
-
const dest = Math.max(0, Math.min(command.destination, totalLines));
|
|
2250
|
-
await this.#applyAtomicChange([":copy"], () => {
|
|
2251
|
-
const lines = this.buffer.lines.slice(range.start - 1, range.end);
|
|
2252
|
-
this.buffer.insertLines(dest, lines);
|
|
2253
|
-
});
|
|
2254
|
-
this.statusMessage = `Copied ${range.end - range.start + 1} line${range.end === range.start ? "" : "s"}`;
|
|
2255
|
-
return;
|
|
2256
|
-
}
|
|
2257
|
-
case "move": {
|
|
2258
|
-
const totalLines = this.buffer.lineCount();
|
|
2259
|
-
const range = this.#resolveExRange(command.range, this.buffer.cursor.line + 1);
|
|
2260
|
-
const dest = Math.max(0, Math.min(command.destination, totalLines));
|
|
2261
|
-
await this.#applyAtomicChange([":move"], () => {
|
|
2262
|
-
const lines = this.buffer.lines.splice(range.start - 1, range.end - range.start + 1);
|
|
2263
|
-
const adjustedDest = dest > range.end - 1 ? dest - lines.length : dest;
|
|
2264
|
-
this.buffer.lines.splice(adjustedDest, 0, ...lines);
|
|
2265
|
-
if (this.buffer.lines.length === 0) this.buffer.lines = [""];
|
|
2266
|
-
this.buffer.setCursor({ line: adjustedDest, col: 0 });
|
|
2267
|
-
});
|
|
2268
|
-
this.statusMessage = `Moved ${range.end - range.start + 1} line${range.end === range.start ? "" : "s"}`;
|
|
2269
|
-
return;
|
|
2270
|
-
}
|
|
2271
|
-
case "sort": {
|
|
2272
|
-
const range = this.#resolveExRange(command.range ?? "all", 1, this.buffer.lineCount());
|
|
2273
|
-
const startLine = range.start;
|
|
2274
|
-
const endLine = range.end;
|
|
2275
|
-
const reverse = command.flags.includes("!");
|
|
2276
|
-
const ignoreCase = command.flags.includes("i");
|
|
2277
|
-
await this.#applyAtomicChange([":sort"], () => {
|
|
2278
|
-
const slice = this.buffer.lines.slice(startLine - 1, endLine);
|
|
2279
|
-
slice.sort((a, b) => {
|
|
2280
|
-
const left = ignoreCase ? a.toLowerCase() : a;
|
|
2281
|
-
const right = ignoreCase ? b.toLowerCase() : b;
|
|
2282
|
-
return left < right ? -1 : left > right ? 1 : 0;
|
|
2283
|
-
});
|
|
2284
|
-
if (reverse) slice.reverse();
|
|
2285
|
-
for (let i = 0; i < slice.length; i++) {
|
|
2286
|
-
this.buffer.lines[startLine - 1 + i] = slice[i]!;
|
|
2287
|
-
}
|
|
2288
|
-
});
|
|
2289
|
-
this.statusMessage = `Sorted ${endLine - startLine + 1} line${endLine === startLine ? "" : "s"}`;
|
|
2290
|
-
return;
|
|
2291
|
-
}
|
|
2292
|
-
case "join": {
|
|
2293
|
-
const currentLine = this.buffer.cursor.line + 1;
|
|
2294
|
-
const baseRange = this.#resolveExRange(
|
|
2295
|
-
command.range,
|
|
2296
|
-
currentLine,
|
|
2297
|
-
command.range ? undefined : Math.min(this.buffer.lineCount(), currentLine + 1),
|
|
2298
|
-
);
|
|
2299
|
-
const startLine = baseRange.start;
|
|
2300
|
-
const endLine =
|
|
2301
|
-
baseRange.start === baseRange.end ? Math.min(this.buffer.lineCount(), baseRange.end + 1) : baseRange.end;
|
|
2302
|
-
const lineCount = endLine - startLine + 1;
|
|
2303
|
-
if (lineCount < 2) {
|
|
2304
|
-
this.statusMessage = "Nothing to join";
|
|
2305
|
-
return;
|
|
2306
|
-
}
|
|
2307
|
-
await this.#applyAtomicChange([":join"], () => {
|
|
2308
|
-
const startIndex = startLine - 1;
|
|
2309
|
-
if (command.trimWhitespace) {
|
|
2310
|
-
this.buffer.joinLines(startIndex, lineCount - 1);
|
|
2311
|
-
return;
|
|
2312
|
-
}
|
|
2313
|
-
const joined = this.buffer.lines.slice(startIndex, endLine).join("");
|
|
2314
|
-
this.buffer.lines.splice(startIndex, lineCount, joined);
|
|
2315
|
-
this.buffer.setCursor({ line: startIndex, col: Math.max(0, joined.length - 1) });
|
|
2316
|
-
});
|
|
2317
|
-
this.statusMessage = `Joined ${lineCount} lines`;
|
|
2318
|
-
return;
|
|
2319
|
-
}
|
|
2320
|
-
case "append": {
|
|
2321
|
-
const anchorRange = this.#resolveExRange(command.range, this.buffer.cursor.line + 1);
|
|
2322
|
-
const anchorLine = anchorRange.end;
|
|
2323
|
-
const lines = command.text.length > 0 ? command.text.split("\n") : [""];
|
|
2324
|
-
await this.#applyAtomicChange([":append"], () => {
|
|
2325
|
-
const insertAt = Math.min(anchorLine, this.buffer.lineCount());
|
|
2326
|
-
this.buffer.insertLines(insertAt, lines);
|
|
2327
|
-
});
|
|
2328
|
-
this.statusMessage = `Appended ${lines.length} line${lines.length === 1 ? "" : "s"}`;
|
|
2329
|
-
return;
|
|
2330
|
-
}
|
|
2331
|
-
case "insert-before": {
|
|
2332
|
-
const anchorRange = this.#resolveExRange(command.range, this.buffer.cursor.line + 1);
|
|
2333
|
-
const anchorLine = anchorRange.start;
|
|
2334
|
-
const lines = command.text.length > 0 ? command.text.split("\n") : [""];
|
|
2335
|
-
await this.#applyAtomicChange([":insert"], () => {
|
|
2336
|
-
const insertAt = Math.max(0, anchorLine - 1);
|
|
2337
|
-
this.buffer.insertLines(insertAt, lines);
|
|
2338
|
-
});
|
|
2339
|
-
this.statusMessage = `Inserted ${lines.length} line${lines.length === 1 ? "" : "s"}`;
|
|
2340
|
-
return;
|
|
2341
|
-
}
|
|
2342
|
-
case "global": {
|
|
2343
|
-
const regex = createSearchRegex(command.pattern);
|
|
2344
|
-
const range = this.#resolveExRange(command.range ?? "all", 1, this.buffer.lineCount());
|
|
2345
|
-
await this.#applyAtomicChange([":global"], () => {
|
|
2346
|
-
const linesToProcess: number[] = [];
|
|
2347
|
-
for (let i = range.start - 1; i <= range.end - 1; i += 1) {
|
|
2348
|
-
const matches = regex.test(this.buffer.getLine(i));
|
|
2349
|
-
regex.lastIndex = 0;
|
|
2350
|
-
if (command.invert ? !matches : matches) {
|
|
2351
|
-
linesToProcess.push(i);
|
|
2352
|
-
}
|
|
2353
|
-
}
|
|
2354
|
-
if (command.command === "d" || command.command === "delete") {
|
|
2355
|
-
// Delete matching lines in reverse to preserve indices
|
|
2356
|
-
for (let i = linesToProcess.length - 1; i >= 0; i--) {
|
|
2357
|
-
this.buffer.lines.splice(linesToProcess[i]!, 1);
|
|
2358
|
-
}
|
|
2359
|
-
if (this.buffer.lines.length === 0) this.buffer.lines = [""];
|
|
2360
|
-
this.buffer.clampCursor();
|
|
2361
|
-
this.buffer.trailingNewline = true;
|
|
2362
|
-
} else {
|
|
2363
|
-
throw new VimError(`Unsupported :global sub-command: ${command.command}`);
|
|
2364
|
-
}
|
|
2365
|
-
});
|
|
2366
|
-
this.statusMessage = `Global: processed ${command.pattern}`;
|
|
2367
|
-
return;
|
|
2368
|
-
}
|
|
2369
|
-
}
|
|
2370
|
-
}
|
|
2371
|
-
|
|
2372
|
-
#paste(after: boolean, count: number): void {
|
|
2373
|
-
if (!this.register.text) {
|
|
2374
|
-
return;
|
|
2375
|
-
}
|
|
2376
|
-
if (this.register.kind === "line") {
|
|
2377
|
-
const lines = this.register.text.split("\n");
|
|
2378
|
-
const insertAt = after ? this.buffer.cursor.line + 1 : this.buffer.cursor.line;
|
|
2379
|
-
for (let iteration = 0; iteration < count; iteration += 1) {
|
|
2380
|
-
this.buffer.insertLines(insertAt + iteration * lines.length, lines);
|
|
2381
|
-
}
|
|
2382
|
-
return;
|
|
2383
|
-
}
|
|
2384
|
-
const text = this.register.text.repeat(count);
|
|
2385
|
-
const offset = this.buffer.currentOffset() + (after ? 1 : 0);
|
|
2386
|
-
this.buffer.replaceOffsets(offset, offset, text, offset + text.length);
|
|
2387
|
-
}
|
|
2388
|
-
|
|
2389
|
-
#readCount(tokens: readonly VimKeyToken[], index: number): { count: number; hasCount: boolean; nextIndex: number } {
|
|
2390
|
-
let cursor = index;
|
|
2391
|
-
let digits = "";
|
|
2392
|
-
while (cursor < tokens.length) {
|
|
2393
|
-
const value = tokens[cursor]?.value ?? "";
|
|
2394
|
-
if (!/^\d$/.test(value)) {
|
|
2395
|
-
break;
|
|
2396
|
-
}
|
|
2397
|
-
if (digits.length === 0 && value === "0") {
|
|
2398
|
-
break;
|
|
2399
|
-
}
|
|
2400
|
-
digits += value;
|
|
2401
|
-
cursor += 1;
|
|
2402
|
-
}
|
|
2403
|
-
return {
|
|
2404
|
-
count: digits.length > 0 ? Number.parseInt(digits, 10) : 1,
|
|
2405
|
-
hasCount: digits.length > 0,
|
|
2406
|
-
nextIndex: cursor,
|
|
2407
|
-
};
|
|
2408
|
-
}
|
|
2409
|
-
}
|