Skip to content

Commit cb3ecbf

Browse files
chickenputtyclaude
andcommitted
fix(web): Open in editor on a chat file chip opens the file the chip names
A click on a chip with a bare filename (`user.ts:261`) looks the name up in the workspace index, so it opens src/.../user.ts. Open in editor and the copy actions skipped that lookup and used `<workspace>/user.ts`, so VS Code opened an empty unsaved file at the root and the copied paths pointed there too. Open in editor, Reveal and both copy actions now resolve through one helper that asks the index first, the same lookup the click and Reveal already used. Fixes #14624. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 8d199b4 commit cb3ecbf

3 files changed

Lines changed: 172 additions & 22 deletions

File tree

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

Lines changed: 53 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
import { MarkdownFindContext, useFindRevealRef } from "./chat/markdownFindContext";
2-
import {
3-
buildFileLinkParentSuffixByPath,
4-
fileLinkLabel,
5-
resolvePathLinkTarget,
6-
} from "@t3tools/shared/fileLinks";
2+
import { buildFileLinkParentSuffixByPath, fileLinkLabel } from "@t3tools/shared/fileLinks";
73
import {
84
isWindowsDrivePathHref,
95
normalizeMarkdownLinkDestination,
@@ -172,6 +168,11 @@ import {
172168
shouldOpenMarkdownFileLinkInEditor,
173169
type MarkdownFileLinkMeta,
174170
} from "../markdown-links";
171+
import {
172+
markdownFileCopyPaths,
173+
markdownFileEditorTarget,
174+
resolveMarkdownFileHostPath,
175+
} from "../markdownFileHostPath";
175176
import { isMarkdownFileLinkLabel } from "@t3tools/shared/markdownLinks";
176177
import { readLocalApi } from "../localApi";
177178
import { useAssetUrlRefresh, useAssetUrlState } from "../assets/assetUrls";
@@ -1208,6 +1209,10 @@ interface MarkdownFileLinkProps {
12081209
threadRef?: ScopedThreadRef | undefined;
12091210
onOpen?: ((targetPath: string) => Promise<AtomCommandResult<unknown, unknown>>) | undefined;
12101211
onOpenInPanel: (panelPath: string, line: number | undefined) => void;
1212+
/** The paths the copy actions use, once a bare filename is found in the workspace. */
1213+
resolveCopyPaths?:
1214+
| (() => Promise<{ readonly fullPath: string; readonly relativePath: string }>)
1215+
| undefined;
12111216
openInEditorMenuLabel: string;
12121217
onOpenInBrowser?: (() => Promise<AtomCommandResult<unknown, unknown>>) | undefined;
12131218
onOpenMedia?: (() => void) | undefined;
@@ -1949,6 +1954,7 @@ const MarkdownFileLink = memo(function MarkdownFileLink({
19491954
onOpenMedia,
19501955
onReveal,
19511956
revealLabel,
1957+
resolveCopyPaths,
19521958
}: MarkdownFileLinkProps) {
19531959
const handleOpenInEditor = useCallback(() => {
19541960
if (!onOpen) {
@@ -2151,12 +2157,15 @@ const MarkdownFileLink = memo(function MarkdownFileLink({
21512157
handleRevealInFileManager();
21522158
return;
21532159
}
2154-
if (clicked === "copy-relative") {
2155-
handleCopy(displayPath, "Relative path");
2156-
return;
2157-
}
2158-
if (clicked === "copy-full") {
2159-
handleCopy(targetPath, "Full path");
2160+
if (clicked === "copy-relative" || clicked === "copy-full") {
2161+
const paths = resolveCopyPaths
2162+
? await resolveCopyPaths()
2163+
: { fullPath: targetPath, relativePath: displayPath };
2164+
if (clicked === "copy-relative") {
2165+
handleCopy(paths.relativePath, "Relative path");
2166+
} else {
2167+
handleCopy(paths.fullPath, "Full path");
2168+
}
21602169
}
21612170
} catch (cause) {
21622171
reportMarkdownActionFailure(
@@ -2176,6 +2185,7 @@ const MarkdownFileLink = memo(function MarkdownFileLink({
21762185
onOpen,
21772186
onReveal,
21782187
openInEditorMenuLabel,
2188+
resolveCopyPaths,
21792189
revealLabel,
21802190
targetPath,
21812191
],
@@ -2299,6 +2309,7 @@ function areMarkdownFileLinkPropsEqual(
22992309
previous.onOpenInBrowser === next.onOpenInBrowser &&
23002310
previous.onOpenMedia === next.onOpenMedia &&
23012311
previous.onReveal === next.onReveal &&
2312+
previous.resolveCopyPaths === next.resolveCopyPaths &&
23022313
previous.revealLabel === next.revealLabel
23032314
);
23042315
}
@@ -2630,16 +2641,34 @@ function useChatMarkdownState({
26302641
},
26312642
[cwd, findWorkspaceBasenameMatch, threadRef],
26322643
);
2644+
// Open in editor, reveal and the copy actions find a bare filename in the
2645+
// workspace first, as a click does. Without it they act on a file of that
2646+
// name at the workspace root, which usually does not exist.
2647+
const resolveChipHostPath = useCallback(
2648+
(fileLinkMeta: MarkdownFileLinkMeta) =>
2649+
resolveMarkdownFileHostPath({
2650+
meta: fileLinkMeta,
2651+
cwd,
2652+
findWorkspaceMatch: findWorkspaceBasenameMatch,
2653+
}),
2654+
[cwd, findWorkspaceBasenameMatch],
2655+
);
2656+
const openMarkdownFileInEditor = useCallback(
2657+
async (fileLinkMeta: MarkdownFileLinkMeta) =>
2658+
openInPreferredEditor(
2659+
markdownFileEditorTarget(fileLinkMeta, await resolveChipHostPath(fileLinkMeta)),
2660+
),
2661+
[openInPreferredEditor, resolveChipHostPath],
2662+
);
26332663
const revealMarkdownFileInFileManager = useCallback(
2634-
async (fileLinkMeta: MarkdownFileLinkMeta) => {
2635-
const workspaceRelativePath = fileLinkMeta.workspaceRelativePath;
2636-
const match = workspaceRelativePath
2637-
? await findWorkspaceBasenameMatch(workspaceRelativePath)
2638-
: null;
2639-
const filePath = match && cwd ? resolvePathLinkTarget(match, cwd) : fileLinkMeta.filePath;
2640-
return revealFileInFileManager(filePath);
2641-
},
2642-
[cwd, findWorkspaceBasenameMatch, revealFileInFileManager],
2664+
async (fileLinkMeta: MarkdownFileLinkMeta) =>
2665+
revealFileInFileManager((await resolveChipHostPath(fileLinkMeta)).absolutePath),
2666+
[resolveChipHostPath, revealFileInFileManager],
2667+
);
2668+
const resolveMarkdownFileCopyPaths = useCallback(
2669+
async (fileLinkMeta: MarkdownFileLinkMeta) =>
2670+
markdownFileCopyPaths(fileLinkMeta, await resolveChipHostPath(fileLinkMeta), cwd),
2671+
[cwd, resolveChipHostPath],
26432672
);
26442673
const fileLinkChip = useCallback(
26452674
(fileLinkMeta: MarkdownFileLinkMeta, copyMarkdown: string, mediaSource?: string) => {
@@ -2674,7 +2703,8 @@ function useChatMarkdownState({
26742703
copyMarkdown={copyMarkdown}
26752704
theme={resolvedTheme}
26762705
threadRef={threadRef}
2677-
{...(canUseShellActions ? { onOpen: openInPreferredEditor } : {})}
2706+
{...(canUseShellActions ? { onOpen: () => openMarkdownFileInEditor(fileLinkMeta) } : {})}
2707+
resolveCopyPaths={() => resolveMarkdownFileCopyPaths(fileLinkMeta)}
26782708
onOpenInPanel={openFileInPanel}
26792709
onOpenMedia={
26802710
threadRef && canPreviewMedia
@@ -2705,10 +2735,11 @@ function useChatMarkdownState({
27052735
environmentId,
27062736
fileLinkParentSuffixByPath,
27072737
openFileInPanel,
2708-
openInPreferredEditor,
2738+
openMarkdownFileInEditor,
27092739
openMarkdownFileInPreview,
27102740
openMarkdownMedia,
27112741
preferredEditorMenuLabel,
2742+
resolveMarkdownFileCopyPaths,
27122743
resolvedTheme,
27132744
revealInFileManagerLabel,
27142745
revealMarkdownFileInFileManager,
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { describe, expect, it } from "vite-plus/test";
2+
3+
import { resolveMarkdownFileLinkMeta } from "./markdown-links";
4+
import {
5+
markdownFileCopyPaths,
6+
markdownFileEditorTarget,
7+
resolveMarkdownFileHostPath,
8+
} from "./markdownFileHostPath";
9+
10+
const CWD = "E:\\Programming\\hogwarts-productivity-hub-bot";
11+
12+
// A link such as [user.ts:261](user.ts:261), as the agent writes it.
13+
function chipMeta(href: string) {
14+
const meta = resolveMarkdownFileLinkMeta(href, CWD);
15+
if (meta === null) throw new Error(`not a file chip: ${href}`);
16+
return meta;
17+
}
18+
19+
describe("resolveMarkdownFileHostPath", () => {
20+
it("opens and copies a bare filename where the workspace index finds it", async () => {
21+
const meta = chipMeta("user.ts:261");
22+
const queries: string[] = [];
23+
const hostPath = await resolveMarkdownFileHostPath({
24+
meta,
25+
cwd: CWD,
26+
findWorkspaceMatch: async (relativePath) => {
27+
queries.push(relativePath);
28+
return "src/discord/events/interactionCreate/user.ts";
29+
},
30+
});
31+
32+
expect(queries).toEqual(["user.ts"]);
33+
expect(hostPath.absolutePath).toBe(
34+
"E:\\Programming\\hogwarts-productivity-hub-bot\\src\\discord\\events\\interactionCreate\\user.ts",
35+
);
36+
expect(markdownFileEditorTarget(meta, hostPath)).toBe(`${hostPath.absolutePath}:261`);
37+
expect(markdownFileCopyPaths(meta, hostPath, CWD)).toEqual({
38+
fullPath: `${hostPath.absolutePath}:261`,
39+
// The same workspace-labelled form the chip shows for a link it can place.
40+
relativePath:
41+
"hogwarts-productivity-hub-bot/src/discord/events/interactionCreate/user.ts:261",
42+
});
43+
});
44+
45+
it("keeps the link's own paths when the index has no match", async () => {
46+
const meta = chipMeta("missing.ts");
47+
const hostPath = await resolveMarkdownFileHostPath({
48+
meta,
49+
cwd: CWD,
50+
findWorkspaceMatch: async () => null,
51+
});
52+
53+
expect(hostPath).toEqual({ absolutePath: meta.filePath, matched: false });
54+
expect(markdownFileEditorTarget(meta, hostPath)).toBe(meta.targetPath);
55+
expect(markdownFileCopyPaths(meta, hostPath, CWD)).toEqual({
56+
fullPath: meta.targetPath,
57+
relativePath: meta.displayPath,
58+
});
59+
});
60+
});
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* Where a chat file chip's file really is on the environment host. A bare
3+
* filename (`user.ts`) resolves against the workspace root, which is rarely
4+
* where it lives, so the workspace index is asked first, as the files panel
5+
* does. Open in editor, reveal and the copy actions all go through here so
6+
* they agree on one file.
7+
*/
8+
import { formatFilePathPosition, resolvePathLinkTarget } from "@t3tools/shared/fileLinks";
9+
10+
import { formatWorkspaceRelativePath } from "./filePathDisplay";
11+
import type { MarkdownFileLinkMeta } from "./markdown-links";
12+
13+
export interface MarkdownFileHostPath {
14+
/** Absolute host path, without a line/column suffix. */
15+
readonly absolutePath: string;
16+
/** Whether the workspace index moved the link to a different file. */
17+
readonly matched: boolean;
18+
}
19+
20+
export async function resolveMarkdownFileHostPath(input: {
21+
readonly meta: MarkdownFileLinkMeta;
22+
readonly cwd: string | undefined;
23+
/** The workspace-relative file the reference names, or null when not found. */
24+
readonly findWorkspaceMatch: (workspaceRelativePath: string) => Promise<string | null>;
25+
}): Promise<MarkdownFileHostPath> {
26+
const { meta, cwd } = input;
27+
const match =
28+
meta.workspaceRelativePath && cwd
29+
? await input.findWorkspaceMatch(meta.workspaceRelativePath)
30+
: null;
31+
if (match && cwd) {
32+
return { absolutePath: resolvePathLinkTarget(match, cwd), matched: true };
33+
}
34+
return { absolutePath: meta.filePath, matched: false };
35+
}
36+
37+
/** The editor target for a resolved chip, keeping the link's line and column. */
38+
export function markdownFileEditorTarget(
39+
meta: MarkdownFileLinkMeta,
40+
hostPath: MarkdownFileHostPath,
41+
): string {
42+
if (!hostPath.matched) return meta.targetPath;
43+
return formatFilePathPosition({
44+
path: hostPath.absolutePath,
45+
...(meta.line !== undefined ? { line: meta.line } : {}),
46+
...(meta.column !== undefined ? { column: meta.column } : {}),
47+
});
48+
}
49+
50+
/** What the chip's Copy full path and Copy relative path put on the clipboard. */
51+
export function markdownFileCopyPaths(
52+
meta: MarkdownFileLinkMeta,
53+
hostPath: MarkdownFileHostPath,
54+
cwd: string | undefined,
55+
): { readonly fullPath: string; readonly relativePath: string } {
56+
if (!hostPath.matched) return { fullPath: meta.targetPath, relativePath: meta.displayPath };
57+
const fullPath = markdownFileEditorTarget(meta, hostPath);
58+
return { fullPath, relativePath: formatWorkspaceRelativePath(fullPath, cwd) };
59+
}

0 commit comments

Comments
 (0)