дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-05 17:47:08 +02:00
feat(vcs): group Commit changes by module (#5789)
Adds View Options › Group By › Module to the Commit and Local Changes trees. Changes are grouped under the innermost module holding them: Maven and Gradle modules (named by their display name, artifactId or Gradle name), sbt projects, modules detected from build files, and attached workspace members. Files outside every module stay at the top level. Under Group By › Repository the module nodes sit inside each repository. The choice is remembered, F7 / Shift+F7 follow the grouped tree, and a module node can't be ignored as a whole. The option shows only when the project has modules. Co-authored-by: RuyimgByCN <[email protected]>
Цей коміт міститься в:
1 джерело
2622c9d778
коміт
dc35952cf4
9 files changed
+625
-12
No files matched your search
@@ -82,6 +82,7 @@ import {
|
||||
} from "./changes";
|
||||
import { amendPreviewNode } from "./amendPreview";
|
||||
import { AmendTargetMenu } from "./AmendTargetMenu";
|
||||
import { useChangeModules } from "./changeModules";
|
||||
import { commitChanges, deleteFiles } from "./changeOps";
|
||||
import { CommitOptionsPopup } from "./CommitOptions";
|
||||
import { CommitCheckSummary } from "./CommitCheckSummary";
|
||||
@@ -113,6 +114,7 @@ import {
|
||||
setCommitDiffPreview,
|
||||
setDiffPreview,
|
||||
setGroupByDir,
|
||||
setGroupByModule,
|
||||
setGroupByRepo,
|
||||
setGroupByStaging,
|
||||
setShowUnversionedSeparately,
|
||||
@@ -806,6 +808,7 @@ export function ChangesTree({ mode, amended }: { mode: "commit" | "local" | "dia
|
||||
const ignored = useIgnoredFiles(root, staging && showIgnored);
|
||||
const groupByStaging = useCommit((s) => s.groupByStaging) || staging;
|
||||
const groupByRepo = useCommit((s) => s.groupByRepo);
|
||||
const groupByModule = useCommit((s) => s.groupByModule);
|
||||
const showUnversionedSeparately = useCommit((s) => s.showUnversionedSeparately);
|
||||
const diffPreview = useCommit((s) => s.diffPreview);
|
||||
const commitDiffPreview = useCommit((s) => s.commitDiffPreview);
|
||||
@@ -825,6 +828,7 @@ export function ChangesTree({ mode, amended }: { mode: "commit" | "local" | "dia
|
||||
const heads = useVcs((s) => s.heads);
|
||||
const projectRoot = useProject((s) => s.project?.root ?? null);
|
||||
const projectRoots = useEffectiveRoots();
|
||||
const changeModules = useChangeModules();
|
||||
const detectedRoots = useDetectedRoots();
|
||||
const attachedProjects = useProjectRoots((s) => (s.root === projectRoot ? s.attached : EMPTY_ATTACHED));
|
||||
const buildPresentation = useProjectPresentation();
|
||||
@@ -840,6 +844,8 @@ export function ChangesTree({ mode, amended }: { mode: "commit" | "local" | "dia
|
||||
groupByRepo,
|
||||
showUnversionedSeparately,
|
||||
ignored?.files,
|
||||
changeModules,
|
||||
groupByModule,
|
||||
);
|
||||
return amended ? [amended, ...changes] : changes;
|
||||
}, [
|
||||
@@ -853,6 +859,8 @@ export function ChangesTree({ mode, amended }: { mode: "commit" | "local" | "dia
|
||||
groupByRepo,
|
||||
showUnversionedSeparately,
|
||||
ignored,
|
||||
changeModules,
|
||||
groupByModule,
|
||||
amended,
|
||||
]);
|
||||
const [byKey, parents] = useMemo(() => {
|
||||
@@ -1007,6 +1015,9 @@ export function ChangesTree({ mode, amended }: { mode: "commit" | "local" | "dia
|
||||
...(repos.length > 1
|
||||
? [{ label: "Repository", checked: groupByRepo, run: () => setGroupByRepo(!groupByRepo) }]
|
||||
: []),
|
||||
...(changeModules.length > 0
|
||||
? [{ label: "Module", checked: groupByModule, run: () => setGroupByModule(!groupByModule) }]
|
||||
: []),
|
||||
...(staging
|
||||
? []
|
||||
: [
|
||||
@@ -1274,6 +1285,8 @@ export function ChangesTree({ mode, amended }: { mode: "commit" | "local" | "dia
|
||||
className={clsx(styles.icon, n.warning && styles.committerWarning)}
|
||||
/>
|
||||
))
|
||||
) : n.module ? (
|
||||
<Icon name="module" size={16} className={styles.icon} />
|
||||
) : ignoredDirectory(n) || n.repository ? (
|
||||
<Icon name={st.expanded ? "folder-open" : "folder"} size={16} className={styles.icon} />
|
||||
) : n.type === "dir" ? (
|
||||
|
||||
@@ -565,3 +565,88 @@ describe("changeFileNodes", () => {
|
||||
expect(nodes.map((n) => n.group)).toEqual(["staged", "unstaged"]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Group By › Module in buildChangeTree", () => {
|
||||
const at = (path: string, unstaged: FileStatus["unstaged"] = "modified"): FileStatus => ({
|
||||
path,
|
||||
relPath: path.replace(/^\/w\//, ""),
|
||||
staged: null,
|
||||
unstaged,
|
||||
renamedFrom: null,
|
||||
});
|
||||
|
||||
const modules = [
|
||||
{ path: "/w/base", name: "base", tool: "maven" as const },
|
||||
{ path: "/w/basemdm", name: "BASEMDM", tool: "maven" as const },
|
||||
{ path: "/w/basemdm/sub", name: "sub-mod", tool: "maven" as const },
|
||||
];
|
||||
|
||||
it("groups files by module alone (flat and by directory)", () => {
|
||||
const files = [
|
||||
at("/w/base/src/Main.java"),
|
||||
at("/w/basemdm/src/Mdm.java"),
|
||||
at("/w/basemdm/sub/src/Sub.java"),
|
||||
at("/w/README.md"),
|
||||
];
|
||||
const flat = buildChangeTree(files, "/w", false, undefined, [], false, {}, false, true, [], modules, true);
|
||||
expect(shape(flat)).toEqual([
|
||||
"Changes",
|
||||
" base",
|
||||
" Main.java",
|
||||
" BASEMDM",
|
||||
" Mdm.java",
|
||||
" sub-mod",
|
||||
" Sub.java",
|
||||
" README.md",
|
||||
]);
|
||||
const dirs = buildChangeTree(files, "/w", true, undefined, [], false, {}, false, true, [], modules, true);
|
||||
expect(shape(dirs)).toEqual([
|
||||
"Changes",
|
||||
" base",
|
||||
" src",
|
||||
" Main.java",
|
||||
" BASEMDM",
|
||||
" src",
|
||||
" Mdm.java",
|
||||
" sub-mod",
|
||||
" src",
|
||||
" Sub.java",
|
||||
" README.md",
|
||||
]);
|
||||
});
|
||||
|
||||
it("nests modules under repositories and computes relative path details", () => {
|
||||
const repos = ["/w/base", "/w/basemdm"];
|
||||
const heads = {
|
||||
"/w/base": { head: "main", headCommit: "11111111" },
|
||||
"/w/basemdm": { head: "master", headCommit: "22222222" },
|
||||
};
|
||||
const files = [at("/w/base/src/Main.java"), at("/w/basemdm/src/Mdm.java")];
|
||||
const tree = buildChangeTree(files, "/w", true, undefined, repos, false, heads, true, true, [], modules, true);
|
||||
expect(shape(tree)).toEqual([
|
||||
"Changes",
|
||||
" base",
|
||||
" base",
|
||||
" src",
|
||||
" Main.java",
|
||||
" basemdm",
|
||||
" BASEMDM",
|
||||
" src",
|
||||
" Mdm.java",
|
||||
]);
|
||||
const [baseRepo, mdmRepo] = tree[0]!.children!;
|
||||
expect(baseRepo).toMatchObject({ repository: "/w/base", detail: "main", detailIcon: "branch" });
|
||||
expect(mdmRepo).toMatchObject({ repository: "/w/basemdm", detail: "master", detailIcon: "branch" });
|
||||
expect(baseRepo!.children![0]).toMatchObject({ module: "/w/base", label: "base" });
|
||||
expect(baseRepo!.children![0]!.detail).toBeUndefined();
|
||||
expect(mdmRepo!.children![0]).toMatchObject({ module: "/w/basemdm", label: "BASEMDM", detail: "basemdm" });
|
||||
});
|
||||
|
||||
it("does not ignore the whole module directory from ignoreTargets", () => {
|
||||
const files = [at("/w/basemdm/a.tmp", "untracked"), at("/w/basemdm/extra/b.tmp", "untracked")];
|
||||
const tree = buildChangeTree(files, "/w", true, undefined, ["/w"], false, {}, true, true, [], modules, true);
|
||||
const mdmMod = tree[0]!.children![0]!;
|
||||
expect(mdmMod.module).toBe("/w/basemdm");
|
||||
expect(ignoreTargets([mdmMod], ["/w"]).sort()).toEqual(["/w/basemdm/a.tmp", "/w/basemdm/extra"]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,228 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { act, cleanup, fireEvent, render, screen } from "@testing-library/react";
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
buildModel: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("@/platform/ipc", async (importOriginal) => {
|
||||
const real = await importOriginal<typeof import("@/platform/ipc")>();
|
||||
return {
|
||||
...real,
|
||||
isTauri: () => true,
|
||||
commands: {
|
||||
...real.commands,
|
||||
buildModel: mocks.buildModel,
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock("@/ui", async (original) => ({
|
||||
...(await original<typeof import("@/ui")>()),
|
||||
Icon: ({ name, className }: { name: string; className?: string }) => (
|
||||
<span data-testid={`icon-${name}`} className={className} />
|
||||
),
|
||||
}));
|
||||
|
||||
vi.mock("../DiffEditor", () => ({ DiffEditor: () => <div /> }));
|
||||
|
||||
import { useProject } from "@/platform/project";
|
||||
import { useBuildPresentation } from "@/platform/projectPresentation";
|
||||
import { useProjectRoots } from "@/platform/projectRoots";
|
||||
import { useContextMenuStore, type MenuItem } from "@/ui/Menu";
|
||||
import { ChangesTree } from "../CommitView";
|
||||
import { resolveChangeModules } from "../changeModules";
|
||||
import { setGroupByModule, useCommit } from "../commitStore";
|
||||
import { useVcs } from "../store";
|
||||
|
||||
const change = (path: string) => ({
|
||||
path,
|
||||
relPath: path.split("/").slice(3).join("/"),
|
||||
staged: null,
|
||||
unstaged: "modified" as const,
|
||||
renamedFrom: null,
|
||||
});
|
||||
|
||||
function groupByItems(): Record<string, boolean> {
|
||||
act(() => {
|
||||
fireEvent.click(screen.getByRole("button", { name: "View Options" }));
|
||||
});
|
||||
const items = useContextMenuStore.getState().menu!.items as Array<MenuItem & { label?: string; checked?: boolean }>;
|
||||
act(() => useContextMenuStore.setState({ menu: null }));
|
||||
const end = items.findIndex((i) => i.kind === "separator");
|
||||
return Object.fromEntries(
|
||||
items
|
||||
.slice(0, end < 0 ? undefined : end)
|
||||
.filter((i) => i.kind !== "header" && i.label)
|
||||
.map((i) => [i.label!, !!i.checked]),
|
||||
);
|
||||
}
|
||||
|
||||
function runItem(label: string) {
|
||||
act(() => {
|
||||
fireEvent.click(screen.getByRole("button", { name: "View Options" }));
|
||||
});
|
||||
const item = useContextMenuStore.getState().menu!.items.find((i) => "label" in i && i.label === label) as {
|
||||
run: () => void;
|
||||
};
|
||||
act(() => {
|
||||
item.run();
|
||||
useContextMenuStore.setState({ menu: null });
|
||||
});
|
||||
}
|
||||
|
||||
describe("resolveChangeModules", () => {
|
||||
it("collects modules from detected roots, attached members, workspace projects, and build models", () => {
|
||||
const modules = resolveChangeModules({
|
||||
projectRoot: "/w",
|
||||
projectName: "workspace",
|
||||
workspaceProjects: ["/w/base", "/w/basemdm"],
|
||||
detected: [{ path: "src/main/java", kind: "source", detectedFrom: "pom.xml" }],
|
||||
attached: [
|
||||
{
|
||||
root: "/w/basemdm",
|
||||
structure: {} as never,
|
||||
detected: [{ path: "sub/src/main/java", kind: "source", detectedFrom: "sub/pom.xml" }],
|
||||
},
|
||||
],
|
||||
build: {
|
||||
modules: new Map([
|
||||
["/w/base", "maven"],
|
||||
["/w/basemdm", "maven"],
|
||||
["/w/basemdm/sub", "gradle"],
|
||||
]),
|
||||
moduleNames: new Map([
|
||||
["/w/base", "base-core"],
|
||||
["/w/basemdm", "BASEMDM"],
|
||||
["/w/basemdm/sub", "sub-gradle"],
|
||||
["/w/scala", "scala-svc"],
|
||||
]),
|
||||
spring: false,
|
||||
},
|
||||
});
|
||||
expect(modules).toEqual([
|
||||
{ path: "/w", name: "workspace", tool: undefined },
|
||||
{ path: "/w/base", name: "base-core", tool: "maven" },
|
||||
{ path: "/w/basemdm", name: "BASEMDM", tool: "maven" },
|
||||
{ path: "/w/basemdm/sub", name: "sub-gradle", tool: "gradle" },
|
||||
{ path: "/w/scala", name: "scala-svc", tool: undefined },
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Group By › Module in ChangesTree", () => {
|
||||
beforeEach(() => {
|
||||
localStorage.clear();
|
||||
mocks.buildModel.mockReset().mockResolvedValue({
|
||||
status: "ok",
|
||||
data: {
|
||||
maven: {
|
||||
projects: [
|
||||
{
|
||||
dir: "/w/base",
|
||||
pom: "/w/base/pom.xml",
|
||||
groupId: "com.acme",
|
||||
artifactId: "base",
|
||||
displayName: "base",
|
||||
version: "1.0",
|
||||
packaging: "jar",
|
||||
parent: null,
|
||||
modules: [],
|
||||
aggregated: false,
|
||||
plugins: [],
|
||||
dependencies: [],
|
||||
profiles: [],
|
||||
javaVersion: 21,
|
||||
projectRoot: "/w/base",
|
||||
},
|
||||
{
|
||||
dir: "/w/basemdm",
|
||||
pom: "/w/basemdm/pom.xml",
|
||||
groupId: "com.acme",
|
||||
artifactId: "BASEMDM",
|
||||
displayName: "BASEMDM",
|
||||
version: "1.0",
|
||||
packaging: "jar",
|
||||
parent: null,
|
||||
modules: [],
|
||||
aggregated: false,
|
||||
plugins: [],
|
||||
dependencies: [],
|
||||
profiles: [],
|
||||
javaVersion: 21,
|
||||
projectRoot: "/w/basemdm",
|
||||
},
|
||||
],
|
||||
profiles: [],
|
||||
syncedAt: null,
|
||||
stale: false,
|
||||
syncError: null,
|
||||
},
|
||||
gradle: [],
|
||||
},
|
||||
});
|
||||
useBuildPresentation.setState({ modules: new Map(), moduleNames: new Map(), spring: false });
|
||||
useCommit.setState({
|
||||
groupByDir: false,
|
||||
groupByStaging: false,
|
||||
groupByRepo: true,
|
||||
groupByModule: false,
|
||||
checks: new Map(),
|
||||
selection: [],
|
||||
});
|
||||
useProject.setState({
|
||||
project: {
|
||||
name: "w",
|
||||
root: "/w",
|
||||
trusted: true,
|
||||
isGit: true,
|
||||
settings: {},
|
||||
workspace: { version: 1, name: "w", projects: ["/w/base", "/w/basemdm"] },
|
||||
} as never,
|
||||
settings: {} as never,
|
||||
});
|
||||
useProjectRoots.setState({ root: "/w", detected: [], attached: [] });
|
||||
useVcs.setState({
|
||||
root: "/w",
|
||||
repo: "/w/base",
|
||||
roots: ["/w/base", "/w/basemdm"],
|
||||
heads: {
|
||||
"/w/base": { head: "main", headCommit: "11111111" },
|
||||
"/w/basemdm": { head: "master", headCommit: "22222222" },
|
||||
},
|
||||
status: [change("/w/base/src/Main.java"), change("/w/basemdm/src/Mdm.java")],
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
});
|
||||
|
||||
it("offers Module in View Options, toggles and persists it, and renders module nodes with module icons", async () => {
|
||||
render(<ChangesTree mode="commit" />);
|
||||
expect(await screen.findByText("base")).toBeInTheDocument();
|
||||
expect(groupByItems()).toEqual({
|
||||
Directory: false,
|
||||
Repository: true,
|
||||
Module: false,
|
||||
"Staging State": false,
|
||||
});
|
||||
|
||||
runItem("Module");
|
||||
expect(useCommit.getState().groupByModule).toBe(true);
|
||||
expect(localStorage.getItem("openidea.vcs.groupByModule")).toBe("true");
|
||||
expect(groupByItems().Module).toBe(true);
|
||||
|
||||
const mdmModuleLabel = await screen.findByText("BASEMDM");
|
||||
const mdmModuleRow = mdmModuleLabel.closest('[role="treeitem"]') as HTMLElement;
|
||||
expect(mdmModuleRow.querySelector('[data-testid="icon-module"]')).not.toBeNull();
|
||||
expect(mdmModuleRow.textContent).toContain("basemdm");
|
||||
});
|
||||
|
||||
it("persists groupByModule across store reloads", async () => {
|
||||
setGroupByModule(true);
|
||||
vi.resetModules();
|
||||
const { useCommit: fresh } = await import("../commitStore");
|
||||
expect(fresh.getState().groupByModule).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -72,6 +72,7 @@ import {
|
||||
type DiffData,
|
||||
} from "./diffInput";
|
||||
import { buildChangeTree, changeFileNodes, changeGroupPaths, selectedChangeNodes, type ChangeNode } from "./changes";
|
||||
import { currentChangeModules } from "./changeModules";
|
||||
import { useChangelists } from "./changelists";
|
||||
import { openDialog } from "./dialogs";
|
||||
import {
|
||||
@@ -190,6 +191,9 @@ export function currentChangeTree(): ChangeNode[] {
|
||||
undefined,
|
||||
commit.groupByRepo,
|
||||
commit.showUnversionedSeparately,
|
||||
[],
|
||||
currentChangeModules(),
|
||||
commit.groupByModule,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,135 @@
|
||||
/** Module resolution for Commit tool window grouping (`Group By › Module`). */
|
||||
import { useMemo } from "react";
|
||||
import type { RootFolder } from "@/platform/languageSettings";
|
||||
import { useProject } from "@/platform/project";
|
||||
import { useBuildPresentation, useProjectPresentation, type BuildPresentation } from "@/platform/projectPresentation";
|
||||
import { useDetectedRoots, useProjectRoots, type AttachedProject } from "@/platform/projectRoots";
|
||||
import { baseName, stripSlash } from "./model";
|
||||
|
||||
export interface ChangeModule {
|
||||
/** Absolute path of the module root directory (no trailing slash). */
|
||||
path: string;
|
||||
/** Display name (Maven displayName/artifactId, Gradle name, sbt id, or folder basename). */
|
||||
name: string;
|
||||
/** Build tool when known. */
|
||||
tool?: "maven" | "gradle";
|
||||
}
|
||||
|
||||
export interface ChangeModulesInput {
|
||||
projectRoot: string | null;
|
||||
projectName?: string;
|
||||
workspaceProjects?: readonly string[];
|
||||
detected?: readonly RootFolder[];
|
||||
attached?: readonly AttachedProject[];
|
||||
build?: BuildPresentation;
|
||||
}
|
||||
|
||||
const EMPTY_ATTACHED: AttachedProject[] = [];
|
||||
|
||||
const isAbsolute = (path: string) => path.startsWith("/") || /^[A-Za-z]:[\\/]/.test(path);
|
||||
|
||||
function absPath(base: string, dir: string): string {
|
||||
if (isAbsolute(dir)) return stripSlash(dir);
|
||||
const cleanBase = stripSlash(base);
|
||||
const cleanDir = stripSlash(dir);
|
||||
return !cleanDir ? cleanBase : cleanBase ? `${cleanBase}/${cleanDir}` : cleanDir;
|
||||
}
|
||||
|
||||
function detectedModuleDir(detectedFrom: string): string {
|
||||
const clean = stripSlash(detectedFrom);
|
||||
const i = clean.lastIndexOf("/");
|
||||
return i < 0 ? "" : clean.slice(0, i);
|
||||
}
|
||||
|
||||
/** Collects project/build modules with display names, sorted by path. */
|
||||
export function resolveChangeModules({
|
||||
projectRoot,
|
||||
projectName,
|
||||
workspaceProjects,
|
||||
detected = [],
|
||||
attached = [],
|
||||
build,
|
||||
}: ChangeModulesInput): ChangeModule[] {
|
||||
const byPath = new Map<string, ChangeModule>();
|
||||
const cleanProjectRoot = projectRoot ? stripSlash(projectRoot) : null;
|
||||
|
||||
const add = (path: string, name?: string, tool?: "maven" | "gradle") => {
|
||||
const p = stripSlash(path);
|
||||
if (!p) return;
|
||||
const prev = byPath.get(p);
|
||||
const fallback = cleanProjectRoot && p === cleanProjectRoot && projectName ? projectName : baseName(p) || p;
|
||||
byPath.set(p, {
|
||||
path: p,
|
||||
name: name?.trim() || prev?.name || fallback,
|
||||
tool: tool ?? prev?.tool,
|
||||
});
|
||||
};
|
||||
|
||||
if (cleanProjectRoot) {
|
||||
for (const r of detected) {
|
||||
if (!r.detectedFrom) continue;
|
||||
add(absPath(cleanProjectRoot, detectedModuleDir(r.detectedFrom)));
|
||||
}
|
||||
}
|
||||
for (const m of attached) {
|
||||
add(m.root);
|
||||
for (const r of m.detected) {
|
||||
if (!r.detectedFrom) continue;
|
||||
add(absPath(m.root, detectedModuleDir(r.detectedFrom)));
|
||||
}
|
||||
}
|
||||
for (const memberRoot of workspaceProjects ?? []) {
|
||||
add(memberRoot);
|
||||
}
|
||||
if (build) {
|
||||
for (const [dir, tool] of build.modules) {
|
||||
if (cleanProjectRoot || isAbsolute(dir)) {
|
||||
add(absPath(cleanProjectRoot ?? "", dir), build.moduleNames?.get(dir), tool);
|
||||
}
|
||||
}
|
||||
for (const [dir, name] of build.moduleNames ?? []) {
|
||||
if (!build.modules.has(dir) && (cleanProjectRoot || isAbsolute(dir))) {
|
||||
add(absPath(cleanProjectRoot ?? "", dir), name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return [...byPath.values()].sort((a, b) => a.path.localeCompare(b.path));
|
||||
}
|
||||
|
||||
/** Resolved modules for the open project (reactive). */
|
||||
export function useChangeModules(): ChangeModule[] {
|
||||
const project = useProject((s) => s.project);
|
||||
const detected = useDetectedRoots();
|
||||
const projectRoot = project?.root ?? null;
|
||||
const attached = useProjectRoots((s) => (s.root === projectRoot ? s.attached : EMPTY_ATTACHED));
|
||||
const build = useProjectPresentation();
|
||||
return useMemo(
|
||||
() =>
|
||||
resolveChangeModules({
|
||||
projectRoot,
|
||||
projectName: project?.name,
|
||||
workspaceProjects: project?.workspace?.projects,
|
||||
detected,
|
||||
attached,
|
||||
build,
|
||||
}),
|
||||
[projectRoot, project?.name, project?.workspace?.projects, detected, attached, build],
|
||||
);
|
||||
}
|
||||
|
||||
/** Non-reactive snapshot of resolved modules for action helpers. */
|
||||
export function currentChangeModules(): ChangeModule[] {
|
||||
const project = useProject.getState().project;
|
||||
const projectRoot = project?.root ?? null;
|
||||
const rootsState = useProjectRoots.getState();
|
||||
const sameRoot = rootsState.root === projectRoot;
|
||||
return resolveChangeModules({
|
||||
projectRoot,
|
||||
projectName: project?.name,
|
||||
workspaceProjects: project?.workspace?.projects,
|
||||
detected: sameRoot ? rootsState.detected : [],
|
||||
attached: sameRoot ? rootsState.attached : [],
|
||||
build: useBuildPresentation.getState(),
|
||||
});
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
import type { ChangelistState, FileStatus, StagingPatchEntry } from "@/bindings";
|
||||
import { baseName, dirName, effectiveKind, relTo, repoLabel, rootOf, stripSlash, type EffectiveKind } from "./model";
|
||||
import { tx } from "@/platform/i18n";
|
||||
import type { ChangeModule } from "./changeModules";
|
||||
import type { DiffData } from "./diffInput";
|
||||
import type { RepoHead } from "./store";
|
||||
|
||||
@@ -48,6 +49,8 @@ export interface ChangeNode {
|
||||
path?: string;
|
||||
/** Repository root of a repository grouping node. */
|
||||
repository?: string;
|
||||
/** Module root path of a module grouping node. */
|
||||
module?: string;
|
||||
/** Detail text: the relative directory (flat mode), the old name or a repository's branch. */
|
||||
detail?: string;
|
||||
/** Icon before the detail: a repository's branch, or its HEAD commit when detached. */
|
||||
@@ -230,6 +233,69 @@ function headHint(head: RepoHead | undefined): Pick<ChangeNode, "detail" | "deta
|
||||
return {};
|
||||
}
|
||||
|
||||
/** Innermost module holding `path`, or null when outside every known module. */
|
||||
export function moduleOf(path: string, modules: readonly ChangeModule[]): ChangeModule | null {
|
||||
const p = stripSlash(path);
|
||||
let best: ChangeModule | null = null;
|
||||
for (const mod of modules) {
|
||||
if ((p === mod.path || p.startsWith(`${mod.path}/`)) && (!best || mod.path.length > best.path.length)) best = mod;
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
/** With `Group By › Module`, a node per module holding its changes above the directory/file level. */
|
||||
function moduleNodes(
|
||||
files: readonly FileStatus[],
|
||||
baseRoot: string,
|
||||
projectRoot: string,
|
||||
group: GroupId,
|
||||
groupByDir: boolean,
|
||||
scope: string,
|
||||
modules: readonly ChangeModule[],
|
||||
): ChangeNode[] {
|
||||
if (!modules.length) return childNodes(files, baseRoot, group, groupByDir, scope);
|
||||
const byMod = new Map<ChangeModule, FileStatus[]>();
|
||||
const loose: FileStatus[] = [];
|
||||
for (const f of files) {
|
||||
const mod = moduleOf(f.path, modules);
|
||||
if (!mod) {
|
||||
loose.push(f);
|
||||
continue;
|
||||
}
|
||||
let list = byMod.get(mod);
|
||||
if (!list) byMod.set(mod, (list = []));
|
||||
list.push(f);
|
||||
}
|
||||
const cleanProject = stripSlash(projectRoot);
|
||||
const grouped = [...byMod]
|
||||
.sort(([a], [b]) => a.name.localeCompare(b.name) || a.path.localeCompare(b.path))
|
||||
.map(([mod, own]): ChangeNode => {
|
||||
const isRootModule = !!cleanProject && mod.path === cleanProject;
|
||||
const rel = isRootModule ? "" : cleanProject ? relTo(cleanProject, mod.path) : mod.path;
|
||||
const detail = isRootModule
|
||||
? mod.name !== baseName(mod.path)
|
||||
? baseName(mod.path)
|
||||
: undefined
|
||||
: rel && rel !== mod.name
|
||||
? rel === mod.path
|
||||
? dirName(mod.path) || undefined
|
||||
: rel
|
||||
: undefined;
|
||||
return {
|
||||
key: `${scope}:mod:${mod.path}`,
|
||||
type: "dir",
|
||||
label: mod.name,
|
||||
group,
|
||||
children: childNodes(own, mod.path, group, groupByDir, `${scope}@mod:${mod.path}`),
|
||||
files: own,
|
||||
path: mod.path,
|
||||
module: mod.path,
|
||||
...(detail ? { detail } : {}),
|
||||
};
|
||||
});
|
||||
return [...grouped, ...(loose.length ? childNodes(loose, baseRoot, group, groupByDir, scope) : [])];
|
||||
}
|
||||
|
||||
/** With several repositories, a node per repository (IDEA's "Group by Repository") above the files. */
|
||||
function repoNodes(
|
||||
files: readonly FileStatus[],
|
||||
@@ -239,8 +305,9 @@ function repoNodes(
|
||||
scope: string,
|
||||
repos: readonly string[],
|
||||
heads: Readonly<Record<string, RepoHead>>,
|
||||
modules: readonly ChangeModule[],
|
||||
): ChangeNode[] {
|
||||
if (repos.length < 2) return childNodes(files, root, group, groupByDir, scope);
|
||||
if (repos.length < 2) return moduleNodes(files, root, root, group, groupByDir, scope, modules);
|
||||
const byRepo = new Map<string, FileStatus[]>();
|
||||
for (const f of files) {
|
||||
const repo = rootOf(f.path, repos) ?? root;
|
||||
@@ -256,7 +323,7 @@ function repoNodes(
|
||||
type: "dir" as const,
|
||||
label: repoLabel(repo, root),
|
||||
group,
|
||||
children: childNodes(own, repo, group, groupByDir, `${scope}@${repo}`),
|
||||
children: moduleNodes(own, repo, root, group, groupByDir, `${scope}@${repo}`, modules),
|
||||
files: own,
|
||||
path: repo,
|
||||
repository: repo,
|
||||
@@ -293,8 +360,11 @@ export function buildChangeTree(
|
||||
byRepo = true,
|
||||
showUnversionedSeparately = true,
|
||||
ignored: FileStatus[] = [],
|
||||
modules: readonly ChangeModule[] = [],
|
||||
byModule = false,
|
||||
): ChangeNode[] {
|
||||
const repoRoots = byRepo ? repos : [];
|
||||
const moduleList = byModule ? modules : [];
|
||||
const byGroup = new Map<GroupId, FileStatus[]>();
|
||||
for (const fs of status) {
|
||||
const kind = groupOf(fs) ?? (byStaging && addedThenDeleted(fs) ? "changes" : null);
|
||||
@@ -315,7 +385,7 @@ export function buildChangeTree(
|
||||
];
|
||||
for (const [side, own] of sides) {
|
||||
if (!own.length) continue;
|
||||
const children = repoNodes(own, root, side, groupByDir, side, repoRoots, heads);
|
||||
const children = repoNodes(own, root, side, groupByDir, side, repoRoots, heads, moduleList);
|
||||
roots.push({
|
||||
key: `group:${side}`,
|
||||
type: "group",
|
||||
@@ -343,7 +413,7 @@ export function buildChangeTree(
|
||||
type: "group",
|
||||
label: l.name,
|
||||
group: g,
|
||||
children: tagList(repoNodes(own, root, g, groupByDir, scope, repoRoots, heads), l.id),
|
||||
children: tagList(repoNodes(own, root, g, groupByDir, scope, repoRoots, heads, moduleList), l.id),
|
||||
files: own,
|
||||
changelist: l.id,
|
||||
active: l.id === lists.active,
|
||||
@@ -353,7 +423,7 @@ export function buildChangeTree(
|
||||
continue;
|
||||
}
|
||||
if (!files.length) continue;
|
||||
const children = repoNodes(files, root, g, groupByDir, g, repoRoots, heads);
|
||||
const children = repoNodes(files, root, g, groupByDir, g, repoRoots, heads, moduleList);
|
||||
roots.push({ key: `group:${g}`, type: "group", label: GROUP_LABEL[g], group: g, children, files });
|
||||
}
|
||||
if (ignored.length) {
|
||||
@@ -362,7 +432,7 @@ export function buildChangeTree(
|
||||
type: "group",
|
||||
label: GROUP_LABEL.ignored,
|
||||
group: "ignored",
|
||||
children: repoNodes(ignored, root, "ignored", groupByDir, "ignored", repoRoots, heads),
|
||||
children: repoNodes(ignored, root, "ignored", groupByDir, "ignored", repoRoots, heads, moduleList),
|
||||
files: ignored,
|
||||
});
|
||||
}
|
||||
@@ -566,8 +636,15 @@ export function ignoreTargets(nodes: readonly ChangeNode[], repos: readonly stri
|
||||
const add = (n: ChangeNode) => {
|
||||
const untracked = n.files.filter((f) => f.unstaged === "untracked");
|
||||
if (!untracked.length) return;
|
||||
// A repository node's path is the root itself, which cannot be ignored.
|
||||
if (n.type === "dir" && n.path && n.files.length === untracked.length && !roots.has(stripSlash(n.path))) {
|
||||
// A repository or module node's path is the root itself, which cannot be ignored.
|
||||
if (
|
||||
n.type === "dir" &&
|
||||
!n.repository &&
|
||||
!n.module &&
|
||||
n.path &&
|
||||
n.files.length === untracked.length &&
|
||||
!roots.has(stripSlash(n.path))
|
||||
) {
|
||||
out.add(stripSlash(n.path));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -51,6 +51,8 @@ interface CommitState extends CommitAuthorDetails {
|
||||
groupByStaging: boolean;
|
||||
/** With several repositories, a node per repository above their files. */
|
||||
groupByRepo: boolean;
|
||||
/** Group the changes by project / build module. */
|
||||
groupByModule: boolean;
|
||||
/** Keep untracked files in their own group instead of Changes / Unstaged Changes. */
|
||||
showUnversionedSeparately: boolean;
|
||||
/** Project-specific visibility of Git-ignored files in the staging tree. */
|
||||
@@ -107,6 +109,7 @@ export const useCommit = create<CommitState>(() => ({
|
||||
groupByDir: readBool("openidea.vcs.groupByDir", false),
|
||||
groupByStaging: readBool("openidea.vcs.groupByStaging", false),
|
||||
groupByRepo: readBool("openidea.vcs.groupByRepo", true),
|
||||
groupByModule: readBool("openidea.vcs.groupByModule", false),
|
||||
showUnversionedSeparately: readBool("openidea.vcs.showUnversionedSeparately", true),
|
||||
ignoredVisibility: new Map(),
|
||||
diffPreview: readBool("openidea.vcs.diffPreview", true),
|
||||
@@ -192,6 +195,15 @@ export function setGroupByRepo(v: boolean) {
|
||||
}
|
||||
}
|
||||
|
||||
export function setGroupByModule(v: boolean) {
|
||||
useCommit.setState({ groupByModule: v });
|
||||
try {
|
||||
localStorage.setItem("openidea.vcs.groupByModule", String(v));
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
export function setShowUnversionedSeparately(v: boolean) {
|
||||
useCommit.setState({ showUnversionedSeparately: v });
|
||||
try {
|
||||
|
||||
@@ -93,3 +93,44 @@ it("includes modules and Spring tasks from every independently linked Gradle bui
|
||||
expect([...context.modules.keys()]).toEqual(["first", "first/app", "second"]);
|
||||
expect(context.spring).toBe(true);
|
||||
});
|
||||
|
||||
it("extracts module display names for Maven, Gradle, and sbt projects", () => {
|
||||
const context = buildPresentation(
|
||||
model({
|
||||
maven: {
|
||||
projects: [
|
||||
{ dir: "", displayName: "Root App", artifactId: "root-app", dependencies: [], plugins: [] },
|
||||
{ dir: "basemdm", displayName: "", artifactId: "BASEMDM", dependencies: [], plugins: [] },
|
||||
],
|
||||
},
|
||||
gradle: [
|
||||
{
|
||||
projects: [
|
||||
{ dir: "billing", name: "billing-core", tasks: [] },
|
||||
{ dir: "basemdm", name: "ignored-dup", tasks: [] },
|
||||
],
|
||||
},
|
||||
],
|
||||
sbt: [
|
||||
{
|
||||
dir: "",
|
||||
projects: [{ id: "scalaRoot", dir: "" }],
|
||||
},
|
||||
{
|
||||
dir: "services",
|
||||
projects: [
|
||||
{ id: "servicesRoot", dir: "" },
|
||||
{ id: "worker", dir: "worker-sub" },
|
||||
],
|
||||
},
|
||||
],
|
||||
}),
|
||||
);
|
||||
expect([...(context.moduleNames ?? new Map())]).toEqual([
|
||||
["", "Root App"],
|
||||
["basemdm", "ignored-dup"],
|
||||
["billing", "billing-core"],
|
||||
["services", "servicesRoot"],
|
||||
["services/worker-sub", "worker"],
|
||||
]);
|
||||
});
|
||||
@@ -6,22 +6,40 @@ import { useProject } from "./project";
|
||||
|
||||
export interface BuildPresentation {
|
||||
modules: ReadonlyMap<string, "maven" | "gradle">;
|
||||
/** Module dir key (same key space as `modules`, plus sbt) → display name. */
|
||||
moduleNames?: ReadonlyMap<string, string>;
|
||||
spring: boolean;
|
||||
}
|
||||
const EMPTY: BuildPresentation = { modules: new Map(), spring: false };
|
||||
const EMPTY: BuildPresentation = { modules: new Map(), moduleNames: new Map(), spring: false };
|
||||
export const useBuildPresentation = create<BuildPresentation>(() => EMPTY);
|
||||
|
||||
export function buildPresentation(model: BuildModel): BuildPresentation {
|
||||
const modules = new Map<string, "maven" | "gradle">();
|
||||
for (const p of model.maven?.projects ?? []) modules.set(p.dir, "maven");
|
||||
for (const p of (model.gradle ?? []).flatMap((g) => g.projects)) modules.set(p.dir, "gradle");
|
||||
const moduleNames = new Map<string, string>();
|
||||
for (const p of model.maven?.projects ?? []) {
|
||||
modules.set(p.dir, "maven");
|
||||
const name = p.displayName?.trim() || p.artifactId?.trim();
|
||||
if (name) moduleNames.set(p.dir, name);
|
||||
}
|
||||
for (const p of (model.gradle ?? []).flatMap((g) => g.projects)) {
|
||||
modules.set(p.dir, "gradle");
|
||||
const name = p.name?.trim();
|
||||
if (name) moduleNames.set(p.dir, name);
|
||||
}
|
||||
for (const b of model.sbt ?? []) {
|
||||
for (const p of b.projects) {
|
||||
const dir = !b.dir ? p.dir : !p.dir ? b.dir : `${b.dir.replace(/\/+$/, "")}/${p.dir}`;
|
||||
const name = p.id?.trim();
|
||||
if (name && !moduleNames.has(dir)) moduleNames.set(dir, name);
|
||||
}
|
||||
}
|
||||
const spring =
|
||||
(model.maven?.projects ?? []).some(
|
||||
(p) =>
|
||||
p.dependencies.some((d) => d.groupId.startsWith("org.springframework")) ||
|
||||
p.plugins.some((p) => p.groupId.startsWith("org.springframework")),
|
||||
) || (model.gradle ?? []).some((g) => g.projects.some((p) => p.tasks.some((t) => t.name === "bootRun")));
|
||||
return { modules, spring };
|
||||
return { modules, moduleNames, spring };
|
||||
}
|
||||
|
||||
/** A Maven / Gradle module folder below the project root: IDEA shows its name in bold. */
|
||||
|
||||
Reference in new issue
Заблокувати користувача