Skip to content

Commit aa8b2fe

Browse files
committed
fix(lsp): preserve hover Markdown and code block languages
1 parent c6cef0b commit aa8b2fe

4 files changed

Lines changed: 318 additions & 3 deletions

File tree

‎rust/contracts/rust-core-api.md‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3723,8 +3723,17 @@ session, and versioned diagnostics must match the current document version.
37233723
The client reducer, raw JSON-RPC message, frame, and parser functions are
37243724
internal Rust implementation seams; they are not public application commands.
37253725
Completion, hover, navigation, edit, hint, folding, and code-lens responses are
3726-
normalized by Rust before they cross the application boundary. Unknown server
3727-
requests receive JSON-RPC `Method not found` instead of being silently ignored.
3726+
normalized by Rust before they cross the application boundary. Hover results are
3727+
`{ hover: { contents, isMarkdown, range } | null }`. LSP `MarkedString` strings
3728+
remain Markdown; `{ language, value }` entries become fenced code blocks retaining
3729+
the language identifier, with a fence longer than any backtick run in the code.
3730+
`MarkupContent` follows its explicit `kind`, so `plaintext` is not interpreted as
3731+
Markdown. `MarkedString[]` entries retain their order and are joined with blank
3732+
lines as Markdown. The host uses a block's language identifier to select an
3733+
available syntax grammar, independently of the hovered document's language.
3734+
See [hover content fixtures](../fixtures/lsp/hover-contents-v1.json).
3735+
Unknown server requests receive JSON-RPC `Method not found` instead of being
3736+
silently ignored.
37283737

37293738
The `history.*` commands accept an adapter-selected `storageRoot`; history
37303739
metadata never stores an absolute workspace or storage path. `history.record`

‎rust/crates/lsp-client/src/client/results.rs‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,25 @@ fn parse_hover(result: &Value) -> Option<Value> {
180180
}))
181181
}
182182

