diff --git a/crates/ide-core/src/editor_settings.rs b/crates/ide-core/src/editor_settings.rs index d9efe84349..e99fd1130d 100644 --- a/crates/ide-core/src/editor_settings.rs +++ b/crates/ide-core/src/editor_settings.rs @@ -486,6 +486,16 @@ pub struct LanguageCodeStyle { /// SQL › Case › Identifiers (unquoted names): `upper` | `lower` | `keep`. #[serde(skip_serializing_if = "Option::is_none")] pub identifier_case: Option, + /// Java: organize imports switches to a wildcard import once this many + /// classes of one package are used (jdtls `starThreshold`; IDEA's default + /// 5; unset keeps the language server's default). + #[serde(skip_serializing_if = "Option::is_none")] + pub class_count_to_use_import_with_star: Option, + /// Java: organize imports switches to a wildcard static import once this + /// many statically imported names are used (jdtls `staticStarThreshold`; + /// IDEA's default 3; unset keeps the language server's default). + #[serde(skip_serializing_if = "Option::is_none")] + pub names_count_to_use_static_import_with_star: Option, /// JavaScript / TypeScript › Punctuation › Semicolons: `ignore` | `insert` | `remove`. #[serde(skip_serializing_if = "Option::is_none")] pub semicolons: Option, diff --git a/crates/ide-lsp/src/jdtls/formatter.rs b/crates/ide-lsp/src/jdtls/formatter.rs index 18c2b3cd00..c5ac926910 100644 --- a/crates/ide-lsp/src/jdtls/formatter.rs +++ b/crates/ide-lsp/src/jdtls/formatter.rs @@ -34,6 +34,20 @@ fn indent_settings(style: &LanguageCodeStyle) -> Option, inherited: &serde_json::Value) -> serde_json::Value { + let explicit = |key: &str| inherited.pointer(&format!("/java/sources/organizeImports/{key}")).cloned(); + let class_count = style.and_then(|s| s.class_count_to_use_import_with_star).unwrap_or(5); + let static_count = style.and_then(|s| s.names_count_to_use_static_import_with_star).unwrap_or(3); + json!({ + "starThreshold": explicit("starThreshold").unwrap_or(json!(class_count)), + "staticStarThreshold": explicit("staticStarThreshold").unwrap_or(json!(static_count)), + }) +} + fn patch_profile_xml(mut xml: String, settings: &[(&'static str, String)]) -> String { for (id, value) in settings { let marker = format!(r#" ({ languages: vi.fn() })); +vi.mock("@/platform/ipc", async (original) => { + const actual = await original(); + return { + ...actual, + isTauri: () => true, + commands: { + ...actual.commands, + languages: mocks.languages, + }, + }; +}); + +beforeEach(() => { + mocks.languages.mockResolvedValue([ + { id: "java", name: "Java", indent: { tabSize: 4, insertSpaces: true } }, + { id: "rust", name: "Rust" }, + ]); + useProject.setState({ settings: structuredClone(DEFAULT_SETTINGS), project: null }); + openSettings("editor.codeStyle.java"); +}); +afterEach(cleanup); + +function CodeStylePage({ id, Page }: { id: string; Page: ComponentType }) { + const value = useMemo(() => ({ pageId: id, scope: "global" as const }), [id]); + return ( + + + + ); +} + +async function page(language: string) { + const pages = await codeStyleLanguagePages(); + const spec = pages.find((p) => p.id === `editor.codeStyle.${language}`)!; + render(); +} +const value = () => stagedValue(useSettingsDialog.getState(), "global", "editor.codeStyle.languages"); + +describe("Java Imports (IntelliSort)", () => { + it("stages the class-count and static-count options on the Java code style", async () => { + await page("java"); + fireEvent.click(screen.getByRole("tab", { name: "Imports" })); + fireEvent.change(screen.getByLabelText("Class count to use import with *"), { target: { value: "7" } }); + fireEvent.change(screen.getByLabelText("Names count to use static import with *"), { target: { value: "2" } }); + expect(value()).toEqual({ + java: { classCountToUseImportWithStar: 7, namesCountToUseStaticImportWithStar: 2 }, + }); + fireEvent.click(screen.getByRole("button", { name: "Reset to Language Defaults" })); + expect(value()).toEqual({}); + }); + + it("keeps the Imports tab specific to Java", async () => { + await page("rust"); + expect(screen.queryByRole("tab", { name: "Imports" })).toBeNull(); + }); +}); diff --git a/src/features/settings/pages/editor/codeStyle.tsx b/src/features/settings/pages/editor/codeStyle.tsx index d15e26dba7..83fff0093e 100644 --- a/src/features/settings/pages/editor/codeStyle.tsx +++ b/src/features/settings/pages/editor/codeStyle.tsx @@ -157,6 +157,11 @@ const COMMENTS = { keywords: ["Line comment at first column", "Add a space at line comment start"], }; const CASE = { id: "case", label: "Case", keywords: ["Keywords", "Identifiers", "To upper", "To lower"] }; +const IMPORTS = { + id: "imports", + label: "Imports", + keywords: ["imports", "static imports", "wildcard", "class count", "import with *"], +}; const SPACES = { id: "spaces", label: "Spaces", keywords: ["Space", "Around binary operators", "Within braces"] }; const PUNCTUATION = { id: "punctuation", label: "Punctuation", keywords: ["Semicolons", "Quotes"] }; @@ -298,6 +303,41 @@ function LanguageStyle({ lang }: { lang: LanguageInfo }) { ), }); } + if (lang.id === "java") { + tabs.push({ + ...IMPORTS, + content: ( + + + set({ classCountToUseImportWithStar: n })} + min={1} + max={999} + integer + aria-label="Class count to use import with *" + /> + + + set({ namesCountToUseStaticImportWithStar: n })} + min={1} + max={999} + integer + aria-label="Names count to use static import with *" + /> + + + ), + }); + } if (TS_STYLE_LANGUAGES.has(lang.id)) { tabs.push( { @@ -501,7 +541,14 @@ export async function codeStyleLanguagePages(): Promise { ...keywords, ]), ...(lang.id === "java" - ? ["Formatting profile", "IntelliJ IDEA", "Custom Eclipse formatter XML", "Eclipse formatter XML"] + ? [ + "Formatting profile", + "IntelliJ IDEA", + "Custom Eclipse formatter XML", + "Eclipse formatter XML", + IMPORTS.label, + ...IMPORTS.keywords, + ] : []), ...(lang.id === "php" ? ["Alignment", "Align key-value pairs", "Align consecutive assignments"] : []), ...(lang.id === "sql" ? [CASE.label, ...CASE.keywords, "Do not change"] : []), diff --git a/src/platform/editorSettings.ts b/src/platform/editorSettings.ts index 450a798200..6daf742996 100644 --- a/src/platform/editorSettings.ts +++ b/src/platform/editorSettings.ts @@ -47,6 +47,10 @@ export interface LanguageCodeStyle { /** SQL › Case: Reformat Code's case for keywords and unquoted identifiers. */ keywordCase?: SqlWordCase; identifierCase?: SqlWordCase; + /** Java: organize imports switches to a wildcard import once this many classes of one package are used (jdtls passthrough). */ + classCountToUseImportWithStar?: number; + /** Java: organize imports switches to a wildcard static import once this many names are used (jdtls passthrough). */ + namesCountToUseStaticImportWithStar?: number; /** JavaScript / TypeScript › Punctuation (unset: tsserver's default, `ignore` / `auto`). */ semicolons?: "ignore" | "insert" | "remove"; quotes?: "auto" | "single" | "double"; diff --git a/src/platform/locales/text/en.json b/src/platform/locales/text/en.json index c88f5635c5..4da81606d4 100644 --- a/src/platform/locales/text/en.json +++ b/src/platform/locales/text/en.json @@ -4118,6 +4118,13 @@ "Run '{command}' in '{rel}' to install them.": "Run '{command}' in '{rel}' to install them.", "Run '{command}' to install them.": "Run '{command}' to install them.", "Install Node.js Dependencies": "Install Node.js Dependencies", + "Imports": "Imports", + "Class count to use import with '*'": "Class count to use import with '*'", + "Use a wildcard import once this many classes of one package are used (5 by default; 999 keeps single-class imports).": "Use a wildcard import once this many classes of one package are used (5 by default; 999 keeps single-class imports).", + "Class count to use import with *": "Class count to use import with *", + "Names count to use static import with '*'": "Names count to use static import with '*'", + "Use a wildcard static import once this many names are imported statically (3 by default; 999 keeps single imports).": "Use a wildcard static import once this many names are imported statically (3 by default; 999 keeps single imports).", + "Names count to use static import with *": "Names count to use static import with *", "Watch for changes": "Watch for changes", "Rerun Automatically": "Rerun Automatically", "Debug runs the tests once, without watching.": "Debug runs the tests once, without watching.", diff --git a/src/platform/locales/text/zh.json b/src/platform/locales/text/zh.json index d2517d5a81..9c33706b1f 100644 --- a/src/platform/locales/text/zh.json +++ b/src/platform/locales/text/zh.json @@ -10564,6 +10564,12 @@ "Run with {title}": "使用 {title} 运行", "Debug with {title}": "使用 {title} 调试", "Launches the selected configuration with the Java agent of {pluginName}": "使用 {pluginName} 的 Java 代理启动所选配置", + "Class count to use import with '*'": "使用通配符导入的类数", + "Use a wildcard import once this many classes of one package are used (5 by default; 999 keeps single-class imports).": "当同一个包使用达到该数量的类时使用通配符导入;默认为 5,999 表示始终保留单类导入。", + "Class count to use import with *": "使用通配符导入的类数", + "Names count to use static import with '*'": "使用通配符静态导入的名称数", + "Use a wildcard static import once this many names are imported statically (3 by default; 999 keeps single imports).": "静态导入的名称达到该数量时使用通配符静态导入;默认为 3,999 表示始终保留单条导入。", + "Names count to use static import with *": "使用通配符静态导入的名称数", "Watch for changes": "监视更改", "Rerun Automatically": "自动重新运行", "Debug runs the tests once, without watching.": "调试时测试只运行一次,不监视更改。",