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

diffFileActions.ts

156 lines5.4 KB

1import { buildRemoteOpenUrl, type EditorId, type ScopedThreadRef } from "@t3tools/contracts";
2import { isWindowsAbsolutePath, normalizeProjectPathForComparison } from "@t3tools/shared/path";
3​
4import type { RemoteOpenState } from "./remoteOpen";
5import { useRightPanelStore } from "./rightPanelStore";
6import { resolvePathLinkTarget } from "./terminal-links";
7​
8interface OpenDiffFilePrimaryActionInput {
9 readonly threadRef: ScopedThreadRef | null;
10 readonly filePath: string;
11 readonly activeCwd: string | undefined;
12 readonly repositoryRoot?: string | undefined;
13 readonly openInEditor: (targetPath: string) => void;
15​
16function normalizedRelativePathSegments(filePath: string): ReadonlyArray<string> | null {
17 if (filePath.startsWith("/") || isWindowsAbsolutePath(filePath) || /^[a-zA-Z]:/.test(filePath)) {
18 return null;
19 }
20​
21 const segments = filePath
22 .replaceAll("\\", "/")
23 .split("/")
24 .filter((segment) => segment.length > 0 && segment !== ".");
25 if (segments.length === 0 || segments.includes("..")) return null;
26 return segments;
28​
29function repositoryRelativeWorkspaceSegments(
30 workspaceRoot: string | undefined,
31 repositoryRoot: string | undefined,
32): ReadonlyArray<string> | null {
33 if (!workspaceRoot || !repositoryRoot) return null;
34​
35 const normalizedWorkspaceRoot = normalizeProjectPathForComparison(workspaceRoot);
36 const normalizedRepositoryRoot = normalizeProjectPathForComparison(repositoryRoot);
37 if (normalizedWorkspaceRoot === normalizedRepositoryRoot) return [];
38​
39 const separator = normalizedRepositoryRoot.includes("\\") ? "\\" : "/";
40 const repositoryPrefix = normalizedRepositoryRoot.endsWith(separator)
41 ? normalizedRepositoryRoot
42 : `${normalizedRepositoryRoot}${separator}`;
43 if (!normalizedWorkspaceRoot.startsWith(repositoryPrefix)) return null;
44​
45 return normalizedWorkspaceRoot
46 .slice(repositoryPrefix.length)
47 .split(/[\\/]+/)
48 .filter(Boolean);
50​
51export function resolveDiffPathForWorkspace(input: {
52 readonly filePath: string;
53 readonly workspaceRoot: string | undefined;
54 readonly repositoryRoot: string | undefined;
55}): string | null {
56 const fileSegments = normalizedRelativePathSegments(input.filePath);
57 if (!fileSegments) return null;
58​
59 const workspaceSegments = repositoryRelativeWorkspaceSegments(
60 input.workspaceRoot,
61 input.repositoryRoot,
62 );
63 if (!workspaceSegments || workspaceSegments.length === 0) {
64 return fileSegments.join("/");
65 }
66​
67 const caseInsensitive = input.repositoryRoot
68 ? isWindowsAbsolutePath(input.repositoryRoot)
69 : false;
70 const belongsToWorkspace = workspaceSegments.every((segment, index) => {
71 const candidate = fileSegments[index];
72 if (candidate === undefined) return false;
73 return caseInsensitive ? candidate.toLowerCase() === segment : candidate === segment;
74 });
75 if (!belongsToWorkspace) return null;
76​
77 const relativeSegments = fileSegments.slice(workspaceSegments.length);
78 return relativeSegments.length > 0 ? relativeSegments.join("/") : null;
80​
81interface DiffFileTarget {
82 readonly workspaceFilePath: string;
83 readonly editorTargetPath: string;
85​
86/** Resolves a repo-relative diff path to the active workspace: null when it lies outside it. */
87function resolveDiffFileTarget(input: {
88 readonly filePath: string;
89 readonly activeCwd: string | undefined;
90 readonly repositoryRoot?: string | undefined;
91}): DiffFileTarget | null {
92 const workspaceFilePath = resolveDiffPathForWorkspace({
93 filePath: input.filePath,
94 workspaceRoot: input.activeCwd,
95 repositoryRoot: input.repositoryRoot,
96 });
97 if (!workspaceFilePath) return null;
98 return {
99 workspaceFilePath,
100 editorTargetPath: input.activeCwd
101 ? resolvePathLinkTarget(workspaceFilePath, input.activeCwd)
102 : workspaceFilePath,
103 };
105​
106export function openDiffFileInEditor(
107 input: Omit<OpenDiffFilePrimaryActionInput, "threadRef">,
108): void {
109 const target = resolveDiffFileTarget(input);
110 if (target) input.openInEditor(target.editorTargetPath);
112​
113export function openDiffFilePrimaryAction(input: OpenDiffFilePrimaryActionInput): void {
114 const target = resolveDiffFileTarget(input);
115 if (!target) return;
116​
117 if (input.threadRef) {
118 useRightPanelStore.getState().openFile(input.threadRef, target.workspaceFilePath);
119 return;
120 }
121​
122 input.openInEditor(target.editorTargetPath);
124​
125export type DiffEditorLaunch =
126 | { readonly kind: "unavailable" }
127 | { readonly kind: "remote-url"; readonly url: string }
128 | { readonly kind: "local-exec"; readonly editor: EditorId; readonly reveal: boolean };
129​
130/**
131 * Picks how a diff file reaches the user's editor. A client on another machine
132 * gets an SSH deep link; with no SSH route the action is unavailable, since a
133 * server-side exec would open the file where the user cannot see it.
134 */
135export function resolveDiffEditorLaunch(input: {
136 readonly remoteOpen: RemoteOpenState;
137 readonly editor: EditorId | null;
138 readonly targetPath: string;
139 readonly revealInFileManager: boolean;
140}): DiffEditorLaunch {
141 const { remoteOpen, editor } = input;
142 if (editor === null || remoteOpen.mode === "remote-unavailable") return { kind: "unavailable" };
143 if (remoteOpen.mode === "remote-links") {
144 const url = buildRemoteOpenUrl({
145 editor,
146 host: remoteOpen.host.host,
147 absolutePath: input.targetPath,
148 });
149 return url === undefined ? { kind: "unavailable" } : { kind: "remote-url", url };
150 }
151 return {
152 kind: "local-exec",
153 editor,
154 reveal: input.revealInFileManager && editor === "file-manager",
155 };