Skip to content
This repository was archived by the owner on Oct 10, 2026. It is now read-only.

Commit c7b21ff

Browse files
maxwellyoungcodexjuliusmarminge
authored
[codex] keep scoped package references as text (pingdotgg#4167)
Co-authored-by: codex <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent 4e09cdd commit c7b21ff

5 files changed

Lines changed: 169 additions & 5 deletions

File tree

‎apps/web/src/components/ComposerPromptEditor.test.ts‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,83 @@ describe("registerComposerInlineTokenPaste", () => {
7070
"<mention:.changeset/improve-deploy-error-logging.md> ",
7171
);
7272
});
73+
74+
it.each([
75+
"yarn expo install @expo/ui",
76+
"npm install @jane/foo.js",
77+
"import '@scope/pkg/sub/path'",
78+
])("leaves scoped package command %s to the plain-text paste fallback", (command) => {
79+
vi.stubGlobal("ClipboardEvent", TestClipboardEvent);
80+
const editor = createEditor();
81+
const plainTextFallback = vi.fn((event: ClipboardEvent) => {
82+
const selection = $getSelection();
83+
if (!$isRangeSelection(selection)) return false;
84+
selection.insertText(event.clipboardData?.getData("text/plain") ?? "");
85+
return true;
86+
});
87+
88+
editor.update(
89+
() => {
90+
const paragraph = $createParagraphNode();
91+
$getRoot().append(paragraph);
92+
paragraph.selectEnd();
93+
},
94+
{ discrete: true },
95+
);
96+
registerComposerInlineTokenPaste(editor, {
97+
createMentionNode: (path) => $createTextNode(`<mention:${path}>`),
98+
getExpandedAbsoluteOffsetForPoint: () => 0,
99+
});
100+
editor.registerCommand(PASTE_COMMAND, plainTextFallback, COMMAND_PRIORITY_EDITOR);
101+
102+
const event = new TestClipboardEvent(command);
103+
let handled = false;
104+
editor.update(
105+
() => {
106+
handled = editor.dispatchCommand(PASTE_COMMAND, event as ClipboardEvent);
107+
},
108+
{ discrete: true },
109+
);
110+
111+
expect(handled).toBe(true);
112+
expect(plainTextFallback).toHaveBeenCalledOnce();
113+
expect(editor.getEditorState().read(() => $getRoot().getTextContent())).toBe(command);
114+
});
115+
116+
it("pastes a canonical scoped folder link as a mention", () => {
117+
vi.stubGlobal("ClipboardEvent", TestClipboardEvent);
118+
const editor = createEditor();
119+
const mention = "[sub](@scope/pkg/sub)";
120+
const plainTextFallback = vi.fn(() => true);
121+
122+
editor.update(
123+
() => {
124+
const paragraph = $createParagraphNode();
125+
$getRoot().append(paragraph);
126+
paragraph.selectEnd();
127+
},
128+
{ discrete: true },
129+
);
130+
registerComposerInlineTokenPaste(editor, {
131+
createMentionNode: (path) => $createTextNode(`<mention:${path}>`),
132+
getExpandedAbsoluteOffsetForPoint: () => 0,
133+
});
134+
editor.registerCommand(PASTE_COMMAND, plainTextFallback, COMMAND_PRIORITY_EDITOR);
135+
136+
const event = new TestClipboardEvent(mention);
137+
let handled = false;
138+
editor.update(
139+
() => {
140+
handled = editor.dispatchCommand(PASTE_COMMAND, event as ClipboardEvent);
141+
},
142+
{ discrete: true },
143+
);
144+
145+
expect(handled).toBe(true);
146+
expect(plainTextFallback).not.toHaveBeenCalled();
147+
expect(event.defaultPrevented).toBe(true);
148+
expect(editor.getEditorState().read(() => $getRoot().getTextContent())).toBe(
149+
"<mention:@scope/pkg/sub> ",
150+
);
151+
});
73152
});

‎apps/web/src/composer-editor-mentions.test.ts‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ describe("splitPromptIntoComposerSegments", () => {
2222
});
2323

