[Bug] LSP hover shows raw Markdown when the server answers with a MarkedString

オープン 初心者向け
#2,373 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
85/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
rust

調査の方向性

rust/crates/lsp-client/src/client/results.rs の hover_contents から始めてください。parse_hover から到達します。報告者の diff と 4 つのユニットテストが想定される形状(単純な文字列、{language, value}、MarkupContent、配列)を示しています。変更の前後に rust/ で cargo test -p lsp-client を実行し、その後 Lithe Desktop で gleam lsp を使い、ポップアップに表示されるフェンスではなくハイライトされた Gleam ブロックが描画されることを確認してください。これらのユニットテストが通り、desktop/internal/workbench/pluginsemantics_hover.go でホバーの Markdown が二重にフェンスされなくなれば完了です。

索引モデルが issue の本文から書いたものです。

説明

bug issue-form:bug review: no-score
Platform / 平台

macOS

Area / 模块

Language support / 语言服务

Lithe version / Lithe 版本

0.8.15 (desktop-v0.8.15, Core 4880022); Core main 529be33 has the same code

Priority / 优先级

P2 / 后续考虑:目前不着急,可安排到后续版本

OS and architecture / 操作系统与架构

macOS, arm64

Environment / 环境信息

Gleam 1.19.1 (gleam lsp) through a declarative language-server plugin

Steps to reproduce / 复现步骤
  1. Use a language server that answers textDocument/hover with a bare string, for example
    gleam lsp. Its answer for a documented function is:

    {"contents": "```gleam\nfn(Int, Int) -> Int\n```\n Adds two numbers and says so.\n",
     "range": {"start": {"line": 6, "character": 2}, "end": {"line": 6, "character": 5}}}
    
  2. Hover over a call of that function in Lithe Desktop.

Expected behavior / 预期行为

The popup renders Markdown: the signature as a highlighted Gleam code block, the documentation
as text below it. The LSP specification defines MarkedString as Markdown when it is a string,
and as a code block in language when it is {language, value}
(https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#markedString).

Actual behavior / 实际行为

The popup shows the source text, fences included (```gleam is visible), unhighlighted
and wrapped mid-word.

Cause: hover_contents in rust/crates/lsp-client/src/client/results.rs returns
isMarkdown: false for Value::String, so the desktop's hoverMarkdown
(desktop/internal/workbench/pluginsemantics_hover.go) wraps the Markdown in a plain fence. A
{language, value} object takes the kind branch, also gets false, and loses its language.

Suggested fix (tested locally on desktop-v0.8.15 with Core 4880022; the lsp-client tests
pass, and the hover renders correctly with gleam lsp):

 fn hover_contents(value: &Value) -> Option<(String, bool)> {
     match value {
-        Value::String(text) => Some((text.clone(), false)),
+        Value::String(text) => Some((text.clone(), true)),
         Value::Object(object) => {
             if let Some(value) = object.get("value").and_then(Value::as_str) {
+                if let Some(language) = object.get("language").and_then(Value::as_str) {
+                    let longest = value
+                        .split(|c| c != '`')
+                        .map(str::len)
+                        .max()
+                        .unwrap_or(0);
+                    let fence = "`".repeat(longest.max(2) + 1);
+                    return Some((
+                        format!("{fence}{language}\n{}\n{fence}", value.trim_end_matches('\n')),
+                        true,
+                    ));
+                }
                 let is_markdown = object

Unit tests for the four shapes (bare string, {language, value}, MarkupContent plaintext and
markdown, array) are in the patch below.

diff --git a/rust/crates/lsp-client/src/client/results.rs b/rust/crates/lsp-client/src/client/results.rs
index 63ecf22..1ef44f4 100644
--- a/rust/crates/lsp-client/src/client/results.rs
+++ b/rust/crates/lsp-client/src/client/results.rs
@@ -180,11 +180,26 @@ fn parse_hover(result: &Value) -> Option<Value> {
     }))
 }
 
+// A bare string is a MarkedString, which the LSP specification defines as Markdown; an object
+// with a language is a MarkedString code block, rendered as a fence in that language; an object
+// with a kind is MarkupContent, Markdown only when its kind says so.
 fn hover_contents(value: &Value) -> Option<(String, bool)> {
     match value {
-        Value::String(text) => Some((text.clone(), false)),
+        Value::String(text) => Some((text.clone(), true)),
         Value::Object(object) => {
             if let Some(value) = object.get("value").and_then(Value::as_str) {
+                if let Some(language) = object.get("language").and_then(Value::as_str) {
+                    let longest = value
+                        .split(|c| c != '`')
+                        .map(str::len)
+                        .max()
+                        .unwrap_or(0);
+                    let fence = "`".repeat(longest.max(2) + 1);
+                    return Some((
+                        format!("{fence}{language}\n{}\n{fence}", value.trim_end_matches('\n')),
+                        true,
+                    ));
+                }
                 let is_markdown = object
                     .get("kind")
                     .and_then(Value::as_str)
@@ -1014,3 +1029,44 @@ fn parse_lsp_position_value(value: &Value) -> Option<Value> {
             .unwrap_or(0)
     }))
 }
+
+#[cfg(test)]
+mod hover_contents_tests {
+    use super::*;
+
+    #[test]
+    fn bare_string_is_markdown() {
+        let text = "```gleam\nfn(Int) -> Int\n```\n Adds.\n";
+        assert_eq!(hover_contents(&json!(text)), Some((text.to_string(), true)));
+    }
+
+    #[test]
+    fn marked_string_code_block_becomes_a_fence() {
+        let value = json!({ "language": "erlang", "value": "add(A, B) -> A + B.\n" });
+        assert_eq!(
+            hover_contents(&value),
+            Some(("```erlang\nadd(A, B) -> A + B.\n```".to_string(), true))
+        );
+    }
+
+    #[test]
+    fn markup_content_follows_its_kind() {
+        assert_eq!(
+            hover_contents(&json!({ "kind": "plaintext", "value": "a *b*" })),
+            Some(("a *b*".to_string(), false))
+        );
+        assert_eq!(
+            hover_contents(&json!({ "kind": "markdown", "value": "a *b*" })),
+            Some(("a *b*".to_string(), true))
+        );
+    }
+
+    #[test]
+    fn array_joins_its_parts_as_markdown() {
+        let value = json!([{ "language": "gleam", "value": "fn() -> Nil" }, "Docs."]);
+        assert_eq!(
+            hover_contents(&value),
+            Some(("```gleam\nfn() -> Nil\n```\n\nDocs.".to_string(), true))
+        );
+    }
+}
```

### Logs or diagnostic output / 日志或诊断输出

```shell
none needed; the cause is in the code above.
```

### Screenshots or recordings / 截图或录屏

before:
<img width="652" height="403" alt="image" src="https://github.com/user-attachments/assets/99cc2e1f-53fd-45d4-9997-afa5280fd63c" />


after: 
<img width="572" height="330" alt="image" src="https://github.com/user-attachments/assets/0a8efa8a-d944-450a-8634-b471e3fb4909" />

### Checklist / 提交前检查

- [x] I searched existing issues for duplicates. / 我已搜索现有 Issue,确认没有重复报告。
- [x] I removed secrets and personal data from the report. / 我已从报告中移除敏感信息和个人数据。
主要言語
TypeScript
スター
1.8k
フォーク
151
平均マージ
4時間 4分
マージ済み PR(30日)
558

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

1lck/Lithe-IDEA のほかの issue

1lck/Lithe-IDEA の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。