Skip to content

feat(lsp-engine): keep hover answers so resting on a symbol again answers at once - #61

Merged
xiaoyumuxi merged 1 commit into
mainfrom
feat/1995-hover-cache
Nov 16, 2026
Merged

xiaoyumuxi merged 1 commit into
mainfrom
feat/1995-hover-cache

Conversation

@xiaoyumuxi

Copy link
Copy Markdown
Contributor

关联 Issue / Linked issue

#1995 的桌面端部分(指针离开文本后同一字符不再悬浮)在 1lck/Lithe-IDEA#2004 修复。本 PR 是 Core 部分:在同一个符号上再次停留时直接给出答案,不再等语言服务器。Lithe-IDEA 把 PINNED_CORE_REVISION 升到包含本 PR 的 commit 后,由那个 PR 关闭 #1995。

PR 类型 / Type

  • feat:新功能或用户可见的改进

改动说明 / What changes

背景 / Why

每次悬浮都会给语言服务器发一次 textDocument/hover。在同一个符号上移开再停回来,桌面端要再等 500 ms 的悬浮延迟,再加一次服务器往返;gopls 在大型仓库上忙于索引时,这次往返也会很慢。答案没有变,却要重新等一次。

本 PR 在 LSP 引擎里为每个会话保留悬浮答案。同一文档版本的同一位置,或答案 range 在同一行内覆盖的位置,直接用保留的答案完成请求,不再写给服务器。服务器可能知道新东西时,清空所有保留的答案。

改动 / Changes

新模块 rust/crates/lsp-engine/src/hover_cache.rs

  • 两个有名常量限定大小:
    • HOVER_CACHE_CAPACITY = 32:每个会话最多 32 个答案,先丢最久没用的(LRU,命中时移到队尾);
    • HOVER_CACHE_MAX_CONTENT_BYTES = 64 KiB:更长的答案照常交付,但不保留。
    • 所以一个会话的悬浮文本最多 2 MiB。
  • 条目的键是 URI + 文档版本 + 位置。答案的 range 是同一行上的非空范围、并且包含请求位置时,范围内的其他列也命中,和编辑器把悬浮窗留在整个符号上的做法一致。
    • 跨行范围不用于其他位置:它可能包住其他有自己悬浮的符号。
    • 不包含请求位置的范围不采用:它描述的是别的东西。
  • 不保留的答案:hover 为 null、contents 为空或只有空白、错误、超长,以及在缓存清空之后才到达的答案。
    • 空答案不保留,是因为服务器加载完项目后同一个位置可能就有答案了。
    • 每次清空都把 generation 加一。请求发出时记下 generation,答案到达时 generation 变了就不保留,因为服务器可能是按变化前的状态算出的答案。
  • server_message_clears_hovers(value):服务器发来的任何请求(刷新、编辑、注册能力、拉配置、创建进度 token 等),以及 $/progress 的 end,都表示服务器可能知道了新东西。report、begin 和 publishDiagnostics 这类通知不清空。

状态(state.rs、engine.rs)

  • SessionState.hover_cache:缓存属于会话状态,会话结束随状态一起释放。
  • PendingRequest.hover: Option<HoverRequest>:只有宿主自己的悬浮请求(PendingKind::Feature)带它,答案到达时据此保留。其他构造点(lifecycle.rs、plugin_server_calls.rs、operations.rs)填 None。

命中与保留(operations.rs、server_messages.rs)

  • request_with_kind:宿主的 Hover 请求在能力检查之后查缓存。文档必须是打开的、版本 > 0、请求带位置。
    • 命中时直接调用 push_request_event 推送 requestCompleted,不写消息、不建 pending。结果和服务器答案逐字节相同,method 不变。
    • 未命中时记下 HoverRequest。
    • 命中后再取消同一个操作,会和服务器很快答完时一样返回 InvalidRequest。
  • handle_server_message:Feature 响应没有错误、有结果、带 HoverRequest 时调用 store。

