Files
openidea/src/features/vcs/copyRepositoryPath.ts
T
Mansur Akhmadiandmansur-akhmadi 3783b601b5 feat(vcs): copy a path from its repository's root in Copy Path/Reference… (#4626)
Copy Path/Reference... gains Path From Repository Root, as in IDEA: each selected path relative to the root of the Git repository holding it (innermost for nested repositories, a repository root as '.'), one per line, through the same copyPath menu group the hosting link uses. Paths outside every repository are left out, and the item is hidden when none is inside one.

Closes #4625

Co-authored-by: mansur-akhmadi <10739460+mansur-akhmadi@users.noreply.github.com>
2026-11-20 07:38:35 +08:00

70 lines
2.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Copy Path/Reference… › Path From Repository Root: each path relative to the root of the
* Git repository holding it, one per line.
*/
import { executeAction, type Action } from "@/platform/actions";
import { writeClipboardText } from "@/platform/clipboard";
import { tx } from "@/platform/i18n";
import { notifications, registerMenuGroup } from "@/workbench/contributions";
import { rootOf, stripSlash } from "./model";
import { useVcs } from "./store";
/** The paths inside a repository, each from its root; null when none is. */
function repositoryPaths(paths: readonly string[]): string | null {
const { roots } = useVcs.getState();
const relative: string[] = [];
for (const path of paths.map(stripSlash)) {
// A nested repository's own root is its root, not a folder of the outer one.
const root = roots.includes(path) ? path : rootOf(path, roots);
if (root) relative.push(path.slice(root.length + 1) || ".");
}
return relative.length ? relative.join("\n") : null;
}
async function copy(text: string) {
try {
await writeClipboardText(text);
void executeAction("editor.showHint", { text: tx`Copied ${text}` });
} catch (error) {
notifications().notify({
level: "error",
title: tx("Cannot access the clipboard"),
body: error instanceof Error ? error.message : String(error),
});
}
}
export const copyRepositoryPathActions: Action[] = [
{
id: "vcs.copyRepositoryPath",
title: "Copy Path From Repository Root",
category: "Git",
icon: "copy",
when: "vcsAvailable",
hidden: true,
run: ({ arg }) => {
const text = Array.isArray(arg) ? repositoryPaths(arg as string[]) : null;
if (text) void copy(text);
},
},
];
export function registerCopyRepositoryPath(): () => void {
return registerMenuGroup({
id: "vcs.copyRepositoryPath",
menus: ["copyPath"],
items: (_menu, { paths }) => {
const text = repositoryPaths(paths);
if (!text) return [];
return [
{
id: "vcs.copyRepositoryPath",
label: "Path From Repository Root",
preview: text,
run: () => void executeAction("vcs.copyRepositoryPath", paths),
},
];
},
});
}