183+
// LSP MarkedString is Markdown, either directly or as a language-tagged code block.
184+
// Only MarkupContent uses `kind` to explicitly select plaintext instead.
183185
fn hover_contents(value: &Value) -> Option<(String, bool)> {
184186
match value {
185-
Value::String(text) => Some((text.clone(), false)),
187+
Value::String(text) => Some((text.clone(), true)),
186188
Value::Object(object) => {
187189
if let Some(value) = object.get("value").and_then(Value::as_str) {
190+
if let Some(language) = object.get("language").and_then(Value::as_str) {
191+
// A longer fence keeps backticks in the code from closing the block.
192+
let longest = value.split(|c| c != '`').map(str::len).max().unwrap_or(0);
193+
let fence = "`".repeat(longest.max(2) + 1);
194+
return Some((
195+
format!(
196+
"{fence}{language}\n{}\n{fence}",
197+
value.trim_end_matches('\n')
198+
),
199+
true,
200+
));
201+
}
188202
let is_markdown = object
189203
.get("kind")
190204
.and_then(Value::as_str)

‎rust/crates/lsp-client/src/tests.rs‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,32 @@ fn document_uri_identity_keeps_reserved_characters_distinct() {
3232
}
3333
}
3434

35+
#[test]
36+
fn client_hover_contents_match_fixture() {
37+
let fixture: Value =
38+
serde_json::from_str(include_str!("../../../fixtures/lsp/hover-contents-v1.json")).unwrap();
39+
let mut mismatches = Vec::new();
40+
for case in fixture["cases"].as_array().unwrap() {
41+
let actual = lsp_feature_result_for_method(
42+
Some("textDocument/hover"),
43+
Some(&case["result"]),
44+
&Default::default(),
45+
)
46+
.unwrap();
47+
if actual["hover"] != case["expected"] {
48+
mismatches.push(json!({
49+
"name": case["name"],
50+
"expected": case["expected"],
51+
"actual": actual["hover"]
52+
}));
53+
}
54+
}
55+
assert!(
56+
mismatches.is_empty(),
57+
"hover normalization: {mismatches:#?}"
58+
);
59+
}
60+
3561
#[test]
3662
fn client_core_initializes_and_applies_server_capabilities() {
3763
let initialized = client_initialize(ClientInitializeRequest {
Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
1+
{
2+
"schemaVersion": 1,
3+
"cases": [
4+
{
5+
"name": "bare-marked-string",
6+
"result": {
7+
"contents": "```rust\nfn add(left: i32, right: i32) -> i32 { left + right }\n```\n\nAdds **two** numbers.\n",
8+
"range": {
9+
"start": {
10+
"line": 6,
11+
"character": 2
12+
},
13+
"end": {
14+
"line": 6,
15+
"character": 5
16+
}
17+
}
18+
},
19+
"expected": {
20+
"contents": "```rust\nfn add(left: i32, right: i32) -> i32 { left + right }\n```\n\nAdds **two** numbers.\n",
21+
"isMarkdown": true,
22+
"range": {
23+
"start": {
24+
"line": 6,
25+
"utf16Column": 2
26+
},
27+
"end": {
28+
"line": 6,
29+
"utf16Column": 5
30+
}
31+
}
32+
},
33+
"codeLanguage": "rust",
34+
"codeLine": "fn add(left: i32, right: i32) -> i32 { left + right }"
35+
},
36+
{
37+
"name": "language-marked-string",
38+
"result": {
39+
"contents": {
40+
"language": "rust",
41+
"value": "fn add(left: i32, right: i32) -> i32 { left + right }\n"
42+
},
43+
"range": {
44+
"start": {
45+
"line": 6,
46+
"character": 2
47+
},
48+
"end": {
49+
"line": 6,
50+
"character": 5
51+
}
52+
}
53+
},
54+
"expected": {
55+
"contents": "```rust\nfn add(left: i32, right: i32) -> i32 { left + right }\n```",
56+
"isMarkdown": true,
57+
"range": {
58+
"start": {
59+
"line": 6,
60+
"utf16Column": 2
61+
},
62+
"end": {
63+
"line": 6,
64+
"utf16Column": 5
65+
}
66+
}
67+
},
68+
"codeLanguage": "rust",
69+
"codeLine": "fn add(left: i32, right: i32) -> i32 { left + right }"
70+
},
71+
{
72+
"name": "language-marked-string-with-backticks",
73+
"result": {
74+
"contents": {
75+
"language": "rust",
76+
"value": "let fence = \"```\";"
77+
},
78+
"range": {
79+
"start": {
80+
"line": 6,
81+
"character": 2
82+
},
83+
"end": {
84+
"line": 6,
85+
"character": 5
86+
}
87+
}
88+
},
89+
"expected": {
90+
"contents": "````rust\nlet fence = \"```\";\n````",
91+
"isMarkdown": true,
92+
"range": {
93+
"start": {
94+
"line": 6,
95+
"utf16Column": 2
96+
},
97+
"end": {
98+
"line": 6,
99+
"utf16Column": 5
100+
}
101+
}
102+
},
103+
"codeLanguage": "rust",
104+
"codeLine": "let fence = \"```\";"
105+
},
106+
{
107+
"name": "markup-plaintext",
108+
"result": {
109+
"contents": {
110+
"kind": "plaintext",
111+
"value": "literal *text* and ```rust"
112+
},
113+
"range": {
114+
"start": {
115+
"line": 6,
116+
"character": 2
117+
},
118+
"end": {
119+
"line": 6,
120+
"character": 5
121+
}
122+
}
123+
},
124+
"expected": {
125+
"contents": "literal *text* and ```rust",
126+
"isMarkdown": false,
127+
"range": {
128+
"start": {
129+
"line": 6,
130+
"utf16Column": 2
131+
},
132+
"end": {
133+
"line": 6,
134+
"utf16Column": 5
135+
}
136+
}
137+
},
138+
"codeLanguage": "",
139+
"codeLine": ""
140+
},
141+
{
142+
"name": "markup-markdown",
143+
"result": {
144+
"contents": {
145+
"kind": "markdown",
146+
"value": "```rust\nfn add(left: i32, right: i32) -> i32 { left + right }\n```\n\nAdds **two** numbers.\n"
147+
},
148+
"range": {
149+
"start": {
150+
"line": 6,
151+
"character": 2
152+
},
153+
"end": {
154+
"line": 6,
155+
"character": 5
156+
}
157+
}
158+
},
159+
"expected": {
160+
"contents": "```rust\nfn add(left: i32, right: i32) -> i32 { left + right }\n```\n\nAdds **two** numbers.\n",
161+
"isMarkdown": true,
162+
"range": {
163+
"start": {
164+
"line": 6,
165+
"utf16Column": 2
166+
},
167+
"end": {
168+
"line": 6,
169+
"utf16Column": 5
170+
}
171+
}
172+
},
173+
"codeLanguage": "rust",
174+
"codeLine": "fn add(left: i32, right: i32) -> i32 { left + right }"
175+
},
176+
{
177+
"name": "marked-string-array",
178+
"result": {
179+
"contents": [
180+
{
181+
"language": "rust",
182+
"value": "fn add(left: i32, right: i32) -> i32 { left + right }"
183+
},
184+
"Adds **two** numbers."
185+
],
186+
"range": {
187+
"start": {
188+
"line": 6,
189+
"character": 2
190+
},
191+
"end": {
192+
"line": 6,
193+
"character": 5
194+
}
195+
}
196+
},
197+
"expected": {
198+
"contents": "```rust\nfn add(left: i32, right: i32) -> i32 { left + right }\n```\n\nAdds **two** numbers.",
199+
"isMarkdown": true,
200+
"range": {
201+
"start": {
202+
"line": 6,
203+
"utf16Column": 2
204+
},
205+
"end": {
206+
"line": 6,
207+
"utf16Column": 5
208+
}
209+
}
210+
},
211+
"codeLanguage": "rust",
212+
"codeLine": "fn add(left: i32, right: i32) -> i32 { left + right }"
213+
},
214+
{
215+
"name": "markdown-string-array",
216+
"result": {
217+
"contents": [
218+
"**First** section.",
219+
"`Second` section."
220+
],
221+
"range": {
222+
"start": {
223+
"line": 6,
224+
"character": 2
225+
},
226+
"end": {
227+
"line": 6,
228+
"character": 5
229+
}
230+
}
231+
},
232+
"expected": {
233+
"contents": "**First** section.\n\n`Second` section.",
234+
"isMarkdown": true,
235+
"range": {
236+
"start": {
237+
"line": 6,
238+
"utf16Column": 2
239+
},
240+
"end": {
241+
"line": 6,
242+
"utf16Column": 5
243+
}
244+
}
245+
},
246+
"codeLanguage": "",
247+
"codeLine": ""
248+
},
249+
{
250+
"name": "null-hover",
251+
"result": null,
252+
"expected": null,
253+
"codeLanguage": "",
254+
"codeLine": ""
255+
},
256+
{
257+
"name": "empty-marked-string-array",
258+
"result": {
259+
"contents": []
260+
},
261+
"expected": null,
262+
"codeLanguage": "",
263+
"codeLine": ""
264+
}
265+
]
266+
}

0 commit comments

Comments
 (0)