Файли
openidea/src/platform/fileManager.ts
T
ntbang0901 16ba1ba3f5 fix(project-tree): keep remote paths away from the local file manager (#2340)
In an SSH or dev-container window, Reveal in Finder, Open In › Finder, Select In and Navigate › File Path passed the server's path to this desktop's file manager, which failed with "No such file or directory" or could open an unrelated local file with the same path. One check, canRevealInFileManager, now decides whether a path belongs to this desktop (plus WSL paths on Windows through \\wsl.localhost); menus hide the item in other remote windows, the action and File Path warn instead, and Create Patch no longer offers to show a patch saved on the server. Log, config folder and local recent projects still reveal from a remote window. Refs #1670.

Co-authored-by: ntbang0901 <[email protected]>
2026-11-10 01:41:26 +08:00

70 рядки
3.0 KiB
TypeScript

import type { RemoteTarget } from "./ipc";
import { isTauri } from "./ipc";
import { usePlatform } from "./platformInfo";
import { useRemote } from "./remote";
function wslSharePath(path: string, target: RemoteTarget | null): string {
if (target?.kind !== "wsl" || !path.startsWith("/")) return path;
return `\\\\wsl.localhost\\${target.distro}${path.replaceAll("/", "\\")}`;
}
function windowTarget(): RemoteTarget | null {
return useRemote.getState().session?.target ?? null;
}
/**
* A path in the form the local OS file manager's Shell APIs accept. Project
* paths use `/` separators and may be Windows verbatim paths, but Explorer's
* `ILCreateFromPathW` rejects `\\?\UNC\...`.
*/
export function fileManagerPath(path: string, os = usePlatform.getState().os, target = windowTarget()): string {
if (os !== "windows") return path;
path = wslSharePath(path, target);
if (/^\/\/\?\/unc\//i.test(path)) path = `\\\\${path.slice(8).replaceAll("/", "\\")}`;
else if (path.startsWith("//?/")) path = path.slice(4);
if (/^\\\\\?\\unc\\/i.test(path)) return `\\\\${path.slice(8)}`;
if (path.startsWith("\\\\?\\")) return path.slice(4);
if (path.startsWith("//")) return path.replaceAll("/", "\\");
if (/^[A-Za-z]:\//.test(path)) return path.replaceAll("/", "\\");
return path;
}
/** English source text; callers and the action registry translate it. */
export function fileManagerName(os = usePlatform.getState().os): string {
return os === "macos" ? "Finder" : os === "windows" ? "Explorer" : "Files";
}
export function showInFileManagerLabel(os = usePlatform.getState().os): string {
return os === "macos" ? "Show in Finder" : os === "windows" ? "Show in Explorer" : "Show in Files";
}
/** The project action has historically used "Reveal" on macOS. */
export function revealInFileManagerLabel(os = usePlatform.getState().os): string {
return os === "macos" ? "Reveal in Finder" : showInFileManagerLabel(os);
}
/**
* Whether this desktop's file manager can show paths of `target` (default: this window's; `null`: this
* desktop's own). SSH and dev-container paths exist only on the server; WSL ones are reachable on
* Windows through their `\\wsl.localhost` share.
*/
export function canRevealInFileManager(target = windowTarget(), os = usePlatform.getState().os): boolean {
return target === null || (target.kind === "wsl" && os === "windows");
}
/**
* Reveals files in Explorer, Finder or the Linux desktop's file manager. `target` says where the paths
* live (default: this window's project host); `false` when this desktop cannot show them.
*/
export async function revealInFileManager(
paths: string | readonly string[],
target = windowTarget(),
): Promise<boolean> {
if (!isTauri() || !canRevealInFileManager(target)) return false;
const list = (typeof paths === "string" ? [paths] : paths).map((path) => fileManagerPath(path, undefined, target));
if (!list.length) return false;
const { revealItemInDir } = await import("@tauri-apps/plugin-opener");
await revealItemInDir(list.length === 1 ? list[0]! : [...list]);
return true;
}