Open History to see the last commit
1export type ComposerInlineToken =
2 | {
3 readonly type: "mention";
4 readonly value: string;
5 readonly source: string;
6 readonly start: number;
7 readonly end: number;
8 }
9 | {
10 readonly type: "skill";
11 readonly value: string;
12 readonly source: string;
13 readonly start: number;
14 readonly end: number;
15 };
16
17export interface CollectComposerInlineTokensOptions {
18 readonly preserveTrailingFrom?: ReadonlyArray<ComposerInlineToken>;
19}
20
21const SKILL_TOKEN_REGEX = /(^|\s)\$([a-zA-Z][a-zA-Z0-9:_-]*)(?=\s)/g;
22const MENTION_TOKEN_REGEX = /(^|\s)@(?:"((?:\\.|[^"\\])*)"|([^\s@"]+))(?=\s)/g;
23const FILE_LINK_TOKEN_REGEX = /(^|\s)\[((?:\\.|[^\]\\])*)\]\(([^)\s]+)\)(?=\s)/g;
24const URI_SCHEME_REGEX = /^[A-Za-z][A-Za-z0-9+.-]*:/;
25const WINDOWS_DRIVE_PATH_REGEX = /^[A-Za-z]:[\\/]/;
26// Autocomplete emits canonical file links, so ambiguous bare @scope/package text stays a package.
27const SCOPED_PACKAGE_REFERENCE_REGEX =
28 /^[a-z0-9][a-z0-9._-]*\/[a-z0-9][a-z0-9._-]*(?:\/[^\s@"]+)*$/;
29
30function collectMentionTokens(text: string): ComposerInlineToken[] {
31 const matches: ComposerInlineToken[] = [];
32
33 for (const match of text.matchAll(FILE_LINK_TOKEN_REGEX)) {
34 const fullMatch = match[0];
35 const prefix = match[1] ?? "";
36 const label = (match[2] ?? "").replace(/\\(.)/g, "$1");
37 const encodedPath = match[3] ?? "";
38 let path = encodedPath;
39 try {
40 path = decodeURIComponent(encodedPath);
41 } catch {
42 // Preserve malformed source rather than dropping a user-authored token.
43 }
44 const separatorIndex = Math.max(path.lastIndexOf("/"), path.lastIndexOf("\\"));
45 const basename = separatorIndex >= 0 ? path.slice(separatorIndex + 1) : path;
46 const hasExternalScheme = URI_SCHEME_REGEX.test(path) && !WINDOWS_DRIVE_PATH_REGEX.test(path);
47 if (!path || hasExternalScheme || label !== basename) {
48 continue;
49 }
50 const start = (match.index ?? 0) + prefix.length;
51 const end = start + fullMatch.length - prefix.length;
52 matches.push({
53 type: "mention",
54 value: path,
55 source: text.slice(start, end),
56 start,
57 end,
58 });
59 }
60
61 for (const match of text.matchAll(MENTION_TOKEN_REGEX)) {
62 const fullMatch = match[0];
63 const prefix = match[1] ?? "";
64 const quotedPath = match[2];
65 const path = quotedPath !== undefined ? quotedPath.replace(/\\(.)/g, "$1") : (match[3] ?? "");
66 if (!path || (quotedPath === undefined && SCOPED_PACKAGE_REFERENCE_REGEX.test(path))) {
67 continue;
68 }
69 const start = (match.index ?? 0) + prefix.length;
70 const end = start + fullMatch.length - prefix.length;
71 matches.push({
72 type: "mention",
73 value: path,
74 source: text.slice(start, end),
75 start,
76 end,
77 });
78 }
79
80 return matches;
81}
82
83export function collectComposerInlineTokens(
84 text: string,
85 options: CollectComposerInlineTokensOptions = {},
86): ReadonlyArray<ComposerInlineToken> {
87 const matches = collectMentionTokens(text);
88
89 for (const match of text.matchAll(SKILL_TOKEN_REGEX)) {
90 const fullMatch = match[0];
91 const prefix = match[1] ?? "";
92 const value = match[2] ?? "";
93 if (!value) {
94 continue;
95 }
96 const start = (match.index ?? 0) + prefix.length;
97 const end = start + fullMatch.length - prefix.length;
98 matches.push({
99 type: "skill",
100 value,
101 source: text.slice(start, end),
102 start,
103 end,
104 });
105 }
106
107 for (const token of options.preserveTrailingFrom ?? []) {
108 if (
109 token.end === text.length &&
110 text.slice(token.start, token.end) === token.source &&
111 !matches.some(
112 (match) =>
113 match.type === token.type && match.start === token.start && match.end === token.end,
114 )
115 ) {
116 matches.push(token);
117 }
118 }
119
120 return [...matches].sort((left, right) => left.start - right.start);
121}