дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-03 16:51:16 +02:00
fix(project): offer New › Package in source roots marked by hand in Maven and Gradle modules (#7542)
A folder marked as Sources or Test Sources Root by hand inside a Maven or Gradle module now offers New › Package and Java paste uses its package, instead of creating a single folder named com.example.
Цей коміт міститься в:
1 джерело
607812d8fa
коміт
4a620287c8
2 files changed
+48
-14
No files matched your search
@@ -1,6 +1,7 @@
|
||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
import type { RootFolder } from "@/platform/languageSettings";
|
||||
import { useProject } from "@/platform/project";
|
||||
import { useBuildPresentation } from "@/platform/projectPresentation";
|
||||
import { useProjectRoots } from "@/platform/projectRoots";
|
||||
import type { MenuItem } from "@/ui";
|
||||
import { newMenuItems } from "./contextMenu";
|
||||
@@ -56,6 +57,24 @@ describe("New › Package", () => {
|
||||
expect(packageKindAt("py/src", [], py)).toBe("python");
|
||||
});
|
||||
|
||||
it("is offered inside a root marked by hand in a Maven or Gradle module", () => {
|
||||
const marked = [...MAVEN, root("src", "source", null), root("tools/gen", "test", null)];
|
||||
expect(packageKindAt("src/com", marked)).toBeNull();
|
||||
try {
|
||||
useBuildPresentation.setState({ modules: new Map([["", "maven"]]) });
|
||||
expect(packageKindAt("src", marked)).toBe("jvm");
|
||||
expect(packageKindAt("src/com/wyh", marked)).toBe("jvm");
|
||||
expect(packageKindAt("tools/gen", marked)).toBe("jvm");
|
||||
useBuildPresentation.setState({ modules: new Map([["app", "gradle"]]) });
|
||||
expect(packageKindAt("src", marked)).toBeNull();
|
||||
expect(packageKindAt("app/src", [root("app/src", "source", null)])).toBe("jvm");
|
||||
// Resources roots stay plain directories.
|
||||
expect(packageKindAt("app/res", [root("app/res", "resources", null)])).toBeNull();
|
||||
} finally {
|
||||
useBuildPresentation.setState({ modules: new Map() });
|
||||
}
|
||||
});
|
||||
|
||||
it("keeps the default layout a Gradle sync reports", () => {
|
||||
// Package in java roots, Directory in resources, as before.
|
||||
const synced = [
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/** Shared folder-role, module-glyph, and package-icon resolution for Project and Commit trees. */
|
||||
import { effectiveRoots, rootFor, rootRole, type FolderRole, type RootFolder } from "@/platform/languageSettings";
|
||||
import { basename, relativePath } from "@/platform/project";
|
||||
import { moduleFolderIcon, type BuildPresentation } from "@/platform/projectPresentation";
|
||||
import { moduleFolderIcon, useBuildPresentation, type BuildPresentation } from "@/platform/projectPresentation";
|
||||
import type { AttachedProject } from "@/platform/projectRoots";
|
||||
import { folderIconName, themedFolderIconName, type FolderKind, type IconName } from "./icons";
|
||||
|
||||
@@ -53,16 +53,35 @@ export function projectScopeForPath(
|
||||
};
|
||||
}
|
||||
|
||||
/** Inside a Java / Kotlin source or test root, where IDEA shows "Package" instead of "Directory". */
|
||||
export function isJvmSourcePackage(rel: string, roots: readonly RootFolder[]): boolean {
|
||||
const root = rootFor(roots, rel);
|
||||
if (root?.kind === "excluded") return false;
|
||||
return (
|
||||
(root?.kind === "source" || root?.kind === "test") &&
|
||||
(JVM_BUILD.test(root.detectedFrom ?? "") || JVM_DIR.test(root.path))
|
||||
/** `path` (absolute) or `rel` (project-relative) lies in one of the Maven / Gradle modules. */
|
||||
function inBuildModule(
|
||||
path: string | null,
|
||||
rel: string,
|
||||
modules: BuildPresentation["modules"],
|
||||
attachedMember = false,
|
||||
): boolean {
|
||||
return [...modules.keys()].some((dir) =>
|
||||
isAbsolute(dir)
|
||||
? path !== null && (path === dir || path.startsWith(`${dir}/`))
|
||||
: !attachedMember && (!dir || rel === dir || rel.startsWith(`${dir}/`)),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Inside a Java / Kotlin source or test root, where IDEA shows "Package" instead of "Directory".
|
||||
* A folder marked as a root by hand holds packages too when it is in a Maven / Gradle module.
|
||||
*/
|
||||
export function isJvmSourcePackage(
|
||||
rel: string,
|
||||
roots: readonly RootFolder[],
|
||||
modules: BuildPresentation["modules"] = useBuildPresentation.getState().modules,
|
||||
): boolean {
|
||||
const root = rootFor(roots, rel);
|
||||
if (root?.kind !== "source" && root?.kind !== "test") return false;
|
||||
if (JVM_BUILD.test(root.detectedFrom ?? "") || JVM_DIR.test(root.path)) return true;
|
||||
return root.detectedFrom === null && inBuildModule(null, rel, modules);
|
||||
}
|
||||
|
||||
/** Output roles are relative to detected JVM modules, never arbitrary package names. */
|
||||
export function outputFolderKind(
|
||||
rel: string,
|
||||
@@ -115,12 +134,8 @@ export function directoryFolderKind(
|
||||
if (role) return ROLE_KIND[role];
|
||||
if (output) return output;
|
||||
if (rel !== null && (containing?.kind === "source" || containing?.kind === "test")) {
|
||||
const inModule = [...(build?.modules.keys() ?? [])].some((dir) =>
|
||||
isAbsolute(dir)
|
||||
? path === dir || path.startsWith(`${dir}/`)
|
||||
: !attachedMember && (!dir || rel.startsWith(`${dir}/`)),
|
||||
);
|
||||
if (inModule || isJvmSourcePackage(rel, roots)) return "package";
|
||||
const modules = build?.modules ?? new Map();
|
||||
if (inBuildModule(path, rel, modules, attachedMember) || isJvmSourcePackage(rel, roots, modules)) return "package";
|
||||
}
|
||||
return basename(path) === ".github" ? "github" : "plain";
|
||||
}
|
||||
|
||||
Reference in new issue
Заблокувати користувача