Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

diffFileActions.ts

119 lines4.2 KB

1import type { ScopedThreadRef } from "@t3tools/contracts";
2import {
3 isAbsolutePath,
4 isWindowsAbsolutePath,
5 normalizeProjectPathForComparison,
6 workspaceRelativeFilePath,
7} from "@t3tools/shared/path";
8​
9import { useRightPanelStore } from "./rightPanelStore";
10import { resolvePathLinkTarget } from "@t3tools/shared/fileLinks";
11​
12interface OpenDiffFilePrimaryActionInput {
13 readonly threadRef: ScopedThreadRef | null;
14 readonly filePath: string;
15 readonly activeCwd: string | undefined;
16 readonly repositoryRoot?: string | undefined;
17 readonly openInEditor: (targetPath: string) => void;
19​
20function normalizedRelativePathSegments(
21 filePath: string,
22 windowsRoot: boolean,
23): ReadonlyArray<string> | null {
24 if (filePath.startsWith("/") || isWindowsAbsolutePath(filePath) || /^[a-zA-Z]:/.test(filePath)) {
25 return null;
26 }
27​
28 // On POSIX a backslash is part of a file name; Windows treats it as a separator
29 // (tool-call patches can name files with either), so reject ".." either way.
30 const parts = filePath.split(/[\\/]/);
31 if (parts.includes("..")) return null;
32 const segments = (windowsRoot ? parts : filePath.split("/")).filter(
33 (segment) => segment.length > 0 && segment !== ".",
34 );
35 if (segments.length === 0) return null;
36 return segments;
38​
39function repositoryRelativeWorkspaceSegments(
40 workspaceRoot: string | undefined,
41 repositoryRoot: string | undefined,
42): ReadonlyArray<string> | null {
43 if (!workspaceRoot || !repositoryRoot) return null;
44​
45 const normalizedWorkspaceRoot = normalizeProjectPathForComparison(workspaceRoot);
46 const normalizedRepositoryRoot = normalizeProjectPathForComparison(repositoryRoot);
47 if (normalizedWorkspaceRoot === normalizedRepositoryRoot) return [];
48​
49 const separator = isWindowsAbsolutePath(normalizedRepositoryRoot) ? "\\" : "/";
50 const repositoryPrefix = normalizedRepositoryRoot.endsWith(separator)
51 ? normalizedRepositoryRoot
52 : `${normalizedRepositoryRoot}${separator}`;
53 if (!normalizedWorkspaceRoot.startsWith(repositoryPrefix)) return null;
54​
55 return normalizedWorkspaceRoot.slice(repositoryPrefix.length).split(separator).filter(Boolean);
57​
58export function resolveDiffPathForWorkspace(input: {
59 readonly filePath: string;
60 readonly workspaceRoot: string | undefined;
61 readonly repositoryRoot: string | undefined;
62}): string | null {
63 const windowsRoot = [input.workspaceRoot, input.repositoryRoot].some(
64 (root) => root !== undefined && isWindowsAbsolutePath(root),
65 );
66 // Claude and ACP tool-call diffs name files by absolute path.
67 if (isAbsolutePath(input.filePath)) {
68 const relativePath = workspaceRelativeFilePath(input.filePath, input.workspaceRoot);
69 const segments =
70 relativePath === null ? null : normalizedRelativePathSegments(relativePath, windowsRoot);
71 return segments ? segments.join("/") : null;
72 }
73​
74 const fileSegments = normalizedRelativePathSegments(input.filePath, windowsRoot);
75 if (!fileSegments) return null;
76​
77 const workspaceSegments = repositoryRelativeWorkspaceSegments(
78 input.workspaceRoot,
79 input.repositoryRoot,
80 );
81 if (!workspaceSegments || workspaceSegments.length === 0) {
82 return fileSegments.join("/");
83 }
84​
85 const caseInsensitive = input.repositoryRoot
86 ? isWindowsAbsolutePath(input.repositoryRoot)
87 : false;
88 const belongsToWorkspace = workspaceSegments.every((segment, index) => {
89 const candidate = fileSegments[index];
90 if (candidate === undefined) return false;
91 return caseInsensitive ? candidate.toLowerCase() === segment : candidate === segment;
92 });
93 if (!belongsToWorkspace) return null;
94​
95 const relativeSegments = fileSegments.slice(workspaceSegments.length);
96 return relativeSegments.length > 0 ? relativeSegments.join("/") : null;
98​
99export function openDiffFilePrimaryAction({
100 threadRef,
101 filePath,
102 activeCwd,
103 repositoryRoot,
104 openInEditor,
105}: OpenDiffFilePrimaryActionInput): void {
106 const workspaceFilePath = resolveDiffPathForWorkspace({
107 filePath,
108 workspaceRoot: activeCwd,
109 repositoryRoot,
110 });
111 if (!workspaceFilePath) return;
112​
113 if (threadRef) {
114 useRightPanelStore.getState().openFile(threadRef, workspaceFilePath);
115 return;
116 }
117​
118 openInEditor(activeCwd ? resolvePathLinkTarget(workspaceFilePath, activeCwd) : workspaceFilePath);