Skip to content

Commit f15794c

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; the rest of the paste is still parsed as markdown.
1 parent 8ddf200 commit f15794c

4 files changed

Lines changed: 77 additions & 10 deletions

File tree

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import {
3838
collapseExpandedComposerCursor,
3939
expandCollapsedComposerCursor,
4040
isCollapsedCursorAdjacentToInlineToken,
41+
pastedPathQueryLength,
4142
} from "~/composer-logic";
4243
import {
4344
collectComposerPromptInlineTokens,
@@ -1063,17 +1064,19 @@ function ComposerPromptEditorTiptapInner(props: ComposerPromptEditorProps) {
10631064
) {
10641065
text += " ";
10651066
}
1067+
const map = serializeEditorDoc(view.state.doc);
1068+
const offset = flatToMarkdown(map, pmToFlat(map, view.state.selection.from));
10661069
if (
10671070
(tokens[0]?.type === "mention" || tokens[0]?.type === "skill") &&
10681071
tokens[0].start === 0
10691072
) {
1070-
const map = serializeEditorDoc(view.state.doc);
1071-
const offset = flatToMarkdown(map, pmToFlat(map, view.state.selection.from));
10721073
if (offset > 0 && !/\s/.test(map.value[offset - 1]!)) text = ` ${text}`;
10731074
}
1075+
const literalLength = pastedPathQueryLength(map.value, offset, text);
10741076
const editorInstance = editorHolder.current;
10751077
if (editorInstance) {
1076-
insertMarkdownParagraphs(text, skillLabelFor, { styling: richText }, (content) => {
1078+
const options = { styling: richText, literalLength };
1079+
insertMarkdownParagraphs(text, skillLabelFor, options, (content) => {
10771080
// Tagged on the same transaction insertContent builds, so the
10781081
// paste is one undo step of its own.
10791082
editorInstance
@@ -1438,7 +1441,7 @@ function ComposerPromptEditorTiptapInner(props: ComposerPromptEditorProps) {
14381441
function insertMarkdownParagraphs(
14391442
value: string,
14401443
skillLabelFor: (name: string) => SkillMeta,
1441-
options: { styling: boolean },
1444+
options: { styling: boolean; literalLength: number },
14421445
insertContent: (content: JSONContent[] | JSONContent) => void,
14431446
): void {
14441447
const blocks = buildTiptapContent(value, skillLabelFor, options);

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,12 @@ export function detectComposerTrigger(text: string, cursorInput: number): Compos
291291
};
292292
}
293293

294+
/** Length of the leading part of `pasted` that continues an `@` path query at `cursor`. */
295+
export function pastedPathQueryLength(text: string, cursor: number, pasted: string): number {
296+
if (detectComposerTrigger(text, cursor)?.kind !== "path") return 0;
297+
return /^\S*/.exec(pasted)![0].length;
298+
}
299+
294300
/** Caret and trigger after replacing composer text and continuing at the end. */
295301
export function composerStateAtPromptEnd(text: string): {
296302
cursor: number;

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

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { getSchemaByResolvedExtensions, Node, resolveExtensions } from "@tiptap/core";
22
import StarterKit from "@tiptap/starter-kit";
33
import { TaskList } from "@tiptap/extension-task-list";
4-
import { Node as ProseMirrorNode } from "@tiptap/pm/model";
4+
import { Fragment, Node as ProseMirrorNode } from "@tiptap/pm/model";
55
import { EditorState, TextSelection } from "@tiptap/pm/state";
66
import { describe, expect, it } from "vite-plus/test";
77

8+
import { pastedPathQueryLength } from "./composer-logic";
89
import {
910
buildDocJson,
11+
buildTiptapContent,
1012
caretTakesMarksBefore,
1113
collapsedToFlat,
1214
ComposerCodeExtension,
@@ -365,6 +367,55 @@ describe("composer rich text document model", () => {
365367
});
366368
});
367369

370+
describe("pasting into a path query", () => {
371+
const skillLabelFor = (name: string) => ({ label: name, description: null });
372+
373+
// Inserts `pasted` at the end of `prompt` the way the composer's paste handler does.
374+
function paste(prompt: string, pasted: string) {
375+
const literalLength = pastedPathQueryLength(prompt, prompt.length, pasted);
376+
const blocks = buildTiptapContent(pasted, skillLabelFor, { literalLength });
377+
const [first, ...rest] = blocks.map((block) => ProseMirrorNode.fromJSON(schema, block));
378+
const head = Fragment.from(schema.text(prompt)).append(first!.content);
379+
const doc = schema.node("doc", null, [first!.copy(head), ...rest]);
380+
doc.check();
381+
return doc;
382+
}
383+
384+
function boldText(doc: ProseMirrorNode) {
385+
const bold: string[] = [];
386+
doc.descendants((node) => {
387+
if (node.isText && node.marks.some((mark) => mark.type.name === "bold")) {
388+
bold.push(node.text!);
389+
}
390+
});
391+
return bold;
392+
}
393+
394+
it.each(["src/__test__.tsx", "src/__tests__/a.ts", "pkg/__init__.py", "a/**b**/c"])(
395+
"keeps %s literal after @ so file search can match it",
396+
(path) => {
397+
const doc = paste("Open @", path);
398+
expect(boldText(doc)).toEqual([]);
399+
expect(serializeEditorDoc(doc).value).toBe(`Open @${path}`);
400+
},
401+
);
402+
403+
it("still styles the text after the path", () => {
404+
const doc = paste("Open @", "src/__test__.tsx and **this**\n- [ ] then __that__");
405+
expect(boldText(doc)).toEqual(["this", "that"]);
406+
expect(doc.lastChild!.type.name).toBe("taskList");
407+
expect(serializeEditorDoc(doc).value).toBe(
408+
"Open @src/__test__.tsx and **this**\n- [ ] then **that**",
409+
);
410+
});
411+
412+
it("parses markdown when the paste does not continue a path query", () => {
413+
expect(boldText(paste("Open ", "src/__test__.tsx"))).toEqual(["test"]);
414+
expect(boldText(paste("Open @a ", "__b__"))).toEqual(["b"]);
415+
expect(boldText(paste("Open @", " __b__"))).toEqual(["b"]);
416+
});
417+
});
418+
368419
describe("caret stops at styled edges", () => {
369420
function stateAt(value: string, pmPos: number) {
370421
const doc = ProseMirrorNode.fromJSON(

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,32 +177,39 @@ function textJsonForSpan(text: string, marks: RichTextMark[]): Record<string, un
177177
return json;
178178
}
179179

180+
/**
181+
* Document content for a prompt. `literalLength` keeps that many leading
182+
* characters as plain text, for a paste that continues a path query: markers
183+
* inside a path such as `src/__init__.py` must reach file search unstyled.
184+
*/
180185
export function buildTiptapContent(
181186
value: string,
182187
skillLabelFor: (name: string) => SkillMeta,
183-
options?: { styling?: boolean },
188+
options?: { styling?: boolean; literalLength?: number },
184189
): Record<string, unknown>[] {
185190
const styling = options?.styling ?? true;
191+
const literal = value.slice(0, options?.literalLength ?? 0);
186192
// Hide token source from the markdown parser, then restore the atoms with
187193
// the marks of their surrounding text. Choose a sentinel absent from input.
188194
let sentinel = "\uFFFC";
189195
for (let codePoint = 0xe000; value.includes(sentinel); codePoint += 1) {
190196
sentinel = String.fromCodePoint(codePoint);
191197
}
192198
const atoms: InlineJson[] = [];
193-
const text = splitPromptIntoComposerSegments(value)
199+
const text = splitPromptIntoComposerSegments(value.slice(literal.length))
194200
.map((segment) => {
195201
if (segment.type === "text") return segment.text;
196202
atoms.push(atomJsonForSegment(segment, skillLabelFor));
197203
return sentinel;
198204
})
199205
.join("");
200206
let atomIndex = 0;
201-
const lines: DocLine[] = text.split("\n").map((line) => {
202-
const parsed = styling ? parseTaskPrefix(line) : null;
207+
const lines: DocLine[] = text.split("\n").map((line, lineIndex) => {
208+
const head = lineIndex === 0 ? literal : "";
209+
const parsed = styling && !head ? parseTaskPrefix(line) : null;
203210
const content = parsed ? line.slice(parsed.markerLength) : line;
204211
const spans = styling ? parseInlineMarkdown(content) : [{ text: content, marks: [] }];
205-
const inline: InlineJson[] = [];
212+
const inline: InlineJson[] = head ? [textJsonForSpan(head, [])] : [];
206213
for (const span of spans) {
207214
span.text.split(sentinel).forEach((piece, index) => {
208215
if (index > 0) {

0 commit comments

Comments
 (0)