Файли
openidea/src/platform/paths.ts
T
l0ng-ai 26aabf5229 fix(project): copy absolute Windows paths with backslashes (#5808)
Copy Absolute Path returned the UI's /-separated form, so C:/work/a.md went to the clipboard and cmd, robocopy and the Explorer address bar rejected it. pathToCopy now writes absolute drive and UNC paths with backslashes (new nativePath, the inverse of ipcPath); paths relative to a content or source root keep /. The editor's Absolute Path action and Search Everywhere's Copy Path go through the same conversion.

Fixes #5805
2026-11-25 15:38:15 +08:00

73 рядки
3.1 KiB
TypeScript

/**
* Paths coming *into* the UI from a native API (`appDataDir()` from
* `@tauri-apps/api/path`, the file dialogs) are native strings: `C:\a\b` on
* Windows. Everything the UI stores and compares — what the backend returns
* (`IpcPath` in `crates/ide-core/src/ipc_path.rs`), the editor's `ctx.path`, the
* project tree — is `/`-separated on every OS, so such a path has to be
* converted before it is used.
*/
const DRIVE = /^[A-Za-z]:\\/;
const NETWORK = /^\\\\/;
/**
* `p` in the form the UI holds paths: `C:\a\b` and `\\?\C:\a\b` become
* `C:/a/b`, `\\server\share` becomes `//server/share`. A path that already is
* in that form — or one that is not a Windows path at all — is returned as is.
*/
export function ipcPath(p: string): string {
let path = p;
if (path.startsWith("\\\\?\\")) path = path.startsWith("\\\\?\\UNC\\") ? `\\\\${path.slice(8)}` : path.slice(4);
return DRIVE.test(path) || NETWORK.test(path) ? path.replace(/\\/g, "/") : path;
}
/**
* `p` (a path in the UI's `/`-separated form) as Windows writes it, for text the user pastes
* elsewhere: `C:/a/b` becomes `C:\a\b`, `//server/share/x` becomes `\\server\share\x`. Any
* other path (POSIX, remote) is returned as is.
*/
export function nativePath(p: string): string {
const path = p.replace(/^\/\/\?\/UNC\//i, "//").replace(/^\/\/\?\/(?=[A-Za-z]:\/)/, "");
return /^[A-Za-z]:\//.test(path) || /^\/\/[^/?.]/.test(path) ? path.replace(/\//g, "\\") : p;
}
const SHARE = /^[\\/]{2}/;
const SHARE_NAME = /^[\\/]{2}(?:\?[\\/]UNC[\\/])?([^\\/]+)[\\/]([^\\/]+)/i;
/**
* `server/share` of a path on a network share, in lower case; null for any other path. The
* host hands out a share's paths as `//?/UNC/server/share/…`, the dialogs as `//server/share/…`.
*/
function shareOf(path: string): string | null {
const m = SHARE_NAME.exec(path);
// Any other device path (`//?/C:/…`, `//./…`) names no server to compare.
return m && m[1] !== "?" && m[1] !== "." ? `${m[1]}/${m[2]}`.toLowerCase() : null;
}
/**
* Whether `path` is on a network share that `base` is not on. Windows reads `//server/share`,
* `\\server\share` and mixes of the two as a share, and opening one signs in to its server
* with the user's credentials: a path that text someone else wrote leads to is followed to no
* share but the one that text is on.
*/
export function onOtherShare(path: string, base: string): boolean {
if (!SHARE.test(path)) return false;
const share = shareOf(path);
return share === null || share !== shareOf(base);
}
/**
* `onOtherShare` for a path resolved against `base`, the folder or file of the text naming
* it: also true when that text spells a share itself (`written`, decoded).
*/
export function opensOtherShare(path: string, written: string, base: string): boolean {
return SHARE.test(written) || onOtherShare(path, base);
}
/** True when `path` equals `ancestor` or lies below it. */
export function isSameOrInside(path: string, ancestor: string): boolean {
path = ipcPath(path);
ancestor = ipcPath(ancestor);
return path === ancestor || path.startsWith(ancestor.endsWith("/") ? ancestor : ancestor + "/");
}