Skip to content

Commit 28628cb

Browse files
authored
feat(backend): 语言服务器由后端启动,运行/调试/测试经宿主通用 API 接管
2 parents 6c5a3b3 + 53550fc commit 28628cb

15 files changed

Lines changed: 3140 additions & 318 deletions

File tree

‎MIGRATION.md‎

Lines changed: 87 additions & 26 deletions
Large diffs are not rendered by default.

‎README.md‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@
1414
1515
> **进行中**:本仓库正在从 Lithe 主仓库拆分出来,目标是 Lithe 宿主(Rust Core 和 macOS 应用)不再包含任何 Java 专属逻辑,
1616
> Java 的领域规则全部由本插件提供,宿主只提供通用插件接口。插件现在带一个 Rust 后端:`plugin.json` 声明了 `backend` 2,
17-
> 后端目前承担 Maven/Gradle 导入设置的中转(见“工作方式”)。语言服务器仍然由宿主的 JDT 集成启动
18-
> (`launch.kind` 为 `runtime`,带 `hostIntegration`);改为后端启动、去掉 `hostIntegration`、由插件接管运行/调试/测试,
19-
> 要等宿主提供 API-23(`toolchains.list`),清单见 `MIGRATION.md` 第 6 节和第 10 节。在那之前 Lithe 仍然内置 Java Support
20-
> (旧 ID `dev.lithe.plugin.java-support`),本仓库发布的版本标为预发布,原因见 `MIGRATION.md` 第 9 节。
17+
> 后端承担 Maven/Gradle 导入设置的中转(见“工作方式”),并解析语言服务器的启动:`plugin.json` 的 `languageServers[0].launch`
18+
> 是 `kind: "backend"`、函数 `java.resolveLaunch`(`serverProcess` 3),不再有 `hostIntegration`;后端在 `backend/src/launch.rs`
19+
> 里定位包内的 Equinox 启动器、按架构的配置目录、Lombok 代理、java-debug 与 java-test 扩展包和随包 JDK,拼出 JDT LS 的命令行、
20+
> 初始化选项和 `language/status` 就绪门。由插件接管运行/调试/测试还在进行,清单见 `MIGRATION.md` 第 6 节和第 10 节。
21+
> 在宿主去掉内置 Java Support(旧 ID `dev.lithe.plugin.java-support`)之前,本仓库发布的版本标为预发布,原因见 `MIGRATION.md` 第 9 节。
2122
> Maven Archetype 生成器、Maven 与 Gradle 的项目模板已经移交 lithe-plugin-maven 和 lithe-plugin-gradle;
2223
> 在这两个插件接手模板之前不要发布本插件的新版本,否则“新建项目”会少掉 Maven 和 Gradle 选项。
2324
@@ -26,8 +27,8 @@
2627
| 功能 | 说明 |
2728
| --- | --- |
2829
| 语言服务 | 用打包的 JDT LS(含 Lombok)提供补全、诊断、导航;JDK 随包提供,只用来运行语言服务器,项目自己的 SDK 仍由用户选择 |
29-
| 运行与调试入口 | 识别 `main` 方法和测试方法,生成运行/调试标记 |
30-
| 测试 | 发现 JUnit 测试项,生成测试选择器,并驱动 java-debug 与 vscode-java-test 运行器 |
30+
| 运行与调试 | 在 `main` 方法和测试旁显示 Run/Debug 行内动作;点击后后端经 JDT LS 构建并解析类路径,用用户选择的 JDK 通过宿主 Run 窗口启动,调试经 java-debug 连接;控制台里的堆栈行可点击跳转 |
31+
| 测试 | 发现 JUnit/TestNG 测试项,按类或方法启动 Eclipse JUnit 运行器(TestNG 用 vscode-java-test 运行器),运行结束后显示通过/失败计数 |
3132
| Spring Boot / MyBatis | 索引 Spring 端点与 MyBatis 映射,支持跳转 |
3233
| 新建项目 | 两个声明式项目生成器:Java(无构建系统,可选示例代码)、Spring Boot;Maven 原型和 Maven/Gradle 项目模板由 Maven、Gradle 插件提供 |
3334
| Maven / Gradle 工程 | 导入设置(settings 文件、本地仓库、offline、Gradle user home 等)由 Maven、Gradle 插件通过 `languageImport.settings` 提供,Java 不计算也不解析它们;后端收下后推给 JDT LS,Maven profiles 变化时触发 `java.project.updateSettings` |
@@ -54,6 +55,9 @@
5455
- 当前不推送 `java.configuration.runtimes`:插件后端还没有列出 JDK 安装的宿主方法,等 API-23(`toolchains.list`)提供。JDT LS 不会因为缺这个键而删除已登记的 VM。
5556