清空点(都在 outbound_order 锁下,与写给服务器的消息原子;锁顺序不变,仍是 indexing_roots → outbound_order → state)

触发 位置
文档打开或修改(内容未变的重复同步不清空) documents.rs sync
保存(只在真正发出 didSave 时;文本落后于已同步版本的延迟保存不发也不清空) documents.rs save
关闭 documents.rs close
工作区文件在磁盘上变化 documents.rs workspace_files_changed
索引排除项变化后推送配置 configuration.rs
插件设置变化(update_plugin_configuration) configuration.rs
运行 workspace/executeCommand operations.rs request_with_kind
后端 languageServer/request(方法名无法区分是不是会改状态的命令,所以一律清空) plugin_server_calls.rs register_plugin_server_request
后端 languageServer/notify operations.rs plugin_server_notify
宿主通知真正写给插件后端时(host_notify) operations.rs host_notify
服务器发来请求,或结束 $/progress server_messages.rs
会话停止、崩溃、重启 events.rs clear_runtime_state;重启是新会话,状态本来就是新的

文档

  • shared/plugins/contract/v1/README.md“Language servers”:写明宿主保留悬浮答案的规则、上限、不保留的答案、清空条件,以及悬浮因其他原因变化的服务器要用请求(例如刷新)或结束一项进度来告知。
  • shared/plugins/reference/domains/languages.md 中英两节各加一段,链接到契约。
  • shared/plugins/reference/protocol-changelog.md 时间线新增 2026-11-16 一行:languageServers 0 内的补充,没有新增方法、字段或能力版本。

测试

  • hover_cache.rs 单元测试:
    • 容量满时丢最久没用的;
    • 同一位置只保留一个、只用于自己的文档版本;
    • 清空后拒绝之前发出的请求的答案;
    • 只有服务器请求和 $/progress 结束会清空。
  • tests.rs 用脚本化服务器的会话测试(新增 harness 方法 request_at、answer_hover、hover_from_cache、hover_asks_server,后两个断言有没有新的 textDocument/hover 写给服务器):
    • a_kept_hover_answers_its_position_and_range_without_asking_again:命中时结果、method 和服务器答案相同;range 内的列命中,range 末尾和别的行重新请求。
    • a_range_over_several_lines_or_elsewhere_answers_only_the_requested_position
    • an_empty_failed_or_oversized_hover_is_asked_for_again:null、空串、只有空白、空数组、-32801 错误、超长答案都重新请求,超长答案仍然完整交付。
    • anything_that_can_change_what_the_server_knows_forgets_kept_hovers:逐个验证编辑另一个文档、保存、关闭后以同一版本重新打开、磁盘文件变化、运行命令、服务器发来请求、$/progress 结束都会清空。
    • progress_reports_and_other_documents_hovers_keep_kept_hovers:进度 report、重复同步相同文本、另一个文档的悬浮都不清空。
    • a_hover_answered_across_an_invalidation_is_delivered_but_not_kept
    • an_edit_to_the_hovered_document_asks_again_at_its_new_version

不在本 PR 范围内 / Out of scope


类型专属:feat

多平台对齐

  • 两端都具备:缓存在 Core 的 LSP 引擎里,macOS 和 Windows 共用;桌面端不需要任何改动,升级固定的 Core 修订即可。

范围检查 / Scope checks

  • 功能矩阵:Core PR 不改矩阵。悬浮行为在 Lithe-IDEA 的 lsp-completion-hover 条目,由 #2004 和随后升级 Core 修订的 PR 更新。
  • 插件归属:不涉及任何具体插件;测试只用脚本化的合成服务器和 file:///workspace/*.go 合成文档,没有按语言或服务器 ID 分支。
  • UI 验证 / i18n:N/A,没有界面和文案。
  • 统一日志系统:N/A。命中是高频路径,不记日志;请求终态仍是原来的 requestCompleted 事件。generate-log-events.mjs --check 通过。
  • 逻辑归属:全部在 Rust Core。
  • 性能与内存:
    • 没有新增线程、进程、轮询或定时器,没有 TTL。
    • 缓存有两个有名常量上限,每个会话最多 32 个答案、2 MiB 悬浮文本;会话结束时随状态释放,停止或崩溃时清空。
    • 命中时少一次服务器往返;未命中时只多一次最多 32 项的线性查找和一次 JSON 克隆。
  • 设置:N/A。
  • 插件契约:契约 README 的“Language servers”写明了宿主的这项行为,并登记了协议更新记录;没有新增宿主方法、通知、字段或能力版本,也没有改插件后端协议,所以不需要跑 sdk-e2e.mjs,也没有方法表。
  • 发行包与资源:N/A,缓存只在内存里,不写任何文件。
  • 无用代码:本次没有让任何代码变成无用。

