feat(java): add java-analyzer as a Java language server (#4420)

java-analyzer joins jdtls and kmp-lsp as a fallback Java language server
(jdtls → java-analyzer → kmp-lsp), so it can be chosen with "Use only
java-analyzer" in Settings › Language Servers. It installs from the GitHub
releases of l0ng-ai/java-analyzer (gzip binaries on macOS and Linux, zip on
Windows).

The project JDK and the Maven local repository, when set, are passed as
`jdkHome` and `maven.localRepository`; otherwise the server finds them
itself, and the server's own `initializationOptions` in settings win.

A built-in server given an executable path in Settings › Language Servers
now reports that executable as its custom path in the tools list, matching
the program that is actually launched.
This commit is contained in:
l0ng-ai
2026-11-19 11:40:53 +08:00
committed by GitHub
parent aab6d9ffdc
commit ea8351d9f6
12 changed files with 178 additions and 21 deletions
+7 -1
View File
@@ -17,11 +17,17 @@ fn tools(host: &Host, window: &str) -> IdeResult<Tools> {
Ok(project) => (project.settings.effective(), project.root.clone()),
Err(_) => (host.global_settings.effective(), Default::default()),
};
let custom = settings
let mut custom: std::collections::BTreeMap<_, _> = settings
.tools
.iter()
.filter_map(|(command, t)| Some((command.clone(), ide_sdk::apply::setting_path(&root, &t.path)?)))
.collect();
let langs = host.languages.all();
custom.extend(ide_sdk::apply::server_command_paths(
langs.iter().flat_map(|l| &l.language_servers),
&root,
&settings,
));
// Godot is usually an app bundle / Flatpak outside the search path.
let found = ide_sdk::godot::find(None).map(|p| (ide_sdk::godot::COMMAND.to_owned(), p));
let runtime = ide_sdk::apply::tool_install_environment(&root, &settings);
+1 -1
View File
@@ -690,7 +690,7 @@ mod tests {
for (lang, servers) in [
("python", vec!["basedpyright", "pyright", "ty", "pyrefly"]),
("typescript", vec!["vtsls", "typescript-language-server"]),
("java", vec!["jdtls", "kmp-lsp"]),
("java", vec!["jdtls", "java-analyzer", "kmp-lsp"]),
("kotlin", vec!["kotlin-lsp", "kotlin-language-server", "kmp-lsp"]),
] {
let cfg = reg.get(&LanguageId::new(lang)).unwrap();
+32
View File
@@ -1247,6 +1247,21 @@ pub fn configure_server(cfg: &mut LanguageServerConfig, root: &Path, server_root
}
}
if name == "java-analyzer" {
// Unset, java-analyzer finds a JDK and the Maven local repository the way Maven does;
// the server's own `initializationOptions` in settings are merged later and win.
let mut layer = serde_json::Map::new();
if let Some(jdk) = project_jdk(root, s) {
layer.insert("jdkHome".into(), json!(jdk.home.to_string_lossy()));
}
if let Some(repo) = maven_local_repository(root, s) {
layer.insert("maven".into(), json!({ "localRepository": repo.to_string_lossy() }));
}
if !layer.is_empty() {
merge_init_options(cfg, Value::Object(layer));
}
}
if let Some(lib) = tsdk(server_root, s).or_else(|| tsdk(root, s)) {
let lib_s = lib.to_string_lossy().into_owned();
match name {
@@ -1405,6 +1420,23 @@ pub fn language_servers(language: &str, defined: &[LanguageServerConfig], s: &Se
out
}
/// Built-in servers given an executable path in Settings › Language Servers, as
/// (definition's command, path): they run that executable (see [`server_launch_overrides`]).
pub fn server_command_paths<'a>(
servers: impl IntoIterator<Item = &'a LanguageServerConfig>,
root: &Path,
s: &Settings,
) -> Vec<(String, PathBuf)> {
servers
.into_iter()
.filter_map(|server| {
let command = s.language_servers.get(&server.name)?.command.trim();
let path = command.contains(['/', '\\']).then(|| setting_path(root, command))??;
Some((server.command.clone(), path))
})
.collect()
}
/// Adds the servers defined in settings to the languages they serve (to list
/// them with the tools); disabled ones stay so they can be turned back on.
pub fn with_custom_servers(langs: &mut [LanguageConfig], s: &Settings) {
+58 -1
View File
@@ -1,7 +1,8 @@
use crate::apply::{
apply_to_run_configs, apply_to_run_configs_in, configure_python_extra_paths, configure_server,
configure_vue_module_resolution, jdtls_jdk, maven_command, maven_options, project_jdk, python_interpreter,
ruff_configuration, terminal_activation, tool_install_environment, tsdk, without_derived, without_derived_in,
ruff_configuration, server_command_paths, server_protocol_overrides, terminal_activation, tool_install_environment,
tsdk, without_derived, without_derived_in,
};
use crate::python::parse_pip_list;
use crate::{bin_dir, exe, venv_of};
@@ -340,6 +341,39 @@ fn jdtls_honors_the_configured_maven_local_repository() {
assert!(cfg.args.iter().all(|a| !a.contains("maven.repo.local")));
}
#[test]
fn java_analyzer_gets_the_project_jdk_and_maven_local_repository() {
let tmp = tempfile::tempdir().unwrap();
let configured = |s: &Settings| {
let mut cfg = server("java-analyzer");
configure_server(&mut cfg, tmp.path(), tmp.path(), s);
server_protocol_overrides(&mut cfg, s);
cfg.initialization_options.map(|v| serde_json::to_value(v).unwrap())
};
// Unset: the server discovers both itself.
assert_eq!(configured(&Settings::default()), None);
let jdk = tmp.path().join("jdk17");
fake_jdk(&jdk);
let repo = tmp.path().join("m2-repo");
let mut s = Settings::default();
s.languages.java.jdk = jdk.to_string_lossy().into_owned();
s.languages.java.maven.local_repository = repo.to_string_lossy().into_owned();
let options = configured(&s).unwrap();
assert_eq!(options["jdkHome"], jdk.canonicalize().unwrap().to_string_lossy().as_ref());
assert_eq!(options["maven"]["localRepository"], repo.to_string_lossy().as_ref());
// A `jdkHome` in the server's own settings wins.
s.language_servers.insert(
"java-analyzer".into(),
ide_core::settings::LanguageServerSettings {
initialization_options: Some(serde_json::json!({ "jdkHome": "/opt/jdk21" })),
..Default::default()
},
);
let options = configured(&s).unwrap();
assert_eq!(options["jdkHome"], "/opt/jdk21");
assert_eq!(options["maven"]["localRepository"], repo.to_string_lossy().as_ref());
}
#[test]
fn jdtls_m2e_gets_effective_settings_with_local_repository() {
fn user_settings(cfg: &LanguageServerConfig) -> Option<std::path::PathBuf> {
@@ -2962,3 +2996,26 @@ fn without_the_vue_plugin_the_module_resolution_plugin_stays_out() {
assert!(!configure_vue_module_resolution(&mut cfg, tmp.path()));
assert!(cfg.initialization_options.is_none());
}
#[test]
fn server_command_paths_report_executables_set_for_built_in_servers() {
let servers = [server("java-analyzer"), server("jdtls"), server("kmp-lsp")];
let mut s = Settings::default();
let entry = |command: &str| ide_core::settings::LanguageServerSettings {
command: command.into(),
languages: vec!["java".into()],
..Default::default()
};
s.language_servers.insert("java-analyzer".into(), entry("/opt/java-analyzer/bin/java-analyzer"));
s.language_servers.insert("jdtls".into(), entry("bin/jdtls"));
// A bare name is looked up like the definition's command.
s.language_servers.insert("kmp-lsp".into(), entry("kmp-lsp-nightly"));
let root = Path::new("/work");
assert_eq!(
server_command_paths(&servers, root, &s),
[
("java-analyzer".to_owned(), "/opt/java-analyzer/bin/java-analyzer".into()),
("jdtls".to_owned(), root.join("bin/jdtls")),
]
);
}
+25
View File
@@ -222,6 +222,31 @@ mod tests {
assert_eq!(select_asset(&names[5..8], rules, WIN), None);
}
#[test]
fn java_analyzer_assets() {
let names = [
"java-analyzer-macos-aarch64.gz",
"java-analyzer-macos-x86_64.gz",
"java-analyzer-linux-aarch64.gz",
"java-analyzer-linux-x86_64.gz",
"java-analyzer-windows-aarch64.zip",
"java-analyzer-windows-x86_64.zip",
"java-analyzer-macos-aarch64.gz.sha256",
"java-analyzer-windows-x86_64.zip.sha256",
];
let rules = crate::registry::find("java-analyzer").unwrap().asset_rules();
for (os, arch, expected) in [
(Os::Mac, Arch::Arm64, names[0]),
(Os::Mac, Arch::X64, names[1]),
(Os::Linux, Arch::Arm64, names[2]),
(Os::Linux, Arch::X64, names[3]),
(Os::Windows, Arch::Arm64, names[4]),
(Os::Windows, Arch::X64, names[5]),
] {
assert_eq!(select_asset(&names, rules, Platform { os, arch }), Some(expected));
}
}
#[test]
fn rosetta_fallback_and_missing() {
let names = ["zls-x86_64-macos.tar.xz", "zls-x86_64-linux.tar.xz", "zls-x86_64-windows.zip"];
+22
View File
@@ -793,6 +793,28 @@ pub static TOOLS: &[ToolSpec] = &[
homepage: "https://github.com/eclipse-jdtls/eclipse.jdt.ls",
needs: &[],
},
ToolSpec {
name: "java-analyzer",
display: "java-analyzer",
kind: LanguageServer,
commands: &["java-analyzer"],
method: Method::Release {
source: ReleaseSource::GitHub { repo: "l0ng-ai/java-analyzer" },
assets: &[
rule(Mac, Arm64, "java-analyzer-macos-aarch64.gz"),
rule(Mac, X64, "java-analyzer-macos-x86_64.gz"),
rule(Linux, Arm64, "java-analyzer-linux-aarch64.gz"),
rule(Linux, X64, "java-analyzer-linux-x86_64.gz"),
rule(Windows, Arm64, "java-analyzer-windows-aarch64.zip"),
rule(Windows, X64, "java-analyzer-windows-x86_64.zip"),
],
locate: &["java-analyzer"],
},
fallback: None,
version: ver("java-analyzer"),
homepage: "https://github.com/l0ng-ai/java-analyzer",
needs: &[],
},
ToolSpec {
name: "kmp-lsp",
display: "kmp-lsp",
+2 -2
View File
@@ -193,8 +193,8 @@ fn interchangeable_servers_list_their_alternatives() {
assert_eq!(find(&list, name).alternatives, python);
}
assert_eq!(find(&list, "vtsls").alternatives, ["vtsls", "typescript-language-server"]);
for name in ["jdtls", "kmp-lsp"] {
assert_eq!(find(&list, name).alternatives, ["jdtls", "kmp-lsp"]);
for name in ["jdtls", "java-analyzer", "kmp-lsp"] {
assert_eq!(find(&list, name).alternatives, ["jdtls", "java-analyzer", "kmp-lsp"]);
assert_eq!(find(&list, name).languages, if name == "kmp-lsp" { &["Java", "Kotlin"][..] } else { &["Java"] });
}
for name in ["kotlin-lsp", "kotlin-language-server"] {
+1 -1
View File
@@ -128,7 +128,7 @@ The table lists every built-in language definition. Server and debugger names in
| HTTP Request | `http` | `.http`, `.rest` | Yes | — | — | — |
| Ignore File | `ignore` | `.gitignore`, `.dockerignore` | — | — | — | — |
| INI | `ini` | `.ini`, `.cfg`, `.conf`, `.cnf`, `.inf`, … | Yes | — | — | — |
| Java | `java` | `.java` | Yes | Yes | **jdtls** → **kmp-lsp** | **java-debug** |
| Java | `java` | `.java` | Yes | Yes | **jdtls** → **java-analyzer** → **kmp-lsp** | **java-debug** |
| JavaScript | `javascript` | `.js`, `.mjs`, `.cjs` | Yes | Yes | **vtsls** → **typescript-language-server** + **oxlint** | **js-debug** |
| JavaScript JSX | `javascriptreact` | `.jsx` | Yes | Yes | **vtsls** → **typescript-language-server** + **oxlint** | **js-debug** |
| JSON | `json` | `.json`, `.geojson`, `.topojson`, `.webmanifest`, `.har`, … | Yes | Yes | **vscode-json-language-server** | — |
+10 -7
View File
@@ -7,15 +7,17 @@ Java code insight comes from the Eclipse JDT Language Server (jdtls), extended w
## Language servers
| Language | Server | Fallback | Debugger |
| ---------------------- | ----------------------------------------- | ------------------------------------------------------------------ | --------------------------------------- |
| Java | Eclipse JDT Language Server (`jdtls`) | `kmp-lsp` | Microsoft java-debug, loaded into jdtls |
| Kotlin | Kotlin LSP (JetBrains, `kotlin-lsp`) | Kotlin Language Server (fwcd, `kotlin-language-server`), `kmp-lsp` | Kotlin Debug Adapter (fwcd) |
| Groovy, Gradle scripts | `groovy-language-server` (you provide it) | | |
| JSP | none (highlighting only) | | |
| Language | Server | Fallback | Debugger |
| ---------------------- | ----------------------------------------- | ------------------------------------------------------------------ | ------------------------------------------------------------------------------- |
| Java | Eclipse JDT Language Server (`jdtls`) | `java-analyzer`, `kmp-lsp` | Microsoft java-debug, loaded into jdtls; `java-analyzer dap` with java-analyzer |
| Kotlin | Kotlin LSP (JetBrains, `kotlin-lsp`) | Kotlin Language Server (fwcd, `kotlin-language-server`), `kmp-lsp` | Kotlin Debug Adapter (fwcd) |
| Groovy, Gradle scripts | `groovy-language-server` (you provide it) | | |
| JSP | none (highlighting only) | | |
A fallback starts only while the server listed before it is not installed. To pick one explicitly, open **Settings › Languages & Frameworks › Language Servers**, select either server and use **Language server for Java** (or Kotlin): **Automatic** uses the first one available, **None** turns code insight off. The choice can be global or for the current project.
`java-analyzer` is a native Java language server that needs no JVM to run. It imports Maven and Gradle projects itself (or a `java-analyzer.toml`), and also serves Run and Debug: Application configurations take their classpath from it, and debugging uses its own adapter (`java-analyzer dap`). OpenIDEA passes it the project JDK and the Maven local repository when they are set; otherwise it finds them itself.
`kmp-lsp` gives syntax-based Java code insight without running a JVM. It has no JDT compiler analysis, no Lombok support and none of the JDT refactorings. Building and running Java Application configurations, classpath discovery and the Java debugger need jdtls.
### What ships with the IDE
@@ -43,6 +45,7 @@ When you open a Java or Kotlin file and its server is missing, a notification of
| Kotlin LSP (JetBrains) | GitHub release of `Kotlin/kotlin-lsp` |
| Kotlin Language Server (fwcd) | GitHub release of `fwcd/kotlin-language-server`; needs Java |
| Kotlin Debug Adapter | GitHub release of `fwcd/kotlin-debug-adapter`; needs Java |
| java-analyzer | GitHub release of `l0ng-ai/java-analyzer` |
| kmp-lsp | GitHub release of `Hessesian/kmp-lsp` |
A `jdtls` already on your `PATH` (Homebrew, Scoop or a release archive) is used too. OpenIDEA starts it on the JVM directly instead of through its Python launcher script, so Python is not required. When you start a debug session without java-debug, the IDE offers to install it and restarts jdtls with the bundle.
@@ -140,7 +143,7 @@ Navigation between MyBatis mapper interfaces and their XML statements is availab
## Limitations
- One jdtls runs per project window. With several large projects open at once, memory adds up. Lower `-Xmx` in **JVM options** or close windows you are not using. **Share JDT LS across builds** helps only within one window.
- Without jdtls (for example with `kmp-lsp` only), Application configurations, classpath-based features and Java debugging are not available.
- With `kmp-lsp` only (no jdtls or java-analyzer), Application configurations, classpath-based features and Java debugging are not available.
- Java tests run through Maven or Gradle. A project without either build tool has no test runner.
- The jdtls compiler (ECJ) is stricter than javac in some places. Known case: a generic method call used directly as an `if`, `while`, `for` or `do` condition or a ternary condition — for example `if (getCacheObject(key))` with a `<T> T getCacheObject(String key)` method — compiles with javac but ECJ reports `Type mismatch: cannot convert from Object to boolean`. Assigning the call to a `boolean` variable first compiles on both (see [#2624](https://github.com/l0ng-ai/openidea/issues/2624)).
- The Kotlin bridge is Maven only and exposes compiled Kotlin classes to Java. It does not give Java-to-Kotlin source navigation or refactoring.
+1 -1
View File
@@ -123,7 +123,7 @@ IDE 安装工具从不需要 `sudo`。工具装在本地数据目录下 IDE 自
| HTTP Request | `http` | `.http`, `.rest` | 是 | — | — | — |
| Ignore File | `ignore` | `.gitignore`, `.dockerignore` | — | — | — | — |
| INI | `ini` | `.ini`, `.cfg`, `.conf`, `.cnf`, `.inf`, … | 是 | — | — | — |
| Java | `java` | `.java` | 是 | 是 | **jdtls** → **kmp-lsp** | **java-debug** |
| Java | `java` | `.java` | 是 | 是 | **jdtls** → **java-analyzer** → **kmp-lsp** | **java-debug** |
| JavaScript | `javascript` | `.js`, `.mjs`, `.cjs` | 是 | 是 | **vtsls** → **typescript-language-server** + **oxlint** | **js-debug** |
| JavaScript JSX | `javascriptreact` | `.jsx` | 是 | 是 | **vtsls** → **typescript-language-server** + **oxlint** | **js-debug** |
| JSON | `json` | `.json`, `.geojson`, `.topojson`, `.webmanifest`, `.har`, … | 是 | 是 | **vscode-json-language-server** | — |
+10 -7
View File
@@ -7,15 +7,17 @@ Java 的代码智能由 Eclipse JDT Language Server(jdtls)提供,OpenIDEA
## 语言服务器
| 语言 | 服务器 | 备选 | 调试器 |
| ------------------- | -------------------------------------- | ------------------------------------------------------------------- | ------------------------------------- |
| Java | Eclipse JDT Language Server(`jdtls`) | `kmp-lsp` | Microsoft java-debug,加载在 jdtls 内 |
| Kotlin | Kotlin LSP(JetBrains,`kotlin-lsp`) | Kotlin Language Server(fwcd,`kotlin-language-server`)、`kmp-lsp` | Kotlin Debug Adapter(fwcd) |
| Groovy、Gradle 脚本 | `groovy-language-server`(需自行提供) | | |
| JSP | 无(仅语法高亮) | | |
| 语言 | 服务器 | 备选 | 调试器 |
| ------------------- | -------------------------------------- | ------------------------------------------------------------------- | -------------------------------------------------------------------------------- |
| Java | Eclipse JDT Language Server(`jdtls`) | `java-analyzer`、`kmp-lsp` | Microsoft java-debug,加载在 jdtls 内;用 java-analyzer 时为 `java-analyzer dap` |
| Kotlin | Kotlin LSP(JetBrains,`kotlin-lsp`) | Kotlin Language Server(fwcd,`kotlin-language-server`)、`kmp-lsp` | Kotlin Debug Adapter(fwcd) |
| Groovy、Gradle 脚本 | `groovy-language-server`(需自行提供) | | |
| JSP | 无(仅语法高亮) | | |
备选服务器只在排在它前面的服务器未安装时才会启动。想手动指定,打开 **设置 › 语言和框架 › 语言服务器**,选中其中一个服务器,在 **Java 语言服务器**(或 Kotlin 语言服务器)里选择:**自动** 使用第一个可用的,**无** 关闭代码智能。这个选择可以全局生效,也可以只对当前项目生效。
`java-analyzer` 是原生的 Java 语言服务器,运行时不需要 JVM。它自己导入 Maven 和 Gradle 项目(或 `java-analyzer.toml`),也支撑运行和调试:“应用程序”配置从它获取类路径,调试使用它自带的调试适配器(`java-analyzer dap`)。设置了项目 JDK 和 Maven 本地仓库时,OpenIDEA 会把它们传给 java-analyzer,否则它自己查找。
`kmp-lsp` 不运行 JVM,提供基于语法的 Java 代码智能,没有 JDT 的编译分析、Lombok 支持和 JDT 的重构。构建和运行 Java “应用程序”配置、类路径发现以及 Java 调试器都需要 jdtls。
### 随 IDE 提供的部分
@@ -43,6 +45,7 @@ Java 的代码智能由 Eclipse JDT Language Server(jdtls)提供,OpenIDEA
| Kotlin LSP(JetBrains) | `Kotlin/kotlin-lsp` 的 GitHub release |
| Kotlin Language Server(fwcd) | `fwcd/kotlin-language-server` 的 GitHub release,需要 Java |
| Kotlin Debug Adapter | `fwcd/kotlin-debug-adapter` 的 GitHub release,需要 Java |
| java-analyzer | `l0ng-ai/java-analyzer` 的 GitHub release |
| kmp-lsp | `Hessesian/kmp-lsp` 的 GitHub release |
`PATH` 上已有的 `jdtls`(Homebrew、Scoop 或 release 压缩包)也能直接用。OpenIDEA 直接在 JVM 上启动它,不经过它自带的 Python 启动脚本,所以不需要装 Python。没装 java-debug 就开始调试时,IDE 会提示安装,并带着这个 bundle 重启 jdtls。
@@ -140,7 +143,7 @@ MyBatis mapper 接口与 XML 语句之间的跳转由 MyBatis Navigator 插件
## 已知限制
- 每个项目窗口运行一个 jdtls,同时打开多个大项目时内存会叠加。可以在 **JVM 选项** 中调低 `-Xmx`,或关闭暂时不用的窗口。**在构建间共享 JDT LS** 只在单个窗口内有效。
- 没有 jdtls 时(例如只用 `kmp-lsp`),“应用程序”配置、依赖类路径的功能和 Java 调试都不可用。
- 只用 `kmp-lsp`(没有 jdtls 或 java-analyzer)时,“应用程序”配置、依赖类路径的功能和 Java 调试都不可用。
- Java 测试通过 Maven 或 Gradle 运行,两者都没有的项目没有测试运行器。
- jdtls 自带的 ECJ 编译器有些地方比 javac 严格。已知例子:把泛型方法调用直接用作 `if`、`while`、`for`、`do` 条件或三元运算符条件——例如 `<T> T getCacheObject(String key)` 方法配合 `if (getCacheObject(key))`——javac 可以编译,ECJ 则报 `Type mismatch: cannot convert from Object to boolean`。先把调用赋值给 `boolean` 变量即可两边都编译通过(见 [#2624](https://github.com/l0ng-ai/openidea/issues/2624))。
- Kotlin 桥接只支持 Maven,只把编译后的 Kotlin 类暴露给 Java,不提供从 Java 到 Kotlin 源码的跳转或重构。
+9
View File
@@ -70,6 +70,15 @@ exclusions = ["**/node_modules/**", "**/.metadata/**", "**/archetype-resources/*
maven = { enabled = true }
gradle = { enabled = true, wrapper = { enabled = true } }
# java-analyzer: a native Java server that imports Maven / Gradle builds itself and
# also serves debugging (`java-analyzer dap`) and the run classpath without jdtls.
[[language-servers]]
name = "java-analyzer"
fallback = true
command = "java-analyzer"
root-markers = ["java-analyzer.toml", "pom.xml", "build.gradle", "build.gradle.kts", "settings.gradle", "settings.gradle.kts", "build.xml", ".project"]
install-hint = "Install java-analyzer in Settings › Languages & Frameworks › Language Servers (https://github.com/l0ng-ai/java-analyzer)"
[[language-servers]]
name = "kmp-lsp"
fallback = true