2424
it("keeps newlines around mention tokens", () => {
25-
expect(splitPromptIntoComposerSegments("one\n@src/index.ts \ntwo")).toEqual([
25+
expect(splitPromptIntoComposerSegments("one\n@AGENTS.md \ntwo")).toEqual([
2626
{ type: "text", text: "one\n" },
27-
{ type: "mention", path: "src/index.ts", source: "@src/index.ts" },
27+
{ type: "mention", path: "AGENTS.md", source: "@AGENTS.md" },
2828
{ type: "text", text: " \ntwo" },
2929
]);
3030
});
@@ -71,6 +71,31 @@ describe("splitPromptIntoComposerSegments", () => {
7171
).toEqual([{ type: "text", text: "Read [the docs](https://example.com/docs) first" }]);
7272
});
7373

74+
it.each(["@expo/ui", "@jane/foo.js", "@scope/pkg/sub/path"])(
75+
"does not turn scoped package reference %s into file mention segments",
76+
(reference) => {
77+
const prompt = `Install ${reference} next`;
78+
expect(splitPromptIntoComposerSegments(prompt)).toEqual([{ type: "text", text: prompt }]);
79+
},
80+
);
81+
82+
it("keeps IME-composed text containing a scoped package reference as text", () => {
83+
const prompt = "入力 @expo/ui を追加";
84+
expect(splitPromptIntoComposerSegments(prompt)).toEqual([{ type: "text", text: prompt }]);
85+
});
86+
87+
it("turns canonical scoped folder links into file mention segments", () => {
88+
expect(splitPromptIntoComposerSegments("Inspect [sub](@scope/pkg/sub) next")).toEqual([
89+
{ type: "text", text: "Inspect " },
90+
{
91+
type: "mention",
92+
path: "@scope/pkg/sub",
93+
source: "[sub](@scope/pkg/sub)",
94+
},
95+
{ type: "text", text: " next" },
96+
]);
97+
});
98+
7499
it("decodes reserved path characters from generated links", () => {
75100
expect(
76101
splitPromptIntoComposerSegments(

‎apps/web/src/composer-logic.test.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,12 +246,21 @@ describe("collapseExpandedComposerCursor", () => {
246246
);
247247
});
248248

249-
it("keeps replacement cursors aligned when another mention already exists earlier", () => {
249+
it("keeps package-like text expanded when another mention already exists earlier", () => {
250250
const text = "open @AGENTS.md then @src/index.ts ";
251251
const expandedCursor = text.length;
252252
const collapsedCursor = collapseExpandedComposerCursor(text, expandedCursor);
253253

254-
expect(collapsedCursor).toBe("open ".length + 1 + " then ".length + 2);
254+
expect(collapsedCursor).toBe("open ".length + 1 + " then @src/index.ts ".length);
255+
expect(expandCollapsedComposerCursor(text, collapsedCursor)).toBe(expandedCursor);
256+
});
257+
258+
it("collapses only genuine mentions when package-like text exists earlier", () => {
259+
const text = "install @scope/pkg then @README.md ";
260+
const expandedCursor = text.length;
261+
const collapsedCursor = collapseExpandedComposerCursor(text, expandedCursor);
262+
263+
expect(collapsedCursor).toBe("install @scope/pkg then ".length + 1 + " ".length);
255264
expect(expandCollapsedComposerCursor(text, collapsedCursor)).toBe(expandedCursor);
256265
});
257266

‎packages/shared/src/composerInlineTokens.test.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,52 @@ describe("collectComposerInlineTokens", () => {
8181
it("ignores normal web links", () => {
8282
expect(collectComposerInlineTokens("Read [docs](https://example.com) first")).toEqual([]);
8383
});
84+
85+
it.each(["@expo/ui", "@jane/foo.js", "@scope/pkg/sub/path"])(
86+
"keeps scoped package reference %s as plain text",
87+
(reference) => {
88+
expect(collectComposerInlineTokens(`Install ${reference} next`)).toEqual([]);
89+
},
90+
);
91+
92+
it("keeps scoped package references plain across incomplete input and IME whitespace", () => {
93+
expect(collectComposerInlineTokens("Install @expo/ui")).toEqual([]);
94+
expect(collectComposerInlineTokens("入力 @expo/ui を追加")).toEqual([]);
95+
});
96+
97+
it("keeps bare non-scoped file paths as mentions", () => {
98+
expect(collectComposerInlineTokens("Inspect @README.md next")).toEqual([
99+
{
100+
type: "mention",
101+
value: "README.md",
102+
source: "@README.md",
103+
start: 8,
104+
end: 18,
105+
},
106+
]);
107+
});
108+
109+
it("keeps canonical file links for scoped paths as mentions", () => {
110+
expect(collectComposerInlineTokens("Inspect [sub](@scope/pkg/sub) next")).toEqual([
111+
{
112+
type: "mention",
113+
value: "@scope/pkg/sub",
114+
source: "[sub](@scope/pkg/sub)",
115+
start: 8,
116+
end: 29,
117+
},
118+
]);
119+
});
120+
121+
it("allows ambiguous scoped paths through explicit quoted mentions", () => {
122+
expect(collectComposerInlineTokens('Inspect @"expo/ui" next')).toEqual([
123+
{
124+
type: "mention",
125+
value: "expo/ui",
126+
source: '@"expo/ui"',
127+
start: 8,
128+
end: 18,
129+
},
130+
]);
131+
});
84132
});

‎packages/shared/src/composerInlineTokens.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ const MENTION_TOKEN_REGEX = /(^|\s)@(?:"((?:\\.|[^"\\])*)"|([^\s@"]+))(?=\s)/g;
2323
const FILE_LINK_TOKEN_REGEX = /(^|\s)\[((?:\\.|[^\]\\])*)\]\(([^)\s]+)\)(?=\s)/g;
2424
const URI_SCHEME_REGEX = /^[A-Za-z][A-Za-z0-9+.-]*:/;
2525
const WINDOWS_DRIVE_PATH_REGEX = /^[A-Za-z]:[\\/]/;
26+
// Autocomplete emits canonical file links, so ambiguous bare @scope/package text stays a package.
27+
const SCOPED_PACKAGE_REFERENCE_REGEX =
28+
/^[a-z0-9][a-z0-9._-]*\/[a-z0-9][a-z0-9._-]*(?:\/[^\s@"]+)*$/;
2629

2730
function collectMentionTokens(text: string): ComposerInlineToken[] {
2831
const matches: ComposerInlineToken[] = [];
@@ -60,7 +63,7 @@ function collectMentionTokens(text: string): ComposerInlineToken[] {
6063
const prefix = match[1] ?? "";
6164
const quotedPath = match[2];
6265
const path = quotedPath !== undefined ? quotedPath.replace(/\\(.)/g, "$1") : (match[3] ?? "");
63-
if (!path) {
66+
if (!path || (quotedPath === undefined && SCOPED_PACKAGE_REFERENCE_REGEX.test(path))) {
6467
continue;
6568
}
6669
const start = (match.index ?? 0) + prefix.length;

0 commit comments

Comments
 (0)