验证 / Verification

本机为 Apple M4、macOS arm64。

命令或操作 平台 结果
cargo fmt --manifest-path rust/Cargo.toml --all -- --check macOS arm64 通过
node scripts/rust-core/affected-crates.mjs macOS arm64 选出 -p lithe-core -p lithe-lsp-engine
cargo test --locked --manifest-path rust/Cargo.toml -p lithe-core -p lithe-lsp-engine macOS arm64 1,020 个通过,0 个失败
cargo clippy --locked …(同上 2 个包)--all-targets -- -D warnings,以及 --all-features macOS arm64 通过
cargo clippy --locked …(同上 2 个包)--all-targets --target x86_64-pc-windows-msvc -- -D warnings macOS 交叉检查 Windows 目标 通过
scripts/rust-core/verify-rust-core-comments.sh lsp-engine macOS arm64 通过
node scripts/rust-core/verify-platform-tests.mjs macOS arm64 通过
node scripts/logging/generate-log-events.mjs --check macOS arm64 通过
node shared/plugins/scripts/verify-docs.mjs、node --test shared/plugins/scripts/*.test.mjs macOS arm64 通过
变异检查一:让 clear() 不清空条目、store() 忽略 generation macOS arm64 三个悬浮测试按预期失败,恢复后通过
变异检查二:去掉 handle_server_message 里的清空 macOS arm64 只有 anything_that_can_change_what_the_server_knows_forgets_kept_hovers 按预期失败(a request from the server must forget kept hovers),恢复后通过

未能执行的检查和原因:

  • 没有在 Windows 上实机运行;Windows 目标只做了 clippy 交叉检查,CI 会跑 Windows 测试。
  • 还没有在桌面端测量命中后的悬浮耗时,要等 Lithe-IDEA 升级 Core 修订后再测。

提交前确认 / Final checks

  • 分支从最新的 main(ca80599)创建,目标分支 main
  • 以 Draft 提交
  • 没有无关改动,没有提交产物、凭据或个人路径
  • 本次没有启动 Lithe 应用

…wers at once

Refs 1lck/Lithe-IDEA#1995. Every hover went to the language server, so resting
on the same symbol again waited for the server a second time.

The LSP engine now keeps each session's hover answers in a bounded cache
(HOVER_CACHE_CAPACITY = 32, least recently used first; answers over
HOVER_CACHE_MAX_CONTENT_BYTES = 64 KiB are not kept). A host hover for the same
document version and position, or for a position inside the answer's same-line
range, completes from the cache without writing textDocument/hover. Empty
answers, errors, and answers that arrive after a clear are delivered but not
kept.

Every kept answer is forgotten when anything can change what the server knows:
document open/change/save/close, workspace file changes, configuration or
indexing-exclusion changes, executeCommand, plugin backend languageServer
requests and notifications, host notifications, any request from the server,
the end of a $/progress, and session stop. A generation counter refuses answers
the server computed before such a clear.

Documents the behaviour in the contract README, the languages reference and the
protocol changelog; the requestCompleted result shape is unchanged.
@xiaoyumuxi
xiaoyumuxi marked this pull request as ready for review November 16, 2026 02:52
@xiaoyumuxi
xiaoyumuxi merged commit 6a14126 into main Nov 16, 2026
6 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant