@f5xc-salesdemos/pi-tui 14.0.2
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 +682 -0
- package/README.md +704 -0
- package/package.json +71 -0
- package/src/autocomplete.ts +787 -0
- package/src/bracketed-paste.ts +47 -0
- package/src/components/box.ts +144 -0
- package/src/components/cancellable-loader.ts +40 -0
- package/src/components/editor.ts +2563 -0
- package/src/components/image.ts +90 -0
- package/src/components/input.ts +439 -0
- package/src/components/loader.ts +67 -0
- package/src/components/markdown.ts +914 -0
- package/src/components/select-list.ts +249 -0
- package/src/components/settings-list.ts +195 -0
- package/src/components/spacer.ts +28 -0
- package/src/components/tab-bar.ts +175 -0
- package/src/components/text.ts +110 -0
- package/src/components/truncated-text.ts +61 -0
- package/src/editor-component.ts +71 -0
- package/src/fuzzy.ts +143 -0
- package/src/index.ts +39 -0
- package/src/keybindings.ts +279 -0
- package/src/keys.ts +404 -0
- package/src/kill-ring.ts +46 -0
- package/src/stdin-buffer.ts +385 -0
- package/src/symbols.ts +24 -0
- package/src/terminal-capabilities.ts +525 -0
- package/src/terminal.ts +630 -0
- package/src/ttyid.ts +66 -0
- package/src/tui.ts +1328 -0
- package/src/utils.ts +301 -0
|
@@ -0,0 +1,2563 @@
|
|
|
1
|
+
import { getProjectDir, logger } from "@f5xc-salesdemos/pi-utils";
|
|
2
|
+
import type { AutocompleteProvider, CombinedAutocompleteProvider } from "../autocomplete";
|
|
3
|
+
import { BracketedPasteHandler } from "../bracketed-paste";
|
|
4
|
+
import { getKeybindings, type KeybindingsManager } from "../keybindings";
|
|
5
|
+
import { extractPrintableText, matchesKey } from "../keys";
|
|
6
|
+
import { KillRing } from "../kill-ring";
|
|
7
|
+
import type { SymbolTheme } from "../symbols";
|
|
8
|
+
import { type Component, CURSOR_MARKER, type Focusable } from "../tui";
|
|
9
|
+
import {
|
|
10
|
+
getSegmenter,
|
|
11
|
+
getWordNavKind,
|
|
12
|
+
moveWordLeft,
|
|
13
|
+
moveWordRight,
|
|
14
|
+
padding,
|
|
15
|
+
replaceTabs,
|
|
16
|
+
sliceByColumn,
|
|
17
|
+
truncateToWidth,
|
|
18
|
+
visibleWidth,
|
|
19
|
+
} from "../utils";
|
|
20
|
+
import { SelectList, type SelectListLayoutOptions, type SelectListTheme } from "./select-list";
|
|
21
|
+
|
|
22
|
+
const SLASH_COMMAND_SELECT_LIST_LAYOUT: SelectListLayoutOptions = {
|
|
23
|
+
minPrimaryColumnWidth: 12,
|
|
24
|
+
maxPrimaryColumnWidth: 32,
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
function sanitizeLoadedText(text: string): string {
|
|
28
|
+
return replaceTabs(text.replace(/\r\n/g, "\n").replace(/\r/g, "\n"))
|
|
29
|
+
.split("")
|
|
30
|
+
.filter(char => char === "\n" || char.charCodeAt(0) >= 32)
|
|
31
|
+
.join("");
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const segmenter = getSegmenter();
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Represents a chunk of text for word-wrap layout.
|
|
38
|
+
* Tracks both the text content and its position in the original line.
|
|
39
|
+
*/
|
|
40
|
+
interface TextChunk {
|
|
41
|
+
text: string;
|
|
42
|
+
startIndex: number;
|
|
43
|
+
endIndex: number;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Split a line into word-wrapped chunks.
|
|
48
|
+
* Wraps at word boundaries when possible, falling back to character-level
|
|
49
|
+
* wrapping for words longer than the available width.
|
|
50
|
+
*
|
|
51
|
+
* @param line - The text line to wrap
|
|
52
|
+
* @param maxWidth - Maximum visible width per chunk
|
|
53
|
+
* @returns Array of chunks with text and position information
|
|
54
|
+
*/
|
|
55
|
+
function wordWrapLine(line: string, maxWidth: number): TextChunk[] {
|
|
56
|
+
if (!line || maxWidth <= 0) {
|
|
57
|
+
return [{ text: "", startIndex: 0, endIndex: 0 }];
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const lineWidth = visibleWidth(line);
|
|
61
|
+
if (lineWidth <= maxWidth) {
|
|
62
|
+
return [{ text: line, startIndex: 0, endIndex: line.length }];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const chunks: TextChunk[] = [];
|
|
66
|
+
|
|
67
|
+
// Split into tokens (words and whitespace runs)
|
|
68
|
+
const tokens: { text: string; startIndex: number; endIndex: number; isWhitespace: boolean }[] = [];
|
|
69
|
+
let currentToken = "";
|
|
70
|
+
let tokenStart = 0;
|
|
71
|
+
let inWhitespace = false;
|
|
72
|
+
let charIndex = 0;
|
|
73
|
+
|
|
74
|
+
for (const seg of segmenter.segment(line)) {
|
|
75
|
+
const grapheme = seg.segment;
|
|
76
|
+
const graphemeIsWhitespace = getWordNavKind(grapheme) === "whitespace";
|
|
77
|
+
|
|
78
|
+
if (currentToken === "") {
|
|
79
|
+
inWhitespace = graphemeIsWhitespace;
|
|
80
|
+
tokenStart = charIndex;
|
|
81
|
+
} else if (graphemeIsWhitespace !== inWhitespace) {
|
|
82
|
+
// Token type changed - save current token
|
|
83
|
+
tokens.push({
|
|
84
|
+
text: currentToken,
|
|
85
|
+
startIndex: tokenStart,
|
|
86
|
+
endIndex: charIndex,
|
|
87
|
+
isWhitespace: inWhitespace,
|
|
88
|
+
});
|
|
89
|
+
currentToken = "";
|
|
90
|
+
tokenStart = charIndex;
|
|
91
|
+
inWhitespace = graphemeIsWhitespace;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
currentToken += grapheme;
|
|
95
|
+
charIndex += grapheme.length;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Push final token
|
|
99
|
+
if (currentToken) {
|
|
100
|
+
tokens.push({
|
|
101
|
+
text: currentToken,
|
|
102
|
+
startIndex: tokenStart,
|
|
103
|
+
endIndex: charIndex,
|
|
104
|
+
isWhitespace: inWhitespace,
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Build chunks using word wrapping
|
|
109
|
+
let currentChunk = "";
|
|
110
|
+
let currentWidth = 0;
|
|
111
|
+
let chunkStartIndex = 0;
|
|
112
|
+
let atLineStart = true; // Track if we're at the start of a line (for skipping whitespace)
|
|
113
|
+
|
|
114
|
+
function consumePrefixToWidth(text: string, availableWidth: number): { text: string; len: number } {
|
|
115
|
+
let prefix = "";
|
|
116
|
+
let prefixWidth = 0;
|
|
117
|
+
let len = 0;
|
|
118
|
+
for (const seg of segmenter.segment(text)) {
|
|
119
|
+
const grapheme = seg.segment;
|
|
120
|
+
const graphemeWidth = visibleWidth(grapheme);
|
|
121
|
+
if (prefixWidth + graphemeWidth > availableWidth) break;
|
|
122
|
+
prefix += grapheme;
|
|
123
|
+
prefixWidth += graphemeWidth;
|
|
124
|
+
len += grapheme.length;
|
|
125
|
+
if (prefixWidth === availableWidth) break;
|
|
126
|
+
}
|
|
127
|
+
return { text: prefix, len };
|
|
128
|
+
}
|
|
129
|
+
function hasWideGrapheme(text: string): boolean {
|
|
130
|
+
for (const seg of segmenter.segment(text)) {
|
|
131
|
+
if (visibleWidth(seg.segment) > 1) return true;
|
|
132
|
+
}
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
for (const token of tokens) {
|
|
136
|
+
const tokenWidth = visibleWidth(token.text);
|
|
137
|
+
|
|
138
|
+
// Skip leading whitespace at line start
|
|
139
|
+
if (atLineStart && token.isWhitespace) {
|
|
140
|
+
chunkStartIndex = token.endIndex;
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
atLineStart = false;
|
|
144
|
+
|
|
145
|
+
// If this single token is wider than maxWidth, we need to break it
|
|
146
|
+
if (tokenWidth > maxWidth) {
|
|
147
|
+
// If we're mid-line, try to use the remaining width by consuming a prefix of this long token.
|
|
148
|
+
let consumedPrefix = "";
|
|
149
|
+
let consumedPrefixLen = 0; // JS string index (code units) consumed from token.text
|
|
150
|
+
if (currentChunk && currentWidth < maxWidth) {
|
|
151
|
+
const remainingWidth = maxWidth - currentWidth;
|
|
152
|
+
const consumed = consumePrefixToWidth(token.text, remainingWidth);
|
|
153
|
+
consumedPrefix = consumed.text;
|
|
154
|
+
consumedPrefixLen = consumed.len;
|
|
155
|
+
}
|
|
156
|
+
// First, push any accumulated chunk (optionally filled with the prefix).
|
|
157
|
+
if (currentChunk) {
|
|
158
|
+
if (consumedPrefix) {
|
|
159
|
+
chunks.push({
|
|
160
|
+
text: currentChunk + consumedPrefix,
|
|
161
|
+
startIndex: chunkStartIndex,
|
|
162
|
+
endIndex: token.startIndex + consumedPrefixLen,
|
|
163
|
+
});
|
|
164
|
+
currentChunk = "";
|
|
165
|
+
currentWidth = 0;
|
|
166
|
+
chunkStartIndex = token.startIndex + consumedPrefixLen;
|
|
167
|
+
} else {
|
|
168
|
+
chunks.push({
|
|
169
|
+
text: currentChunk,
|
|
170
|
+
startIndex: chunkStartIndex,
|
|
171
|
+
endIndex: token.startIndex,
|
|
172
|
+
});
|
|
173
|
+
currentChunk = "";
|
|
174
|
+
currentWidth = 0;
|
|
175
|
+
chunkStartIndex = token.startIndex;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
// Break the remaining long token by grapheme
|
|
179
|
+
const remainingText = consumedPrefixLen > 0 ? token.text.slice(consumedPrefixLen) : token.text;
|
|
180
|
+
let tokenChunk = "";
|
|
181
|
+
let tokenChunkWidth = 0;
|
|
182
|
+
let tokenChunkStart = token.startIndex + consumedPrefixLen;
|
|
183
|
+
let tokenCharIndex = token.startIndex + consumedPrefixLen;
|
|
184
|
+
for (const seg of segmenter.segment(remainingText)) {
|
|
185
|
+
const grapheme = seg.segment;
|
|
186
|
+
const graphemeWidth = visibleWidth(grapheme);
|
|
187
|
+
if (tokenChunkWidth + graphemeWidth > maxWidth && tokenChunk) {
|
|
188
|
+
chunks.push({
|
|
189
|
+
text: tokenChunk,
|
|
190
|
+
startIndex: tokenChunkStart,
|
|
191
|
+
endIndex: tokenCharIndex,
|
|
192
|
+
});
|
|
193
|
+
tokenChunk = grapheme;
|
|
194
|
+
tokenChunkWidth = graphemeWidth;
|
|
195
|
+
tokenChunkStart = tokenCharIndex;
|
|
196
|
+
} else {
|
|
197
|
+
tokenChunk += grapheme;
|
|
198
|
+
tokenChunkWidth += graphemeWidth;
|
|
199
|
+
}
|
|
200
|
+
tokenCharIndex += grapheme.length;
|
|
201
|
+
}
|
|
202
|
+
// Keep remainder as start of next chunk
|
|
203
|
+
if (tokenChunk) {
|
|
204
|
+
currentChunk = tokenChunk;
|
|
205
|
+
currentWidth = tokenChunkWidth;
|
|
206
|
+
chunkStartIndex = tokenChunkStart;
|
|
207
|
+
}
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// Check if adding this token would exceed width
|
|
212
|
+
if (currentWidth + tokenWidth > maxWidth) {
|
|
213
|
+
// For wide-character tokens (e.g., CJK runs), prefer using remaining width before wrapping
|
|
214
|
+
// the whole token to the next line. This avoids leaving a short ASCII word alone.
|
|
215
|
+
if (currentChunk && !token.isWhitespace && currentWidth < maxWidth && hasWideGrapheme(token.text)) {
|
|
216
|
+
const remainingWidth = maxWidth - currentWidth;
|
|
217
|
+
const consumed = consumePrefixToWidth(token.text, remainingWidth);
|
|
218
|
+
if (consumed.text) {
|
|
219
|
+
chunks.push({
|
|
220
|
+
text: currentChunk + consumed.text,
|
|
221
|
+
startIndex: chunkStartIndex,
|
|
222
|
+
endIndex: token.startIndex + consumed.len,
|
|
223
|
+
});
|
|
224
|
+
const remainder = token.text.slice(consumed.len);
|
|
225
|
+
currentChunk = remainder;
|
|
226
|
+
currentWidth = visibleWidth(remainder);
|
|
227
|
+
chunkStartIndex = token.startIndex + consumed.len;
|
|
228
|
+
atLineStart = false;
|
|
229
|
+
continue;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
// Push current chunk (trimming trailing whitespace for display)
|
|
233
|
+
const trimmedChunk = currentChunk.trimEnd();
|
|
234
|
+
if (trimmedChunk || chunks.length === 0) {
|
|
235
|
+
chunks.push({
|
|
236
|
+
text: trimmedChunk,
|
|
237
|
+
startIndex: chunkStartIndex,
|
|
238
|
+
endIndex: chunkStartIndex + currentChunk.length,
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
// Start new line - skip leading whitespace
|
|
242
|
+
atLineStart = true;
|
|
243
|
+
if (token.isWhitespace) {
|
|
244
|
+
currentChunk = "";
|
|
245
|
+
currentWidth = 0;
|
|
246
|
+
chunkStartIndex = token.endIndex;
|
|
247
|
+
} else {
|
|
248
|
+
currentChunk = token.text;
|
|
249
|
+
currentWidth = tokenWidth;
|
|
250
|
+
chunkStartIndex = token.startIndex;
|
|
251
|
+
atLineStart = false;
|
|
252
|
+
}
|
|
253
|
+
} else {
|
|
254
|
+
// Add token to current chunk
|
|
255
|
+
currentChunk += token.text;
|
|
256
|
+
currentWidth += tokenWidth;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// Push final chunk
|
|
261
|
+
if (currentChunk) {
|
|
262
|
+
chunks.push({
|
|
263
|
+
text: currentChunk,
|
|
264
|
+
startIndex: chunkStartIndex,
|
|
265
|
+
endIndex: line.length,
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
return chunks.length > 0 ? chunks : [{ text: "", startIndex: 0, endIndex: 0 }];
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
const DEFAULT_PAGE_SCROLL_LINES = 10;
|
|
273
|
+
|
|
274
|
+
interface EditorState {
|
|
275
|
+
lines: string[];
|
|
276
|
+
cursorLine: number;
|
|
277
|
+
cursorCol: number;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
interface LayoutLine {
|
|
281
|
+
text: string;
|
|
282
|
+
hasCursor: boolean;
|
|
283
|
+
cursorPos?: number;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
export interface EditorTheme {
|
|
287
|
+
borderColor: (str: string) => string;
|
|
288
|
+
selectList: SelectListTheme;
|
|
289
|
+
symbols: SymbolTheme;
|
|
290
|
+
editorPaddingX?: number;
|
|
291
|
+
/** Style function for inline hint/ghost text (dim text after cursor) */
|
|
292
|
+
hintStyle?: (text: string) => string;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
export interface EditorTopBorder {
|
|
296
|
+
/** The status content (already styled) */
|
|
297
|
+
content: string;
|
|
298
|
+
/** Visible width of the content */
|
|
299
|
+
width: number;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
interface HistoryEntry {
|
|
303
|
+
prompt: string;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
interface HistoryStorage {
|
|
307
|
+
add(prompt: string, cwd?: string): Promise<void>;
|
|
308
|
+
getRecent(limit: number): HistoryEntry[];
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
type HistoryCursorAnchor = "start" | "end";
|
|
312
|
+
|
|
313
|
+
export class Editor implements Component, Focusable {
|
|
314
|
+
#state: EditorState = {
|
|
315
|
+
lines: [""],
|
|
316
|
+
cursorLine: 0,
|
|
317
|
+
cursorCol: 0,
|
|
318
|
+
};
|
|
319
|
+
|
|
320
|
+
/** Focusable interface - set by TUI when focus changes */
|
|
321
|
+
focused: boolean = false;
|
|
322
|
+
|
|
323
|
+
#theme: EditorTheme;
|
|
324
|
+
#useTerminalCursor = false;
|
|
325
|
+
|
|
326
|
+
/** When set, replaces the normal cursor glyph at end-of-text with this ANSI-styled string. */
|
|
327
|
+
cursorOverride: string | undefined;
|
|
328
|
+
/** Display width of the cursorOverride glyph (needed because override may contain ANSI escapes). */
|
|
329
|
+
cursorOverrideWidth: number | undefined;
|
|
330
|
+
#promptGutter: string | undefined;
|
|
331
|
+
|
|
332
|
+
// Store last layout width for cursor navigation
|
|
333
|
+
#lastLayoutWidth: number = 80;
|
|
334
|
+
#paddingXOverride: number | undefined;
|
|
335
|
+
#maxHeight?: number;
|
|
336
|
+
#scrollOffset: number = 0;
|
|
337
|
+
|
|
338
|
+
// Emacs-style kill ring
|
|
339
|
+
#killRing = new KillRing();
|
|
340
|
+
#lastAction: "kill" | "yank" | null = null;
|
|
341
|
+
|
|
342
|
+
// Character jump mode
|
|
343
|
+
#jumpMode: "forward" | "backward" | null = null;
|
|
344
|
+
|
|
345
|
+
// Preferred visual column for vertical cursor movement (sticky column)
|
|
346
|
+
#preferredVisualCol: number | null = null;
|
|
347
|
+
|
|
348
|
+
// Border color (can be changed dynamically)
|
|
349
|
+
borderColor: (str: string) => string;
|
|
350
|
+
|
|
351
|
+
// Autocomplete support
|
|
352
|
+
#autocompleteProvider?: AutocompleteProvider;
|
|
353
|
+
#autocompleteList?: SelectList;
|
|
354
|
+
#autocompleteState: "regular" | "force" | null = null;
|
|
355
|
+
#autocompletePrefix: string = "";
|
|
356
|
+
#autocompleteRequestId: number = 0;
|
|
357
|
+
#autocompleteMaxVisible: number = 5;
|
|
358
|
+
onAutocompleteUpdate?: () => void;
|
|
359
|
+
|
|
360
|
+
// Paste tracking for large pastes
|
|
361
|
+
#pastes: Map<number, string> = new Map();
|
|
362
|
+
#pasteCounter: number = 0;
|
|
363
|
+
|
|
364
|
+
// Bracketed paste mode buffering
|
|
365
|
+
#pasteHandler = new BracketedPasteHandler();
|
|
366
|
+
|
|
367
|
+
// Prompt history for up/down navigation
|
|
368
|
+
#history: string[] = [];
|
|
369
|
+
#historyIndex: number = -1; // -1 = not browsing, 0 = most recent, 1 = older, etc.
|
|
370
|
+
#historyStorage?: HistoryStorage;
|
|
371
|
+
|
|
372
|
+
// Undo stack for editor state changes
|
|
373
|
+
#undoStack: EditorState[] = [];
|
|
374
|
+
#suspendUndo = false;
|
|
375
|
+
|
|
376
|
+
// Debounce timer for autocomplete updates
|
|
377
|
+
#autocompleteTimeout?: NodeJS.Timeout;
|
|
378
|
+
|
|
379
|
+
onSubmit?: (text: string) => void;
|
|
380
|
+
onAltEnter?: (text: string) => void;
|
|
381
|
+
onChange?: (text: string) => void;
|
|
382
|
+
onAutocompleteCancel?: () => void;
|
|
383
|
+
disableSubmit: boolean = false;
|
|
384
|
+
|
|
385
|
+
// Custom top border (for status line integration)
|
|
386
|
+
#topBorderContent?: EditorTopBorder;
|
|
387
|
+
#borderVisible = true;
|
|
388
|
+
|
|
389
|
+
constructor(theme: EditorTheme) {
|
|
390
|
+
this.#theme = theme;
|
|
391
|
+
this.borderColor = theme.borderColor;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
setAutocompleteProvider(provider: AutocompleteProvider): void {
|
|
395
|
+
this.#autocompleteProvider = provider;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* Set custom content for the top border (e.g., status line).
|
|
400
|
+
* Pass undefined to use the default plain border.
|
|
401
|
+
*/
|
|
402
|
+
setTopBorder(content: EditorTopBorder | undefined): void {
|
|
403
|
+
this.#topBorderContent = content;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/**
|
|
407
|
+
* Show or hide the editor border chrome.
|
|
408
|
+
*/
|
|
409
|
+
setBorderVisible(borderVisible: boolean): void {
|
|
410
|
+
this.#borderVisible = borderVisible;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
setPromptGutter(promptGutter: string | undefined): void {
|
|
414
|
+
this.#promptGutter = promptGutter;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/**
|
|
418
|
+
* Get the available width for top border content given a total terminal width.
|
|
419
|
+
* Accounts for the border characters and horizontal padding when visible.
|
|
420
|
+
*/
|
|
421
|
+
getTopBorderAvailableWidth(terminalWidth: number): number {
|
|
422
|
+
const paddingX = this.#getEditorPaddingX();
|
|
423
|
+
const borderWidth = this.#getHorizontalChromeWidth(paddingX);
|
|
424
|
+
return Math.max(0, terminalWidth - borderWidth * 2);
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* Use the real terminal cursor instead of rendering a cursor glyph.
|
|
429
|
+
*/
|
|
430
|
+
setUseTerminalCursor(useTerminalCursor: boolean): void {
|
|
431
|
+
this.#useTerminalCursor = useTerminalCursor;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
getUseTerminalCursor(): boolean {
|
|
435
|
+
return this.#useTerminalCursor;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
setMaxHeight(maxHeight: number | undefined): void {
|
|
439
|
+
if (this.#maxHeight === maxHeight) return;
|
|
440
|
+
this.#maxHeight = maxHeight;
|
|
441
|
+
// Don't reset scrollOffset — #updateScrollOffset will clamp it on next render
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
setPaddingX(paddingX: number): void {
|
|
445
|
+
this.#paddingXOverride = Math.max(0, paddingX);
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
getAutocompleteMaxVisible(): number {
|
|
449
|
+
return this.#autocompleteMaxVisible;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
setAutocompleteMaxVisible(maxVisible: number): void {
|
|
453
|
+
const newMaxVisible = Number.isFinite(maxVisible) ? Math.max(3, Math.min(20, Math.floor(maxVisible))) : 5;
|
|
454
|
+
if (this.#autocompleteMaxVisible !== newMaxVisible) {
|
|
455
|
+
this.#autocompleteMaxVisible = newMaxVisible;
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
setHistoryStorage(storage: HistoryStorage): void {
|
|
460
|
+
this.#historyStorage = storage;
|
|
461
|
+
const recent = storage.getRecent(100);
|
|
462
|
+
this.#history = recent.map(entry => entry.prompt);
|
|
463
|
+
this.#historyIndex = -1;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
/**
|
|
467
|
+
* Add a prompt to history for up/down arrow navigation.
|
|
468
|
+
* Called after successful submission.
|
|
469
|
+
*/
|
|
470
|
+
addToHistory(text: string): void {
|
|
471
|
+
const trimmed = text.trim();
|
|
472
|
+
if (!trimmed) return;
|
|
473
|
+
// Don't add consecutive duplicates
|
|
474
|
+
if (this.#history.length > 0 && this.#history[0] === trimmed) return;
|
|
475
|
+
this.#history.unshift(trimmed);
|
|
476
|
+
// Limit history size
|
|
477
|
+
if (this.#history.length > 100) {
|
|
478
|
+
this.#history.pop();
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
const stor = this.#historyStorage;
|
|
482
|
+
if (stor) {
|
|
483
|
+
stor.add(trimmed, getProjectDir()).catch(error => {
|
|
484
|
+
logger.error("HistoryStorage add failed", { error: String(error) });
|
|
485
|
+
});
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
#isEditorEmpty(): boolean {
|
|
490
|
+
return this.#state.lines.length === 1 && this.#state.lines[0] === "";
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
#isOnFirstVisualLine(): boolean {
|
|
494
|
+
const visualLines = this.#buildVisualLineMap(this.#lastLayoutWidth);
|
|
495
|
+
const currentVisualLine = this.#findCurrentVisualLine(visualLines);
|
|
496
|
+
return currentVisualLine === 0;
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
#isOnLastVisualLine(): boolean {
|
|
500
|
+
const visualLines = this.#buildVisualLineMap(this.#lastLayoutWidth);
|
|
501
|
+
const currentVisualLine = this.#findCurrentVisualLine(visualLines);
|
|
502
|
+
return currentVisualLine === visualLines.length - 1;
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
#navigateHistory(direction: 1 | -1): void {
|
|
506
|
+
this.#resetKillSequence();
|
|
507
|
+
if (this.#history.length === 0) return;
|
|
508
|
+
const newIndex = this.#historyIndex - direction; // Up(-1) increases index, Down(1) decreases
|
|
509
|
+
if (newIndex < -1 || newIndex >= this.#history.length) return;
|
|
510
|
+
this.#historyIndex = newIndex;
|
|
511
|
+
if (this.#historyIndex === -1) {
|
|
512
|
+
// Returned to "current" state - clear editor
|
|
513
|
+
this.#setTextInternal("", "end");
|
|
514
|
+
} else {
|
|
515
|
+
const cursorAnchor: HistoryCursorAnchor = direction === -1 ? "start" : "end";
|
|
516
|
+
this.#setTextInternal(this.#history[this.#historyIndex] || "", cursorAnchor);
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
/** Internal setText that doesn't reset history state - used by navigateHistory */
|
|
520
|
+
#setTextInternal(text: string, cursorAnchor: HistoryCursorAnchor = "end"): void {
|
|
521
|
+
this.#undoStack.length = 0;
|
|
522
|
+
const lines = sanitizeLoadedText(text).split("\n");
|
|
523
|
+
this.#state.lines = lines.length === 0 ? [""] : lines;
|
|
524
|
+
if (cursorAnchor === "start") {
|
|
525
|
+
this.#state.cursorLine = 0;
|
|
526
|
+
this.#setCursorCol(0);
|
|
527
|
+
} else {
|
|
528
|
+
this.#state.cursorLine = this.#state.lines.length - 1;
|
|
529
|
+
this.#setCursorCol(this.#state.lines[this.#state.cursorLine]?.length || 0);
|
|
530
|
+
}
|
|
531
|
+
if (this.onChange) {
|
|
532
|
+
this.onChange(this.getText());
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
invalidate(): void {
|
|
537
|
+
// No cached state to invalidate currently
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
#getEditorPaddingX(): number {
|
|
541
|
+
const padding = this.#paddingXOverride ?? this.#theme.editorPaddingX ?? 2;
|
|
542
|
+
return Math.max(0, padding);
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
#getHorizontalChromeWidth(paddingX: number): number {
|
|
546
|
+
return this.#borderVisible ? paddingX + 1 : 0;
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
#getPromptGutterWidth(width: number, paddingX: number): number {
|
|
550
|
+
if (this.#borderVisible || !this.#promptGutter) return 0;
|
|
551
|
+
const chromeWidth = 2 * this.#getHorizontalChromeWidth(paddingX);
|
|
552
|
+
const availableWidth = Math.max(0, width - chromeWidth);
|
|
553
|
+
return Math.min(visibleWidth(this.#promptGutter), availableWidth);
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
#getPromptGutter(
|
|
557
|
+
width: number,
|
|
558
|
+
paddingX: number,
|
|
559
|
+
): { firstLine: string; continuation: string; width: number } | undefined {
|
|
560
|
+
if (this.#borderVisible || !this.#promptGutter) return undefined;
|
|
561
|
+
const gutterWidth = this.#getPromptGutterWidth(width, paddingX);
|
|
562
|
+
if (gutterWidth === 0) return undefined;
|
|
563
|
+
return {
|
|
564
|
+
firstLine: sliceByColumn(this.#promptGutter, 0, gutterWidth, true),
|
|
565
|
+
continuation: padding(gutterWidth),
|
|
566
|
+
width: gutterWidth,
|
|
567
|
+
};
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
#getContentWidth(width: number, paddingX: number): number {
|
|
571
|
+
const chromeWidth = 2 * this.#getHorizontalChromeWidth(paddingX);
|
|
572
|
+
return Math.max(0, width - chromeWidth - this.#getPromptGutterWidth(width, paddingX));
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
#getLayoutWidth(width: number, paddingX: number): number {
|
|
576
|
+
const contentWidth = this.#getContentWidth(width, paddingX);
|
|
577
|
+
const cursorReserve = this.#borderVisible && paddingX === 0 ? 1 : 0;
|
|
578
|
+
// Keep cursor/scroll layout addressable even when a borderless prompt gutter consumes every visible column.
|
|
579
|
+
return Math.max(1, contentWidth - cursorReserve);
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
#getVisibleContentHeight(contentLines: number): number {
|
|
583
|
+
if (this.#maxHeight === undefined) return contentLines;
|
|
584
|
+
const verticalChrome = this.#borderVisible ? 2 : 0;
|
|
585
|
+
return Math.max(1, this.#maxHeight - verticalChrome);
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
#getStyledInputCursor(): { text: string; width: number } {
|
|
589
|
+
const cursorChar = this.#theme.symbols.inputCursor;
|
|
590
|
+
return { text: `\x1b[5m${cursorChar}\x1b[0m`, width: visibleWidth(cursorChar) };
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
#renderEndOfLineCursorAtWidthLimit(
|
|
594
|
+
before: string,
|
|
595
|
+
marker: string,
|
|
596
|
+
maxWidth: number,
|
|
597
|
+
replacement?: { text: string; width: number },
|
|
598
|
+
): { text: string; width: number } {
|
|
599
|
+
const beforeGraphemes = [...segmenter.segment(before)];
|
|
600
|
+
const lastGrapheme = beforeGraphemes[beforeGraphemes.length - 1]?.segment;
|
|
601
|
+
const lastGraphemeWidth = lastGrapheme ? visibleWidth(lastGrapheme) : 0;
|
|
602
|
+
const builtInCursor = this.#getStyledInputCursor();
|
|
603
|
+
const fallbackReplacement = lastGrapheme
|
|
604
|
+
? { text: `\x1b[7m${lastGrapheme}\x1b[0m`, width: lastGraphemeWidth }
|
|
605
|
+
: builtInCursor;
|
|
606
|
+
const clampReplacement = (candidate: { text: string; width: number }): { text: string; width: number } => {
|
|
607
|
+
let text = sliceByColumn(candidate.text, 0, maxWidth, true);
|
|
608
|
+
let width = visibleWidth(text);
|
|
609
|
+
if (width > maxWidth) {
|
|
610
|
+
text = "";
|
|
611
|
+
width = 0;
|
|
612
|
+
}
|
|
613
|
+
return { text, width };
|
|
614
|
+
};
|
|
615
|
+
|
|
616
|
+
let clampedReplacement = clampReplacement(replacement ?? fallbackReplacement);
|
|
617
|
+
if (replacement && clampedReplacement.width === 0) {
|
|
618
|
+
// A custom override that cannot fit at all should first fall back to the highlighted tail.
|
|
619
|
+
clampedReplacement = clampReplacement(fallbackReplacement);
|
|
620
|
+
}
|
|
621
|
+
if (lastGrapheme && clampedReplacement.width === 0) {
|
|
622
|
+
// If even the highlighted trailing grapheme cannot fit, show the built-in single-column cursor.
|
|
623
|
+
clampedReplacement = clampReplacement(builtInCursor);
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
const replacedSpanWidth = Math.min(maxWidth, Math.max(lastGraphemeWidth, clampedReplacement.width));
|
|
627
|
+
const prefixWidth = Math.max(0, maxWidth - replacedSpanWidth);
|
|
628
|
+
const beforePrefix = sliceByColumn(before, 0, prefixWidth, true);
|
|
629
|
+
const replacementPad = padding(Math.max(0, replacedSpanWidth - clampedReplacement.width));
|
|
630
|
+
return {
|
|
631
|
+
text: `${beforePrefix}${replacementPad}${clampedReplacement.text}${marker}`,
|
|
632
|
+
width: visibleWidth(beforePrefix) + replacedSpanWidth,
|
|
633
|
+
};
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
#renderTerminalCursorMarker(text: string, marker: string, maxWidth: number): string {
|
|
637
|
+
if (!marker) return text;
|
|
638
|
+
if (visibleWidth(text) < maxWidth) {
|
|
639
|
+
return text + marker;
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
let insertAt = text.length;
|
|
643
|
+
let offset = 0;
|
|
644
|
+
for (const seg of segmenter.segment(text)) {
|
|
645
|
+
if (visibleWidth(seg.segment) > 0) {
|
|
646
|
+
insertAt = offset;
|
|
647
|
+
}
|
|
648
|
+
offset += seg.segment.length;
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
return `${text.slice(0, insertAt)}${marker}${text.slice(insertAt)}`;
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
#getPageScrollStep(totalVisualLines: number): number {
|
|
655
|
+
const visibleHeight =
|
|
656
|
+
this.#maxHeight === undefined ? DEFAULT_PAGE_SCROLL_LINES : this.#getVisibleContentHeight(totalVisualLines);
|
|
657
|
+
return Math.max(1, visibleHeight - 1);
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
#updateScrollOffset(layoutWidth: number, layoutLines: LayoutLine[], visibleHeight: number): void {
|
|
661
|
+
if (layoutLines.length <= visibleHeight) {
|
|
662
|
+
this.#scrollOffset = 0;
|
|
663
|
+
return;
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
const visualLines = this.#buildVisualLineMap(layoutWidth);
|
|
667
|
+
const cursorLine = this.#findCurrentVisualLine(visualLines);
|
|
668
|
+
if (cursorLine < this.#scrollOffset) {
|
|
669
|
+
this.#scrollOffset = cursorLine;
|
|
670
|
+
} else if (cursorLine >= this.#scrollOffset + visibleHeight) {
|
|
671
|
+
this.#scrollOffset = cursorLine - visibleHeight + 1;
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
const maxOffset = Math.max(0, layoutLines.length - visibleHeight);
|
|
675
|
+
this.#scrollOffset = Math.min(this.#scrollOffset, maxOffset);
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
render(width: number): string[] {
|
|
679
|
+
const paddingX = this.#getEditorPaddingX();
|
|
680
|
+
const borderVisible = this.#borderVisible;
|
|
681
|
+
const promptGutter = this.#getPromptGutter(width, paddingX);
|
|
682
|
+
const contentAreaWidth = this.#getContentWidth(width, paddingX);
|
|
683
|
+
const layoutWidth = this.#getLayoutWidth(width, paddingX);
|
|
684
|
+
this.#lastLayoutWidth = layoutWidth;
|
|
685
|
+
|
|
686
|
+
// Box-drawing characters for rounded corners
|
|
687
|
+
const box = this.#theme.symbols.boxRound;
|
|
688
|
+
const borderWidth = this.#getHorizontalChromeWidth(paddingX);
|
|
689
|
+
const topLeft = this.borderColor(`${box.topLeft}${box.horizontal.repeat(paddingX)}`);
|
|
690
|
+
const topRight = this.borderColor(`${box.horizontal.repeat(paddingX)}${box.topRight}`);
|
|
691
|
+
const bottomLeft = this.borderColor(`${box.bottomLeft}${box.horizontal}${padding(Math.max(0, paddingX - 1))}`);
|
|
692
|
+
const horizontal = this.borderColor(box.horizontal);
|
|
693
|
+
|
|
694
|
+
// Layout the text
|
|
695
|
+
const layoutLines = this.#layoutText(layoutWidth);
|
|
696
|
+
const visibleContentHeight = this.#getVisibleContentHeight(layoutLines.length);
|
|
697
|
+
this.#updateScrollOffset(layoutWidth, layoutLines, visibleContentHeight);
|
|
698
|
+
const visibleLayoutLines = layoutLines.slice(this.#scrollOffset, this.#scrollOffset + visibleContentHeight);
|
|
699
|
+
|
|
700
|
+
const result: string[] = [];
|
|
701
|
+
|
|
702
|
+
if (borderVisible) {
|
|
703
|
+
// Render top border: ╭─ [status content] ────────────────╮
|
|
704
|
+
const topFillWidth = Math.max(0, width - borderWidth * 2);
|
|
705
|
+
if (this.#topBorderContent) {
|
|
706
|
+
const { content, width: statusWidth } = this.#topBorderContent;
|
|
707
|
+
if (statusWidth <= topFillWidth) {
|
|
708
|
+
// Status fits - add fill after it
|
|
709
|
+
const fillWidth = topFillWidth - statusWidth;
|
|
710
|
+
result.push(topLeft + content + this.borderColor(box.horizontal.repeat(fillWidth)) + topRight);
|
|
711
|
+
} else {
|
|
712
|
+
// Status too long - truncate it
|
|
713
|
+
const truncated = truncateToWidth(content, Math.max(0, topFillWidth - 1));
|
|
714
|
+
const truncatedWidth = visibleWidth(truncated);
|
|
715
|
+
const fillWidth = Math.max(0, topFillWidth - truncatedWidth);
|
|
716
|
+
result.push(topLeft + truncated + this.borderColor(box.horizontal.repeat(fillWidth)) + topRight);
|
|
717
|
+
}
|
|
718
|
+
} else {
|
|
719
|
+
result.push(topLeft + horizontal.repeat(topFillWidth) + topRight);
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
// Render each layout line
|
|
724
|
+
// Emit hardware cursor marker only when focused and not showing autocomplete
|
|
725
|
+
const emitCursorMarker = this.focused && !this.#autocompleteState;
|
|
726
|
+
const lineContentWidth = contentAreaWidth;
|
|
727
|
+
|
|
728
|
+
// Compute inline hint text (dim ghost text after cursor)
|
|
729
|
+
const inlineHint = this.#getInlineHint();
|
|
730
|
+
const hintStyle = this.#theme.hintStyle ?? ((t: string) => `\x1b[2m${t}\x1b[0m`);
|
|
731
|
+
|
|
732
|
+
for (let visibleIndex = 0; visibleIndex < visibleLayoutLines.length; visibleIndex++) {
|
|
733
|
+
const layoutLine = visibleLayoutLines[visibleIndex]!;
|
|
734
|
+
let displayText = layoutLine.text;
|
|
735
|
+
let displayWidth = visibleWidth(layoutLine.text);
|
|
736
|
+
let cursorInPadding = false;
|
|
737
|
+
const showPromptGutter = promptGutter !== undefined && visibleIndex === 0;
|
|
738
|
+
const gutterText =
|
|
739
|
+
promptGutter === undefined ? "" : showPromptGutter ? promptGutter.firstLine : promptGutter.continuation;
|
|
740
|
+
|
|
741
|
+
// Add cursor if this line has it
|
|
742
|
+
const hasCursor = layoutLine.hasCursor && layoutLine.cursorPos !== undefined;
|
|
743
|
+
const marker = emitCursorMarker ? CURSOR_MARKER : "";
|
|
744
|
+
|
|
745
|
+
if (!borderVisible && displayWidth > lineContentWidth) {
|
|
746
|
+
displayText = sliceByColumn(displayText, 0, lineContentWidth, true);
|
|
747
|
+
displayWidth = visibleWidth(displayText);
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
if (!borderVisible && lineContentWidth === 0) {
|
|
751
|
+
if (hasCursor && !this.#useTerminalCursor) {
|
|
752
|
+
const zeroWidthCursorBudget = visibleWidth(gutterText);
|
|
753
|
+
const zeroWidthCursorReplacement = this.cursorOverride
|
|
754
|
+
? { text: this.cursorOverride, width: this.cursorOverrideWidth ?? 1 }
|
|
755
|
+
: this.#getStyledInputCursor();
|
|
756
|
+
if (showPromptGutter && zeroWidthCursorBudget > 0) {
|
|
757
|
+
// Keep the leading prompt glyph visible when the gutter consumes the whole row.
|
|
758
|
+
const promptGlyph = [...segmenter.segment(gutterText)][0]?.segment ?? "";
|
|
759
|
+
const promptGlyphWidth = visibleWidth(promptGlyph);
|
|
760
|
+
const remainingCursorWidth = Math.max(0, zeroWidthCursorBudget - promptGlyphWidth);
|
|
761
|
+
if (remainingCursorWidth === 0) {
|
|
762
|
+
result.push(`\x1b[7m${promptGlyph}\x1b[0m${marker}`);
|
|
763
|
+
} else {
|
|
764
|
+
const widthLimitedCursor = this.#renderEndOfLineCursorAtWidthLimit(
|
|
765
|
+
"",
|
|
766
|
+
marker,
|
|
767
|
+
remainingCursorWidth,
|
|
768
|
+
zeroWidthCursorReplacement,
|
|
769
|
+
);
|
|
770
|
+
result.push(`${promptGlyph}${widthLimitedCursor.text}`);
|
|
771
|
+
}
|
|
772
|
+
} else {
|
|
773
|
+
const widthLimitedCursor = this.#renderEndOfLineCursorAtWidthLimit(
|
|
774
|
+
gutterText,
|
|
775
|
+
marker,
|
|
776
|
+
zeroWidthCursorBudget,
|
|
777
|
+
zeroWidthCursorReplacement,
|
|
778
|
+
);
|
|
779
|
+
result.push(widthLimitedCursor.text);
|
|
780
|
+
}
|
|
781
|
+
} else if (hasCursor && this.#useTerminalCursor) {
|
|
782
|
+
result.push(this.#renderTerminalCursorMarker(gutterText, marker, visibleWidth(gutterText)));
|
|
783
|
+
} else {
|
|
784
|
+
result.push(gutterText + (hasCursor ? marker : ""));
|
|
785
|
+
}
|
|
786
|
+
continue;
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
if (hasCursor && this.#useTerminalCursor) {
|
|
790
|
+
if (marker) {
|
|
791
|
+
const before = displayText.slice(0, layoutLine.cursorPos);
|
|
792
|
+
const after = displayText.slice(layoutLine.cursorPos);
|
|
793
|
+
if (after.length === 0 && inlineHint) {
|
|
794
|
+
const hintText = hintStyle(truncateToWidth(inlineHint, Math.max(0, lineContentWidth - displayWidth)));
|
|
795
|
+
displayText = before + marker + hintText;
|
|
796
|
+
displayWidth += visibleWidth(inlineHint);
|
|
797
|
+
} else if (after.length === 0 && !borderVisible && displayWidth >= lineContentWidth) {
|
|
798
|
+
displayText = this.#renderTerminalCursorMarker(before, marker, lineContentWidth);
|
|
799
|
+
} else {
|
|
800
|
+
displayText = before + marker + after;
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
} else if (hasCursor && !this.#useTerminalCursor) {
|
|
804
|
+
const before = displayText.slice(0, layoutLine.cursorPos);
|
|
805
|
+
const after = displayText.slice(layoutLine.cursorPos);
|
|
806
|
+
|
|
807
|
+
if (after.length > 0) {
|
|
808
|
+
// Cursor is on a character (grapheme) - replace it with highlighted version
|
|
809
|
+
// Get the first grapheme from 'after'
|
|
810
|
+
const afterGraphemes = [...segmenter.segment(after)];
|
|
811
|
+
const firstGrapheme = afterGraphemes[0]?.segment || "";
|
|
812
|
+
const restAfter = after.slice(firstGrapheme.length);
|
|
813
|
+
const cursor = `\x1b[7m${firstGrapheme}\x1b[0m`;
|
|
814
|
+
displayText = before + marker + cursor + restAfter;
|
|
815
|
+
// displayWidth stays the same - we're replacing, not adding
|
|
816
|
+
} else if (this.cursorOverride) {
|
|
817
|
+
// Cursor override replaces the normal end-of-text cursor glyph
|
|
818
|
+
const overrideWidth = this.cursorOverrideWidth ?? 1;
|
|
819
|
+
if (!borderVisible && displayWidth + overrideWidth > lineContentWidth) {
|
|
820
|
+
// Borderless editors have no spare padding cell for an end-of-line cursor glyph.
|
|
821
|
+
// Preserve cursorOverride by replacing the tail of the line with it.
|
|
822
|
+
const widthLimitedCursor = this.#renderEndOfLineCursorAtWidthLimit(before, marker, lineContentWidth, {
|
|
823
|
+
text: this.cursorOverride,
|
|
824
|
+
width: overrideWidth,
|
|
825
|
+
});
|
|
826
|
+
displayText = widthLimitedCursor.text;
|
|
827
|
+
displayWidth = widthLimitedCursor.width;
|
|
828
|
+
} else if (inlineHint) {
|
|
829
|
+
const availWidth = Math.max(0, lineContentWidth - displayWidth - overrideWidth);
|
|
830
|
+
const hintText = hintStyle(truncateToWidth(inlineHint, availWidth));
|
|
831
|
+
displayText = before + marker + this.cursorOverride + hintText;
|
|
832
|
+
displayWidth += overrideWidth + Math.min(visibleWidth(inlineHint), availWidth);
|
|
833
|
+
} else {
|
|
834
|
+
displayText = before + marker + this.cursorOverride;
|
|
835
|
+
displayWidth += overrideWidth;
|
|
836
|
+
}
|
|
837
|
+
} else {
|
|
838
|
+
// Cursor is at the end - add thin cursor glyph
|
|
839
|
+
const { text: cursor, width: cursorWidth } = this.#getStyledInputCursor();
|
|
840
|
+
if (!borderVisible && displayWidth + cursorWidth > lineContentWidth) {
|
|
841
|
+
// Borderless editors have no spare padding cell for an end-of-line cursor glyph.
|
|
842
|
+
// Highlight the last grapheme so the cursor stays visible without consuming width.
|
|
843
|
+
const widthLimitedCursor = this.#renderEndOfLineCursorAtWidthLimit(before, marker, lineContentWidth);
|
|
844
|
+
displayText = widthLimitedCursor.text;
|
|
845
|
+
displayWidth = widthLimitedCursor.width;
|
|
846
|
+
} else if (inlineHint) {
|
|
847
|
+
const availWidth = Math.max(0, lineContentWidth - displayWidth - cursorWidth);
|
|
848
|
+
const hintText = hintStyle(truncateToWidth(inlineHint, availWidth));
|
|
849
|
+
displayText = before + marker + cursor + hintText;
|
|
850
|
+
displayWidth += cursorWidth + Math.min(visibleWidth(inlineHint), availWidth);
|
|
851
|
+
} else {
|
|
852
|
+
displayText = before + marker + cursor;
|
|
853
|
+
displayWidth += cursorWidth;
|
|
854
|
+
}
|
|
855
|
+
if (displayWidth > lineContentWidth && paddingX > 0) {
|
|
856
|
+
cursorInPadding = true;
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
const linePad = padding(Math.max(0, lineContentWidth - displayWidth));
|
|
862
|
+
|
|
863
|
+
if (!borderVisible) {
|
|
864
|
+
result.push(gutterText + displayText + linePad);
|
|
865
|
+
continue;
|
|
866
|
+
}
|
|
867
|
+
|
|
868
|
+
// All lines have consistent borders based on padding
|
|
869
|
+
const isLastLine = visibleIndex === visibleLayoutLines.length - 1;
|
|
870
|
+
const rightPaddingWidth = Math.max(0, paddingX - (cursorInPadding ? 1 : 0));
|
|
871
|
+
if (isLastLine) {
|
|
872
|
+
const bottomRightPadding = Math.max(0, paddingX - 1 - (cursorInPadding ? 1 : 0));
|
|
873
|
+
const bottomRightAdjusted = this.borderColor(
|
|
874
|
+
`${padding(bottomRightPadding)}${box.horizontal}${box.bottomRight}`,
|
|
875
|
+
);
|
|
876
|
+
result.push(`${bottomLeft}${displayText}${linePad}${bottomRightAdjusted}`);
|
|
877
|
+
} else {
|
|
878
|
+
const leftBorder = this.borderColor(`${box.vertical}${padding(paddingX)}`);
|
|
879
|
+
const rightBorder = this.borderColor(`${padding(rightPaddingWidth)}${box.vertical}`);
|
|
880
|
+
result.push(leftBorder + displayText + linePad + rightBorder);
|
|
881
|
+
}
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
// Add autocomplete list if active
|
|
885
|
+
if (this.#autocompleteState && this.#autocompleteList) {
|
|
886
|
+
const autocompleteResult = this.#autocompleteList.render(width);
|
|
887
|
+
result.push(...autocompleteResult);
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
return result;
|
|
891
|
+
}
|
|
892
|
+
|
|
893
|
+
handleInput(data: string): void {
|
|
894
|
+
const kb = getKeybindings();
|
|
895
|
+
|
|
896
|
+
// Handle character jump mode (awaiting next character to jump to)
|
|
897
|
+
if (this.#jumpMode !== null) {
|
|
898
|
+
// Cancel if the hotkey is pressed again
|
|
899
|
+
if (kb.matches(data, "tui.editor.jumpForward") || kb.matches(data, "tui.editor.jumpBackward")) {
|
|
900
|
+
this.#jumpMode = null;
|
|
901
|
+
return;
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
const printableText = extractPrintableText(data);
|
|
905
|
+
if (printableText) {
|
|
906
|
+
const direction = this.#jumpMode;
|
|
907
|
+
this.#jumpMode = null;
|
|
908
|
+
this.#jumpToChar(printableText, direction);
|
|
909
|
+
return;
|
|
910
|
+
}
|
|
911
|
+
|
|
912
|
+
// Control character - cancel and fall through to normal handling
|
|
913
|
+
this.#jumpMode = null;
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
// Handle bracketed paste mode
|
|
917
|
+
const paste = this.#pasteHandler.process(data);
|
|
918
|
+
if (paste.handled) {
|
|
919
|
+
if (paste.pasteContent !== undefined) {
|
|
920
|
+
this.#handlePaste(paste.pasteContent);
|
|
921
|
+
if (paste.remaining.length > 0) {
|
|
922
|
+
this.handleInput(paste.remaining);
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
return;
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
// Handle special key combinations first
|
|
929
|
+
|
|
930
|
+
// Ctrl+C is reserved by parent components for app-level handling.
|
|
931
|
+
// Do not consume arbitrary user-bound "copy" keys here, since the editor
|
|
932
|
+
// has no copy implementation and would make those keys disappear.
|
|
933
|
+
if (matchesKey(data, "ctrl+c")) {
|
|
934
|
+
return;
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
// Undo
|
|
938
|
+
if (kb.matches(data, "tui.editor.undo")) {
|
|
939
|
+
this.#applyUndo();
|
|
940
|
+
return;
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
// Handle autocomplete special keys first (but don't block other input)
|
|
944
|
+
if (this.#autocompleteState && this.#autocompleteList) {
|
|
945
|
+
// Escape - cancel autocomplete
|
|
946
|
+
if (kb.matches(data, "tui.select.cancel")) {
|
|
947
|
+
this.#cancelAutocomplete(true);
|
|
948
|
+
return;
|
|
949
|
+
}
|
|
950
|
+
// Let the autocomplete list handle navigation and selection
|
|
951
|
+
else if (
|
|
952
|
+
kb.matches(data, "tui.select.up") ||
|
|
953
|
+
kb.matches(data, "tui.select.down") ||
|
|
954
|
+
kb.matches(data, "tui.select.pageUp") ||
|
|
955
|
+
kb.matches(data, "tui.select.pageDown") ||
|
|
956
|
+
kb.matches(data, "tui.input.submit") ||
|
|
957
|
+
data === "\n" ||
|
|
958
|
+
kb.matches(data, "tui.input.tab")
|
|
959
|
+
) {
|
|
960
|
+
// Only pass navigation keys to the list, not Enter/Tab (we handle those directly)
|
|
961
|
+
if (
|
|
962
|
+
kb.matches(data, "tui.select.up") ||
|
|
963
|
+
kb.matches(data, "tui.select.down") ||
|
|
964
|
+
kb.matches(data, "tui.select.pageUp") ||
|
|
965
|
+
kb.matches(data, "tui.select.pageDown")
|
|
966
|
+
) {
|
|
967
|
+
this.#autocompleteList.handleInput(data);
|
|
968
|
+
this.onAutocompleteUpdate?.();
|
|
969
|
+
return;
|
|
970
|
+
}
|
|
971
|
+
|
|
972
|
+
// If Tab was pressed, always apply the selection
|
|
973
|
+
if (kb.matches(data, "tui.input.tab")) {
|
|
974
|
+
const selected = this.#autocompleteList.getSelectedItem();
|
|
975
|
+
if (selected && this.#autocompleteProvider) {
|
|
976
|
+
const shouldChainSlashCommandAutocomplete = this.#isSlashCommandNameAutocompleteSelection();
|
|
977
|
+
const result = this.#autocompleteProvider.applyCompletion(
|
|
978
|
+
this.#state.lines,
|
|
979
|
+
this.#state.cursorLine,
|
|
980
|
+
this.#state.cursorCol,
|
|
981
|
+
selected,
|
|
982
|
+
this.#autocompletePrefix,
|
|
983
|
+
);
|
|
984
|
+
|
|
985
|
+
this.#state.lines = result.lines;
|
|
986
|
+
this.#state.cursorLine = result.cursorLine;
|
|
987
|
+
this.#setCursorCol(result.cursorCol);
|
|
988
|
+
|
|
989
|
+
this.#cancelAutocomplete();
|
|
990
|
+
|
|
991
|
+
if (this.onChange) {
|
|
992
|
+
this.onChange(this.getText());
|
|
993
|
+
}
|
|
994
|
+
|
|
995
|
+
result.onApplied?.();
|
|
996
|
+
|
|
997
|
+
if (shouldChainSlashCommandAutocomplete && this.#isCompletedSlashCommandAtCursor()) {
|
|
998
|
+
void this.#tryTriggerAutocomplete();
|
|
999
|
+
}
|
|
1000
|
+
}
|
|
1001
|
+
return;
|
|
1002
|
+
}
|
|
1003
|
+
|
|
1004
|
+
// If Enter was pressed on a slash command, apply completion and submit
|
|
1005
|
+
if ((kb.matches(data, "tui.input.submit") || data === "\n") && this.#autocompletePrefix.startsWith("/")) {
|
|
1006
|
+
// Check for stale autocomplete state due to debounce
|
|
1007
|
+
const currentLine = this.#state.lines[this.#state.cursorLine] ?? "";
|
|
1008
|
+
const currentTextBeforeCursor = currentLine.slice(0, this.#state.cursorCol);
|
|
1009
|
+
if (currentTextBeforeCursor !== this.#autocompletePrefix) {
|
|
1010
|
+
// Autocomplete is stale - cancel and fall through to normal submission
|
|
1011
|
+
this.#cancelAutocomplete();
|
|
1012
|
+
} else {
|
|
1013
|
+
const selected = this.#autocompleteList.getSelectedItem();
|
|
1014
|
+
if (selected && this.#autocompleteProvider) {
|
|
1015
|
+
const result = this.#autocompleteProvider.applyCompletion(
|
|
1016
|
+
this.#state.lines,
|
|
1017
|
+
this.#state.cursorLine,
|
|
1018
|
+
this.#state.cursorCol,
|
|
1019
|
+
selected,
|
|
1020
|
+
this.#autocompletePrefix,
|
|
1021
|
+
);
|
|
1022
|
+
|
|
1023
|
+
this.#state.lines = result.lines;
|
|
1024
|
+
this.#state.cursorLine = result.cursorLine;
|
|
1025
|
+
this.#setCursorCol(result.cursorCol);
|
|
1026
|
+
result.onApplied?.();
|
|
1027
|
+
}
|
|
1028
|
+
this.#cancelAutocomplete();
|
|
1029
|
+
}
|
|
1030
|
+
// Don't return - fall through to submission logic
|
|
1031
|
+
}
|
|
1032
|
+
// If Enter was pressed on a file path, apply completion
|
|
1033
|
+
else if (kb.matches(data, "tui.input.submit") || data === "\n") {
|
|
1034
|
+
const selected = this.#autocompleteList.getSelectedItem();
|
|
1035
|
+
if (selected && this.#autocompleteProvider) {
|
|
1036
|
+
const result = this.#autocompleteProvider.applyCompletion(
|
|
1037
|
+
this.#state.lines,
|
|
1038
|
+
this.#state.cursorLine,
|
|
1039
|
+
this.#state.cursorCol,
|
|
1040
|
+
selected,
|
|
1041
|
+
this.#autocompletePrefix,
|
|
1042
|
+
);
|
|
1043
|
+
|
|
1044
|
+
this.#state.lines = result.lines;
|
|
1045
|
+
this.#state.cursorLine = result.cursorLine;
|
|
1046
|
+
this.#setCursorCol(result.cursorCol);
|
|
1047
|
+
|
|
1048
|
+
this.#cancelAutocomplete();
|
|
1049
|
+
|
|
1050
|
+
if (this.onChange) {
|
|
1051
|
+
this.onChange(this.getText());
|
|
1052
|
+
}
|
|
1053
|
+
|
|
1054
|
+
result.onApplied?.();
|
|
1055
|
+
}
|
|
1056
|
+
return;
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
1059
|
+
// For other keys (like regular typing), DON'T return here
|
|
1060
|
+
// Let them fall through to normal character handling
|
|
1061
|
+
}
|
|
1062
|
+
|
|
1063
|
+
// Tab key - context-aware completion (but not when already autocompleting)
|
|
1064
|
+
if (kb.matches(data, "tui.input.tab") && !this.#autocompleteState) {
|
|
1065
|
+
this.#handleTabCompletion();
|
|
1066
|
+
return;
|
|
1067
|
+
}
|
|
1068
|
+
|
|
1069
|
+
// Continue with rest of input handling
|
|
1070
|
+
// Ctrl+K - Delete to end of line
|
|
1071
|
+
if (matchesKey(data, "ctrl+k")) {
|
|
1072
|
+
this.#deleteToEndOfLine();
|
|
1073
|
+
}
|
|
1074
|
+
// Ctrl+U - Delete to start of line
|
|
1075
|
+
else if (matchesKey(data, "ctrl+u")) {
|
|
1076
|
+
this.#deleteToStartOfLine();
|
|
1077
|
+
}
|
|
1078
|
+
// Ctrl+W - Delete word backwards
|
|
1079
|
+
else if (matchesKey(data, "ctrl+w")) {
|
|
1080
|
+
this.#deleteWordBackwards();
|
|
1081
|
+
}
|
|
1082
|
+
// Option/Alt+Backspace - Delete word backwards
|
|
1083
|
+
else if (matchesKey(data, "alt+backspace")) {
|
|
1084
|
+
this.#deleteWordBackwards();
|
|
1085
|
+
}
|
|
1086
|
+
// Option/Alt+D - Delete word forwards
|
|
1087
|
+
else if (matchesKey(data, "alt+d") || matchesKey(data, "alt+delete")) {
|
|
1088
|
+
this.#deleteWordForwards();
|
|
1089
|
+
}
|
|
1090
|
+
// Ctrl+Y - Yank from kill ring
|
|
1091
|
+
else if (matchesKey(data, "ctrl+y")) {
|
|
1092
|
+
this.#yankFromKillRing();
|
|
1093
|
+
}
|
|
1094
|
+
// Alt+Y - Yank-pop (cycle kill ring)
|
|
1095
|
+
else if (matchesKey(data, "alt+y")) {
|
|
1096
|
+
this.#yankPop();
|
|
1097
|
+
}
|
|
1098
|
+
// Ctrl+A - Move to start of line
|
|
1099
|
+
else if (matchesKey(data, "ctrl+a")) {
|
|
1100
|
+
this.#moveToLineStart();
|
|
1101
|
+
}
|
|
1102
|
+
// Ctrl+E - Move to end of line
|
|
1103
|
+
else if (matchesKey(data, "ctrl+e")) {
|
|
1104
|
+
this.#moveToLineEnd();
|
|
1105
|
+
}
|
|
1106
|
+
// Alt+Enter - special handler if callback exists, otherwise new line
|
|
1107
|
+
else if (matchesKey(data, "alt+enter")) {
|
|
1108
|
+
if (this.onAltEnter) {
|
|
1109
|
+
this.onAltEnter(this.getText());
|
|
1110
|
+
} else {
|
|
1111
|
+
this.#addNewLine();
|
|
1112
|
+
}
|
|
1113
|
+
}
|
|
1114
|
+
// New line
|
|
1115
|
+
else if (
|
|
1116
|
+
(data.charCodeAt(0) === 10 && data.length > 1) || // Ctrl+Enter with modifiers
|
|
1117
|
+
data === "\x1b[13;5u" || // Ctrl+Enter (Kitty protocol)
|
|
1118
|
+
data === "\x1b[27;5;13~" || // Ctrl+Enter (legacy format)
|
|
1119
|
+
data === "\x1b\r" || // Option+Enter in some terminals (legacy)
|
|
1120
|
+
data === "\x1b[13;2~" || // Shift+Enter in some terminals (legacy format)
|
|
1121
|
+
kb.matches(data, "tui.input.newLine") || // Shift+Enter (Kitty protocol, handles lock bits)
|
|
1122
|
+
(data.length > 1 && data.includes("\x1b") && data.includes("\r")) ||
|
|
1123
|
+
(data === "\n" && data.length === 1) // Shift+Enter from iTerm2 mapping
|
|
1124
|
+
) {
|
|
1125
|
+
if (this.#shouldSubmitOnBackslashEnter(data, kb)) {
|
|
1126
|
+
this.#handleBackspace();
|
|
1127
|
+
this.#submitValue();
|
|
1128
|
+
return;
|
|
1129
|
+
}
|
|
1130
|
+
this.#addNewLine();
|
|
1131
|
+
}
|
|
1132
|
+
// Plain Enter - submit (handles both legacy \r and Kitty protocol with lock bits)
|
|
1133
|
+
else if (kb.matches(data, "tui.input.submit") || data === "\n") {
|
|
1134
|
+
// If submit is disabled, do nothing
|
|
1135
|
+
if (this.disableSubmit) {
|
|
1136
|
+
return;
|
|
1137
|
+
}
|
|
1138
|
+
|
|
1139
|
+
this.#submitValue();
|
|
1140
|
+
}
|
|
1141
|
+
// Backspace (including Shift+Backspace)
|
|
1142
|
+
else if (kb.matches(data, "tui.editor.deleteCharBackward") || matchesKey(data, "shift+backspace")) {
|
|
1143
|
+
this.#handleBackspace();
|
|
1144
|
+
}
|
|
1145
|
+
// Line navigation shortcuts (Home/End keys)
|
|
1146
|
+
else if (kb.matches(data, "tui.editor.cursorLineStart")) {
|
|
1147
|
+
this.#moveToLineStart();
|
|
1148
|
+
} else if (kb.matches(data, "tui.editor.cursorLineEnd")) {
|
|
1149
|
+
this.#moveToLineEnd();
|
|
1150
|
+
}
|
|
1151
|
+
// Page navigation (PageUp/PageDown)
|
|
1152
|
+
else if (kb.matches(data, "tui.editor.pageUp")) {
|
|
1153
|
+
if (this.#isEditorEmpty()) {
|
|
1154
|
+
this.#navigateHistory(-1);
|
|
1155
|
+
} else if (this.#historyIndex > -1 && this.#isOnFirstVisualLine()) {
|
|
1156
|
+
this.#navigateHistory(-1);
|
|
1157
|
+
} else {
|
|
1158
|
+
this.#pageScroll(-1);
|
|
1159
|
+
}
|
|
1160
|
+
} else if (kb.matches(data, "tui.editor.pageDown")) {
|
|
1161
|
+
if (this.#historyIndex > -1 && this.#isOnLastVisualLine()) {
|
|
1162
|
+
this.#navigateHistory(1);
|
|
1163
|
+
} else {
|
|
1164
|
+
this.#pageScroll(1);
|
|
1165
|
+
}
|
|
1166
|
+
}
|
|
1167
|
+
// Forward delete (Fn+Backspace or Delete key, including Shift+Delete)
|
|
1168
|
+
else if (kb.matches(data, "tui.editor.deleteCharForward") || matchesKey(data, "shift+delete")) {
|
|
1169
|
+
this.#handleForwardDelete();
|
|
1170
|
+
}
|
|
1171
|
+
// Word navigation (Option/Alt + Arrow or Ctrl + Arrow)
|
|
1172
|
+
else if (kb.matches(data, "tui.editor.cursorWordLeft")) {
|
|
1173
|
+
// Word left
|
|
1174
|
+
this.#resetKillSequence();
|
|
1175
|
+
this.#moveWordBackwards();
|
|
1176
|
+
} else if (kb.matches(data, "tui.editor.cursorWordRight")) {
|
|
1177
|
+
// Word right
|
|
1178
|
+
this.#resetKillSequence();
|
|
1179
|
+
this.#moveWordForwards();
|
|
1180
|
+
}
|
|
1181
|
+
// Arrow keys
|
|
1182
|
+
else if (kb.matches(data, "tui.editor.cursorUp")) {
|
|
1183
|
+
// Up - history navigation or cursor movement
|
|
1184
|
+
if (this.#isEditorEmpty()) {
|
|
1185
|
+
this.#navigateHistory(-1); // Start browsing history
|
|
1186
|
+
} else if (this.#historyIndex > -1 && this.#isOnFirstVisualLine()) {
|
|
1187
|
+
this.#navigateHistory(-1); // Navigate to older history entry
|
|
1188
|
+
} else if (this.#isOnFirstVisualLine()) {
|
|
1189
|
+
// Already at top - jump to start of line
|
|
1190
|
+
this.#moveToLineStart();
|
|
1191
|
+
} else {
|
|
1192
|
+
this.#moveCursor(-1, 0); // Cursor movement (within text or history entry)
|
|
1193
|
+
}
|
|
1194
|
+
} else if (kb.matches(data, "tui.editor.cursorDown")) {
|
|
1195
|
+
// Down - history navigation or cursor movement
|
|
1196
|
+
if (this.#historyIndex > -1 && this.#isOnLastVisualLine()) {
|
|
1197
|
+
this.#navigateHistory(1); // Navigate to newer history entry or clear
|
|
1198
|
+
} else if (this.#isOnLastVisualLine()) {
|
|
1199
|
+
// Already at bottom - jump to end of line
|
|
1200
|
+
this.#moveToLineEnd();
|
|
1201
|
+
} else {
|
|
1202
|
+
this.#moveCursor(1, 0); // Cursor movement (within text or history entry)
|
|
1203
|
+
}
|
|
1204
|
+
} else if (kb.matches(data, "tui.editor.cursorRight")) {
|
|
1205
|
+
// Right
|
|
1206
|
+
this.#moveCursor(0, 1);
|
|
1207
|
+
} else if (kb.matches(data, "tui.editor.cursorLeft")) {
|
|
1208
|
+
// Left
|
|
1209
|
+
this.#moveCursor(0, -1);
|
|
1210
|
+
}
|
|
1211
|
+
// Shift+Space - insert regular space (Kitty protocol sends escape sequence)
|
|
1212
|
+
else if (matchesKey(data, "shift+space")) {
|
|
1213
|
+
this.#insertCharacter(" ");
|
|
1214
|
+
}
|
|
1215
|
+
// Character jump mode triggers
|
|
1216
|
+
else if (kb.matches(data, "tui.editor.jumpForward")) {
|
|
1217
|
+
this.#jumpMode = "forward";
|
|
1218
|
+
} else if (kb.matches(data, "tui.editor.jumpBackward")) {
|
|
1219
|
+
this.#jumpMode = "backward";
|
|
1220
|
+
}
|
|
1221
|
+
// Printable keystrokes, including Kitty CSI-u text-producing sequences.
|
|
1222
|
+
else {
|
|
1223
|
+
const printableText = extractPrintableText(data);
|
|
1224
|
+
if (printableText) {
|
|
1225
|
+
this.#insertCharacter(printableText);
|
|
1226
|
+
}
|
|
1227
|
+
}
|
|
1228
|
+
}
|
|
1229
|
+
|
|
1230
|
+
#layoutText(contentWidth: number): LayoutLine[] {
|
|
1231
|
+
const layoutLines: LayoutLine[] = [];
|
|
1232
|
+
|
|
1233
|
+
if (this.#state.lines.length === 0 || (this.#state.lines.length === 1 && this.#state.lines[0] === "")) {
|
|
1234
|
+
// Empty editor
|
|
1235
|
+
layoutLines.push({
|
|
1236
|
+
text: "",
|
|
1237
|
+
hasCursor: true,
|
|
1238
|
+
cursorPos: 0,
|
|
1239
|
+
});
|
|
1240
|
+
return layoutLines;
|
|
1241
|
+
}
|
|
1242
|
+
|
|
1243
|
+
// Process each logical line
|
|
1244
|
+
for (let i = 0; i < this.#state.lines.length; i++) {
|
|
1245
|
+
const line = this.#state.lines[i] || "";
|
|
1246
|
+
const isCurrentLine = i === this.#state.cursorLine;
|
|
1247
|
+
const lineVisibleWidth = visibleWidth(line);
|
|
1248
|
+
|
|
1249
|
+
if (lineVisibleWidth <= contentWidth) {
|
|
1250
|
+
// Line fits in one layout line
|
|
1251
|
+
if (isCurrentLine) {
|
|
1252
|
+
layoutLines.push({
|
|
1253
|
+
text: line,
|
|
1254
|
+
hasCursor: true,
|
|
1255
|
+
cursorPos: this.#state.cursorCol,
|
|
1256
|
+
});
|
|
1257
|
+
} else {
|
|
1258
|
+
layoutLines.push({
|
|
1259
|
+
text: line,
|
|
1260
|
+
hasCursor: false,
|
|
1261
|
+
});
|
|
1262
|
+
}
|
|
1263
|
+
} else {
|
|
1264
|
+
// Line needs wrapping - use word-aware wrapping
|
|
1265
|
+
const chunks = wordWrapLine(line, contentWidth);
|
|
1266
|
+
|
|
1267
|
+
for (let chunkIndex = 0; chunkIndex < chunks.length; chunkIndex++) {
|
|
1268
|
+
const chunk = chunks[chunkIndex];
|
|
1269
|
+
if (!chunk) continue;
|
|
1270
|
+
|
|
1271
|
+
const cursorPos = this.#state.cursorCol;
|
|
1272
|
+
const isLastChunk = chunkIndex === chunks.length - 1;
|
|
1273
|
+
|
|
1274
|
+
// Determine if cursor is in this chunk
|
|
1275
|
+
// For word-wrapped chunks, we need to handle the case where
|
|
1276
|
+
// cursor might be in trimmed whitespace at end of chunk
|
|
1277
|
+
let hasCursorInChunk = false;
|
|
1278
|
+
let adjustedCursorPos = 0;
|
|
1279
|
+
|
|
1280
|
+
if (isCurrentLine) {
|
|
1281
|
+
if (isLastChunk) {
|
|
1282
|
+
// Last chunk: cursor belongs here if >= startIndex
|
|
1283
|
+
hasCursorInChunk = cursorPos >= chunk.startIndex;
|
|
1284
|
+
adjustedCursorPos = cursorPos - chunk.startIndex;
|
|
1285
|
+
} else {
|
|
1286
|
+
// Non-last chunk: cursor belongs here if in range [startIndex, endIndex)
|
|
1287
|
+
// But we need to handle the visual position in the trimmed text
|
|
1288
|
+
hasCursorInChunk = cursorPos >= chunk.startIndex && cursorPos < chunk.endIndex;
|
|
1289
|
+
if (hasCursorInChunk) {
|
|
1290
|
+
adjustedCursorPos = cursorPos - chunk.startIndex;
|
|
1291
|
+
// Clamp to text length (in case cursor was in trimmed whitespace)
|
|
1292
|
+
if (adjustedCursorPos > chunk.text.length) {
|
|
1293
|
+
adjustedCursorPos = chunk.text.length;
|
|
1294
|
+
}
|
|
1295
|
+
}
|
|
1296
|
+
}
|
|
1297
|
+
}
|
|
1298
|
+
|
|
1299
|
+
if (hasCursorInChunk) {
|
|
1300
|
+
layoutLines.push({
|
|
1301
|
+
text: chunk.text,
|
|
1302
|
+
hasCursor: true,
|
|
1303
|
+
cursorPos: adjustedCursorPos,
|
|
1304
|
+
});
|
|
1305
|
+
} else {
|
|
1306
|
+
layoutLines.push({
|
|
1307
|
+
text: chunk.text,
|
|
1308
|
+
hasCursor: false,
|
|
1309
|
+
});
|
|
1310
|
+
}
|
|
1311
|
+
}
|
|
1312
|
+
}
|
|
1313
|
+
}
|
|
1314
|
+
|
|
1315
|
+
return layoutLines;
|
|
1316
|
+
}
|
|
1317
|
+
|
|
1318
|
+
getText(): string {
|
|
1319
|
+
return this.#state.lines.join("\n");
|
|
1320
|
+
}
|
|
1321
|
+
|
|
1322
|
+
#expandPasteMarkers(text: string): string {
|
|
1323
|
+
let result = text;
|
|
1324
|
+
for (const [pasteId, pasteContent] of this.#pastes) {
|
|
1325
|
+
const markerRegex = new RegExp(`\\[paste #${pasteId}( (\\+\\d+ lines|\\d+ chars))?\\]`, "g");
|
|
1326
|
+
result = result.replace(markerRegex, () => pasteContent);
|
|
1327
|
+
}
|
|
1328
|
+
return result;
|
|
1329
|
+
}
|
|
1330
|
+
|
|
1331
|
+
/**
|
|
1332
|
+
* Get text with paste markers expanded to their actual content.
|
|
1333
|
+
* Use this when you need the full content (e.g., for external editor).
|
|
1334
|
+
*/
|
|
1335
|
+
getExpandedText(): string {
|
|
1336
|
+
return this.#expandPasteMarkers(this.#state.lines.join("\n"));
|
|
1337
|
+
}
|
|
1338
|
+
|
|
1339
|
+
getLines(): string[] {
|
|
1340
|
+
return [...this.#state.lines];
|
|
1341
|
+
}
|
|
1342
|
+
|
|
1343
|
+
getCursor(): { line: number; col: number } {
|
|
1344
|
+
return { line: this.#state.cursorLine, col: this.#state.cursorCol };
|
|
1345
|
+
}
|
|
1346
|
+
|
|
1347
|
+
moveToLineStart(): void {
|
|
1348
|
+
this.#moveToLineStart();
|
|
1349
|
+
}
|
|
1350
|
+
|
|
1351
|
+
moveToLineEnd(): void {
|
|
1352
|
+
this.#moveToLineEnd();
|
|
1353
|
+
}
|
|
1354
|
+
|
|
1355
|
+
moveToMessageStart(): void {
|
|
1356
|
+
this.#moveToMessageStart();
|
|
1357
|
+
}
|
|
1358
|
+
|
|
1359
|
+
moveToMessageEnd(): void {
|
|
1360
|
+
this.#moveToMessageEnd();
|
|
1361
|
+
}
|
|
1362
|
+
|
|
1363
|
+
/**
|
|
1364
|
+
* Undo the last meaningful edit while ignoring transient text that is still present at the cursor.
|
|
1365
|
+
* Used for command-like autocomplete actions whose typed trigger should not count as the edit being undone.
|
|
1366
|
+
*/
|
|
1367
|
+
undoPastTransientText(transientText: string): void {
|
|
1368
|
+
if (transientText.length === 0) {
|
|
1369
|
+
this.#applyUndo();
|
|
1370
|
+
return;
|
|
1371
|
+
}
|
|
1372
|
+
|
|
1373
|
+
const currentLine = this.#state.lines[this.#state.cursorLine] || "";
|
|
1374
|
+
const transientStartCol = this.#state.cursorCol - transientText.length;
|
|
1375
|
+
if (transientStartCol < 0 || currentLine.slice(transientStartCol, this.#state.cursorCol) !== transientText) {
|
|
1376
|
+
this.#applyUndo();
|
|
1377
|
+
return;
|
|
1378
|
+
}
|
|
1379
|
+
|
|
1380
|
+
const beforeTransient = currentLine.slice(0, transientStartCol);
|
|
1381
|
+
const afterTransient = currentLine.slice(this.#state.cursorCol);
|
|
1382
|
+
this.#historyIndex = -1;
|
|
1383
|
+
this.#resetKillSequence();
|
|
1384
|
+
this.#preferredVisualCol = null;
|
|
1385
|
+
this.#state.lines[this.#state.cursorLine] = beforeTransient + afterTransient;
|
|
1386
|
+
this.#setCursorCol(transientStartCol);
|
|
1387
|
+
|
|
1388
|
+
while (true) {
|
|
1389
|
+
const snapshot = this.#undoStack.at(-1);
|
|
1390
|
+
if (
|
|
1391
|
+
!snapshot ||
|
|
1392
|
+
!this.#matchesTransientUndoSnapshot(
|
|
1393
|
+
snapshot,
|
|
1394
|
+
transientText,
|
|
1395
|
+
transientStartCol,
|
|
1396
|
+
beforeTransient,
|
|
1397
|
+
afterTransient,
|
|
1398
|
+
)
|
|
1399
|
+
) {
|
|
1400
|
+
break;
|
|
1401
|
+
}
|
|
1402
|
+
this.#undoStack.pop();
|
|
1403
|
+
}
|
|
1404
|
+
|
|
1405
|
+
if (this.#undoStack.length === 0) {
|
|
1406
|
+
if (this.onChange) {
|
|
1407
|
+
this.onChange(this.getText());
|
|
1408
|
+
}
|
|
1409
|
+
return;
|
|
1410
|
+
}
|
|
1411
|
+
|
|
1412
|
+
this.#applyUndo();
|
|
1413
|
+
}
|
|
1414
|
+
|
|
1415
|
+
setText(text: string): void {
|
|
1416
|
+
this.#historyIndex = -1; // Exit history browsing mode
|
|
1417
|
+
this.#resetKillSequence();
|
|
1418
|
+
this.#setTextInternal(text);
|
|
1419
|
+
}
|
|
1420
|
+
|
|
1421
|
+
#exitHistoryForEditing(): void {
|
|
1422
|
+
if (this.#historyIndex === -1) return;
|
|
1423
|
+
if (this.#state.cursorLine === 0 && this.#state.cursorCol === 0) {
|
|
1424
|
+
this.#state.cursorLine = this.#state.lines.length - 1;
|
|
1425
|
+
const line = this.#state.lines[this.#state.cursorLine] || "";
|
|
1426
|
+
this.#setCursorCol(line.length);
|
|
1427
|
+
}
|
|
1428
|
+
this.#historyIndex = -1;
|
|
1429
|
+
}
|
|
1430
|
+
|
|
1431
|
+
/** Insert text at the current cursor position */
|
|
1432
|
+
insertText(text: string): void {
|
|
1433
|
+
this.#exitHistoryForEditing();
|
|
1434
|
+
this.#resetKillSequence();
|
|
1435
|
+
this.#recordUndoState();
|
|
1436
|
+
|
|
1437
|
+
const line = this.#state.lines[this.#state.cursorLine] || "";
|
|
1438
|
+
const before = line.slice(0, this.#state.cursorCol);
|
|
1439
|
+
const after = line.slice(this.#state.cursorCol);
|
|
1440
|
+
|
|
1441
|
+
this.#state.lines[this.#state.cursorLine] = before + text + after;
|
|
1442
|
+
this.#setCursorCol(this.#state.cursorCol + text.length);
|
|
1443
|
+
|
|
1444
|
+
if (this.onChange) {
|
|
1445
|
+
this.onChange(this.getText());
|
|
1446
|
+
}
|
|
1447
|
+
}
|
|
1448
|
+
|
|
1449
|
+
// All the editor methods from before...
|
|
1450
|
+
#insertCharacter(char: string): void {
|
|
1451
|
+
this.#exitHistoryForEditing();
|
|
1452
|
+
this.#resetKillSequence();
|
|
1453
|
+
this.#recordUndoState();
|
|
1454
|
+
|
|
1455
|
+
const line = this.#state.lines[this.#state.cursorLine] || "";
|
|
1456
|
+
|
|
1457
|
+
const before = line.slice(0, this.#state.cursorCol);
|
|
1458
|
+
const after = line.slice(this.#state.cursorCol);
|
|
1459
|
+
|
|
1460
|
+
this.#state.lines[this.#state.cursorLine] = before + char + after;
|
|
1461
|
+
this.#setCursorCol(this.#state.cursorCol + char.length);
|
|
1462
|
+
|
|
1463
|
+
if (this.onChange) {
|
|
1464
|
+
this.onChange(this.getText());
|
|
1465
|
+
}
|
|
1466
|
+
|
|
1467
|
+
// Check if we should trigger or update autocomplete
|
|
1468
|
+
if (!this.#autocompleteState) {
|
|
1469
|
+
// Auto-trigger for "/" at the start of a line (slash commands)
|
|
1470
|
+
if (char === "/" && this.#isAtStartOfMessage()) {
|
|
1471
|
+
this.#tryTriggerAutocomplete();
|
|
1472
|
+
}
|
|
1473
|
+
// Auto-trigger for "@" file reference (fuzzy search)
|
|
1474
|
+
else if (char === "@") {
|
|
1475
|
+
const currentLine = this.#state.lines[this.#state.cursorLine] || "";
|
|
1476
|
+
const textBeforeCursor = currentLine.slice(0, this.#state.cursorCol);
|
|
1477
|
+
// Only trigger if @ is after whitespace or at start of line
|
|
1478
|
+
const charBeforeAt = textBeforeCursor[textBeforeCursor.length - 2];
|
|
1479
|
+
if (textBeforeCursor.length === 1 || charBeforeAt === " " || charBeforeAt === "\t") {
|
|
1480
|
+
this.#tryTriggerAutocomplete();
|
|
1481
|
+
}
|
|
1482
|
+
}
|
|
1483
|
+
// Auto-trigger for "#" prompt actions anywhere in the current token
|
|
1484
|
+
else if (char === "#") {
|
|
1485
|
+
this.#tryTriggerAutocomplete();
|
|
1486
|
+
}
|
|
1487
|
+
// Also auto-trigger when typing letters/path chars in a completable context
|
|
1488
|
+
else if (/[a-zA-Z0-9.\-_/]/.test(char)) {
|
|
1489
|
+
const currentLine = this.#state.lines[this.#state.cursorLine] || "";
|
|
1490
|
+
const textBeforeCursor = currentLine.slice(0, this.#state.cursorCol);
|
|
1491
|
+
// Check if we're in a slash command (with or without space for arguments)
|
|
1492
|
+
if (textBeforeCursor.trimStart().startsWith("/")) {
|
|
1493
|
+
this.#tryTriggerAutocomplete();
|
|
1494
|
+
}
|
|
1495
|
+
// Check if we're in an @ file reference context
|
|
1496
|
+
else if (textBeforeCursor.match(/(?:^|[\s])@[^\s]*$/)) {
|
|
1497
|
+
this.#tryTriggerAutocomplete();
|
|
1498
|
+
}
|
|
1499
|
+
// Check if we're in a # prompt action context
|
|
1500
|
+
else if (textBeforeCursor.match(/#[^\s#]*$/)) {
|
|
1501
|
+
this.#tryTriggerAutocomplete();
|
|
1502
|
+
}
|
|
1503
|
+
}
|
|
1504
|
+
} else {
|
|
1505
|
+
this.#debouncedUpdateAutocomplete();
|
|
1506
|
+
}
|
|
1507
|
+
}
|
|
1508
|
+
|
|
1509
|
+
#handlePaste(pastedText: string): void {
|
|
1510
|
+
this.#historyIndex = -1; // Exit history browsing mode
|
|
1511
|
+
this.#resetKillSequence();
|
|
1512
|
+
this.#recordUndoState();
|
|
1513
|
+
|
|
1514
|
+
this.#withUndoSuspended(() => {
|
|
1515
|
+
// Clean the pasted text
|
|
1516
|
+
const cleanText = pastedText.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
|
|
1517
|
+
|
|
1518
|
+
// Convert tabs to spaces (4 spaces per tab)
|
|
1519
|
+
const tabExpandedText = cleanText.replace(/\t/g, " ");
|
|
1520
|
+
|
|
1521
|
+
// Filter out non-printable characters except newlines
|
|
1522
|
+
let filteredText = tabExpandedText
|
|
1523
|
+
.split("")
|
|
1524
|
+
.filter(char => char === "\n" || char.charCodeAt(0) >= 32)
|
|
1525
|
+
.join("");
|
|
1526
|
+
|
|
1527
|
+
// If pasting a file path (starts with /, ~, or .) and the character before
|
|
1528
|
+
// the cursor is a word character, prepend a space for better readability
|
|
1529
|
+
if (/^[/~.]/.test(filteredText)) {
|
|
1530
|
+
const currentLine = this.#state.lines[this.#state.cursorLine] || "";
|
|
1531
|
+
const charBeforeCursor = this.#state.cursorCol > 0 ? currentLine[this.#state.cursorCol - 1] : "";
|
|
1532
|
+
if (charBeforeCursor && /\w/.test(charBeforeCursor)) {
|
|
1533
|
+
filteredText = ` ${filteredText}`;
|
|
1534
|
+
}
|
|
1535
|
+
}
|
|
1536
|
+
|
|
1537
|
+
// Split into lines
|
|
1538
|
+
const pastedLines = filteredText.split("\n");
|
|
1539
|
+
|
|
1540
|
+
// Check if this is a large paste (> 10 lines or > 1000 characters)
|
|
1541
|
+
const totalChars = filteredText.length;
|
|
1542
|
+
if (pastedLines.length > 10 || totalChars > 1000) {
|
|
1543
|
+
// Store the paste and insert a marker
|
|
1544
|
+
this.#pasteCounter++;
|
|
1545
|
+
const pasteId = this.#pasteCounter;
|
|
1546
|
+
this.#pastes.set(pasteId, filteredText);
|
|
1547
|
+
|
|
1548
|
+
// Insert marker like "[paste #1 +123 lines]" or "[paste #1 1234 chars]"
|
|
1549
|
+
const marker =
|
|
1550
|
+
pastedLines.length > 10
|
|
1551
|
+
? `[paste #${pasteId} +${pastedLines.length} lines]`
|
|
1552
|
+
: `[paste #${pasteId} ${totalChars} chars]`;
|
|
1553
|
+
this.#insertTextAtCursor(marker);
|
|
1554
|
+
|
|
1555
|
+
return;
|
|
1556
|
+
}
|
|
1557
|
+
|
|
1558
|
+
if (pastedLines.length === 1) {
|
|
1559
|
+
// Single line - insert character by character to trigger autocomplete
|
|
1560
|
+
for (const char of filteredText) {
|
|
1561
|
+
this.#insertCharacter(char);
|
|
1562
|
+
}
|
|
1563
|
+
return;
|
|
1564
|
+
}
|
|
1565
|
+
|
|
1566
|
+
// Multi-line paste - use insertTextAtCursor for proper handling
|
|
1567
|
+
this.#insertTextAtCursor(filteredText);
|
|
1568
|
+
});
|
|
1569
|
+
}
|
|
1570
|
+
|
|
1571
|
+
#addNewLine(): void {
|
|
1572
|
+
this.#historyIndex = -1; // Exit history browsing mode
|
|
1573
|
+
this.#resetKillSequence();
|
|
1574
|
+
this.#recordUndoState();
|
|
1575
|
+
|
|
1576
|
+
const currentLine = this.#state.lines[this.#state.cursorLine] || "";
|
|
1577
|
+
|
|
1578
|
+
const before = currentLine.slice(0, this.#state.cursorCol);
|
|
1579
|
+
const after = currentLine.slice(this.#state.cursorCol);
|
|
1580
|
+
|
|
1581
|
+
// Split current line
|
|
1582
|
+
this.#state.lines[this.#state.cursorLine] = before;
|
|
1583
|
+
this.#state.lines.splice(this.#state.cursorLine + 1, 0, after);
|
|
1584
|
+
|
|
1585
|
+
// Move cursor to start of new line
|
|
1586
|
+
this.#state.cursorLine++;
|
|
1587
|
+
this.#setCursorCol(0);
|
|
1588
|
+
|
|
1589
|
+
if (this.onChange) {
|
|
1590
|
+
this.onChange(this.getText());
|
|
1591
|
+
}
|
|
1592
|
+
}
|
|
1593
|
+
|
|
1594
|
+
#shouldSubmitOnBackslashEnter(data: string, kb: KeybindingsManager): boolean {
|
|
1595
|
+
if (this.disableSubmit) return false;
|
|
1596
|
+
if (!matchesKey(data, "enter")) return false;
|
|
1597
|
+
const submitKeys = kb.getKeys("tui.input.submit");
|
|
1598
|
+
const hasShiftEnter = submitKeys.includes("shift+enter") || submitKeys.includes("shift+return");
|
|
1599
|
+
if (!hasShiftEnter) return false;
|
|
1600
|
+
|
|
1601
|
+
const currentLine = this.#state.lines[this.#state.cursorLine] || "";
|
|
1602
|
+
return this.#state.cursorCol > 0 && currentLine[this.#state.cursorCol - 1] === "\\";
|
|
1603
|
+
}
|
|
1604
|
+
|
|
1605
|
+
#submitValue(): void {
|
|
1606
|
+
this.#resetKillSequence();
|
|
1607
|
+
|
|
1608
|
+
const result = this.#expandPasteMarkers(this.#state.lines.join("\n")).trim();
|
|
1609
|
+
|
|
1610
|
+
this.#state = { lines: [""], cursorLine: 0, cursorCol: 0 };
|
|
1611
|
+
this.#pastes.clear();
|
|
1612
|
+
this.#pasteCounter = 0;
|
|
1613
|
+
this.#historyIndex = -1;
|
|
1614
|
+
this.#scrollOffset = 0;
|
|
1615
|
+
this.#undoStack.length = 0;
|
|
1616
|
+
|
|
1617
|
+
if (this.onChange) this.onChange("");
|
|
1618
|
+
if (this.onSubmit) this.onSubmit(result);
|
|
1619
|
+
}
|
|
1620
|
+
|
|
1621
|
+
#handleBackspace(): void {
|
|
1622
|
+
this.#historyIndex = -1; // Exit history browsing mode
|
|
1623
|
+
this.#resetKillSequence();
|
|
1624
|
+
this.#recordUndoState();
|
|
1625
|
+
|
|
1626
|
+
if (this.#state.cursorCol > 0) {
|
|
1627
|
+
// Delete grapheme before cursor (handles emojis, combining characters, etc.)
|
|
1628
|
+
const line = this.#state.lines[this.#state.cursorLine] || "";
|
|
1629
|
+
const beforeCursor = line.slice(0, this.#state.cursorCol);
|
|
1630
|
+
|
|
1631
|
+
// Find the last grapheme in the text before cursor
|
|
1632
|
+
const graphemes = [...segmenter.segment(beforeCursor)];
|
|
1633
|
+
const lastGrapheme = graphemes[graphemes.length - 1];
|
|
1634
|
+
const graphemeLength = lastGrapheme ? lastGrapheme.segment.length : 1;
|
|
1635
|
+
|
|
1636
|
+
const before = line.slice(0, this.#state.cursorCol - graphemeLength);
|
|
1637
|
+
const after = line.slice(this.#state.cursorCol);
|
|
1638
|
+
|
|
1639
|
+
this.#state.lines[this.#state.cursorLine] = before + after;
|
|
1640
|
+
this.#setCursorCol(this.#state.cursorCol - graphemeLength);
|
|
1641
|
+
} else if (this.#state.cursorLine > 0) {
|
|
1642
|
+
// Merge with previous line
|
|
1643
|
+
const currentLine = this.#state.lines[this.#state.cursorLine] || "";
|
|
1644
|
+
const previousLine = this.#state.lines[this.#state.cursorLine - 1] || "";
|
|
1645
|
+
|
|
1646
|
+
this.#state.lines[this.#state.cursorLine - 1] = previousLine + currentLine;
|
|
1647
|
+
this.#state.lines.splice(this.#state.cursorLine, 1);
|
|
1648
|
+
|
|
1649
|
+
this.#state.cursorLine--;
|
|
1650
|
+
this.#setCursorCol(previousLine.length);
|
|
1651
|
+
}
|
|
1652
|
+
|
|
1653
|
+
if (this.onChange) {
|
|
1654
|
+
this.onChange(this.getText());
|
|
1655
|
+
}
|
|
1656
|
+
|
|
1657
|
+
// Update or re-trigger autocomplete after backspace
|
|
1658
|
+
if (this.#autocompleteState) {
|
|
1659
|
+
this.#debouncedUpdateAutocomplete();
|
|
1660
|
+
} else {
|
|
1661
|
+
// If autocomplete was cancelled (no matches), re-trigger if we're in a completable context
|
|
1662
|
+
const currentLine = this.#state.lines[this.#state.cursorLine] || "";
|
|
1663
|
+
const textBeforeCursor = currentLine.slice(0, this.#state.cursorCol);
|
|
1664
|
+
// Slash command context
|
|
1665
|
+
if (textBeforeCursor.trimStart().startsWith("/")) {
|
|
1666
|
+
this.#tryTriggerAutocomplete();
|
|
1667
|
+
}
|
|
1668
|
+
// @ file reference context
|
|
1669
|
+
else if (textBeforeCursor.match(/(?:^|[\s])@[^\s]*$/)) {
|
|
1670
|
+
this.#tryTriggerAutocomplete();
|
|
1671
|
+
}
|
|
1672
|
+
// # prompt action context
|
|
1673
|
+
else if (textBeforeCursor.match(/#[^\s#]*$/)) {
|
|
1674
|
+
this.#tryTriggerAutocomplete();
|
|
1675
|
+
}
|
|
1676
|
+
}
|
|
1677
|
+
}
|
|
1678
|
+
|
|
1679
|
+
/**
|
|
1680
|
+
* Set cursor column and clear preferredVisualCol.
|
|
1681
|
+
* Use this for all non-vertical cursor movements to reset sticky column behavior.
|
|
1682
|
+
*/
|
|
1683
|
+
#setCursorCol(col: number): void {
|
|
1684
|
+
this.#state.cursorCol = col;
|
|
1685
|
+
this.#preferredVisualCol = null;
|
|
1686
|
+
}
|
|
1687
|
+
|
|
1688
|
+
/**
|
|
1689
|
+
* Move cursor to a target visual line, applying sticky column logic.
|
|
1690
|
+
* Shared by moveCursor() and pageScroll().
|
|
1691
|
+
*/
|
|
1692
|
+
#moveToVisualLine(
|
|
1693
|
+
visualLines: Array<{ logicalLine: number; startCol: number; length: number }>,
|
|
1694
|
+
currentVisualLine: number,
|
|
1695
|
+
targetVisualLine: number,
|
|
1696
|
+
): void {
|
|
1697
|
+
const currentVL = visualLines[currentVisualLine];
|
|
1698
|
+
const targetVL = visualLines[targetVisualLine];
|
|
1699
|
+
|
|
1700
|
+
if (currentVL && targetVL) {
|
|
1701
|
+
const currentVisualCol = this.#state.cursorCol - currentVL.startCol;
|
|
1702
|
+
|
|
1703
|
+
// For non-last segments, clamp to length-1 to stay within the segment
|
|
1704
|
+
const isLastSourceSegment =
|
|
1705
|
+
currentVisualLine === visualLines.length - 1 ||
|
|
1706
|
+
visualLines[currentVisualLine + 1]?.logicalLine !== currentVL.logicalLine;
|
|
1707
|
+
const sourceMaxVisualCol = isLastSourceSegment ? currentVL.length : Math.max(0, currentVL.length - 1);
|
|
1708
|
+
|
|
1709
|
+
const isLastTargetSegment =
|
|
1710
|
+
targetVisualLine === visualLines.length - 1 ||
|
|
1711
|
+
visualLines[targetVisualLine + 1]?.logicalLine !== targetVL.logicalLine;
|
|
1712
|
+
const targetMaxVisualCol = isLastTargetSegment ? targetVL.length : Math.max(0, targetVL.length - 1);
|
|
1713
|
+
|
|
1714
|
+
const moveToVisualCol = this.#computeVerticalMoveColumn(
|
|
1715
|
+
currentVisualCol,
|
|
1716
|
+
sourceMaxVisualCol,
|
|
1717
|
+
targetMaxVisualCol,
|
|
1718
|
+
);
|
|
1719
|
+
|
|
1720
|
+
// Set cursor position
|
|
1721
|
+
this.#state.cursorLine = targetVL.logicalLine;
|
|
1722
|
+
const targetCol = targetVL.startCol + moveToVisualCol;
|
|
1723
|
+
const logicalLine = this.#state.lines[targetVL.logicalLine] || "";
|
|
1724
|
+
this.#state.cursorCol = Math.min(targetCol, logicalLine.length);
|
|
1725
|
+
}
|
|
1726
|
+
}
|
|
1727
|
+
|
|
1728
|
+
/**
|
|
1729
|
+
* Compute the target visual column for vertical cursor movement.
|
|
1730
|
+
* Implements the sticky column decision table.
|
|
1731
|
+
*/
|
|
1732
|
+
#computeVerticalMoveColumn(
|
|
1733
|
+
currentVisualCol: number,
|
|
1734
|
+
sourceMaxVisualCol: number,
|
|
1735
|
+
targetMaxVisualCol: number,
|
|
1736
|
+
): number {
|
|
1737
|
+
const hasPreferred = this.#preferredVisualCol !== null;
|
|
1738
|
+
const cursorInMiddle = currentVisualCol < sourceMaxVisualCol;
|
|
1739
|
+
const targetTooShort = targetMaxVisualCol < currentVisualCol;
|
|
1740
|
+
|
|
1741
|
+
if (!hasPreferred || cursorInMiddle) {
|
|
1742
|
+
if (targetTooShort) {
|
|
1743
|
+
this.#preferredVisualCol = currentVisualCol;
|
|
1744
|
+
return targetMaxVisualCol;
|
|
1745
|
+
}
|
|
1746
|
+
this.#preferredVisualCol = null;
|
|
1747
|
+
return currentVisualCol;
|
|
1748
|
+
}
|
|
1749
|
+
|
|
1750
|
+
const targetCantFitPreferred = targetMaxVisualCol < this.#preferredVisualCol!;
|
|
1751
|
+
if (targetTooShort || targetCantFitPreferred) {
|
|
1752
|
+
return targetMaxVisualCol;
|
|
1753
|
+
}
|
|
1754
|
+
|
|
1755
|
+
const result = this.#preferredVisualCol!;
|
|
1756
|
+
this.#preferredVisualCol = null;
|
|
1757
|
+
return result;
|
|
1758
|
+
}
|
|
1759
|
+
|
|
1760
|
+
#moveToLineStart(): void {
|
|
1761
|
+
this.#resetKillSequence();
|
|
1762
|
+
this.#setCursorCol(0);
|
|
1763
|
+
}
|
|
1764
|
+
|
|
1765
|
+
#moveToLineEnd(): void {
|
|
1766
|
+
this.#resetKillSequence();
|
|
1767
|
+
const currentLine = this.#state.lines[this.#state.cursorLine] || "";
|
|
1768
|
+
this.#setCursorCol(currentLine.length);
|
|
1769
|
+
}
|
|
1770
|
+
|
|
1771
|
+
#moveToMessageStart(): void {
|
|
1772
|
+
this.#resetKillSequence();
|
|
1773
|
+
this.#state.cursorLine = 0;
|
|
1774
|
+
this.#setCursorCol(0);
|
|
1775
|
+
}
|
|
1776
|
+
|
|
1777
|
+
#moveToMessageEnd(): void {
|
|
1778
|
+
this.#resetKillSequence();
|
|
1779
|
+
this.#state.cursorLine = this.#state.lines.length - 1;
|
|
1780
|
+
const currentLine = this.#state.lines[this.#state.cursorLine] || "";
|
|
1781
|
+
this.#setCursorCol(currentLine.length);
|
|
1782
|
+
}
|
|
1783
|
+
|
|
1784
|
+
#resetKillSequence(): void {
|
|
1785
|
+
this.#lastAction = null;
|
|
1786
|
+
}
|
|
1787
|
+
|
|
1788
|
+
#withUndoSuspended<T>(fn: () => T): T {
|
|
1789
|
+
const wasSuspended = this.#suspendUndo;
|
|
1790
|
+
this.#suspendUndo = true;
|
|
1791
|
+
try {
|
|
1792
|
+
return fn();
|
|
1793
|
+
} finally {
|
|
1794
|
+
this.#suspendUndo = wasSuspended;
|
|
1795
|
+
}
|
|
1796
|
+
}
|
|
1797
|
+
|
|
1798
|
+
#recordUndoState(): void {
|
|
1799
|
+
if (this.#suspendUndo) return;
|
|
1800
|
+
this.#undoStack.push(structuredClone(this.#state));
|
|
1801
|
+
}
|
|
1802
|
+
|
|
1803
|
+
#applyUndo(): void {
|
|
1804
|
+
const snapshot = this.#undoStack.pop();
|
|
1805
|
+
if (!snapshot) return;
|
|
1806
|
+
|
|
1807
|
+
this.#historyIndex = -1;
|
|
1808
|
+
this.#resetKillSequence();
|
|
1809
|
+
this.#preferredVisualCol = null;
|
|
1810
|
+
Object.assign(this.#state, snapshot);
|
|
1811
|
+
|
|
1812
|
+
if (this.onChange) {
|
|
1813
|
+
this.onChange(this.getText());
|
|
1814
|
+
}
|
|
1815
|
+
|
|
1816
|
+
if (this.#autocompleteState) {
|
|
1817
|
+
this.#debouncedUpdateAutocomplete();
|
|
1818
|
+
} else {
|
|
1819
|
+
const currentLine = this.#state.lines[this.#state.cursorLine] || "";
|
|
1820
|
+
const textBeforeCursor = currentLine.slice(0, this.#state.cursorCol);
|
|
1821
|
+
if (textBeforeCursor.trimStart().startsWith("/")) {
|
|
1822
|
+
this.#tryTriggerAutocomplete();
|
|
1823
|
+
} else if (textBeforeCursor.match(/(?:^|[\s])@[^\s]*$/)) {
|
|
1824
|
+
this.#tryTriggerAutocomplete();
|
|
1825
|
+
} else if (textBeforeCursor.match(/#[^\s#]*$/)) {
|
|
1826
|
+
this.#tryTriggerAutocomplete();
|
|
1827
|
+
}
|
|
1828
|
+
}
|
|
1829
|
+
}
|
|
1830
|
+
|
|
1831
|
+
#matchesTransientUndoSnapshot(
|
|
1832
|
+
snapshot: EditorState,
|
|
1833
|
+
transientText: string,
|
|
1834
|
+
transientStartCol: number,
|
|
1835
|
+
beforeTransient: string,
|
|
1836
|
+
afterTransient: string,
|
|
1837
|
+
): boolean {
|
|
1838
|
+
if (snapshot.cursorLine !== this.#state.cursorLine) return false;
|
|
1839
|
+
if (snapshot.lines.length !== this.#state.lines.length) return false;
|
|
1840
|
+
|
|
1841
|
+
const transientLength = snapshot.cursorCol - transientStartCol;
|
|
1842
|
+
if (transientLength < 0 || transientLength >= transientText.length) return false;
|
|
1843
|
+
|
|
1844
|
+
for (let i = 0; i < snapshot.lines.length; i++) {
|
|
1845
|
+
if (i === this.#state.cursorLine) continue;
|
|
1846
|
+
if (snapshot.lines[i] !== this.#state.lines[i]) return false;
|
|
1847
|
+
}
|
|
1848
|
+
|
|
1849
|
+
return (
|
|
1850
|
+
snapshot.lines[snapshot.cursorLine] ===
|
|
1851
|
+
beforeTransient + transientText.slice(0, transientLength) + afterTransient
|
|
1852
|
+
);
|
|
1853
|
+
}
|
|
1854
|
+
|
|
1855
|
+
#recordKill(text: string, direction: "forward" | "backward", accumulate = this.#lastAction === "kill"): void {
|
|
1856
|
+
if (!text) return;
|
|
1857
|
+
this.#killRing.push(text, { prepend: direction === "backward", accumulate });
|
|
1858
|
+
this.#lastAction = "kill";
|
|
1859
|
+
}
|
|
1860
|
+
|
|
1861
|
+
#insertTextAtCursor(text: string): void {
|
|
1862
|
+
this.#historyIndex = -1;
|
|
1863
|
+
this.#resetKillSequence();
|
|
1864
|
+
this.#recordUndoState();
|
|
1865
|
+
|
|
1866
|
+
const normalized = text.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
|
|
1867
|
+
const lines = normalized.split("\n");
|
|
1868
|
+
|
|
1869
|
+
if (lines.length === 1) {
|
|
1870
|
+
const line = this.#state.lines[this.#state.cursorLine] || "";
|
|
1871
|
+
const before = line.slice(0, this.#state.cursorCol);
|
|
1872
|
+
const after = line.slice(this.#state.cursorCol);
|
|
1873
|
+
this.#state.lines[this.#state.cursorLine] = before + normalized + after;
|
|
1874
|
+
this.#setCursorCol(this.#state.cursorCol + normalized.length);
|
|
1875
|
+
} else {
|
|
1876
|
+
const currentLine = this.#state.lines[this.#state.cursorLine] || "";
|
|
1877
|
+
const beforeCursor = currentLine.slice(0, this.#state.cursorCol);
|
|
1878
|
+
const afterCursor = currentLine.slice(this.#state.cursorCol);
|
|
1879
|
+
|
|
1880
|
+
const newLines: string[] = [];
|
|
1881
|
+
for (let i = 0; i < this.#state.cursorLine; i++) {
|
|
1882
|
+
newLines.push(this.#state.lines[i] || "");
|
|
1883
|
+
}
|
|
1884
|
+
|
|
1885
|
+
newLines.push(beforeCursor + (lines[0] || ""));
|
|
1886
|
+
for (let i = 1; i < lines.length - 1; i++) {
|
|
1887
|
+
newLines.push(lines[i] || "");
|
|
1888
|
+
}
|
|
1889
|
+
newLines.push((lines[lines.length - 1] || "") + afterCursor);
|
|
1890
|
+
|
|
1891
|
+
for (let i = this.#state.cursorLine + 1; i < this.#state.lines.length; i++) {
|
|
1892
|
+
newLines.push(this.#state.lines[i] || "");
|
|
1893
|
+
}
|
|
1894
|
+
|
|
1895
|
+
this.#state.lines = newLines;
|
|
1896
|
+
this.#state.cursorLine += lines.length - 1;
|
|
1897
|
+
this.#setCursorCol((lines[lines.length - 1] || "").length);
|
|
1898
|
+
}
|
|
1899
|
+
|
|
1900
|
+
if (this.onChange) {
|
|
1901
|
+
this.onChange(this.getText());
|
|
1902
|
+
}
|
|
1903
|
+
}
|
|
1904
|
+
|
|
1905
|
+
#yankFromKillRing(): void {
|
|
1906
|
+
const text = this.#killRing.peek();
|
|
1907
|
+
if (!text) return;
|
|
1908
|
+
this.#insertTextAtCursor(text);
|
|
1909
|
+
this.#lastAction = "yank";
|
|
1910
|
+
}
|
|
1911
|
+
|
|
1912
|
+
#yankPop(): void {
|
|
1913
|
+
if (this.#lastAction !== "yank") return;
|
|
1914
|
+
if (this.#killRing.length <= 1) return;
|
|
1915
|
+
|
|
1916
|
+
this.#historyIndex = -1;
|
|
1917
|
+
this.#recordUndoState();
|
|
1918
|
+
|
|
1919
|
+
this.#withUndoSuspended(() => {
|
|
1920
|
+
if (!this.#deleteYankedText()) return;
|
|
1921
|
+
this.#killRing.rotate();
|
|
1922
|
+
const text = this.#killRing.peek();
|
|
1923
|
+
if (text) {
|
|
1924
|
+
this.#insertTextAtCursor(text);
|
|
1925
|
+
}
|
|
1926
|
+
});
|
|
1927
|
+
|
|
1928
|
+
this.#lastAction = "yank";
|
|
1929
|
+
}
|
|
1930
|
+
|
|
1931
|
+
/**
|
|
1932
|
+
* Delete the most recently yanked text from the buffer.
|
|
1933
|
+
*
|
|
1934
|
+
* This is a best-effort operation and assumes the cursor is still positioned
|
|
1935
|
+
* at the end of the yanked text.
|
|
1936
|
+
*/
|
|
1937
|
+
#deleteYankedText(): boolean {
|
|
1938
|
+
const yankedText = this.#killRing.peek();
|
|
1939
|
+
if (!yankedText) return false;
|
|
1940
|
+
|
|
1941
|
+
const yankLines = yankedText.split("\n");
|
|
1942
|
+
const endLine = this.#state.cursorLine;
|
|
1943
|
+
const endCol = this.#state.cursorCol;
|
|
1944
|
+
const startLine = endLine - (yankLines.length - 1);
|
|
1945
|
+
if (startLine < 0) return false;
|
|
1946
|
+
|
|
1947
|
+
if (yankLines.length === 1) {
|
|
1948
|
+
const line = this.#state.lines[endLine] ?? "";
|
|
1949
|
+
const startCol = endCol - yankedText.length;
|
|
1950
|
+
if (startCol < 0) return false;
|
|
1951
|
+
if (line.slice(startCol, endCol) !== yankedText) return false;
|
|
1952
|
+
|
|
1953
|
+
this.#state.lines[endLine] = line.slice(0, startCol) + line.slice(endCol);
|
|
1954
|
+
this.#state.cursorLine = endLine;
|
|
1955
|
+
this.#setCursorCol(startCol);
|
|
1956
|
+
return true;
|
|
1957
|
+
}
|
|
1958
|
+
|
|
1959
|
+
const firstInserted = yankLines[0] ?? "";
|
|
1960
|
+
const lastInserted = yankLines[yankLines.length - 1] ?? "";
|
|
1961
|
+
const firstLineText = this.#state.lines[startLine] ?? "";
|
|
1962
|
+
const lastLineText = this.#state.lines[endLine] ?? "";
|
|
1963
|
+
|
|
1964
|
+
if (!firstLineText.endsWith(firstInserted)) return false;
|
|
1965
|
+
if (endCol !== lastInserted.length) return false;
|
|
1966
|
+
if (lastLineText.slice(0, endCol) !== lastInserted) return false;
|
|
1967
|
+
|
|
1968
|
+
const startCol = firstLineText.length - firstInserted.length;
|
|
1969
|
+
if (startCol < 0) return false;
|
|
1970
|
+
|
|
1971
|
+
const suffix = lastLineText.slice(endCol);
|
|
1972
|
+
const newLine = firstLineText.slice(0, startCol) + suffix;
|
|
1973
|
+
|
|
1974
|
+
this.#state.lines.splice(startLine, yankLines.length, newLine);
|
|
1975
|
+
this.#state.cursorLine = startLine;
|
|
1976
|
+
this.#setCursorCol(startCol);
|
|
1977
|
+
return true;
|
|
1978
|
+
}
|
|
1979
|
+
|
|
1980
|
+
#deleteToStartOfLine(): void {
|
|
1981
|
+
this.#historyIndex = -1; // Exit history browsing mode
|
|
1982
|
+
this.#recordUndoState();
|
|
1983
|
+
|
|
1984
|
+
const currentLine = this.#state.lines[this.#state.cursorLine] || "";
|
|
1985
|
+
let deletedText = "";
|
|
1986
|
+
|
|
1987
|
+
if (this.#state.cursorCol > 0) {
|
|
1988
|
+
// Delete from start of line up to cursor
|
|
1989
|
+
deletedText = currentLine.slice(0, this.#state.cursorCol);
|
|
1990
|
+
this.#state.lines[this.#state.cursorLine] = currentLine.slice(this.#state.cursorCol);
|
|
1991
|
+
this.#setCursorCol(0);
|
|
1992
|
+
} else if (this.#state.cursorLine > 0) {
|
|
1993
|
+
// At start of line - merge with previous line
|
|
1994
|
+
deletedText = "\n";
|
|
1995
|
+
const previousLine = this.#state.lines[this.#state.cursorLine - 1] || "";
|
|
1996
|
+
this.#state.lines[this.#state.cursorLine - 1] = previousLine + currentLine;
|
|
1997
|
+
this.#state.lines.splice(this.#state.cursorLine, 1);
|
|
1998
|
+
this.#state.cursorLine--;
|
|
1999
|
+
this.#setCursorCol(previousLine.length);
|
|
2000
|
+
}
|
|
2001
|
+
|
|
2002
|
+
this.#recordKill(deletedText, "backward");
|
|
2003
|
+
|
|
2004
|
+
if (this.onChange) {
|
|
2005
|
+
this.onChange(this.getText());
|
|
2006
|
+
}
|
|
2007
|
+
}
|
|
2008
|
+
|
|
2009
|
+
#deleteToEndOfLine(): void {
|
|
2010
|
+
this.#historyIndex = -1; // Exit history browsing mode
|
|
2011
|
+
this.#recordUndoState();
|
|
2012
|
+
|
|
2013
|
+
const currentLine = this.#state.lines[this.#state.cursorLine] || "";
|
|
2014
|
+
let deletedText = "";
|
|
2015
|
+
|
|
2016
|
+
if (this.#state.cursorCol < currentLine.length) {
|
|
2017
|
+
// Delete from cursor to end of line
|
|
2018
|
+
deletedText = currentLine.slice(this.#state.cursorCol);
|
|
2019
|
+
this.#state.lines[this.#state.cursorLine] = currentLine.slice(0, this.#state.cursorCol);
|
|
2020
|
+
} else if (this.#state.cursorLine < this.#state.lines.length - 1) {
|
|
2021
|
+
// At end of line - merge with next line
|
|
2022
|
+
const nextLine = this.#state.lines[this.#state.cursorLine + 1] || "";
|
|
2023
|
+
deletedText = "\n";
|
|
2024
|
+
this.#state.lines[this.#state.cursorLine] = currentLine + nextLine;
|
|
2025
|
+
this.#state.lines.splice(this.#state.cursorLine + 1, 1);
|
|
2026
|
+
}
|
|
2027
|
+
|
|
2028
|
+
this.#recordKill(deletedText, "forward");
|
|
2029
|
+
|
|
2030
|
+
if (this.onChange) {
|
|
2031
|
+
this.onChange(this.getText());
|
|
2032
|
+
}
|
|
2033
|
+
}
|
|
2034
|
+
|
|
2035
|
+
#deleteWordBackwards(): void {
|
|
2036
|
+
this.#historyIndex = -1; // Exit history browsing mode
|
|
2037
|
+
this.#recordUndoState();
|
|
2038
|
+
|
|
2039
|
+
const currentLine = this.#state.lines[this.#state.cursorLine] || "";
|
|
2040
|
+
|
|
2041
|
+
// If at start of line, behave like backspace at column 0 (merge with previous line)
|
|
2042
|
+
if (this.#state.cursorCol === 0) {
|
|
2043
|
+
if (this.#state.cursorLine > 0) {
|
|
2044
|
+
this.#recordKill("\n", "backward");
|
|
2045
|
+
const previousLine = this.#state.lines[this.#state.cursorLine - 1] || "";
|
|
2046
|
+
this.#state.lines[this.#state.cursorLine - 1] = previousLine + currentLine;
|
|
2047
|
+
this.#state.lines.splice(this.#state.cursorLine, 1);
|
|
2048
|
+
this.#state.cursorLine--;
|
|
2049
|
+
this.#setCursorCol(previousLine.length);
|
|
2050
|
+
}
|
|
2051
|
+
} else {
|
|
2052
|
+
const oldCursorCol = this.#state.cursorCol;
|
|
2053
|
+
this.#moveWordBackwards();
|
|
2054
|
+
const deleteFrom = this.#state.cursorCol;
|
|
2055
|
+
this.#setCursorCol(oldCursorCol);
|
|
2056
|
+
|
|
2057
|
+
const deletedText = currentLine.slice(deleteFrom, oldCursorCol);
|
|
2058
|
+
this.#state.lines[this.#state.cursorLine] =
|
|
2059
|
+
currentLine.slice(0, deleteFrom) + currentLine.slice(this.#state.cursorCol);
|
|
2060
|
+
this.#setCursorCol(deleteFrom);
|
|
2061
|
+
this.#recordKill(deletedText, "backward");
|
|
2062
|
+
}
|
|
2063
|
+
|
|
2064
|
+
if (this.onChange) {
|
|
2065
|
+
this.onChange(this.getText());
|
|
2066
|
+
}
|
|
2067
|
+
}
|
|
2068
|
+
|
|
2069
|
+
#deleteWordForwards(): void {
|
|
2070
|
+
this.#historyIndex = -1; // Exit history browsing mode
|
|
2071
|
+
this.#recordUndoState();
|
|
2072
|
+
|
|
2073
|
+
const currentLine = this.#state.lines[this.#state.cursorLine] || "";
|
|
2074
|
+
|
|
2075
|
+
if (this.#state.cursorCol >= currentLine.length) {
|
|
2076
|
+
if (this.#state.cursorLine < this.#state.lines.length - 1) {
|
|
2077
|
+
this.#recordKill("\n", "forward");
|
|
2078
|
+
const nextLine = this.#state.lines[this.#state.cursorLine + 1] || "";
|
|
2079
|
+
this.#state.lines[this.#state.cursorLine] = currentLine + nextLine;
|
|
2080
|
+
this.#state.lines.splice(this.#state.cursorLine + 1, 1);
|
|
2081
|
+
}
|
|
2082
|
+
} else {
|
|
2083
|
+
const oldCursorCol = this.#state.cursorCol;
|
|
2084
|
+
this.#moveWordForwards();
|
|
2085
|
+
const deleteTo = this.#state.cursorCol;
|
|
2086
|
+
this.#setCursorCol(oldCursorCol);
|
|
2087
|
+
|
|
2088
|
+
const deletedText = currentLine.slice(oldCursorCol, deleteTo);
|
|
2089
|
+
this.#state.lines[this.#state.cursorLine] = currentLine.slice(0, oldCursorCol) + currentLine.slice(deleteTo);
|
|
2090
|
+
this.#recordKill(deletedText, "forward");
|
|
2091
|
+
}
|
|
2092
|
+
|
|
2093
|
+
if (this.onChange) {
|
|
2094
|
+
this.onChange(this.getText());
|
|
2095
|
+
}
|
|
2096
|
+
}
|
|
2097
|
+
|
|
2098
|
+
#handleForwardDelete(): void {
|
|
2099
|
+
this.#historyIndex = -1; // Exit history browsing mode
|
|
2100
|
+
this.#resetKillSequence();
|
|
2101
|
+
this.#recordUndoState();
|
|
2102
|
+
|
|
2103
|
+
const currentLine = this.#state.lines[this.#state.cursorLine] || "";
|
|
2104
|
+
|
|
2105
|
+
if (this.#state.cursorCol < currentLine.length) {
|
|
2106
|
+
// Delete grapheme at cursor position (handles emojis, combining characters, etc.)
|
|
2107
|
+
const afterCursor = currentLine.slice(this.#state.cursorCol);
|
|
2108
|
+
|
|
2109
|
+
// Find the first grapheme at cursor
|
|
2110
|
+
const graphemes = [...segmenter.segment(afterCursor)];
|
|
2111
|
+
const firstGrapheme = graphemes[0];
|
|
2112
|
+
const graphemeLength = firstGrapheme ? firstGrapheme.segment.length : 1;
|
|
2113
|
+
|
|
2114
|
+
const before = currentLine.slice(0, this.#state.cursorCol);
|
|
2115
|
+
const after = currentLine.slice(this.#state.cursorCol + graphemeLength);
|
|
2116
|
+
this.#state.lines[this.#state.cursorLine] = before + after;
|
|
2117
|
+
} else if (this.#state.cursorLine < this.#state.lines.length - 1) {
|
|
2118
|
+
// At end of line - merge with next line
|
|
2119
|
+
const nextLine = this.#state.lines[this.#state.cursorLine + 1] || "";
|
|
2120
|
+
this.#state.lines[this.#state.cursorLine] = currentLine + nextLine;
|
|
2121
|
+
this.#state.lines.splice(this.#state.cursorLine + 1, 1);
|
|
2122
|
+
}
|
|
2123
|
+
|
|
2124
|
+
if (this.onChange) {
|
|
2125
|
+
this.onChange(this.getText());
|
|
2126
|
+
}
|
|
2127
|
+
|
|
2128
|
+
// Update or re-trigger autocomplete after forward delete
|
|
2129
|
+
if (this.#autocompleteState) {
|
|
2130
|
+
this.#debouncedUpdateAutocomplete();
|
|
2131
|
+
} else {
|
|
2132
|
+
const currentLine = this.#state.lines[this.#state.cursorLine] || "";
|
|
2133
|
+
const textBeforeCursor = currentLine.slice(0, this.#state.cursorCol);
|
|
2134
|
+
// Slash command context
|
|
2135
|
+
if (textBeforeCursor.trimStart().startsWith("/")) {
|
|
2136
|
+
this.#tryTriggerAutocomplete();
|
|
2137
|
+
}
|
|
2138
|
+
// @ file reference context
|
|
2139
|
+
else if (textBeforeCursor.match(/(?:^|[\s])@[^\s]*$/)) {
|
|
2140
|
+
this.#tryTriggerAutocomplete();
|
|
2141
|
+
}
|
|
2142
|
+
// # prompt action context
|
|
2143
|
+
else if (textBeforeCursor.match(/#[^\s#]*$/)) {
|
|
2144
|
+
this.#tryTriggerAutocomplete();
|
|
2145
|
+
}
|
|
2146
|
+
}
|
|
2147
|
+
}
|
|
2148
|
+
|
|
2149
|
+
/**
|
|
2150
|
+
* Build a mapping from visual lines to logical positions.
|
|
2151
|
+
* Returns an array where each element represents a visual line with:
|
|
2152
|
+
* - logicalLine: index into this.#state.lines
|
|
2153
|
+
* - startCol: starting column in the logical line
|
|
2154
|
+
* - length: length of this visual line segment
|
|
2155
|
+
*/
|
|
2156
|
+
#buildVisualLineMap(width: number): Array<{ logicalLine: number; startCol: number; length: number }> {
|
|
2157
|
+
const visualLines: Array<{ logicalLine: number; startCol: number; length: number }> = [];
|
|
2158
|
+
|
|
2159
|
+
for (let i = 0; i < this.#state.lines.length; i++) {
|
|
2160
|
+
const line = this.#state.lines[i] || "";
|
|
2161
|
+
const lineVisWidth = visibleWidth(line);
|
|
2162
|
+
if (line.length === 0) {
|
|
2163
|
+
// Empty line still takes one visual line
|
|
2164
|
+
visualLines.push({ logicalLine: i, startCol: 0, length: 0 });
|
|
2165
|
+
} else if (lineVisWidth <= width) {
|
|
2166
|
+
visualLines.push({ logicalLine: i, startCol: 0, length: line.length });
|
|
2167
|
+
} else {
|
|
2168
|
+
// Line needs wrapping - use word-aware wrapping
|
|
2169
|
+
const chunks = wordWrapLine(line, width);
|
|
2170
|
+
for (const chunk of chunks) {
|
|
2171
|
+
visualLines.push({
|
|
2172
|
+
logicalLine: i,
|
|
2173
|
+
startCol: chunk.startIndex,
|
|
2174
|
+
length: chunk.endIndex - chunk.startIndex,
|
|
2175
|
+
});
|
|
2176
|
+
}
|
|
2177
|
+
}
|
|
2178
|
+
}
|
|
2179
|
+
|
|
2180
|
+
return visualLines;
|
|
2181
|
+
}
|
|
2182
|
+
|
|
2183
|
+
/**
|
|
2184
|
+
* Find the visual line index for the current cursor position.
|
|
2185
|
+
*/
|
|
2186
|
+
#findCurrentVisualLine(visualLines: Array<{ logicalLine: number; startCol: number; length: number }>): number {
|
|
2187
|
+
for (let i = 0; i < visualLines.length; i++) {
|
|
2188
|
+
const vl = visualLines[i];
|
|
2189
|
+
if (!vl) continue;
|
|
2190
|
+
if (vl.logicalLine === this.#state.cursorLine) {
|
|
2191
|
+
const colInSegment = this.#state.cursorCol - vl.startCol;
|
|
2192
|
+
// Cursor is in this segment if it's within range
|
|
2193
|
+
// For the last segment of a logical line, cursor can be at length (end position)
|
|
2194
|
+
const isLastSegmentOfLine =
|
|
2195
|
+
i === visualLines.length - 1 || visualLines[i + 1]?.logicalLine !== vl.logicalLine;
|
|
2196
|
+
if (colInSegment >= 0 && (colInSegment < vl.length || (isLastSegmentOfLine && colInSegment <= vl.length))) {
|
|
2197
|
+
return i;
|
|
2198
|
+
}
|
|
2199
|
+
}
|
|
2200
|
+
}
|
|
2201
|
+
// Fallback: return last visual line
|
|
2202
|
+
return visualLines.length - 1;
|
|
2203
|
+
}
|
|
2204
|
+
|
|
2205
|
+
#moveCursor(deltaLine: number, deltaCol: number): void {
|
|
2206
|
+
this.#resetKillSequence();
|
|
2207
|
+
const visualLines = this.#buildVisualLineMap(this.#lastLayoutWidth);
|
|
2208
|
+
const currentVisualLine = this.#findCurrentVisualLine(visualLines);
|
|
2209
|
+
|
|
2210
|
+
if (deltaLine !== 0) {
|
|
2211
|
+
const targetVisualLine = currentVisualLine + deltaLine;
|
|
2212
|
+
|
|
2213
|
+
if (targetVisualLine >= 0 && targetVisualLine < visualLines.length) {
|
|
2214
|
+
this.#moveToVisualLine(visualLines, currentVisualLine, targetVisualLine);
|
|
2215
|
+
}
|
|
2216
|
+
}
|
|
2217
|
+
|
|
2218
|
+
if (deltaCol !== 0) {
|
|
2219
|
+
const currentLine = this.#state.lines[this.#state.cursorLine] || "";
|
|
2220
|
+
|
|
2221
|
+
if (deltaCol > 0) {
|
|
2222
|
+
// Moving right - move by one grapheme (handles emojis, combining characters, etc.)
|
|
2223
|
+
if (this.#state.cursorCol < currentLine.length) {
|
|
2224
|
+
const afterCursor = currentLine.slice(this.#state.cursorCol);
|
|
2225
|
+
const graphemes = [...segmenter.segment(afterCursor)];
|
|
2226
|
+
const firstGrapheme = graphemes[0];
|
|
2227
|
+
this.#setCursorCol(this.#state.cursorCol + (firstGrapheme ? firstGrapheme.segment.length : 1));
|
|
2228
|
+
} else if (this.#state.cursorLine < this.#state.lines.length - 1) {
|
|
2229
|
+
// Wrap to start of next logical line
|
|
2230
|
+
this.#state.cursorLine++;
|
|
2231
|
+
this.#setCursorCol(0);
|
|
2232
|
+
} else {
|
|
2233
|
+
// At end of last line - can't move, but set preferredVisualCol for up/down navigation
|
|
2234
|
+
const currentVL = visualLines[currentVisualLine];
|
|
2235
|
+
if (currentVL) {
|
|
2236
|
+
this.#preferredVisualCol = this.#state.cursorCol - currentVL.startCol;
|
|
2237
|
+
}
|
|
2238
|
+
}
|
|
2239
|
+
} else {
|
|
2240
|
+
// Moving left - move by one grapheme (handles emojis, combining characters, etc.)
|
|
2241
|
+
if (this.#state.cursorCol > 0) {
|
|
2242
|
+
const beforeCursor = currentLine.slice(0, this.#state.cursorCol);
|
|
2243
|
+
const graphemes = [...segmenter.segment(beforeCursor)];
|
|
2244
|
+
const lastGrapheme = graphemes[graphemes.length - 1];
|
|
2245
|
+
this.#setCursorCol(this.#state.cursorCol - (lastGrapheme ? lastGrapheme.segment.length : 1));
|
|
2246
|
+
} else if (this.#state.cursorLine > 0) {
|
|
2247
|
+
// Wrap to end of previous logical line
|
|
2248
|
+
this.#state.cursorLine--;
|
|
2249
|
+
const prevLine = this.#state.lines[this.#state.cursorLine] || "";
|
|
2250
|
+
this.#setCursorCol(prevLine.length);
|
|
2251
|
+
}
|
|
2252
|
+
}
|
|
2253
|
+
}
|
|
2254
|
+
}
|
|
2255
|
+
|
|
2256
|
+
#pageScroll(direction: -1 | 1): void {
|
|
2257
|
+
this.#resetKillSequence();
|
|
2258
|
+
const visualLines = this.#buildVisualLineMap(this.#lastLayoutWidth);
|
|
2259
|
+
const currentVisualLine = this.#findCurrentVisualLine(visualLines);
|
|
2260
|
+
const step = this.#getPageScrollStep(visualLines.length);
|
|
2261
|
+
const targetVisualLine = Math.max(0, Math.min(visualLines.length - 1, currentVisualLine + direction * step));
|
|
2262
|
+
if (targetVisualLine === currentVisualLine) return;
|
|
2263
|
+
this.#moveToVisualLine(visualLines, currentVisualLine, targetVisualLine);
|
|
2264
|
+
}
|
|
2265
|
+
|
|
2266
|
+
#moveWordBackwards(): void {
|
|
2267
|
+
const currentLine = this.#state.lines[this.#state.cursorLine] || "";
|
|
2268
|
+
|
|
2269
|
+
// If at start of line, move to end of previous line
|
|
2270
|
+
if (this.#state.cursorCol === 0) {
|
|
2271
|
+
if (this.#state.cursorLine > 0) {
|
|
2272
|
+
this.#state.cursorLine--;
|
|
2273
|
+
const prevLine = this.#state.lines[this.#state.cursorLine] || "";
|
|
2274
|
+
this.#setCursorCol(prevLine.length);
|
|
2275
|
+
}
|
|
2276
|
+
return;
|
|
2277
|
+
}
|
|
2278
|
+
|
|
2279
|
+
this.#setCursorCol(moveWordLeft(currentLine, this.#state.cursorCol));
|
|
2280
|
+
}
|
|
2281
|
+
|
|
2282
|
+
/**
|
|
2283
|
+
* Jump to the first occurrence of a character in the specified direction.
|
|
2284
|
+
* Multi-line search. Case-sensitive. Skips the current cursor position.
|
|
2285
|
+
*/
|
|
2286
|
+
#jumpToChar(char: string, direction: "forward" | "backward"): void {
|
|
2287
|
+
this.#resetKillSequence();
|
|
2288
|
+
const isForward = direction === "forward";
|
|
2289
|
+
const lines = this.#state.lines;
|
|
2290
|
+
|
|
2291
|
+
const end = isForward ? lines.length : -1;
|
|
2292
|
+
const step = isForward ? 1 : -1;
|
|
2293
|
+
|
|
2294
|
+
for (let lineIdx = this.#state.cursorLine; lineIdx !== end; lineIdx += step) {
|
|
2295
|
+
const line = lines[lineIdx] || "";
|
|
2296
|
+
const isCurrentLine = lineIdx === this.#state.cursorLine;
|
|
2297
|
+
|
|
2298
|
+
// Current line: start after/before cursor; other lines: search full line
|
|
2299
|
+
const searchFrom = isCurrentLine
|
|
2300
|
+
? isForward
|
|
2301
|
+
? this.#state.cursorCol + 1
|
|
2302
|
+
: this.#state.cursorCol - 1
|
|
2303
|
+
: undefined;
|
|
2304
|
+
|
|
2305
|
+
const idx = isForward ? line.indexOf(char, searchFrom) : line.lastIndexOf(char, searchFrom);
|
|
2306
|
+
|
|
2307
|
+
if (idx !== -1) {
|
|
2308
|
+
this.#state.cursorLine = lineIdx;
|
|
2309
|
+
this.#setCursorCol(idx);
|
|
2310
|
+
return;
|
|
2311
|
+
}
|
|
2312
|
+
}
|
|
2313
|
+
// No match found - cursor stays in place
|
|
2314
|
+
}
|
|
2315
|
+
|
|
2316
|
+
#moveWordForwards(): void {
|
|
2317
|
+
const currentLine = this.#state.lines[this.#state.cursorLine] || "";
|
|
2318
|
+
|
|
2319
|
+
// If at end of line, move to start of next line
|
|
2320
|
+
if (this.#state.cursorCol >= currentLine.length) {
|
|
2321
|
+
if (this.#state.cursorLine < this.#state.lines.length - 1) {
|
|
2322
|
+
this.#state.cursorLine++;
|
|
2323
|
+
this.#setCursorCol(0);
|
|
2324
|
+
}
|
|
2325
|
+
return;
|
|
2326
|
+
}
|
|
2327
|
+
|
|
2328
|
+
this.#setCursorCol(moveWordRight(currentLine, this.#state.cursorCol));
|
|
2329
|
+
}
|
|
2330
|
+
|
|
2331
|
+
// Helper method to check if cursor is at start of message (for slash command detection)
|
|
2332
|
+
#isAtStartOfMessage(): boolean {
|
|
2333
|
+
const currentLine = this.#state.lines[this.#state.cursorLine] || "";
|
|
2334
|
+
const beforeCursor = currentLine.slice(0, this.#state.cursorCol);
|
|
2335
|
+
|
|
2336
|
+
// At start if line is empty, only contains whitespace, or is just "/"
|
|
2337
|
+
return beforeCursor.trim() === "" || beforeCursor.trim() === "/";
|
|
2338
|
+
}
|
|
2339
|
+
|
|
2340
|
+
#isSlashCommandNameAutocompleteSelection(): boolean {
|
|
2341
|
+
if (this.#autocompleteState !== "regular") {
|
|
2342
|
+
return false;
|
|
2343
|
+
}
|
|
2344
|
+
|
|
2345
|
+
const currentLine = this.#state.lines[this.#state.cursorLine] || "";
|
|
2346
|
+
const textBeforeCursor = currentLine.slice(0, this.#state.cursorCol).trimStart();
|
|
2347
|
+
return textBeforeCursor.startsWith("/") && !textBeforeCursor.includes(" ");
|
|
2348
|
+
}
|
|
2349
|
+
|
|
2350
|
+
#isCompletedSlashCommandAtCursor(): boolean {
|
|
2351
|
+
const currentLine = this.#state.lines[this.#state.cursorLine] || "";
|
|
2352
|
+
if (this.#state.cursorCol !== currentLine.length) {
|
|
2353
|
+
return false;
|
|
2354
|
+
}
|
|
2355
|
+
|
|
2356
|
+
const textBeforeCursor = currentLine.slice(0, this.#state.cursorCol).trimStart();
|
|
2357
|
+
return /^\/\S+ $/.test(textBeforeCursor);
|
|
2358
|
+
}
|
|
2359
|
+
|
|
2360
|
+
// Autocomplete methods
|
|
2361
|
+
async #tryTriggerAutocomplete(explicitTab: boolean = false): Promise<void> {
|
|
2362
|
+
if (!this.#autocompleteProvider) return;
|
|
2363
|
+
// Check if we should trigger file completion on Tab
|
|
2364
|
+
if (explicitTab) {
|
|
2365
|
+
const provider = this.#autocompleteProvider as CombinedAutocompleteProvider;
|
|
2366
|
+
const shouldTrigger =
|
|
2367
|
+
!provider.shouldTriggerFileCompletion ||
|
|
2368
|
+
provider.shouldTriggerFileCompletion(this.#state.lines, this.#state.cursorLine, this.#state.cursorCol);
|
|
2369
|
+
if (!shouldTrigger) {
|
|
2370
|
+
return;
|
|
2371
|
+
}
|
|
2372
|
+
}
|
|
2373
|
+
|
|
2374
|
+
const requestId = ++this.#autocompleteRequestId;
|
|
2375
|
+
|
|
2376
|
+
const suggestions = await this.#autocompleteProvider.getSuggestions(
|
|
2377
|
+
this.#state.lines,
|
|
2378
|
+
this.#state.cursorLine,
|
|
2379
|
+
this.#state.cursorCol,
|
|
2380
|
+
);
|
|
2381
|
+
if (requestId !== this.#autocompleteRequestId) return;
|
|
2382
|
+
|
|
2383
|
+
if (suggestions && suggestions.items.length > 0) {
|
|
2384
|
+
this.#autocompletePrefix = suggestions.prefix;
|
|
2385
|
+
this.#autocompleteList = this.#createAutocompleteList(suggestions.prefix, suggestions.items);
|
|
2386
|
+
this.#autocompleteState = "regular";
|
|
2387
|
+
this.onAutocompleteUpdate?.();
|
|
2388
|
+
} else {
|
|
2389
|
+
this.#cancelAutocomplete();
|
|
2390
|
+
this.onAutocompleteUpdate?.();
|
|
2391
|
+
}
|
|
2392
|
+
}
|
|
2393
|
+
#createAutocompleteList(
|
|
2394
|
+
prefix: string,
|
|
2395
|
+
items: Array<{ value: string; label: string; description?: string }>,
|
|
2396
|
+
): SelectList {
|
|
2397
|
+
// Layout options prepared for future SelectList enhancements (e.g., for slash commands)
|
|
2398
|
+
const layout = prefix.startsWith("/") ? SLASH_COMMAND_SELECT_LIST_LAYOUT : undefined;
|
|
2399
|
+
// TODO: Pass layout to SelectList when constructor is updated to support it
|
|
2400
|
+
void layout; // Use layout variable to avoid lint warnings
|
|
2401
|
+
return new SelectList(items, this.#autocompleteMaxVisible, this.#theme.selectList);
|
|
2402
|
+
}
|
|
2403
|
+
|
|
2404
|
+
#handleTabCompletion(): void {
|
|
2405
|
+
if (!this.#autocompleteProvider) return;
|
|
2406
|
+
|
|
2407
|
+
const currentLine = this.#state.lines[this.#state.cursorLine] || "";
|
|
2408
|
+
const beforeCursor = currentLine.slice(0, this.#state.cursorCol);
|
|
2409
|
+
|
|
2410
|
+
// Check if we're in a slash command context
|
|
2411
|
+
if (beforeCursor.trimStart().startsWith("/") && !beforeCursor.trimStart().includes(" ")) {
|
|
2412
|
+
this.#handleSlashCommandCompletion();
|
|
2413
|
+
} else {
|
|
2414
|
+
this.#forceFileAutocomplete(true);
|
|
2415
|
+
}
|
|
2416
|
+
}
|
|
2417
|
+
|
|
2418
|
+
#handleSlashCommandCompletion(): void {
|
|
2419
|
+
this.#tryTriggerAutocomplete(true);
|
|
2420
|
+
}
|
|
2421
|
+
|
|
2422
|
+
/*
|
|
2423
|
+
https://github.com/EsotericSoftware/spine-runtimes/actions/runs/19536643416/job/559322883
|
|
2424
|
+
17 this job fails with https://github.com/EsotericSoftware/spine-runtimes/actions/runs/19
|
|
2425
|
+
536643416/job/55932288317 havea look at .gi
|
|
2426
|
+
*/
|
|
2427
|
+
async #forceFileAutocomplete(explicitTab: boolean = false): Promise<void> {
|
|
2428
|
+
if (!this.#autocompleteProvider) return;
|
|
2429
|
+
|
|
2430
|
+
// Check if provider supports force file suggestions via runtime check
|
|
2431
|
+
const provider = this.#autocompleteProvider as {
|
|
2432
|
+
getForceFileSuggestions?: CombinedAutocompleteProvider["getForceFileSuggestions"];
|
|
2433
|
+
};
|
|
2434
|
+
if (typeof provider.getForceFileSuggestions !== "function") {
|
|
2435
|
+
await this.#tryTriggerAutocomplete(true);
|
|
2436
|
+
return;
|
|
2437
|
+
}
|
|
2438
|
+
|
|
2439
|
+
const requestId = ++this.#autocompleteRequestId;
|
|
2440
|
+
const suggestions = await provider.getForceFileSuggestions(
|
|
2441
|
+
this.#state.lines,
|
|
2442
|
+
this.#state.cursorLine,
|
|
2443
|
+
this.#state.cursorCol,
|
|
2444
|
+
);
|
|
2445
|
+
if (requestId !== this.#autocompleteRequestId) return;
|
|
2446
|
+
|
|
2447
|
+
if (suggestions && suggestions.items.length > 0) {
|
|
2448
|
+
// If there's exactly one suggestion and this was an explicit Tab press, apply it immediately
|
|
2449
|
+
if (explicitTab && suggestions.items.length === 1) {
|
|
2450
|
+
const item = suggestions.items[0]!;
|
|
2451
|
+
const result = this.#autocompleteProvider.applyCompletion(
|
|
2452
|
+
this.#state.lines,
|
|
2453
|
+
this.#state.cursorLine,
|
|
2454
|
+
this.#state.cursorCol,
|
|
2455
|
+
item,
|
|
2456
|
+
suggestions.prefix,
|
|
2457
|
+
);
|
|
2458
|
+
|
|
2459
|
+
this.#state.lines = result.lines;
|
|
2460
|
+
this.#state.cursorLine = result.cursorLine;
|
|
2461
|
+
this.#setCursorCol(result.cursorCol);
|
|
2462
|
+
|
|
2463
|
+
if (this.onChange) {
|
|
2464
|
+
this.onChange(this.getText());
|
|
2465
|
+
}
|
|
2466
|
+
return;
|
|
2467
|
+
}
|
|
2468
|
+
|
|
2469
|
+
this.#autocompletePrefix = suggestions.prefix;
|
|
2470
|
+
this.#autocompleteList = this.#createAutocompleteList(suggestions.prefix, suggestions.items);
|
|
2471
|
+
this.#autocompleteState = "force";
|
|
2472
|
+
this.onAutocompleteUpdate?.();
|
|
2473
|
+
} else {
|
|
2474
|
+
this.#cancelAutocomplete();
|
|
2475
|
+
this.onAutocompleteUpdate?.();
|
|
2476
|
+
}
|
|
2477
|
+
}
|
|
2478
|
+
|
|
2479
|
+
#cancelAutocomplete(notifyCancel: boolean = false): void {
|
|
2480
|
+
const wasAutocompleting = this.#autocompleteState !== null;
|
|
2481
|
+
this.#clearAutocompleteTimeout();
|
|
2482
|
+
this.#autocompleteRequestId += 1;
|
|
2483
|
+
this.#autocompleteState = null;
|
|
2484
|
+
this.#autocompleteList = undefined;
|
|
2485
|
+
this.#autocompletePrefix = "";
|
|
2486
|
+
if (notifyCancel && wasAutocompleting) {
|
|
2487
|
+
this.onAutocompleteCancel?.();
|
|
2488
|
+
}
|
|
2489
|
+
}
|
|
2490
|
+
|
|
2491
|
+
isShowingAutocomplete(): boolean {
|
|
2492
|
+
return this.#autocompleteState !== null;
|
|
2493
|
+
}
|
|
2494
|
+
|
|
2495
|
+
async #updateAutocomplete(): Promise<void> {
|
|
2496
|
+
if (!this.#autocompleteState || !this.#autocompleteProvider) return;
|
|
2497
|
+
|
|
2498
|
+
// In force mode, use forceFileAutocomplete to get suggestions
|
|
2499
|
+
if (this.#autocompleteState === "force") {
|
|
2500
|
+
this.#forceFileAutocomplete();
|
|
2501
|
+
return;
|
|
2502
|
+
}
|
|
2503
|
+
|
|
2504
|
+
const requestId = ++this.#autocompleteRequestId;
|
|
2505
|
+
|
|
2506
|
+
const suggestions = await this.#autocompleteProvider.getSuggestions(
|
|
2507
|
+
this.#state.lines,
|
|
2508
|
+
this.#state.cursorLine,
|
|
2509
|
+
this.#state.cursorCol,
|
|
2510
|
+
);
|
|
2511
|
+
if (requestId !== this.#autocompleteRequestId) return;
|
|
2512
|
+
|
|
2513
|
+
if (suggestions && suggestions.items.length > 0) {
|
|
2514
|
+
this.#autocompletePrefix = suggestions.prefix;
|
|
2515
|
+
// Always create new SelectList to ensure update
|
|
2516
|
+
this.#autocompleteList = this.#createAutocompleteList(suggestions.prefix, suggestions.items);
|
|
2517
|
+
this.onAutocompleteUpdate?.();
|
|
2518
|
+
} else {
|
|
2519
|
+
this.#cancelAutocomplete();
|
|
2520
|
+
this.onAutocompleteUpdate?.();
|
|
2521
|
+
}
|
|
2522
|
+
}
|
|
2523
|
+
|
|
2524
|
+
#debouncedUpdateAutocomplete(): void {
|
|
2525
|
+
if (this.#autocompleteTimeout) {
|
|
2526
|
+
clearTimeout(this.#autocompleteTimeout);
|
|
2527
|
+
}
|
|
2528
|
+
this.#autocompleteTimeout = setTimeout(() => {
|
|
2529
|
+
this.#updateAutocomplete();
|
|
2530
|
+
this.#autocompleteTimeout = undefined;
|
|
2531
|
+
}, 100);
|
|
2532
|
+
}
|
|
2533
|
+
|
|
2534
|
+
#clearAutocompleteTimeout(): void {
|
|
2535
|
+
if (this.#autocompleteTimeout) {
|
|
2536
|
+
clearTimeout(this.#autocompleteTimeout);
|
|
2537
|
+
this.#autocompleteTimeout = undefined;
|
|
2538
|
+
}
|
|
2539
|
+
}
|
|
2540
|
+
|
|
2541
|
+
/**
|
|
2542
|
+
* Get inline hint text to show as dim ghost text after the cursor.
|
|
2543
|
+
* Checks selected autocomplete item's hint first, then falls back to provider.
|
|
2544
|
+
*/
|
|
2545
|
+
#getInlineHint(): string | null {
|
|
2546
|
+
// Check selected autocomplete item for a hint
|
|
2547
|
+
if (this.#autocompleteState && this.#autocompleteList) {
|
|
2548
|
+
const selected = this.#autocompleteList.getSelectedItem();
|
|
2549
|
+
return selected?.hint ?? null;
|
|
2550
|
+
}
|
|
2551
|
+
|
|
2552
|
+
// Fall back to provider's getInlineHint
|
|
2553
|
+
if (this.#autocompleteProvider?.getInlineHint) {
|
|
2554
|
+
return this.#autocompleteProvider.getInlineHint(
|
|
2555
|
+
this.#state.lines,
|
|
2556
|
+
this.#state.cursorLine,
|
|
2557
|
+
this.#state.cursorCol,
|
|
2558
|
+
);
|
|
2559
|
+
}
|
|
2560
|
+
|
|
2561
|
+
return null;
|
|
2562
|
+
}
|
|
2563
|
+
}
|