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