11import { 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" ;
73import {
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" ;
175176import { isMarkdownFileLinkLabel } from "@t3tools/shared/markdownLinks" ;
176177import { readLocalApi } from "../localApi" ;
177178import { 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 ,
0 commit comments