Skip to content

Commit d6db011

Browse files
committed
fix(web): paths pasted after @ keep their underscores
Pasting src/__test__.tsx after typing @ ran the path through inline markdown parsing: __test__ became bold and the prompt text turned into @src/**test**.tsx. The part of a paste that continues an @ path query is now inserted as plain text, and the rest of its line cannot open a block; the rest of the paste is still parsed as markdown. A mention chip serializes to the same @path text, so the query has to be plain text directly before the caret.
1 parent c9fa136 commit d6db011

4 files changed

Lines changed: 120 additions & 7 deletions

File tree

‎apps/web/src/components/ComposerPromptEditorTiptap.tsx‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import {
4545
collapseExpandedComposerCursor,
4646
expandCollapsedComposerCursor,
4747
isCollapsedCursorAdjacentToInlineToken,
48+
pastedPathQueryLength,
4849
} from "~/composer-logic";
4950
import {
5051
collectComposerPromptInlineTokens,
@@ -1404,14 +1405,19 @@ function ComposerPromptEditorTiptapInner(props: ComposerPromptEditorProps) {
14041405
) {
14051406
text += " ";
14061407
}
1408+
const map = serializeEditorDoc(view.state.doc);
1409+
const offset = flatToMarkdown(map, pmToFlat(map, view.state.selection.from));
14071410
if (
14081411
(tokens[0]?.type === "mention" || tokens[0]?.type === "skill") &&
14091412
tokens[0].start === 0
14101413
) {
1411-
const map = serializeEditorDoc(view.state.doc);
1412-
const offset = flatToMarkdown(map, pmToFlat(map, view.state.selection.from));
14131414
if (offset > 0 && !/\s/.test(map.value[offset - 1]!)) text = ` ${text}`;
14141415
}
1416+
const typed = view.state.selection.$from.nodeBefore;
1417+
const typedLength = typed?.isText ? typed.text!.length : 0;
1418+
const literalLength = literalText
1419+
? 0
1420+
: pastedPathQueryLength(map.value, offset, text, typedLength);
14151421
const editorInstance = editorHolder.current;
14161422
if (editorInstance) {
14171423
// Inside a list item or quote, pasted block markup has nowhere to
@@ -1424,7 +1430,7 @@ function ComposerPromptEditorTiptapInner(props: ComposerPromptEditorProps) {
14241430
insertMarkdownParagraphs(
14251431
text,
14261432
skillLabelFor,
1427-
{ styling: richText, blocks: !nested, literalText },
1433+
{ styling: richText, blocks: !nested, literalText, literalLength },
14281434
(content) => {
14291435
// Tagged on the same transaction insertContent builds, so the
14301436
// paste is one undo step of its own.
@@ -1815,7 +1821,7 @@ function ComposerPromptEditorTiptapInner(props: ComposerPromptEditorProps) {
18151821
function insertMarkdownParagraphs(
18161822
value: string,
18171823
skillLabelFor: (name: string) => SkillMeta,
1818-
options: { styling: boolean; blocks?: boolean; literalText?: boolean },
1824+
options: { styling: boolean; blocks?: boolean; literalText?: boolean; literalLength?: number },
18191825
insertContent: (content: JSONContent[] | JSONContent) => void,
18201826
): void {
18211827
const blocks = buildTiptapContent(value, skillLabelFor, options);

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,22 @@ export function detectComposerTrigger(text: string, cursorInput: number): Compos
306306
};
307307
}
308308

309+
/**
310+
* Length of the leading part of `pasted` that continues an `@` path query at
311+
* `cursor`. `typedLength` is the plain text directly before the cursor: a
312+
* query is typed text, while a mention chip serializes to the same `@path`.
313+
*/
314+
export function pastedPathQueryLength(
315+
text: string,
316+
cursor: number,
317+
pasted: string,
318+
typedLength: number,
319+
): number {
320+
const trigger = detectComposerTrigger(text, cursor);
321+
if (trigger?.kind !== "path" || cursor - trigger.rangeStart > typedLength) return 0;
322+
return /^\S*/.exec(pasted)![0].length;
323+
}
324+
309325
/** Caret and trigger after replacing composer text and continuing at the end. */
310326
export function composerStateAtPromptEnd(
311327
text: string,

‎apps/web/src/composer-rich-text-doc.test.ts‎

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { EditorState, TextSelection } from "@tiptap/pm/state";
66
import { splitBlockKeepMarks } from "@tiptap/pm/commands";
77
import { describe, expect, it } from "vite-plus/test";
88

9-
import { collapseExpandedComposerCursor } from "./composer-logic";
9+
import { collapseExpandedComposerCursor, pastedPathQueryLength } from "./composer-logic";
1010

1111
import {
1212
buildDocJson,
@@ -942,6 +942,83 @@ describe("pasting into a list item or quote", () => {
942942
});
943943
});
944944

