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

diffFileActions.ts

123 lines4.3 KB

1import type { ScopedThreadRef } from "@t3tools/contracts";
2import { joinPath } from "@t3tools/shared/fileLinks";
3import {
4 isAbsolutePath,
5 isWindowsAbsolutePath,
6 normalizeProjectPathForComparison,
7 workspaceRelativeFilePath,
8} from "@t3tools/shared/path";
9​
10import { useRightPanelStore } from "./rightPanelStore";
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 joinDiffFilePath(cwd: string, relativePath: string): string {
100 return joinPath(cwd, relativePath, isWindowsAbsolutePath(cwd) ? "\\" : "/");
102​
103export function openDiffFilePrimaryAction({
104 threadRef,
105 filePath,
106 activeCwd,
107 repositoryRoot,
108 openInEditor,
109}: OpenDiffFilePrimaryActionInput): void {
110 const workspaceFilePath = resolveDiffPathForWorkspace({
111 filePath,
112 workspaceRoot: activeCwd,
113 repositoryRoot,
114 });
115 if (!workspaceFilePath) return;
116​
117 if (threadRef) {
118 useRightPanelStore.getState().openFile(threadRef, workspaceFilePath);
119 return;
120 }
121​
122 openInEditor(activeCwd ? joinDiffFilePath(activeCwd, workspaceFilePath) : workspaceFilePath);