5657
细节见 `MIGRATION.md` 第 5 节。
58+
- **运行/调试/测试**在 `backend/src/run.rs`:用宿主的编辑器动作(`editor/registerActions`)提供行内 Run/Debug 标记,
59+
用 `run/start` 启动进程(进程用清单 `requiresTools: ["java"]` 引用的 JDK 工具定义),测试结果由后端自己在回环端口上监听
60+
JUnit 运行器并用窗口消息显示,输出链接匹配器把堆栈行解析成工作区文件。细节见 `MIGRATION.md` 第 5.9 节。
5761
- **运行时只读**:插件包安装后在运行时只读。JDT LS、Lombok、java-debug、测试运行器、JDK 和后端二进制都在构建阶段固定来源、校验哈希(或由锁定的源码编译)后放进包里,
5862
运行时状态只写宿主提供的状态目录,不写包目录。
5963

‎backend/src/launch.rs‎

Lines changed: 1122 additions & 0 deletions
Large diffs are not rendered by default.

‎backend/src/lib.rs‎

Lines changed: 109 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,18 @@
66
//! other plugins contribute. The plugin manifest declares it as the plugin's
77
//! `backend`.
88
//!
9-
//! The Java rules themselves still run in the host for the editor providers,
10-
//! run configurations and the Java test workflow; the backend is where they
11-
//! move, one host API at a time (see `MIGRATION.md`).
9+
//! The run, debug and test actions of Java files are the backend's own as
10+
//! well ([`run`]): it collects them through editor actions and starts the
11+
//! processes through the host's `run/start` method. What still waits for a
12+
//! host API is listed in `MIGRATION.md`.
13+
//!
14+
//! # Language server launch (serverProcess 3)
15+
//!
16+
//! The manifest declares the `java` language server with
17+
//! `launch.kind: "backend"`, so the host asks [`launch`] for the JDT LS
18+
//! command line, initialization options and startup settings before each
19+
//! start. The launch reads the settings the [`settings_session`] knows for
20+
//! the workspace, so a restart starts with the last pushed settings.
1221
//!
1322
//! # Cross-plugin settings (API-35)
1423
//!
@@ -38,9 +47,16 @@
3847
//! `settings_session::SESSION_CATEGORIES`. The backend never writes to
3948
//! stderr, and a fact carries no path, source or tool output.
4049
50+
pub mod launch;
4151
pub mod plugin_functions;
52+
pub mod run;
4253
pub mod settings_session;
4354

