Open History to see the last commit
1import { splitPromptIntoComposerSegments } from "./composer-editor-mentions";
2import { INLINE_TERMINAL_CONTEXT_PLACEHOLDER } from "./lib/terminalContext";
3
4export type ComposerTriggerKind = "path" | "slash-command" | "slash-model";
5export type ComposerSlashCommand = "model" | "plan" | "default";
6
7export interface ComposerTrigger {
8 kind: ComposerTriggerKind;
9 query: string;
10 rangeStart: number;
11 rangeEnd: number;
12}
13
14const SLASH_COMMANDS: readonly ComposerSlashCommand[] = ["model", "plan", "default"];
15const isInlineTokenSegment = (
16 segment: { type: "text"; text: string } | { type: "mention" } | { type: "terminal-context" },
17): boolean => segment.type !== "text";
18
19function clampCursor(text: string, cursor: number): number {
20 if (!Number.isFinite(cursor)) return text.length;
21 return Math.max(0, Math.min(text.length, Math.floor(cursor)));
22}
23
24function isWhitespace(char: string): boolean {
25 return (
26 char === " " ||
27 char === "\n" ||
28 char === "\t" ||
29 char === "\r" ||
30 char === INLINE_TERMINAL_CONTEXT_PLACEHOLDER
31 );
32}
33
34function tokenStartForCursor(text: string, cursor: number): number {
35 let index = cursor - 1;
36 while (index >= 0 && !isWhitespace(text[index] ?? "")) {
37 index -= 1;
38 }
39 return index + 1;
40}
41
42export function expandCollapsedComposerCursor(text: string, cursorInput: number): number {
43 const collapsedCursor = clampCursor(text, cursorInput);
44 const segments = splitPromptIntoComposerSegments(text);
45 if (segments.length === 0) {
46 return collapsedCursor;
47 }
48
49 let remaining = collapsedCursor;
50 let expandedCursor = 0;
51
52 for (const segment of segments) {
53 if (segment.type === "mention") {
54 const expandedLength = segment.path.length + 1;
55 if (remaining <= 1) {
56 return expandedCursor + (remaining === 0 ? 0 : expandedLength);
57 }
58 remaining -= 1;
59 expandedCursor += expandedLength;
60 continue;
61 }
62 if (segment.type === "terminal-context") {
63 if (remaining <= 1) {
64 return expandedCursor + remaining;
65 }
66 remaining -= 1;
67 expandedCursor += 1;
68 continue;
69 }
70
71 const segmentLength = segment.text.length;
72 if (remaining <= segmentLength) {
73 return expandedCursor + remaining;
74 }
75 remaining -= segmentLength;
76 expandedCursor += segmentLength;
77 }
78
79 return expandedCursor;
80}
81
82function collapsedSegmentLength(
83 segment: { type: "text"; text: string } | { type: "mention" } | { type: "terminal-context" },
84): number {
85 if (segment.type === "text") {
86 return segment.text.length;
87 }
88 return 1;
89}
90
91function clampCollapsedComposerCursorForSegments(
92 segments: ReadonlyArray<
93 { type: "text"; text: string } | { type: "mention" } | { type: "terminal-context" }
94 >,
95 cursorInput: number,
96): number {
97 const collapsedLength = segments.reduce(
98 (total, segment) => total + collapsedSegmentLength(segment),
99 0,
100 );
101 if (!Number.isFinite(cursorInput)) {
102 return collapsedLength;
103 }
104 return Math.max(0, Math.min(collapsedLength, Math.floor(cursorInput)));
105}
106
107export function clampCollapsedComposerCursor(text: string, cursorInput: number): number {
108 return clampCollapsedComposerCursorForSegments(
109 splitPromptIntoComposerSegments(text),
110 cursorInput,
111 );
112}
113
114export function collapseExpandedComposerCursor(text: string, cursorInput: number): number {
115 const expandedCursor = clampCursor(text, cursorInput);
116 const segments = splitPromptIntoComposerSegments(text);
117 if (segments.length === 0) {
118 return expandedCursor;
119 }
120
121 let remaining = expandedCursor;
122 let collapsedCursor = 0;
123
124 for (const segment of segments) {
125 if (segment.type === "mention") {
126 const expandedLength = segment.path.length + 1;
127 if (remaining === 0) {
128 return collapsedCursor;
129 }
130 if (remaining <= expandedLength) {
131 return collapsedCursor + 1;
132 }
133 remaining -= expandedLength;
134 collapsedCursor += 1;
135 continue;
136 }
137 if (segment.type === "terminal-context") {
138 if (remaining <= 1) {
139 return collapsedCursor + remaining;
140 }
141 remaining -= 1;
142 collapsedCursor += 1;
143 continue;
144 }
145
146 const segmentLength = segment.text.length;
147 if (remaining <= segmentLength) {
148 return collapsedCursor + remaining;
149 }
150 remaining -= segmentLength;
151 collapsedCursor += segmentLength;
152 }
153
154 return collapsedCursor;
155}
156
157export function isCollapsedCursorAdjacentToInlineToken(
158 text: string,
159 cursorInput: number,
160 direction: "left" | "right",
161): boolean {
162 const segments = splitPromptIntoComposerSegments(text);
163 if (!segments.some(isInlineTokenSegment)) {
164 return false;
165 }
166
167 const cursor = clampCollapsedComposerCursorForSegments(segments, cursorInput);
168 let collapsedOffset = 0;
169
170 for (const segment of segments) {
171 if (isInlineTokenSegment(segment)) {
172 if (direction === "left" && cursor === collapsedOffset + 1) {
173 return true;
174 }
175 if (direction === "right" && cursor === collapsedOffset) {
176 return true;
177 }
178 }
179 collapsedOffset += collapsedSegmentLength(segment);
180 }
181
182 return false;
183}
184
185export const isCollapsedCursorAdjacentToMention = isCollapsedCursorAdjacentToInlineToken;
186
187export function detectComposerTrigger(text: string, cursorInput: number): ComposerTrigger | null {
188 const cursor = clampCursor(text, cursorInput);
189 const lineStart = text.lastIndexOf("\n", Math.max(0, cursor - 1)) + 1;
190 const linePrefix = text.slice(lineStart, cursor);
191
192 if (linePrefix.startsWith("/")) {
193 const commandMatch = /^\/(\S*)$/.exec(linePrefix);
194 if (commandMatch) {
195 const commandQuery = commandMatch[1] ?? "";
196 if (commandQuery.toLowerCase() === "model") {
197 return {
198 kind: "slash-model",
199 query: "",
200 rangeStart: lineStart,
201 rangeEnd: cursor,
202 };
203 }
204 if (SLASH_COMMANDS.some((command) => command.startsWith(commandQuery.toLowerCase()))) {
205 return {
206 kind: "slash-command",
207 query: commandQuery,
208 rangeStart: lineStart,
209 rangeEnd: cursor,
210 };
211 }
212 return null;
213 }
214
215 const modelMatch = /^\/model(?:\s+(.*))?$/.exec(linePrefix);
216 if (modelMatch) {
217 return {
218 kind: "slash-model",
219 query: (modelMatch[1] ?? "").trim(),
220 rangeStart: lineStart,
221 rangeEnd: cursor,
222 };
223 }
224 }
225
226 const tokenStart = tokenStartForCursor(text, cursor);
227 const token = text.slice(tokenStart, cursor);
228 if (!token.startsWith("@")) {
229 return null;
230 }
231
232 return {
233 kind: "path",
234 query: token.slice(1),
235 rangeStart: tokenStart,
236 rangeEnd: cursor,
237 };
238}
239
240export function parseStandaloneComposerSlashCommand(
241 text: string,
242): Exclude<ComposerSlashCommand, "model"> | null {
243 const match = /^\/(plan|default)\s*$/i.exec(text.trim());
244 if (!match) {
245 return null;
246 }
247 const command = match[1]?.toLowerCase();
248 if (command === "plan") return "plan";
249 return "default";
250}
251
252export function replaceTextRange(
253 text: string,
254 rangeStart: number,
255 rangeEnd: number,
256 replacement: string,
257): { text: string; cursor: number } {
258 const safeStart = Math.max(0, Math.min(text.length, rangeStart));
259 const safeEnd = Math.max(safeStart, Math.min(text.length, rangeEnd));
260 const nextText = `${text.slice(0, safeStart)}${replacement}${text.slice(safeEnd)}`;
261 return { text: nextText, cursor: safeStart + replacement.length };
262}