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.
Цей коміт міститься в:
l0ng-ai authored and GitHub committed 2026-12-02 20:37:58 +08:00
1 джерело 607812d8fa
коміт 4a620287c8
2 files changed
+48 -14

No files matched your search

+29 -14
Переглянути файл
@@ -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";
}