55+
use crate::launch::LAUNCH_FUNCTION_ID;
56+
use crate::run::{
57+
RunSession, COLLECT_FUNCTION_ID, LANGUAGE_SERVER_NOTIFICATION, RERUN_FUNCTION_ID,
58+
RESOLVE_LINK_FUNCTION_ID, RUN_ACTION_FUNCTION_ID, RUN_DID_EXIT_NOTIFICATION,
59+
};
4460
use crate::settings_session::{
4561
HostServices, SettingsSession, LANGUAGE_SERVER_STATE_NOTIFICATION, SETTINGS_CHANGED_FUNCTION_ID,
4662
};
@@ -66,10 +82,19 @@ const NOTIFICATION_OPERATION: &str = "notification";
6682
/// `languageImport.settingsChanged` is a key operation: another plugin
6783
/// reports a change a user made, it happens rarely, and its success is the
6884
/// only trace that the change reached the language server.
85+
/// `java.resolveLaunch` is one too: it runs once per language server start
86+
/// and its success is the only trace of which package files the server
87+
/// started from. `java.runAction` and `java.rerun` are key operations
88+
/// because each is one user click that starts a process; collection and
89+
/// output link resolution run on every edit and every output line, so their
90+
/// successes are suppressed.
6991
pub const KEY_OPERATIONS: &[&str] = &[
7092
"debug.javaTestLaunch",
7193
"execution.planLaunchCommand",
7294
"java.prepareLaunch",
95+
LAUNCH_FUNCTION_ID,
96+
RERUN_FUNCTION_ID,
97+
RUN_ACTION_FUNCTION_ID,
7398
SETTINGS_CHANGED_FUNCTION_ID,
7499
"runConfig.selectJava",
75100
];
@@ -78,8 +103,9 @@ pub const KEY_OPERATIONS: &[&str] = &[
78103
///
79104
/// A function can only be suppressed once it is registered, so the successes
80105
/// of the functions outside [`KEY_OPERATIONS`] are suppressed after the
81-
/// registration loop. `languageImport.settingsChanged` is registered after
82-
/// that loop and is a key operation, so it needs no suppression.
106+
/// registration loop. `languageImport.settingsChanged` and
107+
/// `java.resolveLaunch` are registered after that loop and are key
108+
/// operations, so they need no suppression.
83109
///
84110
/// Every input is a compile-time constant, so the result is the same on each
85111
/// run: it fails only when a function ID is invalid, repeated or over the
@@ -120,9 +146,69 @@ pub fn backend() -> Result<Backend, SdkError> {
120146
Ok(())
121147
}
122148
})?;
123-
backend.function(SETTINGS_CHANGED_FUNCTION_ID, move |params, context| {
124-
settings_changed(&session, params, context)
149+
backend.function(SETTINGS_CHANGED_FUNCTION_ID, {
150+
let session = Arc::clone(&session);
151+
move |params, context| settings_changed(&session, params, context)
152+
})?;
153+
backend.function(LAUNCH_FUNCTION_ID, move |params, context| {
154+
launch::resolve_launch_function(&session, params, context)
155+
})?;
156+
157+
// The run session registers the editor actions of every workspace context
158+
// and keeps the result listener of each test run; both happen outside a
159+
// function call, so it holds the backend handle too.
160+
let run_session = Arc::new(RunSession::new(backend.handle()));
161+
let report: run::Report = {
162+
let handle = backend.handle();
163+
Arc::new(move |code: &str, count: u64| {
164+
let _ = handle
165+
.diagnostics()
166+
.degraded(NOTIFICATION_OPERATION, code, Some(count));
167+
})
168+
};
169+
backend.on_initialize({
170+
let run_session = Arc::clone(&run_session);
171+
let report = Arc::clone(&report);
172+
move |_initialize| {
173+
run_session.sync_after_initialize(Arc::clone(&report));
174+
Ok(())
175+
}
176+
});
177+
backend.on_contexts_changed({
178+
let run_session = Arc::clone(&run_session);
179+
let report = Arc::clone(&report);
180+
move |_params| {
181+
run_session.sync_contexts_reporting(&*report);
182+
Ok(())
183+
}
184+
});
185+
backend.on_host_notification(RUN_DID_EXIT_NOTIFICATION, {
186+
let run_session = Arc::clone(&run_session);
187+
move |params| {
188+
run_session.run_did_exit(&params);
189+
Ok(())
190+
}
191+
})?;
192+
backend.on_host_notification(LANGUAGE_SERVER_NOTIFICATION, {
193+
let run_session = Arc::clone(&run_session);
194+
let report = Arc::clone(&report);
195+
move |params| {
196+
run_session.did_receive_notification(&params, &*report);
197+
Ok(())
198+
}
199+
})?;
200+
backend.function(COLLECT_FUNCTION_ID, run::collect_actions_function)?;
201+
backend.suppress_success_diagnostics(COLLECT_FUNCTION_ID)?;
202+
backend.function(RUN_ACTION_FUNCTION_ID, {
203+
let run_session = Arc::clone(&run_session);
204+
move |params, context| run::run_action_function(&run_session, params, context)
205+
})?;
206+
backend.function(RERUN_FUNCTION_ID, {
207+
let run_session = Arc::clone(&run_session);
208+
move |params, context| run::rerun_function(&run_session, params, context)
125209
})?;
210+
backend.function(RESOLVE_LINK_FUNCTION_ID, run::resolve_output_link_function)?;
211+
backend.suppress_success_diagnostics(RESOLVE_LINK_FUNCTION_ID)?;
126212
Ok(backend)
127213
}
128214

@@ -263,9 +349,9 @@ mod tests {
263349
.all(|code| lithe_plugin_sdk::is_plugin_error_code(code)));
264350
}
265351

266-
/// An error code names one cause. The codes `settingsChanged` answers
267-
/// with, the domain codes and the diagnostics categories of the settings
268-
/// modules must never share a name.
352+
/// An error code names one cause. The codes `settingsChanged` and
353+
/// `java.resolveLaunch` answer with, the domain codes and the diagnostics
354+
/// categories of the settings and launch modules must never share a name.
269355
#[test]
270356
fn settings_relay_codes_never_share_a_name_with_another_code() {
271357
let domain: Vec<&str> = [
@@ -290,6 +376,10 @@ mod tests {
290376
.iter()
291377
.chain(settings_session::SESSION_CATEGORIES)
292378
.chain(plugin_functions::RELAY_CATEGORIES)
379+
.chain(launch::LAUNCH_ERROR_CODES)
380+
.chain(launch::LAUNCH_CATEGORIES)
381+
.chain(run::RUN_ERROR_CODES)
382+
.chain(run::RUN_CATEGORIES)
293383
{
294384
assert!(seen.insert(*name), "{name} names two causes");
295385
}
@@ -309,12 +399,19 @@ mod tests {
309399

310400
#[test]
311401
fn key_operations_are_registered_functions_without_duplicates() {
312-
// The commands, and the one function registered next to them.
402+
// The commands, and the functions registered next to them.
313403
let commands: std::collections::BTreeSet<&str> = SUPPORTED_COMMANDS
314404
.iter()
315405
.chain(SESSION_COMMANDS)
316406
.copied()
317-
.chain([SETTINGS_CHANGED_FUNCTION_ID])
407+
.chain([
408+
SETTINGS_CHANGED_FUNCTION_ID,
409+
LAUNCH_FUNCTION_ID,
410+
COLLECT_FUNCTION_ID,
411+
RUN_ACTION_FUNCTION_ID,
412+
RERUN_FUNCTION_ID,
413+
RESOLVE_LINK_FUNCTION_ID,
414+
])
318415
.collect();
319416
let distinct: std::collections::BTreeSet<&str> = KEY_OPERATIONS.iter().copied().collect();
320417
assert_eq!(

0 commit comments

Comments
 (0)