дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-06 18:17:15 +02:00
A folder inside an attached project now has Mark Directory as › Excluded (and Cancel Exclusion), written to that project's own .openidea settings so it also applies when the project is opened alone. New commands project_workspace_members and project_workspace_member_set (projectStructure.* keys of attached projects only); the existing markFolder logic computes the change. Other roles stay out of the menu for attached projects until their source and test roots are applied. Closes #4712 Co-authored-by: wildcatDownstairs <[email protected]>
201 рядки
7.1 KiB
TypeScript
201 рядки
7.1 KiB
TypeScript
/**
|
|
* Source / test / resource / excluded roots of the open project: detected by
|
|
* the backend from build files (`project_roots`, refreshed on
|
|
* `projectRootsChanged`) and merged here with the Project Structure settings,
|
|
* so marking a folder shows at once.
|
|
*/
|
|
import { useMemo } from "react";
|
|
import { create } from "zustand";
|
|
import { call, commands, isTauri, onIdeEvent } from "./ipc";
|
|
import {
|
|
effectiveRoots,
|
|
isTestPath,
|
|
rootFor,
|
|
useProjectStructure,
|
|
validateProjectStructure,
|
|
type ProjectStructureValid,
|
|
type RootFolder,
|
|
} from "./languageSettings";
|
|
import { basename, projectFolder, useProject } from "./project";
|
|
|
|
/** A project attached to the open one: it keeps its own Project Structure. */
|
|
export interface AttachedProject {
|
|
root: string;
|
|
structure: ProjectStructureValid;
|
|
detected: RootFolder[];
|
|
}
|
|
|
|
interface RootsState {
|
|
/** Project the roots belong to. */
|
|
root: string | null;
|
|
detected: RootFolder[];
|
|
attached: AttachedProject[];
|
|
}
|
|
|
|
export const useProjectRoots = create<RootsState>(() => ({ root: null, detected: [], attached: [] }));
|
|
|
|
const rootFolders = (folders: readonly { path: string; kind: RootFolder["kind"]; detectedFrom?: string | null }[]) =>
|
|
folders.map((r) => ({ path: r.path, kind: r.kind, detectedFrom: r.detectedFrom ?? null }));
|
|
|
|
let generation = 0;
|
|
|
|
/** Re-reads the detected roots of the open project. */
|
|
export async function loadProjectRoots(): Promise<void> {
|
|
const root = useProject.getState().project?.root ?? null;
|
|
const gen = ++generation;
|
|
if (!root || !isTauri()) {
|
|
useProjectRoots.setState({ root, detected: [], attached: [] });
|
|
return;
|
|
}
|
|
try {
|
|
const [roots, members] = await Promise.all([
|
|
call(commands.projectRoots()),
|
|
call(commands.projectWorkspaceMembers()).catch(() => []),
|
|
]);
|
|
if (gen !== generation) return;
|
|
useProjectRoots.setState({
|
|
root,
|
|
detected: rootFolders(roots.detected),
|
|
attached: members.map((m) => ({
|
|
root: m.root,
|
|
structure: validateProjectStructure(m.structure),
|
|
detected: rootFolders(m.detected),
|
|
})),
|
|
});
|
|
} catch {
|
|
if (gen === generation) useProjectRoots.setState({ root, detected: [], attached: [] });
|
|
}
|
|
}
|
|
|
|
let users = 0;
|
|
let dispose: (() => void) | null = null;
|
|
|
|
/** Keeps the roots in sync with the open project (reference-counted; returns a disposer). */
|
|
export function initProjectRoots(): () => void {
|
|
if (users++ === 0) {
|
|
void loadProjectRoots();
|
|
const unsubProject = useProject.subscribe((s, prev) => {
|
|
if (s.project?.root !== prev.project?.root) void loadProjectRoots();
|
|
});
|
|
const unsubEvent = onIdeEvent("projectRootsChanged", () => void loadProjectRoots());
|
|
dispose = () => {
|
|
unsubProject();
|
|
unsubEvent();
|
|
};
|
|
}
|
|
let done = false;
|
|
return () => {
|
|
if (done) return;
|
|
done = true;
|
|
if (--users === 0) {
|
|
dispose?.();
|
|
dispose = null;
|
|
}
|
|
};
|
|
}
|
|
|
|
function currentDetected(): RootFolder[] {
|
|
const s = useProjectRoots.getState();
|
|
return s.root === (useProject.getState().project?.root ?? null) ? s.detected : [];
|
|
}
|
|
|
|
/** Effective roots right now (non-reactive). */
|
|
export function currentRoots(): RootFolder[] {
|
|
return effectiveRoots(currentDetected(), validateProjectStructure(useProject.getState().settings?.projectStructure));
|
|
}
|
|
|
|
/** The attached project holding the absolute `path` (outside the open project's folder), if any. */
|
|
export function attachedProjectOf(path: string): AttachedProject | null {
|
|
const s = useProjectRoots.getState();
|
|
if (s.root !== (useProject.getState().project?.root ?? null)) return null;
|
|
return s.attached.find((p) => path === p.root || path.startsWith(`${p.root}/`)) ?? null;
|
|
}
|
|
|
|
/** Detected roots of the open project right now (non-reactive). */
|
|
export function currentDetectedRoots(): RootFolder[] {
|
|
return currentDetected();
|
|
}
|
|
|
|
/** Effective roots (reactive). */
|
|
export function useEffectiveRoots(): RootFolder[] {
|
|
const structure = useProjectStructure();
|
|
const projectRoot = useProject((s) => s.project?.root ?? null);
|
|
const detected = useProjectRoots((s) => (s.root === projectRoot ? s.detected : null));
|
|
return useMemo(() => effectiveRoots(detected ?? [], structure), [detected, structure]);
|
|
}
|
|
|
|
/** Detected roots (reactive). */
|
|
export function useDetectedRoots(): RootFolder[] {
|
|
const projectRoot = useProject((s) => s.project?.root ?? null);
|
|
const detected = useProjectRoots((s) => (s.root === projectRoot ? s.detected : null));
|
|
return detected ?? EMPTY;
|
|
}
|
|
|
|
const EMPTY: RootFolder[] = [];
|
|
|
|
/** Project-relative path of an absolute `path`, or null outside the project. */
|
|
export function projectRelative(path: string): string | null {
|
|
const root = projectFolder(useProject.getState().project, path)?.root;
|
|
if (!root) return null;
|
|
if (path === root) return "";
|
|
return path.startsWith(root + "/") ? path.slice(root.length + 1) : null;
|
|
}
|
|
|
|
/** The innermost root containing the absolute `path`. */
|
|
export function rootOfPath(path: string, roots: readonly RootFolder[] = currentRoots()): RootFolder | null {
|
|
const rel = projectRelative(path);
|
|
return rel === null ? null : rootFor(roots, rel);
|
|
}
|
|
|
|
/** Whether the absolute `path` is a test file (test root or test-file name). */
|
|
export function isTestFile(path: string, roots: readonly RootFolder[] = currentRoots()): boolean {
|
|
const rel = projectRelative(path);
|
|
return rel !== null && isTestPath(roots, rel);
|
|
}
|
|
|
|
/** IDEA's named scopes over the roots. */
|
|
export type ProjectFileSet = "all" | "production" | "test";
|
|
|
|
/** Whether the absolute `path` belongs to a named project scope ("Project Production Files" …). */
|
|
export function inFileSet(set: ProjectFileSet, path: string, roots: readonly RootFolder[] = currentRoots()): boolean {
|
|
const rel = projectRelative(path);
|
|
if (rel === null) return false;
|
|
const root = rootFor(roots, rel);
|
|
if (root?.kind === "excluded") return false;
|
|
if (set === "all") return true;
|
|
return isTestPath(roots, rel) === (set === "test");
|
|
}
|
|
|
|
/** A module: the project itself or a folder with a build file roots were detected from. */
|
|
export interface ProjectModule {
|
|
name: string;
|
|
/** Project-relative, or absolute for a workspace member; `""` is a plain project root. */
|
|
path: string;
|
|
}
|
|
|
|
/** Modules of the project, root first, then by path. */
|
|
export function projectModules(detected: readonly RootFolder[], projectName: string): ProjectModule[] {
|
|
const dirs = new Set([""]);
|
|
for (const r of detected) {
|
|
if (!r.detectedFrom) continue;
|
|
const i = r.detectedFrom.lastIndexOf("/");
|
|
dirs.add(i < 0 ? "" : r.detectedFrom.slice(0, i));
|
|
}
|
|
return [...dirs]
|
|
.sort((a, b) => (a === "" ? -1 : b === "" ? 1 : a < b ? -1 : a > b ? 1 : 0))
|
|
.map((path) => ({ path, name: path ? path.slice(path.lastIndexOf("/") + 1) : projectName }));
|
|
}
|
|
|
|
/** Modules of the open project (reactive). */
|
|
export function useProjectModules(): ProjectModule[] {
|
|
const detected = useDetectedRoots();
|
|
const project = useProject((s) => s.project);
|
|
return useMemo(
|
|
() =>
|
|
project?.workspace
|
|
? project.workspace.projects.map((path) => ({ path, name: basename(path) }))
|
|
: projectModules(detected, project?.name ?? ""),
|
|
[detected, project],
|
|
);
|
|
}
|