The IDE you know, rebuilt in Rust. IntelliJ IDEA–style keymap, tool windows and search-everywhere in a fast native app: editor, code insight, debugger, Git, terminal, database and HTTP clients for 60+ languages.
TypeScript code-editorcross-platformdapdatabase-clientdebuggerdesktop-appdeveloper-toolsgithttp-clientideintellijlsprusttauriterminal
feat(java): selectable Java formatting profiles (#680)
l0ng-aibf2427bd3ac4
diff --git a/Cargo.lock b/Cargo.lockindex 6a87905c3..02bb0950e 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -4374,6 +4374,7 @@ dependencies = ["ide-jsonrpc","ide-language","ide-text",+ "ide-vfs","ignore","lsp-types","parking_lot",diff --git a/crates/ide-core/src/editor_settings.rs b/crates/ide-core/src/editor_settings.rsindex a478558c6..4806bf2cd 100644--- a/crates/ide-core/src/editor_settings.rs+++ b/crates/ide-core/src/editor_settings.rs@@ -371,6 +371,12 @@ pub struct TextAttributesOverride {#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, Type)]#[serde(default, rename_all = "camelCase")]pub struct LanguageCodeStyle {+ /// Java formatter: `default` | `intellij` | `custom` (other languages ignore it).+ #[serde(skip_serializing_if = "Option::is_none")]+ pub formatter: Option<String>,+ /// Eclipse formatter XML; relative paths start at the project root.+ #[serde(skip_serializing_if = "Option::is_none")]+ pub formatter_path: Option<String>,#[serde(skip_serializing_if = "Option::is_none")]pub use_tab: Option<bool>,#[serde(skip_serializing_if = "Option::is_none")]diff --git a/crates/ide-host/src/ops/project.rs b/crates/ide-host/src/ops/project.rsindex 4c17cdde1..e6632c1d1 100644--- a/crates/ide-host/src/ops/project.rs+++ b/crates/ide-host/src/ops/project.rs@@ -172,12 +172,14 @@ pub fn set_settings(host.apply_proxy();}// A new interpreter / toolchain / server executable restarts the language servers it affects.- if ["languages", "tools", "languageServers", "projectStructure"]+ let code_style = key == "editor" || key == "editor.codeStyle" || key.starts_with("editor.codeStyle.");+ let toolchain = ["languages", "tools", "languageServers", "projectStructure"].iter()- .any(|k| key == *k || key.starts_with(&format!("{k}.")))- {+ .any(|k| key == *k || key.starts_with(&format!("{k}.")));+ // Code style only reaches servers as settings (e.g. jdtls' formatter profile); builds don't depend on it.+ if code_style || toolchain {for project in affected() {- if let Some(bridge) = &project.kotlin_bridge {+ if toolchain && let Some(bridge) = &project.kotlin_bridge {bridge.schedule();}let lsp = project.lsp.clone();diff --git a/crates/ide-host/src/project.rs b/crates/ide-host/src/project.rsindex f28c7cffe..baf7e10ea 100644--- a/crates/ide-host/src/project.rs+++ b/crates/ide-host/src/project.rs@@ -129,6 +129,8 @@ impl Host {let root = root.clone();let data_dir = self.data_dir.clone();let config_dir = self.config_dir.clone();+ let sink = sink.clone();+ let formatter_problem = std::sync::Mutex::new(None);lsp.set_config_hook(Box::new(move |cfg, server_root| {let member = ide_workspace::workspace::Workspace::load(&root).ok().flatten().and_then(|w| {w.projects.into_iter().filter(|p| server_root.starts_with(p)).max_by_key(|p| p.components().count())@@ -142,6 +144,27 @@ impl Host {ide_sdk::apply::configure_server(cfg, root, server_root, &effective);ide_sdk::java::cap_java_home(cfg, &ide_sdk::java::installed_jdks());if cfg.name == "jdtls" {+ let problem = ide_lsp::jdtls::configure_formatter(+ cfg,+ effective.editor.code_style.languages.get("java"),+ root,+ &data_dir,+ effective.language_servers.get("jdtls"),+ )+ .err()+ .map(|e| e.to_string());+ let mut previous = formatter_problem.lock().unwrap();+ if problem != *previous {+ if let Some(body) = &problem {+ sink.emit(IdeEvent::Notify {+ level: ide_core::event::NotifyLevel::Warning,+ title: "Java formatter configuration unavailable".into(),+ body: Some(body.clone()),+ });+ }+ *previous = problem;+ }+ drop(previous);ide_lsp::jdtls::configure_source_paths(cfg,ide_workspace::roots::unmanaged_java_source_paths(diff --git a/crates/ide-lsp/Cargo.toml b/crates/ide-lsp/Cargo.tomlindex 8b011c795..81cd83810 100644--- a/crates/ide-lsp/Cargo.toml+++ b/crates/ide-lsp/Cargo.toml@@ -7,6 +7,7 @@ license.workspace = true[dependencies]ide-core.workspace = true+ide-vfs.workspace = truedunce.workspace = trueide-text.workspace = trueide-language.workspace = truediff --git a/crates/ide-lsp/src/jdtls.rs b/crates/ide-lsp/src/jdtls.rsindex f2b79e0c1..82a52ef41 100644--- a/crates/ide-lsp/src/jdtls.rs+++ b/crates/ide-lsp/src/jdtls.rs@@ -17,6 +17,9 @@ use std::sync::Arc;use std::time::{Duration, Instant};const SERVER: &str = "jdtls";++mod formatter;+pub use formatter::configure_formatter;/// First start imports the Maven / Gradle model, which can take minutes.const READY_TIMEOUT: Duration = Duration::from_secs(240);/// A workspace build of a large multi-module project can take far longer thandiff --git a/crates/ide-lsp/src/jdtls/formatter.rs b/crates/ide-lsp/src/jdtls/formatter.rsnew file mode 100644index 000000000..f7c19dea6--- /dev/null+++ b/crates/ide-lsp/src/jdtls/formatter.rs@@ -0,0 +1,224 @@+use ide_core::IdeResult;+use ide_core::editor_settings::LanguageCodeStyle;+use ide_core::settings::LanguageServerSettings;+use ide_language::LanguageServerConfig;+use serde_json::json;+use std::path::Path;++const PROFILE: &str = "OpenIDEA IntelliJ IDEA";+const XML: &str = include_str!("intellij.xml");++/// Protocol settings only: switching profiles reaches a running jdtls through+/// didChangeConfiguration. Explicit language-server settings are applied later.+pub fn configure_formatter(+ cfg: &mut LanguageServerConfig,+ style: Option<&LanguageCodeStyle>,+ project_root: &Path,+ data_dir: &Path,+ overrides: Option<&LanguageServerSettings>,+) -> IdeResult<()> {+ if cfg.name != "jdtls" {+ return Ok(());+ }+ // jdtls retains omitted preferences. Send empty values when returning to+ // Default, while preserving any profile from the base server configuration.+ let inherited = crate::handler::all_settings(cfg);+ let defaults = json!({+ "url": inherited.pointer("/java/format/settings/url").cloned().unwrap_or(json!("")),+ "profile": inherited.pointer("/java/format/settings/profile").cloned().unwrap_or(json!("")),+ });+ let mut settings = cfg.settings.take().unwrap_or_else(|| json!({}));+ settings["java"]["format"]["settings"] = formatter_overrides(defaults, overrides);+ cfg.settings = Some(settings);+ let Some(style) = style else { return Ok(()) };+ let (path, profile) = match style.formatter.as_deref() {+ Some("intellij") => {+ let path = data_dir.join("lsp/jdtls/intellij.xml");+ std::fs::create_dir_all(path.parent().unwrap())?;+ if std::fs::read_to_string(&path).ok().as_deref() != Some(XML) {+ ide_vfs::write_atomic(&path, XML.as_bytes())?;+ }+ (path, PROFILE)+ }+ Some("custom") => {+ let value = style.formatter_path.as_deref().unwrap_or_default().trim();+ if value.is_empty() {+ return Err(ide_core::IdeError::invalid("Choose an Eclipse formatter XML file."));+ }+ let path = project_root.join(value);+ // Fail without poisoning the server launch; code intelligence remains available.+ std::fs::File::open(&path).map_err(|error| {+ let path = path.display();+ ide_core::IdeError::invalid(format!("Cannot read Java formatter XML {path}: {error}"))+ })?;+ if !path.is_file() {+ return Err(ide_core::IdeError::invalid(format!("Not a formatter XML file: {}", path.display())));+ }+ (path, "")+ }+ _ => return Ok(()),+ };+ let mut settings = cfg.settings.take().unwrap_or_else(|| json!({}));+ settings["java"]["format"]["settings"] = formatter_overrides(+ json!({+ "url": crate::uri::path_to_uri_string(&path),+ "profile": profile,+ }),+ overrides,+ );+ cfg.settings = Some(settings);+ Ok(())+}++fn formatter_overrides(mut format: serde_json::Value, overrides: Option<&LanguageServerSettings>) -> serde_json::Value {+ if let Some(overrides) = overrides {+ // all_settings merges protocol settings over initializationOptions.+ // Mirror explicit formatter keys so generated protocol values cannot+ // accidentally mask an advanced initializationOptions override.+ for layer in [+ overrides.initialization_options.as_ref().and_then(|v| v.pointer("/settings/java/format/settings")),+ overrides.settings.as_ref().and_then(|v| v.pointer("/settings/java/format/settings")),+ overrides.settings.as_ref().and_then(|v| v.pointer("/java/format/settings")),+ ]+ .into_iter()+ .flatten()+ {+ for key in ["url", "profile"] {+ if let Some(value) = layer.get(key) {+ format[key] = value.clone();+ }+ }+ }+ }+ format+}++#[cfg(test)]+mod tests {+ use super::*;++ fn config(name: &str) -> LanguageServerConfig {+ serde_json::from_value(json!({"name": name, "command": name})).unwrap()+ }++ #[test]+ fn profiles_resolve_paths_preserve_settings_and_leave_other_servers_alone() {+ let dir = tempfile::tempdir().unwrap();+ let root = dir.path().join("project with 空格");+ let data = dir.path().join("data");+ std::fs::create_dir_all(&root).unwrap();+ let custom = root.join("style.xml");+ std::fs::write(&custom, XML).unwrap();+ for name in ["jdtls", "rust-analyzer"] {+ for mode in ["default", "intellij", "custom", "unknown"] {+ let mut cfg = LanguageServerConfig {+ name: name.into(),+ settings: Some(json!({"java": {"autobuild": {"enabled": false}}})),+ ..config(name)+ };+ let initial = cfg.clone();+ let style = LanguageCodeStyle {+ formatter: Some(mode.into()),+ formatter_path: Some("style.xml".into()),+ ..Default::default()+ };+ configure_formatter(&mut cfg, Some(&style), &root, &data, None).unwrap();+ if name != "jdtls" {+ assert_eq!(cfg, initial);+ continue;+ }+ let settings = crate::handler::all_settings(&cfg);+ if !["intellij", "custom"].contains(&mode) {+ assert_eq!(settings["java"]["format"]["settings"], json!({"url": "", "profile": ""}));+ continue;+ }+ let path = if mode == "custom" { custom.clone() } else { data.join("lsp/jdtls/intellij.xml") };+ assert_eq!(settings["java"]["format"]["settings"]["url"], crate::uri::path_to_uri_string(&path));+ assert_eq!(+ settings["java"]["format"]["settings"]["profile"],+ if mode == "custom" { "" } else { PROFILE }+ );+ assert_eq!(settings["java"]["autobuild"]["enabled"], false);+ assert_eq!(std::fs::read_to_string(path).unwrap(), XML);+ }+ }+ }++ #[test]+ fn unavailable_custom_paths_are_nonfatal_and_do_not_set_launch_error() {+ let dir = tempfile::tempdir().unwrap();+ for path in ["", "missing.xml", "."] {+ let mut cfg = config("jdtls");+ let style = LanguageCodeStyle {+ formatter: Some("custom".into()),+ formatter_path: Some(path.into()),+ ..Default::default()+ };+ assert!(configure_formatter(&mut cfg, Some(&style), dir.path(), dir.path(), None).is_err());+ assert!(cfg.launch_error.is_none());+ assert_eq!(crate::handler::all_settings(&cfg)["java"]["format"]["settings"]["url"], "");+ }+ }++ #[test]+ fn default_keeps_explicit_server_configuration_and_custom_accepts_absolute_paths() {+ let dir = tempfile::tempdir().unwrap();+ let path = dir.path().join("custom.xml");+ std::fs::write(&path, XML).unwrap();+ let mut cfg = config("jdtls");+ cfg.initialization_options = Some(+ toml::Value::try_from(json!({+ "settings": {"java": {"format": {"settings": {"url": "existing.xml", "profile": "Existing"}}}}+ }))+ .unwrap(),+ );+ configure_formatter(&mut cfg, None, dir.path(), dir.path(), None).unwrap();+ assert_eq!(+ crate::handler::all_settings(&cfg)["java"]["format"]["settings"],+ json!({"url": "existing.xml", "profile": "Existing"})+ );+ let style = LanguageCodeStyle {+ formatter: Some("custom".into()),+ formatter_path: Some(path.to_string_lossy().into_owned()),+ ..Default::default()+ };+ configure_formatter(&mut cfg, Some(&style), &dir.path().join("different-root"), dir.path(), None).unwrap();+ assert_eq!(+ crate::handler::all_settings(&cfg)["java"]["format"]["settings"]["url"],+ crate::uri::path_to_uri_string(&path)+ );+ }+ #[test]+ fn advanced_formatter_options_win_for_each_profile_and_protocol_layer() {+ let dir = tempfile::tempdir().unwrap();+ std::fs::write(dir.path().join("custom.xml"), XML).unwrap();+ for mode in ["default", "intellij", "custom"] {+ for layer in ["initialization", "wrapped", "direct"] {+ let mut overrides = LanguageServerSettings {+ initialization_options: Some(json!({"settings": {"java": {"format": {"settings": {+ "url": "advanced.xml", "profile": "Advanced"+ }}}}})),+ ..Default::default()+ };+ if layer != "initialization" {+ let value = json!({"java": {"format": {"settings": {"profile": "Protocol"}}}});+ overrides.settings = Some(if layer == "wrapped" { json!({"settings": value}) } else { value });+ }+ let mut cfg = config("jdtls");+ let style = LanguageCodeStyle {+ formatter: Some(mode.into()),+ formatter_path: Some("custom.xml".into()),+ ..Default::default()+ };+ configure_formatter(&mut cfg, Some(&style), dir.path(), dir.path(), Some(&overrides)).unwrap();+ assert_eq!(+ crate::handler::all_settings(&cfg)["java"]["format"]["settings"],+ json!({+ "url": "advanced.xml",+ "profile": if layer == "initialization" { "Advanced" } else { "Protocol" },+ })+ );+ }+ }+ }+}diff --git a/crates/ide-lsp/src/jdtls/intellij.xml b/crates/ide-lsp/src/jdtls/intellij.xmlnew file mode 100644index 000000000..18b3cf8de--- /dev/null+++ b/crates/ide-lsp/src/jdtls/intellij.xml@@ -0,0 +1,235 @@+<?xml version="1.0" encoding="UTF-8"?>+<!--+ IntelliJ IDEA Java defaults, approximated using Eclipse JDT formatter options.+ Source revision: JetBrains/intellij-community 8fe3709d185cd13053d17b76d9690aef674d4268+ platform/code-style-api/src/com/intellij/psi/codeStyle/CommonCodeStyleSettings.java+ platform/code-style-api/src/com/intellij/psi/codeStyle/CodeStyleSettings.java+ platform/code-style-api/src/com/intellij/psi/codeStyle/CodeStyleDefaults.java+ java/java-frontback-impl/src/com/intellij/psi/codeStyle/JavaCodeStyleSettings.java+ JavaLanguageCodeStyleSettingsProvider.customizeDefaults does not override these values.+ https://www.jetbrains.com/help/idea/code-style-java.html describes the corresponding controls.+ Each option below names its IDEA setting; JDT has no exact equivalent for every rule.+ A formatter profile does not sort/group imports (Organize Imports is a separate action).+ Preserve IDEA KEEP_LINE_BREAKS=true using Wrap where necessary (WRAP_COMPACT) and+ join_wrapped_lines=false (Never join already wrapped lines). JDT NO_SPLIT joins some+ manual parameter/call/chain breaks even when joining is disabled. Preserving manual+ breaks takes priority: wrappable code exceeding 120 columns will also wrap, a known+ difference from IDEA defaults (DO_NOT_WRAP / WRAP_LONG_LINES=false).+ Alignment values: 16 = WRAP_COMPACT + INDENT_DEFAULT; 18 = WRAP_COMPACT + INDENT_ON_COLUMN.+ The comment width 999999 approximates WRAP_COMMENTS=false without JDT integer overflow.+ JDT has a shared blank-line preservation limit and different empty-body/Javadoc handling.+-->+<profiles version="23">+ <profile kind="CodeFormatterProfile" name="OpenIDEA IntelliJ IDEA" version="23">+ <!-- IndentOptions.USE_TAB_CHARACTER = false -->+ <setting id="org.eclipse.jdt.core.formatter.tabulation.char" value="space"/>+ <!-- IndentOptions.TAB_SIZE = 4 -->+ <setting id="org.eclipse.jdt.core.formatter.tabulation.size" value="4"/>+ <!-- IndentOptions.INDENT_SIZE = 4 -->+ <setting id="org.eclipse.jdt.core.formatter.indentation.size" value="4"/>+ <!-- IndentOptions.CONTINUATION_INDENT_SIZE = 8 (two 4-space units) -->+ <setting id="org.eclipse.jdt.core.formatter.continuation_indentation" value="2"/>+ <!-- IndentOptions.CONTINUATION_INDENT_SIZE = 8 -->+ <setting id="org.eclipse.jdt.core.formatter.continuation_indentation_for_array_initializer" value="2"/>+ <!-- CodeStyleSettings.RIGHT_MARGIN = 120; wrap where necessary to preserve manual line breaks (see known difference above) -->+ <setting id="org.eclipse.jdt.core.formatter.lineSplit" value="120"/>+ <!-- KEEP_LINE_BREAKS = true; Never join already wrapped lines -->+ <setting id="org.eclipse.jdt.core.formatter.join_wrapped_lines" value="false"/>+ <!-- KEEP_BLANK_LINES_IN_DECLARATIONS / IN_CODE / BEFORE_RBRACE = 2 (JDT has one shared limit) -->+ <setting id="org.eclipse.jdt.core.formatter.number_of_empty_lines_to_preserve" value="2"/>+ <!-- METHOD_PARAMETERS_WRAP = DO_NOT_WRAP; ALIGN_MULTILINE_PARAMETERS = true (indent on column); JDT wrap where necessary preserves manual breaks -->+ <setting id="org.eclipse.jdt.core.formatter.alignment_for_parameters_in_method_declaration" value="18"/>+ <!-- METHOD_PARAMETERS_WRAP = DO_NOT_WRAP; ALIGN_MULTILINE_PARAMETERS = true (indent on column); JDT wrap where necessary preserves manual breaks -->+ <setting id="org.eclipse.jdt.core.formatter.alignment_for_parameters_in_constructor_declaration" value="18"/>+ <!-- CALL_PARAMETERS_WRAP / ANNOTATION_PARAMETER_WRAP = DO_NOT_WRAP; ALIGN_MULTILINE_PARAMETERS_IN_CALLS = false; JDT wrap where necessary preserves manual breaks -->+ <setting id="org.eclipse.jdt.core.formatter.alignment_for_arguments_in_method_invocation" value="16"/>+ <!-- CALL_PARAMETERS_WRAP / ANNOTATION_PARAMETER_WRAP = DO_NOT_WRAP; ALIGN_MULTILINE_PARAMETERS_IN_CALLS = false; JDT wrap where necessary preserves manual breaks -->+ <setting id="org.eclipse.jdt.core.formatter.alignment_for_arguments_in_allocation_expression" value="16"/>+ <!-- CALL_PARAMETERS_WRAP / ANNOTATION_PARAMETER_WRAP = DO_NOT_WRAP; ALIGN_MULTILINE_PARAMETERS_IN_CALLS = false; JDT wrap where necessary preserves manual breaks -->+ <setting id="org.eclipse.jdt.core.formatter.alignment_for_arguments_in_explicit_constructor_call" value="16"/>+ <!-- CALL_PARAMETERS_WRAP / ANNOTATION_PARAMETER_WRAP = DO_NOT_WRAP; ALIGN_MULTILINE_PARAMETERS_IN_CALLS = false; JDT wrap where necessary preserves manual breaks -->+ <setting id="org.eclipse.jdt.core.formatter.alignment_for_arguments_in_enum_constant" value="16"/>+ <!-- CALL_PARAMETERS_WRAP / ANNOTATION_PARAMETER_WRAP = DO_NOT_WRAP; ALIGN_MULTILINE_PARAMETERS_IN_CALLS = false; JDT wrap where necessary preserves manual breaks -->+ <setting id="org.eclipse.jdt.core.formatter.alignment_for_arguments_in_annotation" value="16"/>+ <!-- METHOD_CALL_CHAIN_WRAP = DO_NOT_WRAP; JDT wrap where necessary preserves manual breaks -->+ <setting id="org.eclipse.jdt.core.formatter.alignment_for_selector_in_method_invocation" value="16"/>+ <!-- ARRAY_INITIALIZER_WRAP = DO_NOT_WRAP; JDT wrap where necessary preserves manual breaks -->+ <setting id="org.eclipse.jdt.core.formatter.alignment_for_expressions_in_array_initializer" value="16"/>+ <!-- ASSIGNMENT_WRAP = DO_NOT_WRAP; JDT wrap where necessary preserves manual breaks -->+ <setting id="org.eclipse.jdt.core.formatter.alignment_for_assignment" value="16"/>+ <!-- TERNARY_OPERATION_WRAP = DO_NOT_WRAP; JDT wrap where necessary preserves manual breaks -->+ <setting id="org.eclipse.jdt.core.formatter.alignment_for_conditional_expression" value="16"/>+ <!-- EXTENDS_LIST_WRAP = DO_NOT_WRAP; JDT wrap where necessary preserves manual breaks -->+ <setting id="org.eclipse.jdt.core.formatter.alignment_for_superclass_in_type_declaration" value="16"/>+ <!-- EXTENDS_LIST_WRAP = DO_NOT_WRAP; JDT wrap where necessary preserves manual breaks -->+ <setting id="org.eclipse.jdt.core.formatter.alignment_for_superinterfaces_in_type_declaration" value="16"/>+ <!-- THROWS_LIST_WRAP = DO_NOT_WRAP; JDT wrap where necessary preserves manual breaks -->+ <setting id="org.eclipse.jdt.core.formatter.alignment_for_throws_clause_in_method_declaration" value="16"/>+ <!-- THROWS_LIST_WRAP = DO_NOT_WRAP; JDT wrap where necessary preserves manual breaks -->+ <setting id="org.eclipse.jdt.core.formatter.alignment_for_throws_clause_in_constructor_declaration" value="16"/>+ <!-- RESOURCE_LIST_WRAP = DO_NOT_WRAP; JDT wrap where necessary preserves manual breaks -->+ <setting id="org.eclipse.jdt.core.formatter.alignment_for_resources_in_try" value="16"/>+ <!-- BINARY_OPERATION_WRAP = DO_NOT_WRAP; JDT wrap where necessary preserves manual breaks -->+ <setting id="org.eclipse.jdt.core.formatter.alignment_for_multiplicative_operator" value="16"/>+ <!-- BINARY_OPERATION_WRAP = DO_NOT_WRAP; JDT wrap where necessary preserves manual breaks -->+ <setting id="org.eclipse.jdt.core.formatter.alignment_for_additive_operator" value="16"/>+ <!-- BINARY_OPERATION_WRAP = DO_NOT_WRAP; JDT wrap where necessary preserves manual breaks -->+ <setting id="org.eclipse.jdt.core.formatter.alignment_for_string_concatenation" value="16"/>+ <!-- BINARY_OPERATION_WRAP = DO_NOT_WRAP; JDT wrap where necessary preserves manual breaks -->+ <setting id="org.eclipse.jdt.core.formatter.alignment_for_shift_operator" value="16"/>+ <!-- BINARY_OPERATION_WRAP = DO_NOT_WRAP; JDT wrap where necessary preserves manual breaks -->+ <setting id="org.eclipse.jdt.core.formatter.alignment_for_relational_operator" value="16"/>+ <!-- BINARY_OPERATION_WRAP = DO_NOT_WRAP; JDT wrap where necessary preserves manual breaks -->+ <setting id="org.eclipse.jdt.core.formatter.alignment_for_bitwise_operator" value="16"/>+ <!-- BINARY_OPERATION_WRAP = DO_NOT_WRAP; JDT wrap where necessary preserves manual breaks -->+ <setting id="org.eclipse.jdt.core.formatter.alignment_for_logical_operator" value="16"/>+ <!-- CLASS_BRACE_STYLE / METHOD_BRACE_STYLE / LAMBDA_BRACE_STYLE / BRACE_STYLE = END_OF_LINE; ARRAY_INITIALIZER_LBRACE_ON_NEXT_LINE = false -->+ <setting id="org.eclipse.jdt.core.formatter.brace_position_for_type_declaration" value="end_of_line"/>+ <!-- CLASS_BRACE_STYLE / METHOD_BRACE_STYLE / LAMBDA_BRACE_STYLE / BRACE_STYLE = END_OF_LINE; ARRAY_INITIALIZER_LBRACE_ON_NEXT_LINE = false -->+ <setting id="org.eclipse.jdt.core.formatter.brace_position_for_method_declaration" value="end_of_line"/>+ <!-- CLASS_BRACE_STYLE / METHOD_BRACE_STYLE / LAMBDA_BRACE_STYLE / BRACE_STYLE = END_OF_LINE; ARRAY_INITIALIZER_LBRACE_ON_NEXT_LINE = false -->+ <setting id="org.eclipse.jdt.core.formatter.brace_position_for_constructor_declaration" value="end_of_line"/>+ <!-- CLASS_BRACE_STYLE / METHOD_BRACE_STYLE / LAMBDA_BRACE_STYLE / BRACE_STYLE = END_OF_LINE; ARRAY_INITIALIZER_LBRACE_ON_NEXT_LINE = false -->+ <setting id="org.eclipse.jdt.core.formatter.brace_position_for_block" value="end_of_line"/>+ <!-- CLASS_BRACE_STYLE / METHOD_BRACE_STYLE / LAMBDA_BRACE_STYLE / BRACE_STYLE = END_OF_LINE; ARRAY_INITIALIZER_LBRACE_ON_NEXT_LINE = false -->+ <setting id="org.eclipse.jdt.core.formatter.brace_position_for_block_in_case" value="end_of_line"/>+ <!-- CLASS_BRACE_STYLE / METHOD_BRACE_STYLE / LAMBDA_BRACE_STYLE / BRACE_STYLE = END_OF_LINE; ARRAY_INITIALIZER_LBRACE_ON_NEXT_LINE = false -->+ <setting id="org.eclipse.jdt.core.formatter.brace_position_for_switch" value="end_of_line"/>+ <!-- CLASS_BRACE_STYLE / METHOD_BRACE_STYLE / LAMBDA_BRACE_STYLE / BRACE_STYLE = END_OF_LINE; ARRAY_INITIALIZER_LBRACE_ON_NEXT_LINE = false -->+ <setting id="org.eclipse.jdt.core.formatter.brace_position_for_anonymous_type_declaration" value="end_of_line"/>+ <!-- CLASS_BRACE_STYLE / METHOD_BRACE_STYLE / LAMBDA_BRACE_STYLE / BRACE_STYLE = END_OF_LINE; ARRAY_INITIALIZER_LBRACE_ON_NEXT_LINE = false -->+ <setting id="org.eclipse.jdt.core.formatter.brace_position_for_enum_declaration" value="end_of_line"/>+ <!-- CLASS_BRACE_STYLE / METHOD_BRACE_STYLE / LAMBDA_BRACE_STYLE / BRACE_STYLE = END_OF_LINE; ARRAY_INITIALIZER_LBRACE_ON_NEXT_LINE = false -->+ <setting id="org.eclipse.jdt.core.formatter.brace_position_for_enum_constant" value="end_of_line"/>+ <!-- CLASS_BRACE_STYLE / METHOD_BRACE_STYLE / LAMBDA_BRACE_STYLE / BRACE_STYLE = END_OF_LINE; ARRAY_INITIALIZER_LBRACE_ON_NEXT_LINE = false -->+ <setting id="org.eclipse.jdt.core.formatter.brace_position_for_annotation_type_declaration" value="end_of_line"/>+ <!-- CLASS_BRACE_STYLE / METHOD_BRACE_STYLE / LAMBDA_BRACE_STYLE / BRACE_STYLE = END_OF_LINE; ARRAY_INITIALIZER_LBRACE_ON_NEXT_LINE = false -->+ <setting id="org.eclipse.jdt.core.formatter.brace_position_for_lambda_body" value="end_of_line"/>+ <!-- CLASS_BRACE_STYLE / METHOD_BRACE_STYLE / LAMBDA_BRACE_STYLE / BRACE_STYLE = END_OF_LINE; ARRAY_INITIALIZER_LBRACE_ON_NEXT_LINE = false -->+ <setting id="org.eclipse.jdt.core.formatter.brace_position_for_array_initializer" value="end_of_line"/>+ <!-- ELSE_ON_NEW_LINE / CATCH_ON_NEW_LINE / FINALLY_ON_NEW_LINE / WHILE_ON_NEW_LINE = false -->+ <setting id="org.eclipse.jdt.core.formatter.insert_new_line_before_else_in_if_statement" value="do not insert"/>+ <!-- ELSE_ON_NEW_LINE / CATCH_ON_NEW_LINE / FINALLY_ON_NEW_LINE / WHILE_ON_NEW_LINE = false -->+ <setting id="org.eclipse.jdt.core.formatter.insert_new_line_before_catch_in_try_statement" value="do not insert"/>+ <!-- ELSE_ON_NEW_LINE / CATCH_ON_NEW_LINE / FINALLY_ON_NEW_LINE / WHILE_ON_NEW_LINE = false -->+ <setting id="org.eclipse.jdt.core.formatter.insert_new_line_before_finally_in_try_statement" value="do not insert"/>+ <!-- ELSE_ON_NEW_LINE / CATCH_ON_NEW_LINE / FINALLY_ON_NEW_LINE / WHILE_ON_NEW_LINE = false -->+ <setting id="org.eclipse.jdt.core.formatter.insert_new_line_before_while_in_do_statement" value="do not insert"/>+ <!-- CLASS / METHOD / FIELD_ANNOTATION_WRAP = WRAP_ALWAYS; DO_NOT_WRAP_AFTER_SINGLE_ANNOTATION = false -->+ <setting id="org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_type" value="insert"/>+ <!-- CLASS / METHOD / FIELD_ANNOTATION_WRAP = WRAP_ALWAYS; DO_NOT_WRAP_AFTER_SINGLE_ANNOTATION = false -->+ <setting id="org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_method" value="insert"/>+ <!-- CLASS / METHOD / FIELD_ANNOTATION_WRAP = WRAP_ALWAYS; DO_NOT_WRAP_AFTER_SINGLE_ANNOTATION = false -->+ <setting id="org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_field" value="insert"/>+ <!-- PARAMETER / VARIABLE / ENUM_FIELD_ANNOTATION_WRAP = DO_NOT_WRAP -->+ <setting id="org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_parameter" value="do not insert"/>+ <!-- PARAMETER / VARIABLE / ENUM_FIELD_ANNOTATION_WRAP = DO_NOT_WRAP -->+ <setting id="org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_local_variable" value="do not insert"/>+ <!-- PARAMETER / VARIABLE / ENUM_FIELD_ANNOTATION_WRAP = DO_NOT_WRAP -->+ <setting id="org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_enum_constant" value="do not insert"/>+ <!-- BLANK_LINES_BEFORE_PACKAGE = 0 (minimum) -->+ <setting id="org.eclipse.jdt.core.formatter.blank_lines_before_package" value="0"/>+ <!-- BLANK_LINES_AFTER_PACKAGE = 1 (minimum) -->+ <setting id="org.eclipse.jdt.core.formatter.blank_lines_after_package" value="1"/>+ <!-- BLANK_LINES_BEFORE_IMPORTS = 1 (minimum) -->+ <setting id="org.eclipse.jdt.core.formatter.blank_lines_before_imports" value="1"/>+ <!-- BLANK_LINES_AFTER_IMPORTS = 1 (minimum) -->+ <setting id="org.eclipse.jdt.core.formatter.blank_lines_after_imports" value="1"/>+ <!-- BLANK_LINES_AROUND_FIELD / BLANK_LINES_AROUND_FIELD_IN_INTERFACE = 0 (minimum) -->+ <setting id="org.eclipse.jdt.core.formatter.blank_lines_before_field" value="0"/>+ <!-- BLANK_LINES_AROUND_METHOD = 1 (minimum) -->+ <setting id="org.eclipse.jdt.core.formatter.blank_lines_before_method" value="1"/>+ <!-- BLANK_LINES_AROUND_METHOD_IN_INTERFACE = 1 (minimum) -->+ <setting id="org.eclipse.jdt.core.formatter.blank_lines_before_abstract_method" value="1"/>+ <!-- BLANK_LINES_AROUND_CLASS = 1 (minimum) -->+ <setting id="org.eclipse.jdt.core.formatter.blank_lines_before_member_type" value="1"/>+ <!-- BLANK_LINES_AFTER_CLASS_HEADER = 0 (minimum) -->+ <setting id="org.eclipse.jdt.core.formatter.blank_lines_before_first_class_body_declaration" value="0"/>+ <!-- BLANK_LINES_BEFORE_CLASS_END = 0 (minimum) -->+ <setting id="org.eclipse.jdt.core.formatter.blank_lines_after_last_class_body_declaration" value="0"/>+ <!-- BLANK_LINES_AROUND_METHOD / AROUND_CLASS = 1; JDT chunks separate member kinds, not consecutive fields -->+ <setting id="org.eclipse.jdt.core.formatter.blank_lines_before_new_chunk" value="1"/>+ <!-- DO_NOT_INDENT_TOP_LEVEL_CLASS_MEMBERS = false -->+ <setting id="org.eclipse.jdt.core.formatter.indent_body_declarations_compare_to_type_header" value="true"/>+ <!-- DO_NOT_INDENT_TOP_LEVEL_CLASS_MEMBERS = false -->+ <setting id="org.eclipse.jdt.core.formatter.indent_body_declarations_compare_to_enum_declaration_header" value="true"/>+ <!-- DO_NOT_INDENT_TOP_LEVEL_CLASS_MEMBERS = false -->+ <setting id="org.eclipse.jdt.core.formatter.indent_body_declarations_compare_to_enum_constant_header" value="true"/>+ <!-- DO_NOT_INDENT_TOP_LEVEL_CLASS_MEMBERS = false -->+ <setting id="org.eclipse.jdt.core.formatter.indent_body_declarations_compare_to_annotation_declaration_header" value="true"/>+ <!-- INDENT_CASE_FROM_SWITCH = true -->+ <setting id="org.eclipse.jdt.core.formatter.indent_switchstatements_compare_to_switch" value="true"/>+ <!-- INDENT_BREAK_FROM_CASE = true -->+ <setting id="org.eclipse.jdt.core.formatter.indent_switchstatements_compare_to_cases" value="true"/>+ <!-- KEEP_SIMPLE_BLOCKS / METHODS / LAMBDAS / CLASSES_IN_ONE_LINE = false -->+ <setting id="org.eclipse.jdt.core.formatter.keep_code_block_on_one_line" value="one_line_never"/>+ <!-- KEEP_SIMPLE_BLOCKS / METHODS / LAMBDAS / CLASSES_IN_ONE_LINE = false -->+ <setting id="org.eclipse.jdt.core.formatter.keep_method_body_on_one_line" value="one_line_never"/>+ <!-- KEEP_SIMPLE_BLOCKS / METHODS / LAMBDAS / CLASSES_IN_ONE_LINE = false -->+ <setting id="org.eclipse.jdt.core.formatter.keep_lambda_body_block_on_one_line" value="one_line_never"/>+ <!-- KEEP_SIMPLE_BLOCKS / METHODS / LAMBDAS / CLASSES_IN_ONE_LINE = false -->+ <setting id="org.eclipse.jdt.core.formatter.keep_type_declaration_on_one_line" value="one_line_never"/>+ <!-- KEEP_SIMPLE_BLOCKS / METHODS / LAMBDAS / CLASSES_IN_ONE_LINE = false -->+ <setting id="org.eclipse.jdt.core.formatter.keep_anonymous_type_declaration_on_one_line" value="one_line_never"/>+ <!-- KEEP_SIMPLE_BLOCKS / METHODS / LAMBDAS / CLASSES_IN_ONE_LINE = false -->+ <setting id="org.eclipse.jdt.core.formatter.keep_enum_declaration_on_one_line" value="one_line_never"/>+ <!-- KEEP_SIMPLE_BLOCKS / METHODS / LAMBDAS / CLASSES_IN_ONE_LINE = false -->+ <setting id="org.eclipse.jdt.core.formatter.keep_annotation_declaration_on_one_line" value="one_line_never"/>+ <!-- KEEP_SIMPLE_BLOCKS / METHODS / LAMBDAS / CLASSES_IN_ONE_LINE = false -->+ <setting id="org.eclipse.jdt.core.formatter.keep_if_then_body_block_on_one_line" value="one_line_never"/>+ <!-- KEEP_SIMPLE_BLOCKS / METHODS / LAMBDAS / CLASSES_IN_ONE_LINE = false -->+ <setting id="org.eclipse.jdt.core.formatter.keep_loop_body_block_on_one_line" value="one_line_never"/>+ <!-- KEEP_SIMPLE_BLOCKS / METHODS / CLASSES_IN_ONE_LINE = false; empty-block handling may differ in JDT -->+ <setting id="org.eclipse.jdt.core.formatter.insert_new_line_in_empty_type_declaration" value="insert"/>+ <!-- KEEP_SIMPLE_BLOCKS / METHODS / CLASSES_IN_ONE_LINE = false; empty-block handling may differ in JDT -->+ <setting id="org.eclipse.jdt.core.formatter.insert_new_line_in_empty_method_body" value="insert"/>+ <!-- KEEP_SIMPLE_BLOCKS / METHODS / CLASSES_IN_ONE_LINE = false; empty-block handling may differ in JDT -->+ <setting id="org.eclipse.jdt.core.formatter.insert_new_line_in_empty_block" value="insert"/>+ <!-- KEEP_SIMPLE_BLOCKS / METHODS / CLASSES_IN_ONE_LINE = false; empty-block handling may differ in JDT -->+ <setting id="org.eclipse.jdt.core.formatter.insert_new_line_in_empty_anonymous_type_declaration" value="insert"/>+ <!-- ARRAY_INITIALIZER_LBRACE_ON_NEXT_LINE / RBRACE_ON_NEXT_LINE = false -->+ <setting id="org.eclipse.jdt.core.formatter.keep_empty_array_initializer_on_one_line" value="true"/>+ <!-- ARRAY_INITIALIZER_LBRACE_ON_NEXT_LINE = false; KEEP_LINE_BREAKS = true -->+ <setting id="org.eclipse.jdt.core.formatter.insert_new_line_after_opening_brace_in_array_initializer" value="do not insert"/>+ <!-- ARRAY_INITIALIZER_RBRACE_ON_NEXT_LINE = false; KEEP_LINE_BREAKS = true -->+ <setting id="org.eclipse.jdt.core.formatter.insert_new_line_before_closing_brace_in_array_initializer" value="do not insert"/>+ <!-- SPACE_BEFORE_ARRAY_INITIALIZER_LBRACE / SPACE_WITHIN_ARRAY_INITIALIZER_BRACES / SPACE_WITHIN_EMPTY_ARRAY_INITIALIZER_BRACES = false -->+ <setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_array_initializer" value="do not insert"/>+ <!-- SPACE_BEFORE_ARRAY_INITIALIZER_LBRACE / SPACE_WITHIN_ARRAY_INITIALIZER_BRACES / SPACE_WITHIN_EMPTY_ARRAY_INITIALIZER_BRACES = false -->+ <setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_brace_in_array_initializer" value="do not insert"/>+ <!-- SPACE_BEFORE_ARRAY_INITIALIZER_LBRACE / SPACE_WITHIN_ARRAY_INITIALIZER_BRACES / SPACE_WITHIN_EMPTY_ARRAY_INITIALIZER_BRACES = false -->+ <setting id="org.eclipse.jdt.core.formatter.insert_space_before_closing_brace_in_array_initializer" value="do not insert"/>+ <!-- SPACE_BEFORE_ARRAY_INITIALIZER_LBRACE / SPACE_WITHIN_ARRAY_INITIALIZER_BRACES / SPACE_WITHIN_EMPTY_ARRAY_INITIALIZER_BRACES = false -->+ <setting id="org.eclipse.jdt.core.formatter.insert_space_between_empty_braces_in_array_initializer" value="do not insert"/>+ <!-- SPACE_AROUND_LAMBDA_ARROW = true -->+ <setting id="org.eclipse.jdt.core.formatter.insert_space_before_lambda_arrow" value="insert"/>+ <!-- SPACE_AROUND_LAMBDA_ARROW = true -->+ <setting id="org.eclipse.jdt.core.formatter.insert_space_after_lambda_arrow" value="insert"/>+ <!-- WRAP_COMMENTS = false; KEEP_FIRST_COLUMN_COMMENT = true (JDT comment formatting disabled to preserve text) -->+ <setting id="org.eclipse.jdt.core.formatter.comment.format_line_comments" value="false"/>+ <!-- WRAP_COMMENTS = false (JDT comment formatting disabled to preserve text) -->+ <setting id="org.eclipse.jdt.core.formatter.comment.format_block_comments" value="false"/>+ <!-- JavaCodeStyleSettings.ENABLE_JAVADOC_FORMATTING = true -->+ <setting id="org.eclipse.jdt.core.formatter.comment.format_javadoc_comments" value="true"/>+ <!-- WRAP_COMMENTS = false; JDT has a separate comment width, so disable its width-based wrapping -->+ <setting id="org.eclipse.jdt.core.formatter.comment.line_length" value="999999"/>+ <!-- JavaCodeStyleSettings.JD_INDENT_ON_CONTINUATION = false -->+ <setting id="org.eclipse.jdt.core.formatter.comment.indent_parameter_description" value="false"/>+ <!-- JavaCodeStyleSettings.JD_PARAM_DESCRIPTION_ON_NEW_LINE = false -->+ <setting id="org.eclipse.jdt.core.formatter.comment.insert_new_line_for_parameter" value="false"/>+ <!-- CodeStyleSettings.FORMATTER_TAGS_ENABLED = true -->+ <setting id="org.eclipse.jdt.core.formatter.use_on_off_tags" value="true"/>+ <!-- KEEP_CONTROL_STATEMENT_IN_ONE_LINE = true -->+ <setting id="org.eclipse.jdt.core.formatter.keep_then_statement_on_same_line" value="true"/>+ <!-- KEEP_CONTROL_STATEMENT_IN_ONE_LINE = true -->+ <setting id="org.eclipse.jdt.core.formatter.keep_else_statement_on_same_line" value="true"/>+ <!-- KEEP_CONTROL_STATEMENT_IN_ONE_LINE = true -->+ <setting id="org.eclipse.jdt.core.formatter.keep_imple_if_on_one_line" value="true"/>+ <!-- KEEP_CONTROL_STATEMENT_IN_ONE_LINE = true -->+ <setting id="org.eclipse.jdt.core.formatter.keep_simple_for_body_on_same_line" value="true"/>+ <!-- KEEP_CONTROL_STATEMENT_IN_ONE_LINE = true -->+ <setting id="org.eclipse.jdt.core.formatter.keep_simple_while_body_on_same_line" value="true"/>+ <!-- KEEP_CONTROL_STATEMENT_IN_ONE_LINE = true -->+ <setting id="org.eclipse.jdt.core.formatter.keep_simple_do_while_body_on_same_line" value="true"/>+ </profile>+</profiles>diff --git a/crates/ide-lsp/tests/fixtures/FormatterSample.input.java b/crates/ide-lsp/tests/fixtures/FormatterSample.input.javanew file mode 100644index 000000000..6c7121a84--- /dev/null+++ b/crates/ide-lsp/tests/fixtures/FormatterSample.input.java@@ -0,0 +1,25 @@+package demo;+import java.util.List;+import java.util.function.Function;+@Deprecated @SuppressWarnings("unused") public class FormatterSample {+@Deprecated @SuppressWarnings("unused") private String first="first";+private String second="second";+/** A description which remains on a very long single line to check whether formatting introduces wrapping when the right margin is exceeded with ordinary words in a JavaDoc comment. */+@Deprecated @SuppressWarnings("unused") public String combine(@Deprecated @SuppressWarnings("unused") String firstParameterWithAVeryLongName, String secondParameterWithAVeryLongName, String thirdParameterWithAVeryLongName){if(firstParameterWithAVeryLongName==null){return "none";}else{try{return List.of(firstParameterWithAVeryLongName,secondParameterWithAVeryLongName,thirdParameterWithAVeryLongName).stream().map(value->value.trim()).filter(value->!value.isEmpty()).findFirst().orElse("none");}catch(RuntimeException problem){throw problem;}finally{System.out.println("done");}}}+public void multiline(String firstParameter,+String secondParameter,+String thirdParameter){+combine(firstParameter,+secondParameter,+thirdParameter);+List.of(firstParameter, secondParameter)+.stream()+.map(value->value.trim())+.forEach(System.out::println);+}+public void retained(String firstParameter, // retain declaration break+String secondParameter){combine(firstParameter, // retain call break+secondParameter, "third");}+public void arrays(){int[] values=new int[]{1,2,3};int[] empty=new int[]{};Function<String,String> clean=value->{return value.trim();};Runnable run=()->{System.out.println(values.length);};}+// A comment which intentionally exceeds the right margin and should remain unwrapped with the built-in profile even when it contains many ordinary words and spaces to split on.+}diff --git a/crates/ide-lsp/tests/fixtures/FormatterSample.intellij.java b/crates/ide-lsp/tests/fixtures/FormatterSample.intellij.javanew file mode 100644index 000000000..f1211e24c--- /dev/null+++ b/crates/ide-lsp/tests/fixtures/FormatterSample.intellij.java@@ -0,0 +1,65 @@+package demo;++import java.util.List;+import java.util.function.Function;++@Deprecated+@SuppressWarnings("unused")+public class FormatterSample {+ @Deprecated+ @SuppressWarnings("unused")+ private String first = "first";+ private String second = "second";++ /** A description which remains on a very long single line to check whether formatting introduces wrapping when the right margin is exceeded with ordinary words in a JavaDoc comment. */+ @Deprecated+ @SuppressWarnings("unused")+ public String combine(@Deprecated @SuppressWarnings("unused") String firstParameterWithAVeryLongName,+ String secondParameterWithAVeryLongName, String thirdParameterWithAVeryLongName) {+ if (firstParameterWithAVeryLongName == null) {+ return "none";+ } else {+ try {+ return List+ .of(firstParameterWithAVeryLongName, secondParameterWithAVeryLongName,+ thirdParameterWithAVeryLongName)+ .stream().map(value -> value.trim()).filter(value -> !value.isEmpty()).findFirst()+ .orElse("none");+ } catch (RuntimeException problem) {+ throw problem;+ } finally {+ System.out.println("done");+ }+ }+ }++ public void multiline(String firstParameter,+ String secondParameter,+ String thirdParameter) {+ combine(firstParameter,+ secondParameter,+ thirdParameter);+ List.of(firstParameter, secondParameter)+ .stream()+ .map(value -> value.trim())+ .forEach(System.out::println);+ }++ public void retained(String firstParameter, // retain declaration break+ String secondParameter) {+ combine(firstParameter, // retain call break+ secondParameter, "third");+ }++ public void arrays() {+ int[] values = new int[]{1, 2, 3};+ int[] empty = new int[]{};+ Function<String, String> clean = value -> {+ return value.trim();+ };+ Runnable run = () -> {+ System.out.println(values.length);+ };+ }+ // A comment which intentionally exceeds the right margin and should remain unwrapped with the built-in profile even when it contains many ordinary words and spaces to split on.+}diff --git a/crates/ide-lsp/tests/mock_server.rs b/crates/ide-lsp/tests/mock_server.rsindex a636e5e4d..7bfb45ccd 100644--- a/crates/ide-lsp/tests/mock_server.rs+++ b/crates/ide-lsp/tests/mock_server.rs@@ -1017,6 +1017,70 @@ async fn settings_changes_reach_servers_without_restarts() {assert_eq!(s.launches.load(Ordering::SeqCst), 1);}+#[tokio::test(flavor = "multi_thread")]+async fn java_formatter_profiles_reach_initialization_and_live_configuration_without_restart() {+ let h = harness_configured("java", "java", &[("jdtls", incremental_caps())], |cfg| {+ cfg.initialization_options = Some(+ toml::Value::try_from(json!({+ "settings": {"java": {"autobuild": {"enabled": false}}}+ }))+ .unwrap(),+ );+ });+ let data = tempfile::tempdir().unwrap();+ std::fs::write(h.root.join("custom.xml"), "<profiles/>").unwrap();+ let style = Arc::new(Mutex::new(ide_core::editor_settings::LanguageCodeStyle {+ formatter: Some("intellij".into()),+ formatter_path: Some("custom.xml".into()),+ ..Default::default()+ }));+ h.mgr.set_config_hook(Box::new({+ let style = style.clone();+ let root = h.root.clone();+ let data = data.path().to_path_buf();+ move |cfg, _| ide_lsp::jdtls::configure_formatter(cfg, Some(&style.lock()), &root, &data, None).unwrap()+ }));+ h.open("Main.java", "public class Main {}");+ h.wait_status("jdtls", ServerStatus::Ready).await;+ let state = h.launcher.state("jdtls");+ let init = state.requested("initialize")[0].clone();+ assert_eq!(+ init["initializationOptions"]["settings"]["java"]["format"]["settings"]["profile"],+ "OpenIDEA IntelliJ IDEA"+ );+ for mode in ["custom", "default", "intellij", "default"] {+ style.lock().formatter = Some(mode.into());+ h.mgr.reconfigure().await;+ let expected = match mode {+ "custom" => uri_of(&h.root.join("custom.xml")),+ "intellij" => uri_of(&data.path().join("lsp/jdtls/intellij.xml")),+ _ => String::new(),+ };+ eventually(|| {+ state+ .notified("workspace/didChangeConfiguration")+ .last()+ .is_some_and(|p| p["settings"]["java"]["format"]["settings"]["url"] == expected)+ })+ .await;+ let result: Value = state+ .peer()+ .request(+ "workspace/configuration",+ json!({+ "items": [{"section": "java.format.settings"}, {"section": "java.autobuild.enabled"}]+ }),+ None,+ )+ .await+ .unwrap();+ assert_eq!(result[0]["url"], expected);+ assert_eq!(result[0]["profile"], if mode == "intellij" { "OpenIDEA IntelliJ IDEA" } else { "" });+ assert_eq!(result[1], false);+ assert_eq!(state.launches.load(Ordering::SeqCst), 1);+ }+}+#[tokio::test(flavor = "multi_thread")]async fn java_source_paths_reach_initialization_live_configuration_and_builds() {let h = harness_configured("java", "java", &[("jdtls", incremental_caps())], |cfg| {diff --git a/crates/ide-lsp/tests/real_servers.rs b/crates/ide-lsp/tests/real_servers.rsindex 43c250a20..f25f77745 100644--- a/crates/ide-lsp/tests/real_servers.rs+++ b/crates/ide-lsp/tests/real_servers.rs@@ -62,6 +62,53 @@ fn lang(id: &str, ext: &str, server: LanguageServerConfig) -> LanguageConfig {}}+#[tokio::test(flavor = "multi_thread")]+#[ignore = "requires jdtls on PATH and a compatible JDK"]+async fn intellij_java_formatter_preserves_manual_breaks_and_wraps_long_lines() {+ let dir = tempfile::tempdir().unwrap();+ let root = dir.path().canonicalize().unwrap();+ let source = include_str!("fixtures/FormatterSample.input.java");+ let expected = include_str!("fixtures/FormatterSample.intellij.java");+ let path = root.join("src/demo/FormatterSample.java");+ std::fs::create_dir_all(path.parent().unwrap()).unwrap();+ std::fs::write(&path, source).unwrap();+ let mut java = LanguageRegistry::load(None).unwrap().get(&LanguageId::new("java")).unwrap();+ let server = &mut java.language_servers[0];+ if let Ok(bin) = std::env::var("OPENIDEA_TEST_JDTLS") {+ server.command = bin;+ }+ server.args.extend(["-data".into(), root.join("jdtls-workspace").to_string_lossy().into_owned()]);+ server.settings = Some(serde_json::json!({"java": {"autobuild": {"enabled": false}}}));+ let (mgr, events) = manager(&root, LanguageRegistry::from_configs(vec![java]));+ mgr.set_config_hook(Box::new({+ let root = root.clone();+ move |cfg, _| {+ ide_lsp::jdtls::configure_source_paths(cfg, Some(vec!["src".into()]));+ let style = ide_core::editor_settings::LanguageCodeStyle {+ formatter: Some("intellij".into()),+ ..Default::default()+ };+ ide_lsp::jdtls::configure_formatter(cfg, Some(&style), &root, &root.join("data"), None).unwrap();+ }+ }));+ let doc = DocId::next();+ mgr.did_open(doc, &path, &LanguageId::new("java"), 1, source);+ retry("jdtls ready", Duration::from_secs(120), || {+ let status = last_status(&events, "jdtls");+ if let Some((ServerStatus::Crashed | ServerStatus::Missing | ServerStatus::Misconfigured, msg)) = &status {+ panic!("jdtls failed: {msg:?}");+ }+ async move { matches!(status, Some((ServerStatus::Ready | ServerStatus::Busy, _))).then_some(()) }+ })+ .await;+ let edits =+ mgr.format(doc, None, ide_lsp::types::FormatOptions { tab_size: 4, insert_spaces: true, on_save: false }).await;+ tokio::time::timeout(Duration::from_secs(10), mgr.shutdown_all()).await.unwrap();+ let mut document = ide_text::Document::new(source);+ document.apply_edits(&edits.unwrap()).unwrap();+ assert_eq!(document.text(), expected);+}+#[tokio::test(flavor = "multi_thread")]async fn missing_binary_is_reported_with_install_hint() {let dir = tempfile::tempdir().unwrap();diff --git a/src/bindings.ts b/src/bindings.tsindex c3d5811cd..b65240187 100644--- a/src/bindings.ts+++ b/src/bindings.ts@@ -3903,6 +3903,10 @@ export type LanguageCodeStyle = LanguageCodeStyle_Serialize | LanguageCodeStyle_/** Editor › Code Style › <Language> › Tabs and Indents (unset fields inherit). */export type LanguageCodeStyle_Deserialize = {+ /** Java formatter: `default` | `intellij` | `custom` (other languages ignore it). */+ formatter?: string | null,+ /** Eclipse formatter XML; relative paths start at the project root. */+ formatterPath?: string | null,useTab?: boolean | null,tabSize?: number | null,indent?: number | null,@@ -3915,6 +3919,10 @@ export type LanguageCodeStyle_Deserialize = {/** Editor › Code Style › <Language> › Tabs and Indents (unset fields inherit). */export type LanguageCodeStyle_Serialize = {+ /** Java formatter: `default` | `intellij` | `custom` (other languages ignore it). */+ formatter?: string | null,+ /** Eclipse formatter XML; relative paths start at the project root. */+ formatterPath?: string | null,useTab?: boolean | null,tabSize?: number | null,indent?: number | null,diff --git a/src/features/settings/__tests__/fileTypes.test.ts b/src/features/settings/__tests__/fileTypes.test.tsindex 35c6c3261..0da59f0bd 100644--- a/src/features/settings/__tests__/fileTypes.test.ts+++ b/src/features/settings/__tests__/fileTypes.test.ts@@ -87,6 +87,9 @@ describe("code style scheme", () => {it("copies every Code Style value to the other scheme", () => {stage("editor.codeStyle", "global", "editor.codeStyle.hardWrapAt", 100);stage("editor.codeStyle", "global", "editor.codeStyle.formatterTagsEnabled", true);+ stage("editor.codeStyle", "global", "editor.codeStyle.languages", {+ java: { formatter: "custom", formatterPath: "config/formatter.xml" },+ });copyScheme("editor.codeStyle", "global", "project");const s = useSettingsDialog.getState();expect(stagedValue(s, "project", "editor.codeStyle.hardWrapAt")).toBe(100);diff --git a/src/features/settings/__tests__/javaFormatter.test.tsx b/src/features/settings/__tests__/javaFormatter.test.tsxnew file mode 100644index 000000000..f019ca2e4--- /dev/null+++ b/src/features/settings/__tests__/javaFormatter.test.tsx@@ -0,0 +1,68 @@+import { act, cleanup, fireEvent, render, screen } from "@testing-library/react";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";+import { useProject } from "@/platform/project";+import { PageContext } from "../components/context";+import { DEFAULT_SETTINGS } from "../model/schema";+import { openSettings, stagedValue, useSettingsDialog } from "../model/staging";+import { codeStyleLanguagePages } from "../pages/editor/codeStyle";++const mocks = vi.hoisted(() => ({ open: vi.fn() }));+vi.mock("@tauri-apps/plugin-dialog", () => ({ open: mocks.open }));+vi.mock("@/platform/ipc", async (original) => {+ const actual = await original<typeof import("@/platform/ipc")>();+ return {+ ...actual,+ isTauri: () => true,+ commands: {+ ...actual.commands,+ languages: async () => [+ { id: "java", name: "Java", indent: { tabSize: 4, insertSpaces: true } },+ { id: "rust", name: "Rust" },+ ],+ },+ };+});++beforeEach(() => {+ useProject.setState({ settings: structuredClone(DEFAULT_SETTINGS), project: null });+ openSettings("editor.codeStyle.java");+});+afterEach(cleanup);++async function page(language: string) {+ const pages = await codeStyleLanguagePages();+ const spec = pages.find((p) => p.id === `editor.codeStyle.${language}`)!;+ const Page = spec.component!;+ render(+ <PageContext.Provider value={{ pageId: spec.id, scope: "global" }}>+ <Page />+ </PageContext.Provider>,+ );+}+const value = () => stagedValue(useSettingsDialog.getState(), "global", "editor.codeStyle.languages");++describe("Java formatting profile", () => {+ it("stages profile and relative XML paths, browses for XML, and resets language defaults", async () => {+ await page("java");+ fireEvent.click(screen.getByRole("button", { name: "Formatting profile" }));+ fireEvent.click(screen.getByRole("option", { name: "Custom Eclipse formatter XML" }));+ fireEvent.change(screen.getByLabelText("Eclipse formatter XML"), { target: { value: "config/style.xml" } });+ expect(value()).toEqual({ java: { formatter: "custom", formatterPath: "config/style.xml" } });+ mocks.open.mockResolvedValue("/project/custom.xml");+ await act(async () => fireEvent.click(screen.getByRole("button", { name: "Browse…" })));+ expect(value()).toEqual({ java: { formatter: "custom", formatterPath: "/project/custom.xml" } });+ expect(mocks.open).toHaveBeenCalledWith(+ expect.objectContaining({ filters: [{ name: "XML", extensions: ["xml"] }] }),+ );+ fireEvent.click(screen.getByRole("button", { name: "Formatting profile" }));+ fireEvent.click(screen.getByRole("option", { name: "IntelliJ IDEA" }));+ expect(screen.queryByLabelText("Eclipse formatter XML")).toBeNull();+ fireEvent.click(screen.getByRole("button", { name: "Reset to Language Defaults" }));+ expect(value()).toEqual({});+ });++ it("keeps the profile controls specific to Java", async () => {+ await page("rust");+ expect(screen.queryByRole("button", { name: "Formatting profile" })).toBeNull();+ });+});diff --git a/src/features/settings/pages/editor/codeStyle.tsx b/src/features/settings/pages/editor/codeStyle.tsxindex 3138f68e1..43402d6d2 100644--- a/src/features/settings/pages/editor/codeStyle.tsx+++ b/src/features/settings/pages/editor/codeStyle.tsx@@ -12,7 +12,7 @@ import { Button, Checkbox, Input, type SelectOption, T } from "@/ui";import { commands, isTauri, type LanguageInfo_Serialize as LanguageInfo } from "@/platform/ipc";import { ENCODING_CHOICES, lineCommentDefaults, type LanguageCodeStyle } from "@/platform/editorSettings";import { NATIVE_TO_ASCII_ACTION } from "../../nativeToAscii";-import { NumberInput, Row } from "../../components/Form";+import { NumberInput, Row, Select } from "../../components/Form";import { useSetting } from "../../components/context";import type { PageSpec } from "../spec";import { CODE_STYLE_SCOPE, CodeStyleMain, SchemeRow, formatterLabels } from "./codeStyleScheme";@@ -159,6 +159,7 @@ function LanguageStyle({ lang }: { lang: LanguageInfo }) {return (<div className={styles.page}><SchemeRow />+ {lang.id === "java" && <JavaFormatter style={own} onChange={set} />}<section aria-label={tx("Tabs and Indents")}><div className={styles.previewTitle}><T>Tabs and Indents</T>@@ -238,6 +239,62 @@ function LanguageStyle({ lang }: { lang: LanguageInfo }) {);}+const JAVA_FORMATTERS: SelectOption<NonNullable<LanguageCodeStyle["formatter"]>>[] = [+ { value: "default", label: "Default" },+ { value: "intellij", label: "IntelliJ IDEA" },+ { value: "custom", label: "Custom Eclipse formatter XML" },+];++function JavaFormatter({+ style,+ onChange,+}: {+ style: LanguageCodeStyle;+ onChange: (patch: Partial<LanguageCodeStyle>) => void;+}) {+ const id = useId();+ const browse = async () => {+ const { open } = await import("@tauri-apps/plugin-dialog");+ const path = await open({+ multiple: false,+ title: tx("Eclipse formatter XML"),+ filters: [{ name: "XML", extensions: ["xml"] }],+ });+ if (typeof path === "string") onChange({ formatterPath: path });+ };+ return (+ <section aria-label={tx("Formatting profile")}>+ <Row+ label="Formatting profile"+ hint="Changes take effect after Apply without restarting the Java language server."+ >+ <Select+ value={style.formatter ?? "default"}+ options={JAVA_FORMATTERS}+ onChange={(formatter) => onChange({ formatter })}+ aria-label="Formatting profile"+ />+ </Row>+ {style.formatter === "custom" && (+ <Row+ label="Eclipse formatter XML"+ htmlFor={id}+ hint="Absolute or project-relative path. To reload an edited XML file, switch to Default, apply, then select Custom again."+ >+ <Input+ id={id}+ value={style.formatterPath ?? ""}+ onChange={(e) => onChange({ formatterPath: e.target.value })}+ />+ <Button size="small" onClick={browse}>+ <T>Browse…</T>+ </Button>+ </Row>+ )}+ </section>+ );+}+const SKIP = new Set(["plaintext", "log", "diff", "ignore", "gitattributes"]);/** One Code Style page per language (registered once the language list is known). */@@ -261,6 +318,9 @@ export async function codeStyleLanguagePages(): Promise<PageSpec[]> {"Hard wrap at","Comment Code","Line comment at first column",+ ...(lang.id === "java"+ ? ["Formatting profile", "IntelliJ IDEA", "Custom Eclipse formatter XML", "Eclipse formatter XML"]+ : []),],component: () => <LanguageStyle lang={lang} />,}));diff --git a/src/platform/editorSettings.ts b/src/platform/editorSettings.tsindex 6471898dd..79b37e144 100644--- a/src/platform/editorSettings.ts+++ b/src/platform/editorSettings.ts@@ -24,6 +24,9 @@ export interface CustomColorScheme {export type CustomColorSchemes = Record<string, CustomColorScheme>;export interface LanguageCodeStyle {+ /** Java formatter; relative XML paths start at the project root. */+ formatter?: "default" | "intellij" | "custom";+ formatterPath?: string;useTab?: boolean;tabSize?: number;indent?: number;diff --git a/src/platform/locales/text/zh.json b/src/platform/locales/text/zh.jsonindex 339e38bca..5c81a726f 100644--- a/src/platform/locales/text/zh.json+++ b/src/platform/locales/text/zh.json@@ -1364,12 +1364,10 @@"Open console for tool output": "为工具输出打开控制台","Open documents": "打开的文档","Open in Browser": "在浏览器中打开",- "Open in Editor": "在编辑器中打开","Open in Find Tool Window": "在“查找”工具窗口中打开","Open in Find Window": "在查找窗口中打开","Open in Opposite Group": "在相对组中打开","Open in New Window": "在新窗口中打开",- "Open in Tool Window": "在工具窗口中打开","Open in Terminal": "在终端中打开","Open in editor": "在编辑器中打开","Open moved files in editor": "在编辑器中打开已移动的文件",@@ -5447,5 +5445,14 @@"Chat in Tool Window": "在工具窗口中聊天","Chat in Editor": "在编辑器中聊天","Agent settings": "智能体设置",- "Describe a task…": "描述一个任务…"+ "Describe a task…": "描述一个任务…",+ "Formatting profile": "格式化方案",+ "Custom Eclipse formatter XML": "自定义 Eclipse 格式化 XML",+ "Eclipse formatter XML": "Eclipse 格式化 XML",+ "Changes take effect after Apply without restarting the Java language server.": "应用后即生效,无需重启 Java 语言服务器。",+ "Absolute or project-relative path. To reload an edited XML file, switch to Default, apply, then select Custom again.": "使用绝对路径或项目相对路径。修改 XML 文件后,请切换到默认方案并应用,再重新选择自定义方案。",+ "Cannot read Java formatter XML {path}: {error}": "无法读取 Java 格式化 XML {path}:{error}",+ "Java formatter configuration unavailable": "Java 格式化配置不可用",+ "Choose an Eclipse formatter XML file.": "请选择 Eclipse 格式化 XML 文件。",+ "Not a formatter XML file: {path}": "不是格式化 XML 文件:{path}"}diff --git a/src/platform/locales/textCatalog.test.ts b/src/platform/locales/textCatalog.test.tsindex 86841e022..7d74d60e9 100644--- a/src/platform/locales/textCatalog.test.ts+++ b/src/platform/locales/textCatalog.test.ts@@ -81,6 +81,7 @@ const BUILT_AT_RUNTIME = new Set<string>([]);const UNTRANSLATED = new Set<string>([// Product names"Prettier",+ "IntelliJ IDEA",// Go environment variables and placeholder values"CGO","GOPROXY",