Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

composer-editor-mentions.ts

211 lines5.0 KB

1import {
2 INLINE_TERMINAL_CONTEXT_PLACEHOLDER,
3 type TerminalContextDraft,
4} from "./lib/terminalContext";
5​
6export type ComposerPromptSegment =
7 | {
8 type: "text";
9 text: string;
10 }
11 | {
12 type: "mention";
13 path: string;
14 }
15 | {
16 type: "terminal-context";
17 context: TerminalContextDraft | null;
18 };
19​
20const MENTION_TOKEN_REGEX = /(^|\s)@([^\s@]+)(?=\s)/g;
21​
22function rangeIncludesIndex(start: number, end: number, index: number): boolean {
23 return start <= index && index < end;
25​
26function pushTextSegment(segments: ComposerPromptSegment[], text: string): void {
27 if (!text) return;
28 const last = segments[segments.length - 1];
29 if (last && last.type === "text") {
30 last.text += text;
31 return;
32 }
33 segments.push({ type: "text", text });
35​
36function forEachPromptSegmentSlice(
37 prompt: string,
38 visitor: (
39 slice:
40 | {
41 type: "text";
42 text: string;
43 promptOffset: number;
44 }
45 | {
46 type: "terminal-context";
47 promptOffset: number;
48 },
49 ) => boolean | void,
50): boolean {
51 let textCursor = 0;
52​
53 for (let index = 0; index < prompt.length; index += 1) {
54 if (prompt[index] !== INLINE_TERMINAL_CONTEXT_PLACEHOLDER) {
55 continue;
56 }
57​
58 if (
59 index > textCursor &&
60 visitor({
61 type: "text",
62 text: prompt.slice(textCursor, index),
63 promptOffset: textCursor,
64 }) === true
65 ) {
66 return true;
67 }
68 if (visitor({ type: "terminal-context", promptOffset: index }) === true) {
69 return true;
70 }
71 textCursor = index + 1;
72 }
73​
74 if (
75 textCursor < prompt.length &&
76 visitor({
77 type: "text",
78 text: prompt.slice(textCursor),
79 promptOffset: textCursor,
80 }) === true
81 ) {
82 return true;
83 }
84​
85 return false;
87​
88function forEachPromptTextSlice(
89 prompt: string,
90 visitor: (text: string, promptOffset: number) => boolean | void,
91): boolean {
92 return forEachPromptSegmentSlice(prompt, (slice) => {
93 if (slice.type !== "text") {
94 return false;
95 }
96 return visitor(slice.text, slice.promptOffset);
97 });
99​
100function forEachMentionMatch(
101 prompt: string,
102 visitor: (match: RegExpMatchArray, promptOffset: number) => boolean | void,
103): boolean {
104 return forEachPromptTextSlice(prompt, (text, promptOffset) => {
105 for (const match of text.matchAll(MENTION_TOKEN_REGEX)) {
106 if (visitor(match, promptOffset) === true) {
107 return true;
108 }
109 }
110 return false;
111 });
113​
114function splitPromptTextIntoComposerSegments(text: string): ComposerPromptSegment[] {
115 const segments: ComposerPromptSegment[] = [];
116 if (!text) {
117 return segments;
118 }
119​
120 let cursor = 0;
121 for (const match of text.matchAll(MENTION_TOKEN_REGEX)) {
122 const fullMatch = match[0];
123 const prefix = match[1] ?? "";
124 const path = match[2] ?? "";
125 const matchIndex = match.index ?? 0;
126 const mentionStart = matchIndex + prefix.length;
127 const mentionEnd = mentionStart + fullMatch.length - prefix.length;
128​
129 if (mentionStart > cursor) {
130 pushTextSegment(segments, text.slice(cursor, mentionStart));
131 }
132​
133 if (path.length > 0) {
134 segments.push({ type: "mention", path });
135 } else {
136 pushTextSegment(segments, text.slice(mentionStart, mentionEnd));
137 }
138​
139 cursor = mentionEnd;
140 }
141​
142 if (cursor < text.length) {
143 pushTextSegment(segments, text.slice(cursor));
144 }
145​
146 return segments;
148​
149export function selectionTouchesMentionBoundary(
150 prompt: string,
151 start: number,
152 end: number,
153): boolean {
154 if (!prompt || start >= end) {
155 return false;
156 }
157​
158 return forEachMentionMatch(prompt, (match, promptOffset) => {
159 const fullMatch = match[0];
160 const prefix = match[1] ?? "";
161 const matchIndex = match.index ?? 0;
162 const mentionStart = promptOffset + matchIndex + prefix.length;
163 const mentionEnd = mentionStart + fullMatch.length - prefix.length;
164 const beforeMentionIndex = mentionStart - 1;
165 const afterMentionIndex = mentionEnd;
166​
167 if (
168 beforeMentionIndex >= 0 &&
169 /\s/.test(prompt[beforeMentionIndex] ?? "") &&
170 rangeIncludesIndex(start, end, beforeMentionIndex)
171 ) {
172 return true;
173 }
174​
175 if (
176 afterMentionIndex < prompt.length &&
177 /\s/.test(prompt[afterMentionIndex] ?? "") &&
178 rangeIncludesIndex(start, end, afterMentionIndex)
179 ) {
180 return true;
181 }
182 return false;
183 });
185​
186export function splitPromptIntoComposerSegments(
187 prompt: string,
188 terminalContexts: ReadonlyArray<TerminalContextDraft> = [],
189): ComposerPromptSegment[] {
190 if (!prompt) {
191 return [];
192 }
193​
194 const segments: ComposerPromptSegment[] = [];
195 let terminalContextIndex = 0;
196 forEachPromptSegmentSlice(prompt, (slice) => {
197 if (slice.type === "text") {
198 segments.push(...splitPromptTextIntoComposerSegments(slice.text));
199 return false;
200 }
201​
202 segments.push({
203 type: "terminal-context",
204 context: terminalContexts[terminalContextIndex] ?? null,
205 });
206 terminalContextIndex += 1;
207 return false;
208 });
209​
210 return segments;