945+
describe("pasting into a path query", () => {
946+
const skillLabelFor = (name: string) => ({ label: name, description: null });
947+
948+
// Inserts `pasted` at the end of a one-line prompt the way the composer's
949+
// paste handler does. `prompt` may be a document, to place the caret after a chip.
950+
function paste(prompt: string | ProseMirrorNode, pasted: string) {
951+
const promptDoc =
952+
typeof prompt === "string"
953+
? ProseMirrorNode.fromJSON(schema, buildDocJson(prompt, skillLabelFor))
954+
: prompt;
955+
const value = serializeEditorDoc(promptDoc).value;
956+
const typed = promptDoc.lastChild!.lastChild;
957+
const typedLength = typed?.isText ? typed.text!.length : 0;
958+
const literalLength = pastedPathQueryLength(value, value.length, pasted, typedLength);
959+
const blocks = buildTiptapContent(pasted, skillLabelFor, { literalLength });
960+
const [first, ...rest] = blocks.map((block) => ProseMirrorNode.fromJSON(schema, block));
961+
const head = promptDoc.lastChild!.content.append(first!.content);
962+
const doc = schema.node("doc", null, [first!.copy(head), ...rest]);
963+
doc.check();
964+
return doc;
965+
}
966+
967+
function boldText(doc: ProseMirrorNode) {
968+
const bold: string[] = [];
969+
doc.descendants((node) => {
970+
if (node.isText && node.marks.some((mark) => mark.type.name === "bold")) {
971+
bold.push(node.text!);
972+
}
973+
});
974+
return bold;
975+
}
976+
977+
it.each(["src/__test__.tsx", "src/__tests__/a.ts", "pkg/__init__.py", "a/**b**/c"])(
978+
"keeps %s literal after @ so file search can match it",
979+
(path) => {
980+
const doc = paste("Open @", path);
981+
expect(boldText(doc)).toEqual([]);
982+
expect(serializeEditorDoc(doc).value).toBe(`Open @${path}`);
983+
},
984+
);
985+
986+
it("still styles the text after the path", () => {
987+
const doc = paste("Open @", "src/__test__.tsx and **this**\n- [ ] then __that__");
988+
expect(boldText(doc)).toEqual(["this", "that"]);
989+
expect(doc.lastChild!.type.name).toBe("taskList");
990+
expect(serializeEditorDoc(doc).value).toBe(
991+
"Open @src/__test__.tsx and **this**\n- [ ] then **that**",
992+
);
993+
});
994+
995+
it("keeps the rest of the path's line on that line", () => {
996+
const doc = paste("Open @", "src/a.ts - not a list item");
997+
expect(doc.childCount).toBe(1);
998+
expect(serializeEditorDoc(doc).value).toBe("Open @src/a.ts - not a list item");
999+
});
1000+
1001+
it("parses markdown when the paste does not continue a path query", () => {
1002+
expect(boldText(paste("Open ", "src/__test__.tsx"))).toEqual(["test"]);
1003+
expect(boldText(paste("Open @a ", "__b__"))).toEqual(["b"]);
1004+
expect(boldText(paste("Open @", " __b__"))).toEqual(["b"]);
1005+
});
1006+
1007+
it("parses markdown pasted right after a mention chip", () => {
1008+
const withSpace = ProseMirrorNode.fromJSON(
1009+
schema,
1010+
buildDocJson("Open @README.md ", skillLabelFor),
1011+
);
1012+
const paragraph = withSpace.firstChild!;
1013+
expect(paragraph.lastChild!.text).toBe(" ");
1014+
const chipLast = schema.node("doc", null, [
1015+
paragraph.copy(paragraph.content.cut(0, paragraph.content.size - 1)),
1016+
]);
1017+
expect(chipLast.firstChild!.lastChild!.type.name).toBe("composer-mention");
1018+
expect(boldText(paste(chipLast, "__b__"))).toEqual(["b"]);
1019+
});
1020+
});
1021+
9451022
describe("caret stops at styled edges", () => {
9461023
function stateAt(value: string, pmPos: number) {
9471024
const doc = ProseMirrorNode.fromJSON(

‎apps/web/src/composer-rich-text-doc.ts‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -534,10 +534,15 @@ function textJsonForSpan(text: string, marks: RichTextMark[]): Record<string, un
534534
return json;
535535
}
536536

537+
/**
538+
* Document content for a prompt. `literalLength` keeps that many leading
539+
* characters as plain text, for a paste that continues a path query: markers
540+
* inside a path such as `src/__init__.py` must reach file search unstyled.
541+
*/
537542
export function buildTiptapContent(
538543
value: string,
539544
skillLabelFor: (name: string) => SkillMeta,
540-
options?: { styling?: boolean; blocks?: boolean; literalText?: boolean },
545+
options?: { styling?: boolean; blocks?: boolean; literalText?: boolean; literalLength?: number },
541546
): Record<string, unknown>[] {
542547
// Editor answers are verbatim text, including Markdown and context-token sources.
543548
if (options?.literalText) {
@@ -560,7 +565,8 @@ export function buildTiptapContent(
560565
// Code fences hold no chips, so their lines put the original source back in
561566
// place of the sentinel rather than building an atom for it.
562567
const atomSources: string[] = [];
563-
const text = splitPromptIntoComposerSegments(value)
568+
const literal = value.slice(0, options?.literalLength ?? 0);
569+
const text = splitPromptIntoComposerSegments(value.slice(literal.length))
564570
.map((segment) => {
565571
if (segment.type === "text") return segment.text;
566572
atoms.push(atomJsonForSegment(segment, skillLabelFor));
@@ -608,6 +614,14 @@ export function buildTiptapContent(
608614

609615
for (let index = 0; index < sourceLines.length; index += 1) {
610616
const line = sourceLines[index]!;
617+
// The rest of the literal's line continues the text before the caret, so
618+
// it cannot open a block.
619+
if (index === 0 && literal) {
620+
entries.push({
621+
line: { list: null, inline: [textJsonForSpan(literal, []), ...buildInline(line)] },
622+
});
623+
continue;
624+
}
611625
const opening = blockSyntax ? parseOpeningFence(line) : null;
612626
if (!opening) {
613627
// A thematic break outranks a list: `- - -` and `* * *` are rules, and

0 commit comments

Comments
 (0)