From b298313b67cd130a8a47a9e116752b103bee3de5 Mon Sep 17 00:00:00 2001 From: l0ng-ai <24760907+l0ng-ai@users.noreply.github.com> Date: Sun, 15 Nov 2026 23:39:33 +0800 Subject: [PATCH] fix(lsp): highlight a library class the Kotlin server shows as Java as Java A virtual `.class` document has no language by its name, so it took its server's first language. The Kotlin language server shows classes from Java jars as Java stubs, which were then highlighted as Kotlin: Java keywords such as `public` and `private` stayed uncoloured. A `.class` stub whose package or import line ends with `;` is now Java. --- crates/ide-lsp/src/lib.rs | 5 ++++- crates/ide-lsp/src/virtual_doc.rs | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/crates/ide-lsp/src/lib.rs b/crates/ide-lsp/src/lib.rs index 1d05b417fe..aa0c2ce674 100644 --- a/crates/ide-lsp/src/lib.rs +++ b/crates/ide-lsp/src/lib.rs @@ -3469,7 +3469,10 @@ impl LspManager { virtual_doc::is_decompiled(&text) }; let title = title.unwrap_or_else(|| virtual_doc::title(uri, decompiled)); - let detected = self.languages.detect(Path::new(&title), None); + let mut detected = self.languages.detect(Path::new(&title), None); + if let Some(stem) = title.strip_suffix(".class").filter(|_| virtual_doc::is_java_class_stub(&text)) { + detected = self.languages.detect(Path::new(&format!("{stem}.java")), None); + } let language_id = if detected == LanguageId::plain_text() { languages.first().cloned().unwrap_or(detected) } else { diff --git a/crates/ide-lsp/src/virtual_doc.rs b/crates/ide-lsp/src/virtual_doc.rs index 619a7aee3c..42aa52b1b6 100644 --- a/crates/ide-lsp/src/virtual_doc.rs +++ b/crates/ide-lsp/src/virtual_doc.rs @@ -110,6 +110,17 @@ pub fn is_decompiled(text: &str) -> bool { }) } +/// Whether a `.class` stub is written in Java: its first `package` or `import` line ends with `;`. +/// The Kotlin language server shows Java classes from jars as Java and Kotlin ones as Kotlin, under +/// the same `.class` name. +pub fn is_java_class_stub(text: &str) -> bool { + text.lines() + .take(200) + .map(str::trim) + .find(|line| line.starts_with("package ") || line.starts_with("import ")) + .is_some_and(|line| line.ends_with(';')) +} + /// `scheme` of a URI (`jdt` for `jdt://…`). pub fn scheme(uri: &str) -> &str { uri.split_once(':').map(|(s, _)| s).unwrap_or("") @@ -205,6 +216,17 @@ mod tests { const JDT: &str = "jdt://contents/spring-boot-4.1.1.jar/org.springframework.boot/SpringApplication.class?=spring-demo/%5C/Users%5C/me%5C/.m2%5C/repository%5C/spring-boot-4.1.1.jar%3Corg.springframework.boot%28SpringApplication.class"; + #[test] + fn class_stubs_in_java_or_kotlin() { + let java = "// IntelliJ API Decompiler stub source generated from a class file\n\npackage com.boydti.fawe.util;\n\npublic class EditSessionBuilder {\n}\n"; + let kotlin = "// IntelliJ API Decompiler stub source generated from a class file\n// Implementation of methods is not available\n\npackage kotlin.collections\n\npublic fun listOf(): List { /* compiled code */ }\n"; + let imports_only = "import java.util.List;\n\npublic class A {}\n"; + assert!(is_java_class_stub(java)); + assert!(is_java_class_stub(imports_only)); + assert!(!is_java_class_stub(kotlin)); + assert!(!is_java_class_stub("public class A {}")); + } + #[test] fn csharp_metadata_titles_and_kinds() { for (uri, expected) in [