дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-04 09:11:10 +02:00
A .NET project node in the Project view's solution tree was a leaf for its .csproj holding only Dependencies. It now lists the project's folder (bin and obj left out) below Dependencies, keeps the project's .NET context actions, and Select Opened File reveals files of a project in the solution tree. The project file opens from the node's Edit '<name>.csproj' menu item. Closes #7859 Co-authored-by: Inf1mum <[email protected]>
204 рядки
7.3 KiB
TypeScript
204 рядки
7.3 KiB
TypeScript
/**
|
|
* Solution and project-reference edits from the Project view (#7421): `dotnet sln add|remove`
|
|
* and `dotnet add|remove reference` through the same trusted, cancellable process host as
|
|
* Build / Restore / Clean. Edits run one at a time (two writers on one `.sln` would race);
|
|
* afterwards the workspace reloads so the tree shows the new state, and a CLI failure
|
|
* surfaces as a notification.
|
|
*/
|
|
import { dotnet, dotnetAbsolutePath } from "@/platform/dotnet";
|
|
import { IpcError } from "@/platform/ipc";
|
|
import { tx } from "@/platform/i18n";
|
|
import { choosePaths } from "@/platform/choosePath";
|
|
import { basename, dirname, useProject } from "@/platform/project";
|
|
import type { MenuItem } from "@/ui/Menu";
|
|
import { notifications } from "@/workbench/contributions";
|
|
import { reloadDotnet, useDotnet } from "./store";
|
|
import { isDotnetSolutionPath } from "./target";
|
|
|
|
let queue: Promise<unknown> = Promise.resolve();
|
|
/** Edits that are queued or running: Cancel aborts all of them. */
|
|
const pending = new Set<AbortController>();
|
|
|
|
export const cancelDotnetEdit = () => pending.forEach((c) => c.abort());
|
|
|
|
/** The CLI arguments of an edit; `solution` and `project` are absolute paths. */
|
|
export const slnArgs = (op: "add" | "remove", solution: string, projects: string[]) => [
|
|
"sln",
|
|
solution,
|
|
op,
|
|
...projects,
|
|
];
|
|
export const referenceArgs = (op: "add" | "remove", project: string, references: string[]) => [
|
|
op,
|
|
project,
|
|
"reference",
|
|
...references,
|
|
];
|
|
|
|
/**
|
|
* `dotnet sln add` exits 0 without adding a project whose file name another project of the
|
|
* solution already has ("already contains project"); the projects missing from `dotnet sln list`
|
|
* afterwards are warned about instead of passing as added.
|
|
*/
|
|
async function warnIfNotAdded(solution: string, projects: string[], signal: AbortSignal) {
|
|
let output: string;
|
|
try {
|
|
output = await dotnet(["sln", solution, "list"], dirname(solution), signal);
|
|
} catch {
|
|
return; // The add itself succeeded; a failing list is not worth a false alarm.
|
|
}
|
|
const listed = output
|
|
.split(/\r?\n/)
|
|
.filter((line) => /\.(?:cs|fs|vb)proj$/i.test(line.trim()))
|
|
.map((line) => dotnetAbsolutePath(dirname(solution), line.trim()));
|
|
const missing = projects.filter((path) => !listed.includes(dotnetAbsolutePath(dirname(solution), path)));
|
|
const names = missing.map(basename).join(", ");
|
|
if (missing.length)
|
|
notifications().notify({
|
|
level: "warning",
|
|
title: tx("The project was not added to the solution"),
|
|
body: tx`The solution already contains a project with the same name as ${names}.`,
|
|
group: "tools",
|
|
});
|
|
}
|
|
|
|
/** Runs `args` after the edits queued before it, then reloads the model. */
|
|
function run(
|
|
title: string,
|
|
args: string[],
|
|
cwd: string,
|
|
check?: (signal: AbortSignal) => Promise<void>,
|
|
): Promise<void> {
|
|
const root = useProject.getState().project?.root;
|
|
const stillInProject = () => useProject.getState().project?.root === root;
|
|
const abort = new AbortController();
|
|
const unsubscribe = useProject.subscribe(() => {
|
|
if (!stillInProject()) abort.abort();
|
|
});
|
|
pending.add(abort);
|
|
const next = queue
|
|
.catch(() => {})
|
|
.then(async () => {
|
|
try {
|
|
if (abort.signal.aborted) return;
|
|
let refresh = false;
|
|
try {
|
|
await dotnet(args, cwd, abort.signal);
|
|
refresh = true;
|
|
await check?.(abort.signal);
|
|
} catch (e) {
|
|
// A CLI process can have written part of a multi-entry edit before failing.
|
|
// An IPC rejection means the process could not be started.
|
|
refresh ||= !(e instanceof IpcError);
|
|
if (!abort.signal.aborted)
|
|
notifications().notify({ level: "error", title, body: String(e).trim(), group: "tools" });
|
|
}
|
|
if (refresh && stillInProject()) await reloadDotnet();
|
|
} finally {
|
|
unsubscribe();
|
|
pending.delete(abort);
|
|
}
|
|
});
|
|
queue = next;
|
|
return next;
|
|
}
|
|
|
|
const PROJECT_FILTER = [{ name: ".NET project", extensions: ["csproj", "fsproj", "vbproj"] }];
|
|
|
|
export async function addProjectsToSolution(solution: string, projects?: string[]) {
|
|
const root = useProject.getState().project?.root;
|
|
const picked =
|
|
projects ??
|
|
(await choosePaths({
|
|
directory: false,
|
|
title: tx("Add Existing Project"),
|
|
defaultPath: dirname(solution),
|
|
filters: PROJECT_FILTER,
|
|
}));
|
|
if (!picked.length || useProject.getState().project?.root !== root) return;
|
|
return run(
|
|
tx("Could not add the project to the solution"),
|
|
slnArgs("add", solution, picked),
|
|
dirname(solution),
|
|
(signal) => warnIfNotAdded(solution, picked, signal),
|
|
);
|
|
}
|
|
|
|
export function removeProjectFromSolution(solution: string, project: string) {
|
|
return run(
|
|
tx("Could not remove the project from the solution"),
|
|
slnArgs("remove", solution, [project]),
|
|
dirname(solution),
|
|
);
|
|
}
|
|
|
|
export async function addProjectReference(project: string, reference?: string) {
|
|
const root = useProject.getState().project?.root;
|
|
const picked =
|
|
reference ??
|
|
(await choosePaths({
|
|
directory: false,
|
|
title: tx("Add Project Reference"),
|
|
defaultPath: dirname(project),
|
|
filters: PROJECT_FILTER,
|
|
}));
|
|
const list = typeof picked === "string" ? [picked] : picked;
|
|
if (!list.length || useProject.getState().project?.root !== root) return;
|
|
return run(tx("Could not add the project reference"), referenceArgs("add", project, list), dirname(project));
|
|
}
|
|
|
|
export function removeProjectReference(project: string, reference: string) {
|
|
return run(
|
|
tx("Could not remove the project reference"),
|
|
referenceArgs("remove", project, [reference]),
|
|
dirname(project),
|
|
);
|
|
}
|
|
|
|
/** #7421: the solution / reference edits of a solution or project node. */
|
|
const solutionName = (path: string) => basename(path).replace(/\.slnx?$/i, "");
|
|
|
|
export function editItems(path: string): MenuItem[] {
|
|
const { model } = useDotnet.getState();
|
|
if (isDotnetSolutionPath(path))
|
|
return [
|
|
{ kind: "separator" },
|
|
{ kind: "action", action: "dotnet.solution.addProject", arg: { solution: path }, label: "Add Existing Project…" },
|
|
];
|
|
const solutions = model.solutions.filter((s) => s.projects.includes(path));
|
|
const references = model.projects.find((p) => p.path === path)?.references ?? [];
|
|
return [
|
|
{ kind: "separator" },
|
|
// The project node lists the project's folder: its project file opens from here.
|
|
{
|
|
kind: "action",
|
|
action: "dotnet.project.editFile",
|
|
arg: path,
|
|
label: tx`Edit '${basename(path)}'`,
|
|
verbatim: true,
|
|
},
|
|
{ kind: "action", action: "dotnet.project.addReference", arg: { project: path }, label: "Add Project Reference…" },
|
|
...(references.length
|
|
? [
|
|
{
|
|
label: "Remove Project Reference",
|
|
submenu: references.map((reference): MenuItem => ({
|
|
kind: "action",
|
|
action: "dotnet.project.removeReference",
|
|
arg: { project: path, reference },
|
|
label: basename(reference),
|
|
verbatim: true,
|
|
})),
|
|
} satisfies MenuItem,
|
|
]
|
|
: []),
|
|
...solutions.map((s): MenuItem => ({
|
|
kind: "action",
|
|
action: "dotnet.solution.removeProject",
|
|
arg: { solution: s.path, project: path },
|
|
label: solutions.length > 1 ? tx`Remove from Solution '${solutionName(s.path)}'` : "Remove from Solution",
|
|
verbatim: solutions.length > 1,
|
|
})),
|
|
];
|
|
}
|