11import type { ScopedThreadRef } from "@t3tools/contracts" ;
2+ import { isWindowsAbsolutePath , normalizeProjectPathForComparison } from "@t3tools/shared/path" ;
23
34import { useRightPanelStore } from "./rightPanelStore" ;
45import { resolvePathLinkTarget } from "./terminal-links" ;
@@ -7,19 +8,93 @@ interface OpenDiffFilePrimaryActionInput {
78 readonly threadRef : ScopedThreadRef | null ;
89 readonly filePath : string ;
910 readonly activeCwd : string | undefined ;
11+ readonly repositoryRoot ?: string | undefined ;
1012 readonly openInEditor : ( targetPath : string ) => void ;
1113}
1214
15+ function normalizedRelativePathSegments ( filePath : string ) : ReadonlyArray < string > | null {
16+ if ( filePath . startsWith ( "/" ) || isWindowsAbsolutePath ( filePath ) || / ^ [ a - z A - Z ] : / . test ( filePath ) ) {
17+ return null ;
18+ }
19+
20+ const segments = filePath
21+ . replaceAll ( "\\" , "/" )
22+ . split ( "/" )
23+ . filter ( ( segment ) => segment . length > 0 && segment !== "." ) ;
24+ if ( segments . length === 0 || segments . includes ( ".." ) ) return null ;
25+ return segments ;
26+ }
27+
28+ function repositoryRelativeWorkspaceSegments (
29+ workspaceRoot : string | undefined ,
30+ repositoryRoot : string | undefined ,
31+ ) : ReadonlyArray < string > | null {
32+ if ( ! workspaceRoot || ! repositoryRoot ) return null ;
33+
34+ const normalizedWorkspaceRoot = normalizeProjectPathForComparison ( workspaceRoot ) ;
35+ const normalizedRepositoryRoot = normalizeProjectPathForComparison ( repositoryRoot ) ;
36+ if ( normalizedWorkspaceRoot === normalizedRepositoryRoot ) return [ ] ;
37+
38+ const separator = normalizedRepositoryRoot . includes ( "\\" ) ? "\\" : "/" ;
39+ const repositoryPrefix = normalizedRepositoryRoot . endsWith ( separator )
40+ ? normalizedRepositoryRoot
41+ : `${ normalizedRepositoryRoot } ${ separator } ` ;
42+ if ( ! normalizedWorkspaceRoot . startsWith ( repositoryPrefix ) ) return null ;
43+
44+ return normalizedWorkspaceRoot
45+ . slice ( repositoryPrefix . length )
46+ . split ( / [ \\ / ] + / )
47+ . filter ( Boolean ) ;
48+ }
49+
50+ export function resolveDiffPathForWorkspace ( input : {
51+ readonly filePath : string ;
52+ readonly workspaceRoot : string | undefined ;
53+ readonly repositoryRoot : string | undefined ;
54+ } ) : string | null {
55+ const fileSegments = normalizedRelativePathSegments ( input . filePath ) ;
56+ if ( ! fileSegments ) return null ;
57+
58+ const workspaceSegments = repositoryRelativeWorkspaceSegments (
59+ input . workspaceRoot ,
60+ input . repositoryRoot ,
61+ ) ;
62+ if ( ! workspaceSegments || workspaceSegments . length === 0 ) {
63+ return fileSegments . join ( "/" ) ;
64+ }
65+
66+ const caseInsensitive = input . repositoryRoot
67+ ? isWindowsAbsolutePath ( input . repositoryRoot )
68+ : false ;
69+ const belongsToWorkspace = workspaceSegments . every ( ( segment , index ) => {
70+ const candidate = fileSegments [ index ] ;
71+ if ( candidate === undefined ) return false ;
72+ return caseInsensitive ? candidate . toLowerCase ( ) === segment : candidate === segment ;
73+ } ) ;
74+ if ( ! belongsToWorkspace ) return null ;
75+
76+ const relativeSegments = fileSegments . slice ( workspaceSegments . length ) ;
77+ return relativeSegments . length > 0 ? relativeSegments . join ( "/" ) : null ;
78+ }
79+
1380export function openDiffFilePrimaryAction ( {
1481 threadRef,
1582 filePath,
1683 activeCwd,
84+ repositoryRoot,
1785 openInEditor,
1886} : OpenDiffFilePrimaryActionInput ) : void {
87+ const workspaceFilePath = resolveDiffPathForWorkspace ( {
88+ filePath,
89+ workspaceRoot : activeCwd ,
90+ repositoryRoot,
91+ } ) ;
92+ if ( ! workspaceFilePath ) return ;
93+
1994 if ( threadRef ) {
20- useRightPanelStore . getState ( ) . openFile ( threadRef , filePath ) ;
95+ useRightPanelStore . getState ( ) . openFile ( threadRef , workspaceFilePath ) ;
2196 return ;
2297 }
2398
24- openInEditor ( activeCwd ? resolvePathLinkTarget ( filePath , activeCwd ) : filePath ) ;
99+ openInEditor ( activeCwd ? resolvePathLinkTarget ( workspaceFilePath , activeCwd ) : workspaceFilePath ) ;
25100}
0 commit comments