Файли
openidea/src/platform/languageSettings.ts
T
RuyimgByCN 11e520dedb feat(project-tree): add "Add as Library…" for JARs and library directories (#7077)
Right-clicking a .jar or a folder of jars (such as WEB-INF/lib) in the Project view, or running it from Find Action, opens a Create Library dialog (name, scope, classes) that appends the library to projectStructure.libraries. Collapsed folders are probed briefly before the menu opens. Registered library folders get the library icon and an Open Library Settings entry instead of a duplicate add, External Libraries refreshes when jars inside them are added or removed, and manifest-free Java projects with libraries show the project JDK there.

Library class entries (jar globs, root-relative paths) are parsed once in platform/languageSettings for both the action and the refresh.

Closes #7051

Co-authored-by: RuyimgByCN <[email protected]>
2026-12-01 01:24:16 +08:00

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

Цей файл містить неоднозначні символи Юнікоду
Цей файл містить символи Юнікоду, які можна сплутати з іншими символами. Якщо ви вважаєте, що це зроблено навмисно, можете сміливо ігнорувати це попередження. Щоб показати їх, скористайтеся кнопкою Escape.
/**
* `languages.*` (interpreters / toolchains) and `projectStructure.*` settings:
* shape, defaults and normalisation (mirrors `ide-core::settings`).
*/
import { useMemo } from "react";
import type { MarkdownPreviewLayout } from "@/bindings";
import { useProject } from "./project";
export interface GoBuildTags {
os: string;
arch: string;
version: string;
compiler: string;
cgo: string;
custom: string;
experiments: string;
}
const DEFAULT_GO_BUILD_TAGS: GoBuildTags = {
os: "",
arch: "",
version: "",
compiler: "",
cgo: "",
custom: "",
experiments: "",
};
/** An external formatter (Prettier, oxfmt, Biome, dprint, Ruff, Black) and the languages it formats. */
interface ExternalFormatterValid {
path: string;
configurationDirectory: string;
languages: string[];
prefer: boolean;
runOnSave: boolean;
}
const DEFAULT_FORMATTER: ExternalFormatterValid = {
path: "",
configurationDirectory: "",
languages: [
"markdown",
"yaml",
"css",
"scss",
"less",
"html",
"graphql",
"javascript",
"javascriptreact",
"typescript",
"typescriptreact",
"json",
"jsonc",
"vue",
],
prefer: false,
runOnSave: false,
};
/** Biome formats these of Prettier's languages (`BIOME_LANGUAGES` in `ide-core`). */
const DEFAULT_BIOME_FORMATTER: ExternalFormatterValid = {
...DEFAULT_FORMATTER,
languages: ["css", "graphql", "javascript", "javascriptreact", "typescript", "typescriptreact", "json", "jsonc"],
};
/** dprint formats what its configuration covers: no language list. */
const DEFAULT_DPRINT_FORMATTER: ExternalFormatterValid = { ...DEFAULT_FORMATTER, languages: [] };
const DEFAULT_PYTHON_FORMATTER: ExternalFormatterValid = {
...DEFAULT_FORMATTER,
languages: ["python"],
};
const DEFAULT_RUFF_FORMATTER: LanguagesValid["ruff"] = {
...DEFAULT_PYTHON_FORMATTER,
fixOnSave: false,
};
/** `languages.php.languageLevel` values, newest first; `""` = Auto (from `composer.json`). */
export const PHP_LANGUAGE_LEVELS = [
"",
"8.5",
"8.4",
"8.3",
"8.2",
"8.1",
"8.0",
"7.4",
"7.3",
"7.2",
"7.1",
"7.0",
"5.6",
] as const;
/** `languages.java.languageLevel` values; `""` = SDK default (Auto). Levels before 8 need a jdtls
* that still compiles them (Settings › Languages & Frameworks › Language Servers). */
export const JAVA_LANGUAGE_LEVELS = [
"",
"5",
"6",
"7",
"8",
"9",
"10",
"11",
"12",
"13",
"14",
"15",
"16",
"17",
"18",
"19",
"20",
"21",
"22",
"23",
"24",
"25",
] as const;
export type DependencyScopeValid = "compile" | "test" | "runtime" | "provided";
export const DEPENDENCY_SCOPES = ["compile", "test", "runtime", "provided"] as const;
export interface ProjectLibraryValid {
name: string;
classes: string[];
sources: string[];
javadocs: string[];
scope: DependencyScopeValid;
}
export interface ModuleDependencyValid {
kind: "library" | "module";
target: string;
scope: DependencyScopeValid;
exported: boolean;
}
export interface ModuleStructureValid {
jdk: string;
languageLevel: (typeof JAVA_LANGUAGE_LEVELS)[number];
compilerOutput: string;
testCompilerOutput: string;
dependencies: ModuleDependencyValid[];
}
export interface FacetConfigValid {
kind: "web" | "spring" | "jpa";
module: string;
descriptor: string;
webRoot: string;
}
export type ArtifactKindValid = "jar" | "war" | "warExploded";
export const ARTIFACT_KINDS = ["jar", "war", "warExploded"] as const;
export interface ArtifactConfigValid {
name: string;
kind: ArtifactKindValid;
module: string;
outputDirectory: string;
mainClass: string;
includeLibraries: string[];
}
export interface LanguagesValid {
pluginFormatters: Record<string, string>;
python: {
interpreter: string;
activateVirtualenv: boolean;
/** Interpreters added to the IDE-wide list (Settings › Python › All Interpreters). */
interpreters: string[];
/** Interpreters removed from that list: detection does not show them again. */
hiddenInterpreters: string[];
/** Hide interpreters that belong to another project. */
hideOtherProjectInterpreters: boolean;
/** Show each interpreter's path under its name on the interpreters page. */
showInterpreterPaths: boolean;
/** Folders added to an interpreter's `sys.path`, by interpreter executable. */
interpreterPaths: Record<string, string[]>;
/** Default test runner of gutter and context-menu runs (Settings › Python › Testing). */
testRunner: PythonTestRunner;
/** What Profile runs Python under (Settings › Python › Profiler). */
profiler: PythonProfiler;
/** matplotlib figures of Python runs go to the Plots tool window. */
showPlotsInToolWindow: boolean;
};
ruby: { interpreter: string };
zig: { zig: string };
/** Flutter SDK root; empty = `flutter` / `dart` on PATH. */
flutter: { sdk: string };
/** `Xcode.app` (or its `Contents/Developer`) the project's Apple tools use; empty = `xcode-select`'s. */
swift: { xcode: string };
node: {
runTarget: string;
interpreter: string;
packageManager: "" | "npm" | "pnpm" | "yarn" | "bun";
runtime: "" | "node" | "bun" | "deno";
};
php: {
runTarget: string;
interpreter: string;
languageLevel: (typeof PHP_LANGUAGE_LEVELS)[number];
includePaths: string[];
stubs: string[];
phpstanConfig: string;
};
rust: {
toolchain: string;
stdlib: string;
checkOnSave: boolean;
checkCommand: "check" | "clippy";
rustfmtArgs: string;
};
go: {
goroot: string;
gopath: string;
gofumpt: boolean;
buildTags: GoBuildTags;
modules: { env: Record<string, string>; autoVendor: boolean };
};
typescript: { version: string };
prettier: ExternalFormatterValid;
oxfmt: ExternalFormatterValid;
biome: ExternalFormatterValid;
dprint: ExternalFormatterValid;
ruff: ExternalFormatterValid & { fixOnSave: boolean };
black: ExternalFormatterValid;
java: {
jdk: string;
languageLevel: (typeof JAVA_LANGUAGE_LEVELS)[number];
jdtlsJdk: string;
jdtlsVmArgs: string;
jdtlsShared: boolean;
gradle: {
linkedProjects: string[] | null;
jvm: string;
distribution: "wrapper" | "local";
home: string;
userHome: string;
/** IDEA's "Build and run using": Gradle (`JavaExec`) or the IDE (the language server's classpath). */
runUsing: "gradle" | "ide";
};
maven: {
runnerJre: string;
home: string;
userSettings: string;
toolchains: string;
localRepository: string;
unlinkedProjects: string[];
linkedProjects: string[];
daemon: boolean;
mvndHome: string;
/** `-T`: a thread count (4) or a core multiplier (2C, 2.5C); empty = Maven's default. */
threads: string;
/** Comma-separated `-P` profiles; empty = POM/settings defaults. */
activeProfiles: string;
/** Delegate IDE build/run actions to Maven (`javac`) instead of jdtls (ECJ). */
delegateBuild: boolean;
/** Download dependencies' `-sources.jar` when jdtls imports the project. */
downloadSources: boolean;
/** Always update snapshots: `-U` on Maven runs, `java.maven.updateSnapshots` for jdtls. */
updateSnapshots: boolean;
/** Who runs the gutter's JUnit 5 tests: OpenIDEA's runner or Surefire. */
runTestsUsing: "ide" | "maven";
};
};
markdown: {
previewLayout: MarkdownPreviewLayout;
/** Preview font size in px; `null` follows the editor font size. */
previewFontSize: number | null;
/** Run icons on shell code blocks and command lines. */
runCommands: boolean;
/** Where commands run; `null` asks when the project and the file's folder differ. */
commandDirectory: "project" | "file" | null;
};
}
export interface ProjectStructureValid {
sourceRoots: string[];
testRoots: string[];
resourceRoots: string[];
testResourceRoots: string[];
excluded: string[];
/** Auto-detected roots the user unmarked. */
unmarked: string[];
/** Content roots besides the project folder (absolute paths). */
contentRoots: string[];
/** Other projects (absolute paths) this project depends on; indexed and navigable with it. */
dependencies: string[];
/** Per-workspace-member dependencies: member path → the roots it depends on. */
memberDependencies: Record<string, string[]>;
/** "Exclude files": `;`-separated name patterns. */
excludeFiles: string;
/** Project compiler output directory; empty = default. */
compilerOutput: string;
/** Per-module overrides keyed by module relative path (`"."` for root). */
modules: Record<string, ModuleStructureValid>;
/** Project-level libraries. */
libraries: ProjectLibraryValid[];
/** Module framework facets (`web`, `spring`, `jpa`). */
facets: FacetConfigValid[];
/** Packaging artifacts (`jar`, `war`, `warExploded`). */
artifacts: ArtifactConfigValid[];
}
/** The folder lists of `ProjectStructureValid` (marks). */
export type FolderListKey =
| "sourceRoots"
| "testRoots"
| "resourceRoots"
| "testResourceRoots"
| "excluded"
| "unmarked";
export const PYTHON_TEST_RUNNERS = ["auto", "pytest", "unittest"] as const;
export type PythonTestRunner = (typeof PYTHON_TEST_RUNNERS)[number];
export const PYTHON_PROFILERS = ["auto", "cprofile", "py-spy"] as const;
export type PythonProfiler = (typeof PYTHON_PROFILERS)[number];
export const DEFAULT_LANGUAGES: LanguagesValid = {
pluginFormatters: {},
python: {
interpreter: "",
activateVirtualenv: true,
interpreters: [],
hiddenInterpreters: [],
hideOtherProjectInterpreters: false,
showInterpreterPaths: true,
interpreterPaths: {},
testRunner: "auto",
profiler: "auto",
showPlotsInToolWindow: true,
},
ruby: { interpreter: "" },
zig: { zig: "" },
flutter: { sdk: "" },
swift: { xcode: "" },
node: { runTarget: "", interpreter: "", packageManager: "", runtime: "" },
php: { runTarget: "", interpreter: "", languageLevel: "", includePaths: [], stubs: [], phpstanConfig: "" },
rust: { toolchain: "", stdlib: "", checkOnSave: true, checkCommand: "check", rustfmtArgs: "" },
go: {
goroot: "",
gopath: "",
gofumpt: false,
buildTags: DEFAULT_GO_BUILD_TAGS,
modules: { env: {}, autoVendor: true },
},
typescript: { version: "bundled" },
prettier: DEFAULT_FORMATTER,
oxfmt: DEFAULT_FORMATTER,
biome: DEFAULT_BIOME_FORMATTER,
dprint: DEFAULT_DPRINT_FORMATTER,
ruff: DEFAULT_RUFF_FORMATTER,
black: DEFAULT_PYTHON_FORMATTER,
java: {
jdk: "",
languageLevel: "",
jdtlsJdk: "",
jdtlsVmArgs: "",
jdtlsShared: false,
gradle: { linkedProjects: null, jvm: "", distribution: "wrapper", home: "", userHome: "", runUsing: "gradle" },
maven: {
runnerJre: "",
home: "",
userSettings: "",
toolchains: "",
localRepository: "",
unlinkedProjects: [],
linkedProjects: [],
daemon: false,
mvndHome: "",
threads: "",
activeProfiles: "",
delegateBuild: false,
downloadSources: false,
updateSnapshots: false,
runTestsUsing: "ide",
},
},
markdown: { previewLayout: "sideBySide", previewFontSize: null, runCommands: true, commandDirectory: null },
};
export const DEFAULT_PROJECT_STRUCTURE: ProjectStructureValid = {
sourceRoots: [],
testRoots: [],
resourceRoots: [],
testResourceRoots: [],
excluded: [],
unmarked: [],
contentRoots: [],
dependencies: [],
memberDependencies: {},
excludeFiles: "",
compilerOutput: "",
modules: {},
libraries: [],
facets: [],
artifacts: [],
};
type Obj = Record<string, unknown>;
const obj = (v: unknown): Obj => (v && typeof v === "object" && !Array.isArray(v) ? (v as Obj) : {});
const text = (v: unknown, d: string) => (typeof v === "string" ? v.trim() : d);
const bool = (v: unknown, d: boolean) => (typeof v === "boolean" ? v : d);
const oneOf = <T extends string>(v: unknown, allowed: readonly T[], d: T): T =>
typeof v === "string" && (allowed as readonly string[]).includes(v) ? (v as T) : d;
const markdownPreviewLayout = (v: unknown): MarkdownPreviewLayout =>
oneOf(v, ["sideBySide", "stacked"], DEFAULT_LANGUAGES.markdown.previewLayout);
/** Project-relative folder: `/`-separated, no leading `./` or `/`, no trailing `/`. */
export function normalizeFolder(path: string): string {
return path
.replace(/\\/g, "/")
.trim()
.replace(/^(\.\/)+/, "")
.replace(/^\/+|\/+$/g, "");
}
/**
* A Project Structure library class entry (a jar, a folder, or a folder with a `*.jar` or recursive
* jar glob; absolute or relative) as a `/` path relative to `root` when inside it, plus its glob.
*/
export function libraryClassEntry(
entry: string,
root: string,
): { path: string; glob: "none" | "shallow" | "recursive" } | null {
let s = entry.trim().replace(/\\/g, "/");
if (!s) return null;
let glob: "none" | "shallow" | "recursive" = "none";
if (s.endsWith("/**/*.jar")) [s, glob] = [s.slice(0, -"/**/*.jar".length), "recursive"];
else if (s.endsWith("/*.jar")) [s, glob] = [s.slice(0, -"/*.jar".length), "shallow"];
const base = root.replace(/\\/g, "/").replace(/\/+$/, "");
const path = s === base ? "" : s.startsWith(`${base}/`) ? s.slice(base.length + 1) : s;
return { path: normalizeFolder(path), glob };
}
/** A stored preview font size within IDEA's 8..72, or `null` (follow the editor font). */
const previewSize = (v: unknown): number | null =>
typeof v === "number" && Number.isFinite(v) ? Math.min(72, Math.max(8, Math.round(v))) : null;
/** Non-empty trimmed strings, without duplicates. */
const strings = (v: unknown): string[] =>
Array.isArray(v)
? [...new Set(v.filter((x): x is string => typeof x === "string").map((x) => x.trim()))].filter((x) => x)
: [];
const folders = (v: unknown): string[] =>
Array.isArray(v)
? [
...new Set(
v
.filter((x): x is string => typeof x === "string")
.map(normalizeFolder)
.filter((x) => x),
),
]
: [];
/** Settings store the content root itself as `.`; roots use `""`. */
const folderKey = (stored: string) => (stored === "." ? "" : stored);
const storedFolder = (rel: string) => normalizeFolder(rel) || ".";
function validateFormatter(raw: unknown, d = DEFAULT_FORMATTER): ExternalFormatterValid {
const f = obj(raw);
return {
path: text(f.path, d.path),
configurationDirectory: text(f.configurationDirectory, d.configurationDirectory),
languages: Array.isArray(f.languages)
? [...new Set(f.languages.filter((v): v is string => typeof v === "string"))]
: d.languages,
prefer: bool(f.prefer, d.prefer),
runOnSave: bool(f.runOnSave, d.runOnSave),
};
}
function validateRuffFormatter(raw: unknown): LanguagesValid["ruff"] {
const ruff = obj(raw);
return {
...validateFormatter(ruff, DEFAULT_RUFF_FORMATTER),
fixOnSave: bool(ruff.fixOnSave, DEFAULT_RUFF_FORMATTER.fixOnSave),
};
}
export function validateLanguages(raw: unknown): LanguagesValid {
const d = DEFAULT_LANGUAGES;
const l = obj(raw);
const py = obj(l.python);
const ruby = obj(l.ruby);
const zig = obj(l.zig);
const flutter = obj(l.flutter);
const swift = obj(l.swift);
const node = obj(l.node);
const php = obj(l.php);
const rust = obj(l.rust);
const go = obj(l.go);
const tags = obj(go.buildTags);
const modules = obj(go.modules);
const ts = obj(l.typescript);
const java = obj(l.java);
const maven = obj(java.maven);
const gradle = obj(java.gradle);
const markdown = obj(l.markdown);
return {
pluginFormatters: Object.fromEntries(
Object.entries(obj(l.pluginFormatters)).filter(
(entry): entry is [string, string] => typeof entry[1] === "string",
),
),
python: {
interpreter: text(py.interpreter, d.python.interpreter),
activateVirtualenv: bool(py.activateVirtualenv, d.python.activateVirtualenv),
interpreters: strings(py.interpreters),
hiddenInterpreters: strings(py.hiddenInterpreters),
hideOtherProjectInterpreters: bool(py.hideOtherProjectInterpreters, d.python.hideOtherProjectInterpreters),
showInterpreterPaths: bool(py.showInterpreterPaths, d.python.showInterpreterPaths),
interpreterPaths: Object.fromEntries(
Object.entries(obj(py.interpreterPaths)).map(([python, paths]) => [python, strings(paths)]),
),
testRunner: oneOf(py.testRunner, PYTHON_TEST_RUNNERS, d.python.testRunner),
profiler: oneOf(py.profiler, PYTHON_PROFILERS, d.python.profiler),
showPlotsInToolWindow: bool(py.showPlotsInToolWindow, d.python.showPlotsInToolWindow),
},
ruby: { interpreter: text(ruby.interpreter, d.ruby.interpreter) },
zig: { zig: text(zig.zig, d.zig.zig) },
flutter: { sdk: text(flutter.sdk, d.flutter.sdk) },
swift: { xcode: text(swift.xcode, d.swift.xcode) },
node: {
runTarget: text(node.runTarget, ""),
interpreter: text(node.interpreter, d.node.interpreter),
packageManager: oneOf(node.packageManager, ["", "npm", "pnpm", "yarn", "bun"] as const, d.node.packageManager),
runtime: oneOf(node.runtime, ["", "node", "bun", "deno"] as const, d.node.runtime),
},
php: {
runTarget: text(php.runTarget, ""),
interpreter: text(php.interpreter, d.php.interpreter),
languageLevel: oneOf(php.languageLevel, PHP_LANGUAGE_LEVELS, d.php.languageLevel),
includePaths: strings(php.includePaths),
stubs: strings(php.stubs),
phpstanConfig: text(php.phpstanConfig, d.php.phpstanConfig),
},
rust: {
toolchain: text(rust.toolchain, d.rust.toolchain),
stdlib: text(rust.stdlib, d.rust.stdlib),
checkOnSave: bool(rust.checkOnSave, d.rust.checkOnSave),
checkCommand: oneOf(rust.checkCommand, ["check", "clippy"] as const, d.rust.checkCommand),
rustfmtArgs: typeof rust.rustfmtArgs === "string" ? rust.rustfmtArgs : d.rust.rustfmtArgs,
},
go: {
goroot: text(go.goroot, d.go.goroot),
gopath: text(go.gopath, d.go.gopath),
gofumpt: bool(go.gofumpt, d.go.gofumpt),
buildTags: Object.fromEntries(
Object.entries(DEFAULT_GO_BUILD_TAGS).map(([key, value]) => [key, text(tags[key], value)]),
) as unknown as GoBuildTags,
modules: {
autoVendor: bool(modules.autoVendor, true),
env: Object.fromEntries(
Object.entries(obj(modules.env)).filter(
([key, value]) => !!key && !/[=\s\0]/.test(key) && typeof value === "string" && !value.includes("\0"),
),
) as Record<string, string>,
},
},
typescript: { version: text(ts.version, d.typescript.version) || d.typescript.version },
prettier: validateFormatter(l.prettier),
oxfmt: validateFormatter(l.oxfmt),
biome: validateFormatter(l.biome, DEFAULT_BIOME_FORMATTER),
dprint: validateFormatter(l.dprint, DEFAULT_DPRINT_FORMATTER),
ruff: validateRuffFormatter(l.ruff),
black: validateFormatter(l.black, DEFAULT_PYTHON_FORMATTER),
java: {
jdk: text(java.jdk, d.java.jdk),
languageLevel: oneOf(java.languageLevel, JAVA_LANGUAGE_LEVELS, d.java.languageLevel),
jdtlsJdk: text(java.jdtlsJdk, d.java.jdtlsJdk),
jdtlsVmArgs: text(java.jdtlsVmArgs, d.java.jdtlsVmArgs),
jdtlsShared: bool(java.jdtlsShared, d.java.jdtlsShared),
gradle: {
linkedProjects: Array.isArray(gradle.linkedProjects)
? gradle.linkedProjects.filter((p): p is string => typeof p === "string")
: null,
jvm: text(gradle.jvm, d.java.gradle.jvm),
distribution: oneOf(gradle.distribution, ["wrapper", "local"] as const, d.java.gradle.distribution),
home: text(gradle.home, d.java.gradle.home),
userHome: text(gradle.userHome, d.java.gradle.userHome),
runUsing: oneOf(gradle.runUsing, ["gradle", "ide"] as const, d.java.gradle.runUsing),
},
maven: {
runnerJre: text(maven.runnerJre, d.java.maven.runnerJre),
home: text(maven.home, d.java.maven.home),
userSettings: text(maven.userSettings, d.java.maven.userSettings),
toolchains: text(maven.toolchains, d.java.maven.toolchains),
localRepository: text(maven.localRepository, d.java.maven.localRepository),
unlinkedProjects: folders(maven.unlinkedProjects),
linkedProjects: folders(maven.linkedProjects),
daemon: bool(maven.daemon, d.java.maven.daemon),
mvndHome: text(maven.mvndHome, d.java.maven.mvndHome),
threads: text(maven.threads, d.java.maven.threads),
activeProfiles: text(maven.activeProfiles, d.java.maven.activeProfiles),
delegateBuild: bool(maven.delegateBuild, d.java.maven.delegateBuild),
downloadSources: bool(maven.downloadSources, d.java.maven.downloadSources),
updateSnapshots: bool(maven.updateSnapshots, d.java.maven.updateSnapshots),
runTestsUsing: oneOf(maven.runTestsUsing, ["ide", "maven"] as const, d.java.maven.runTestsUsing),
},
},
markdown: {
previewLayout: markdownPreviewLayout(markdown.previewLayout),
previewFontSize: previewSize(markdown.previewFontSize),
runCommands: bool(markdown.runCommands, d.markdown.runCommands),
commandDirectory:
markdown.commandDirectory === "project" || markdown.commandDirectory === "file"
? markdown.commandDirectory
: null,
},
};
}
/**
* Markdown preview (and notebook Markdown cell) font size in px: Languages &
* Frameworks › Markdown, else the editor font size, as IDEA defaults it.
*/
export function markdownPreviewFontSize(settings: unknown): number {
const s = obj(settings);
const editorSize = obj(s.editor).fontSize;
return (
previewSize(obj(obj(s.languages).markdown).previewFontSize) ??
// 13: the editor's default font size.
(typeof editorSize === "number" && editorSize > 0 ? editorSize : 13)
);
}
/** Languages & Frameworks › Markdown › Commands of `settings`. */
export function markdownCommandSettings(
settings: unknown,
): Pick<LanguagesValid["markdown"], "runCommands" | "commandDirectory"> {
const markdown = validateLanguages(obj(settings).languages).markdown;
return { runCommands: markdown.runCommands, commandDirectory: markdown.commandDirectory };
}
/** Reactive `markdownPreviewFontSize` of the open project. */
export function useMarkdownPreviewFontSize(): number {
return useProject((s) => markdownPreviewFontSize(s.settings));
}
export function useMarkdownPreviewLayout(): MarkdownPreviewLayout {
return useProject((s) => markdownPreviewLayout(s.settings?.languages?.markdown?.previewLayout));
}
function validateModuleDependencies(raw: unknown): ModuleDependencyValid[] {
if (!Array.isArray(raw)) return [];
const out: ModuleDependencyValid[] = [];
for (const item of raw) {
const m = obj(item);
const target = text(m.target, "");
if (!target) continue;
out.push({
kind: oneOf(m.kind, ["library", "module"] as const, "library"),
target,
scope: oneOf(m.scope, DEPENDENCY_SCOPES, "compile"),
exported: bool(m.exported, false),
});
}
return out;
}
function validateModulesMap(raw: unknown): Record<string, ModuleStructureValid> {
const m = obj(raw);
const out: Record<string, ModuleStructureValid> = {};
for (const [k, v] of Object.entries(m)) {
const key = normalizeFolder(k) || ".";
const mod = obj(v);
out[key] = {
jdk: text(mod.jdk, ""),
languageLevel: oneOf(mod.languageLevel, JAVA_LANGUAGE_LEVELS, ""),
compilerOutput: text(mod.compilerOutput, ""),
testCompilerOutput: text(mod.testCompilerOutput, ""),
dependencies: validateModuleDependencies(mod.dependencies),
};
}
return out;
}
function validateLibrariesList(raw: unknown): ProjectLibraryValid[] {
if (!Array.isArray(raw)) return [];
const out: ProjectLibraryValid[] = [];
for (const item of raw) {
const l = obj(item);
const name = text(l.name, "");
const classes = strings(l.classes);
const sources = strings(l.sources);
const javadocs = strings(l.javadocs);
if (!name && classes.length === 0 && sources.length === 0) continue;
out.push({
name: name || (classes[0]?.split(/[\\/]/).pop() ?? "Library"),
classes,
sources,
javadocs,
scope: oneOf(l.scope, DEPENDENCY_SCOPES, "compile"),
});
}
return out;
}
function validateFacetsList(raw: unknown): FacetConfigValid[] {
if (!Array.isArray(raw)) return [];
const out: FacetConfigValid[] = [];
for (const item of raw) {
const f = obj(item);
const kind = oneOf(f.kind, ["web", "spring", "jpa"] as const, "web");
const module = normalizeFolder(text(f.module, ".")) || ".";
out.push({
kind,
module,
descriptor: text(f.descriptor, ""),
webRoot: text(f.webRoot, ""),
});
}
return out;
}
function validateArtifactsList(raw: unknown): ArtifactConfigValid[] {
if (!Array.isArray(raw)) return [];
const out: ArtifactConfigValid[] = [];
for (const item of raw) {
const a = obj(item);
const name = text(a.name, "");
if (!name) continue;
out.push({
name,
kind: oneOf(a.kind, ARTIFACT_KINDS, "jar"),
module: normalizeFolder(text(a.module, ".")) || ".",
outputDirectory: text(a.outputDirectory, ""),
mainClass: text(a.mainClass, ""),
includeLibraries: strings(a.includeLibraries),
});
}
return out;
}
export function validateProjectStructure(raw: unknown): ProjectStructureValid {
const p = obj(raw);
return {
sourceRoots: folders(p.sourceRoots),
testRoots: folders(p.testRoots),
resourceRoots: folders(p.resourceRoots),
testResourceRoots: folders(p.testResourceRoots),
excluded: folders(p.excluded),
unmarked: folders(p.unmarked),
contentRoots: Array.isArray(p.contentRoots)
? [...new Set(p.contentRoots.filter((x): x is string => typeof x === "string" && !!x.trim()))]
: [],
dependencies: Array.isArray(p.dependencies)
? [...new Set(p.dependencies.filter((x): x is string => typeof x === "string" && !!x.trim()))]
: [],
memberDependencies: Object.fromEntries(
Object.entries(obj(p.memberDependencies))
.map(([member, deps]) => [
member,
Array.isArray(deps) ? [...new Set(deps.filter((x): x is string => typeof x === "string" && !!x.trim()))] : [],
])
.filter(([member, deps]) => !!member && (deps as string[]).length > 0),
),
excludeFiles: typeof p.excludeFiles === "string" ? p.excludeFiles : "",
compilerOutput: text(p.compilerOutput, ""),
modules: validateModulesMap(p.modules),
libraries: validateLibrariesList(p.libraries),
facets: validateFacetsList(p.facets),
artifacts: validateArtifactsList(p.artifacts),
};
}
/** Content roots plus dependency projects, deduplicated (what the Project view, index and run see). */
export function effectiveContentRoots(ps: ProjectStructureValid): string[] {
return [...new Set([...ps.contentRoots, ...ps.dependencies])];
}
/** Root kinds, as in IDEA's Project Structure (`ide-workspace::roots::RootKind`). */
export type FolderRole = "source" | "test" | "resources" | "testResources" | "excluded";
export const FOLDER_ROLES: readonly FolderRole[] = ["source", "test", "resources", "testResources", "excluded"];
const ROLE_KEYS: Record<FolderRole, Exclude<FolderListKey, "unmarked">> = {
source: "sourceRoots",
test: "testRoots",
resources: "resourceRoots",
testResources: "testResourceRoots",
excluded: "excluded",
};
/** A root of the project: user-marked (`detectedFrom` null) or detected from a build file. */
export interface RootFolder {
/** Project-relative, `/`-separated; `""` is the project root. */
path: string;
kind: FolderRole;
detectedFrom: string | null;
}
/** User-marked role of a project-relative folder (exact match), or null. */
export function folderRole(ps: ProjectStructureValid, rel: string): FolderRole | null {
const r = storedFolder(rel);
for (const role of FOLDER_ROLES) {
if (ps[ROLE_KEYS[role]].includes(r)) return role;
}
return null;
}
/**
* Effective roots: user marks win, `unmarked` hides detected roots
* (mirrors `ProjectRoots::compute`), sorted by path.
*/
export function effectiveRoots(detected: readonly RootFolder[], ps: ProjectStructureValid): RootFolder[] {
const out: RootFolder[] = [];
for (const role of FOLDER_ROLES)
for (const path of ps[ROLE_KEYS[role]].map(folderKey))
if (!out.some((r) => r.path === path)) out.push({ path, kind: role, detectedFrom: null });
const unmarked = ps.unmarked.map(folderKey);
for (const d of detected) if (!unmarked.includes(d.path) && !out.some((r) => r.path === d.path)) out.push({ ...d });
return out.sort((a, b) => (a.path < b.path ? -1 : a.path > b.path ? 1 : 0));
}
/** Effective role of a folder (exact match): what its icon shows. */
export function rootRole(roots: readonly RootFolder[], rel: string): FolderRole | null {
const r = normalizeFolder(rel);
return roots.find((x) => x.path === r)?.kind ?? null;
}
const inside = (rel: string, root: string) => !root || rel === root || rel.startsWith(`${root}/`);
/** The innermost root containing the project-relative path `rel`. */
export function rootFor(roots: readonly RootFolder[], rel: string): RootFolder | null {
const r = normalizeFolder(rel);
let best: RootFolder | null = null;
for (const root of roots) if (inside(r, root.path) && (!best || root.path.length > best.path.length)) best = root;
return best;
}
/** Test files recognised by name (mirrors `ide-workspace::roots::is_test_file_name`). */
export function isTestFileName(name: string): boolean {
if (name.endsWith("_test.go")) return true;
if (name.endsWith(".py")) {
const stem = name.slice(0, -3);
return stem.startsWith("test_") || stem.endsWith("_test") || stem === "conftest";
}
return /^.+\.(test|spec)\.(js|jsx|ts|tsx|mjs|cjs|mts|cts)$/.test(name);
}
/** IDEA's "Project Test Files": under a test (resource) root, or a test file by name. */
export function isTestPath(roots: readonly RootFolder[], rel: string): boolean {
const root = rootFor(roots, rel);
if (root?.kind === "test" || root?.kind === "testResources") return true;
if (root?.kind === "excluded") return false;
return isTestFileName(normalizeFolder(rel).split("/").pop() ?? "");
}
/**
* Gives `rel` the `role` (null = Unmark). Marking a detected root with its
* detected role needs no override; unmarking a detected root records it in `unmarked`.
*/
export function markFolder(
ps: ProjectStructureValid,
rel: string,
role: FolderRole | null,
detected: readonly RootFolder[] = [],
): ProjectStructureValid {
const r = storedFolder(rel);
const next: ProjectStructureValid = {
...ps,
sourceRoots: ps.sourceRoots.filter((x) => x !== r),
testRoots: ps.testRoots.filter((x) => x !== r),
resourceRoots: ps.resourceRoots.filter((x) => x !== r),
testResourceRoots: ps.testResourceRoots.filter((x) => x !== r),
excluded: ps.excluded.filter((x) => x !== r),
unmarked: ps.unmarked.filter((x) => x !== r),
};
const auto = detected.find((d) => d.path === folderKey(r))?.kind ?? null;
if (role === null) {
if (auto) next.unmarked = [...next.unmarked, r].sort();
} else if (role !== auto) {
next[ROLE_KEYS[role]] = [...next[ROLE_KEYS[role]], r].sort();
}
return next;
}
/** Detected roots the user unmarked. */
export function unmarkedRoots(detected: readonly RootFolder[], ps: ProjectStructureValid): RootFolder[] {
const unmarked = ps.unmarked.map(folderKey);
return detected.filter((d) => unmarked.includes(d.path));
}
/** Brings back every unmarked detected root. */
export function restoreDetected(ps: ProjectStructureValid, detected: readonly RootFolder[]): ProjectStructureValid {
return { ...ps, unmarked: ps.unmarked.filter((p) => !detected.some((d) => d.path === folderKey(p))) };
}
/** Settings keys of `ProjectStructureValid`, in a stable order. */
export const PROJECT_STRUCTURE_KEYS = [
"sourceRoots",
"testRoots",
"resourceRoots",
"testResourceRoots",
"excluded",
"unmarked",
] as const satisfies ReadonlyArray<FolderListKey>;
/** Effective Project Structure of the open project (reactive). */
export function useProjectStructure(): ProjectStructureValid {
const raw = useProject((s) => (s.settings as { projectStructure?: unknown } | null)?.projectStructure);
return useMemo(() => validateProjectStructure(raw), [raw]);
}