mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 16:04:21 +00:00
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>
70 lines
2.2 KiB
TypeScript
70 lines
2.2 KiB
TypeScript
/**
|
||
* 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),
|
||
},
|
||
];
|
||
},
|
||
});
|
||
}
|