From 11e520dedb9b4306977a706b23ba6aa9b0bd5aa7 Mon Sep 17 00:00:00 2001 From: RuyimgByCN <45452808+RuyimgByCN@users.noreply.github.com> Date: Tue, 1 Dec 2026 01:24:16 +0800 Subject: [PATCH] =?UTF-8?q?feat(project-tree):=20add=20"Add=20as=20Library?= =?UTF-8?q?=E2=80=A6"=20for=20JARs=20and=20library=20directories=20(#7077)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 <45452808+RuyimgByCN@users.noreply.github.com> --- crates/ide-sdk/src/libraries.rs | 3 +- src/features/libraries/index.ts | 24 +- .../project-tree/AddLibraryDialog.module.css | 64 ++++ .../project-tree/AddLibraryDialog.tsx | 164 +++++++++ src/features/project-tree/ProjectView.tsx | 20 +- src/features/project-tree/actions.ts | 14 + src/features/project-tree/addLibrary.test.tsx | 309 ++++++++++++++++ src/features/project-tree/addLibrary.ts | 335 ++++++++++++++++++ src/features/project-tree/contextMenu.test.ts | 40 +++ src/features/project-tree/contextMenu.ts | 10 + src/features/project-tree/index.ts | 9 +- src/features/project-tree/store.ts | 10 +- src/platform/languageSettings.ts | 18 + src/platform/locales/text/en.json | 8 +- src/platform/locales/text/es.json | 8 +- src/platform/locales/text/fr.json | 8 +- src/platform/locales/text/ko.json | 8 +- src/platform/locales/text/pl.json | 8 +- src/platform/locales/text/pt.json | 8 +- src/platform/locales/text/ru.json | 8 +- src/platform/locales/text/tr.json | 8 +- src/platform/locales/text/zh-TW.json | 8 +- src/platform/locales/text/zh.json | 8 +- 23 files changed, 1079 insertions(+), 21 deletions(-) create mode 100644 src/features/project-tree/AddLibraryDialog.module.css create mode 100644 src/features/project-tree/AddLibraryDialog.tsx create mode 100644 src/features/project-tree/addLibrary.test.tsx create mode 100644 src/features/project-tree/addLibrary.ts diff --git a/crates/ide-sdk/src/libraries.rs b/crates/ide-sdk/src/libraries.rs index 2fc23e1276..e74a0adc53 100644 --- a/crates/ide-sdk/src/libraries.rs +++ b/crates/ide-sdk/src/libraries.rs @@ -506,7 +506,8 @@ fn jdk_groups(root: &Path, _project: &Path, s: &Settings, cancel: &CancellationT let java = [".classpath", ".project"].iter().any(|f| root.join(f).is_file()) || idea_java_project(root) || !s.languages.java.jdk.trim().is_empty() - || !s.project_structure.source_roots.is_empty(); + || !s.project_structure.source_roots.is_empty() + || !s.project_structure.libraries.is_empty(); if !java { return Vec::new(); } diff --git a/src/features/libraries/index.ts b/src/features/libraries/index.ts index 5bbd8e523d..f18e8ae8c0 100644 --- a/src/features/libraries/index.ts +++ b/src/features/libraries/index.ts @@ -20,7 +20,8 @@ import { useEditorRegistry, } from "@/editor/api"; import { call, isCancelled, isTauri, nextRequestId, onIdeEvent } from "@/platform/ipc"; -import { useProject } from "@/platform/project"; +import { libraryClassEntry, normalizeFolder, validateProjectStructure } from "@/platform/languageSettings"; +import { relativePath, useProject } from "@/platform/project"; import { libraryArchiveRoot } from "@/platform/virtualDocs"; import { registerProjectViewRoot, type ProjectViewRootContribution } from "@/workbench/contributions"; import { goModNavigationExtension } from "./goModNavigation"; @@ -110,6 +111,24 @@ const MANIFESTS = new Set([ "mise.toml", ]); +/** Whether an `fsChanged` event adds or removes a `.jar` inside a configured Project Structure library. */ +export function touchesConfiguredLibrary(paths: readonly string[]): boolean { + const { project, settings } = useProject.getState(); + if (!project || !paths.length) return false; + const libraries = validateProjectStructure(settings?.projectStructure).libraries; + if (!libraries.length) return false; + const jarPaths = paths.filter((p) => /\.jar$/i.test(p)); + if (!jarPaths.length) return false; + const bases = libraries.flatMap((lib) => + lib.classes.map((raw) => libraryClassEntry(raw, project.root)?.path ?? "").filter(Boolean), + ); + if (!bases.length) return false; + return jarPaths.some((p) => { + const rel = normalizeFolder(relativePath(project.root, p)); + return bases.some((b) => rel === b || rel.startsWith(`${b}/`)); + }); +} + /** Whether an `fsChanged` event touches a library manifest. */ export function touchesManifest(paths: readonly string[]): boolean { return paths.some( @@ -306,7 +325,8 @@ export function activate(): () => void { showOpenedLibrary(useEditorRegistry.getState().active?.ctx.path ?? null); let timer: ReturnType | undefined; const unsubFs = onIdeEvent("fsChanged", (e) => { - if (!touchesManifest([...e.created, ...e.modified, ...e.removed])) return; + const changed = [...e.created, ...e.modified, ...e.removed]; + if (!touchesManifest(changed) && !touchesConfiguredLibrary([...e.created, ...e.removed])) return; clearTimeout(timer); timer = setTimeout(refresh, MANIFEST_REFRESH_DELAY); }); diff --git a/src/features/project-tree/AddLibraryDialog.module.css b/src/features/project-tree/AddLibraryDialog.module.css new file mode 100644 index 0000000000..0254453b28 --- /dev/null +++ b/src/features/project-tree/AddLibraryDialog.module.css @@ -0,0 +1,64 @@ +.form { + display: flex; + flex-direction: column; + gap: var(--space-3); + padding: var(--space-3) var(--space-4); +} + +.fields { + display: grid; + grid-template-columns: fit-content(36%) minmax(0, 1fr); + align-items: start; + gap: var(--space-2) var(--space-3); +} + +.label { + display: flex; + align-items: center; + min-height: var(--h-field); + color: var(--fg); + font-size: var(--fs-ui); +} + +.control { + display: flex; + flex-direction: column; + gap: var(--space-1); + min-width: 0; +} + +.pathList { + display: flex; + flex-direction: column; + gap: var(--space-1); + max-height: 120px; + overflow: auto; + padding: var(--space-1-5) var(--space-2); + border: 1px solid var(--border); + border-radius: var(--radius-small); + background: var(--bg-field); + font-family: var(--font-mono); + font-size: var(--fs-small); + color: var(--fg-muted); +} + +.pathItem { + display: flex; + align-items: center; + gap: var(--space-1-5); + min-width: 0; +} + +.pathItem > svg { + flex: none; +} + +.pathText { + min-width: 0; + overflow-wrap: anywhere; +} + +.error { + color: var(--danger); + font-size: var(--fs-small); +} diff --git a/src/features/project-tree/AddLibraryDialog.tsx b/src/features/project-tree/AddLibraryDialog.tsx new file mode 100644 index 0000000000..85723ff87b --- /dev/null +++ b/src/features/project-tree/AddLibraryDialog.tsx @@ -0,0 +1,164 @@ +/** + * "Create Library" dialog opened from Project view › Add as Library…: + * mirrors IDEA's dialog for turning a `.jar` or JAR directory (such as `WEB-INF/lib`) + * into a project library in `projectStructure.libraries`. + */ +import { useEffect, useId, useMemo, useRef, useState } from "react"; +import { Dialog, Icon, Input, Select, T, type SelectOption } from "@/ui"; +import { + DEPENDENCY_SCOPES, + normalizeFolder, + validateProjectStructure, + type DependencyScopeValid, +} from "@/platform/languageSettings"; +import { relativePath, useProject } from "@/platform/project"; +import { tx } from "@/platform/i18n"; +import { + addProjectLibrary, + closeAddAsLibraryDialog, + isJarPath, + libraryClassPaths, + suggestLibraryName, + useAddLibraryRequest, + type AddLibraryRequest, +} from "./addLibrary"; +import styles from "./AddLibraryDialog.module.css"; + +const SCOPE_OPTIONS: SelectOption[] = DEPENDENCY_SCOPES.map((s) => ({ + value: s, + label: s === "compile" ? "Compile" : s === "test" ? "Test" : s === "runtime" ? "Runtime" : "Provided", +})); + +function AddLibraryDialog({ request }: { request: AddLibraryRequest }) { + const root = useProject((s) => s.project?.root ?? null); + const settings = useProject((s) => s.settings); + const existing = useMemo(() => validateProjectStructure(settings?.projectStructure).libraries, [settings]); + const classPaths = useMemo(() => { + if (!root) return []; + const filtered = libraryClassPaths(request.paths, root, existing); + return filtered.length > 0 + ? filtered + : [...new Set(request.paths.map((p) => normalizeFolder(relativePath(root, p))).filter(Boolean))]; + }, [request.paths, root, existing]); + + const [name, setName] = useState(() => suggestLibraryName(request.paths, root ?? "", existing)); + const [scope, setScope] = useState("compile"); + const [saving, setSaving] = useState(false); + const [submitError, setSubmitError] = useState(null); + const inputRef = useRef(null); + const nameId = useId(); + + useEffect(() => { + const el = inputRef.current; + if (!el) return; + el.focus(); + el.select(); + }, []); + + const trimmed = name.trim(); + const duplicate = trimmed.length > 0 && existing.some((l) => l.name.trim() === trimmed); + const validationError = !trimmed + ? tx("Library name cannot be empty.") + : duplicate + ? tx("A library with this name already exists.") + : null; + + const submit = async () => { + if (saving || validationError) return; + setSaving(true); + setSubmitError(null); + try { + const saved = await addProjectLibrary({ name: trimmed, paths: request.paths, scope }); + if (saved) closeAddAsLibraryDialog(); + } catch (e) { + setSubmitError(e instanceof Error ? e.message : String(e)); + } finally { + setSaving(false); + } + }; + + return ( + !saving && closeAddAsLibraryDialog()} + width={460} + buttons={[ + { label: "OK", role: "default", disabled: saving || !!validationError, onClick: () => void submit() }, + { label: "Cancel", role: "cancel", disabled: saving, onClick: closeAddAsLibraryDialog }, + ]} + > +
{ + e.preventDefault(); + void submit(); + }} + > +
+ +
+ { + setName(e.target.value); + setSubmitError(null); + }} + /> + {validationError && ( +
+ {validationError} +
+ )} +
+ + + Scope + +
+