Файли
openidea/src/features/project-tree/packages.ts
T
audichuang f8a148c06d fix(project-tree): don't offer Python Package in a nested non-Python module's roots (#6028)
In a project with a root Python build file and a nested JavaScript app (frontend/package.json),
New inside frontend/src offered Python Package: any folder under a Python build file counted as
Python, even inside a root a deeper non-Python build file detected. The innermost claim now
wins; folders outside those roots, and roots a Python build file claimed first, keep Python
Package.

Co-authored-by: audichuang <[email protected]>
2026-11-26 18:34:37 +08:00

36 рядки
1.5 KiB
TypeScript
Неформатований Анотація Історія

Цей файл містить неоднозначні символи Юнікоду
Цей файл містить символи Юнікоду, які можна сплутати з іншими символами. Якщо ви вважаєте, що це зроблено навмисно, можете сміливо ігнорувати це попередження. Щоб показати їх, скористайтеся кнопкою Escape.
/** Which "New › Package" flavour a folder offers, from the project's roots. */
import { rootFor, type RootFolder } from "@/platform/languageSettings";
import { isJvmSourcePackage } from "@/ui/folderPresentation";
import type { PackageKind } from "./paths";
const PY_BUILD = /(^|\/)(pyproject\.toml|setup\.py|setup\.cfg|Pipfile|requirements\.txt)$/;
const moduleDir = (buildFile: string) =>
buildFile.includes("/") ? buildFile.slice(0, buildFile.lastIndexOf("/")) : "";
const within = (rel: string, dir: string) => !dir || rel === dir || rel.startsWith(`${dir}/`);
/**
* `jvm` inside a Java / Kotlin source or test root (IDEA shows "Package" there
* instead of "Directory"); `python` inside a Python module (PyCharm's "Python Package").
*/
export function packageKindAt(
rel: string,
roots: readonly RootFolder[],
detected: readonly RootFolder[] = [],
): PackageKind | null {
if (rootFor(roots, rel)?.kind === "excluded") return null;
if (isJvmSourcePackage(rel, roots)) return "jvm";
// A root `pyproject.toml` must not claim `frontend/src`, a root that
// `frontend/package.json` detected, unless a deeper Python module holds the folder.
let python = -1;
let other = -1;
for (const r of [...roots, ...detected]) {
if (!r.detectedFrom) continue;
if (PY_BUILD.test(r.detectedFrom)) {
const dir = moduleDir(r.detectedFrom);
if (within(rel, dir)) python = Math.max(python, dir.length);
} else if (within(rel, r.path)) other = Math.max(other, r.path.length);
}
return python >= 0 && python >= other ? "python" : null;
}