Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions rust/contracts/rust-core-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -3723,8 +3723,17 @@ session, and versioned diagnostics must match the current document version.
The client reducer, raw JSON-RPC message, frame, and parser functions are
internal Rust implementation seams; they are not public application commands.
Completion, hover, navigation, edit, hint, folding, and code-lens responses are
normalized by Rust before they cross the application boundary. Unknown server
requests receive JSON-RPC `Method not found` instead of being silently ignored.
normalized by Rust before they cross the application boundary. Hover results are
`{ hover: { contents, isMarkdown, range } | null }`. LSP `MarkedString` strings
remain Markdown; `{ language, value }` entries become fenced code blocks retaining
the language identifier, with a fence longer than any backtick run in the code.
`MarkupContent` follows its explicit `kind`, so `plaintext` is not interpreted as
Markdown. `MarkedString[]` entries retain their order and are joined with blank
lines as Markdown. The host uses a block's language identifier to select an
available syntax grammar, independently of the hovered document's language.
See [hover content fixtures](../fixtures/lsp/hover-contents-v1.json).
Unknown server requests receive JSON-RPC `Method not found` instead of being
silently ignored.

The `history.*` commands accept an adapter-selected `storageRoot`; history
metadata never stores an absolute workspace or storage path. `history.record`
Expand Down
16 changes: 15 additions & 1 deletion rust/crates/lsp-client/src/client/results.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,25 @@ fn parse_hover(result: &Value) -> Option<Value> {
}))
}

// LSP MarkedString is Markdown, either directly or as a language-tagged code block.
// Only MarkupContent uses `kind` to explicitly select plaintext instead.
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) {
// A longer fence keeps backticks in the code from closing the block.
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)
Expand Down
26 changes: 26 additions & 0 deletions rust/crates/lsp-client/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,32 @@ fn document_uri_identity_keeps_reserved_characters_distinct() {
}
}

#[test]
fn client_hover_contents_match_fixture() {
let fixture: Value =
serde_json::from_str(include_str!("../../../fixtures/lsp/hover-contents-v1.json")).unwrap();
let mut mismatches = Vec::new();
for case in fixture["cases"].as_array().unwrap() {
let actual = lsp_feature_result_for_method(
Some("textDocument/hover"),
Some(&case["result"]),
&Default::default(),
)
.unwrap();
if actual["hover"] != case["expected"] {
mismatches.push(json!({
"name": case["name"],
"expected": case["expected"],
"actual": actual["hover"]
}));
}
}
assert!(
mismatches.is_empty(),
"hover normalization: {mismatches:#?}"
);
}

#[test]
fn client_core_initializes_and_applies_server_capabilities() {
let initialized = client_initialize(ClientInitializeRequest {
Expand Down
266 changes: 266 additions & 0 deletions rust/fixtures/lsp/hover-contents-v1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
{
"schemaVersion": 1,
"cases": [
{
"name": "bare-marked-string",
"result": {
"contents": "```rust\nfn add(left: i32, right: i32) -> i32 { left + right }\n```\n\nAdds **two** numbers.\n",
"range": {
"start": {
"line": 6,
"character": 2
},
"end": {
"line": 6,
"character": 5
}
}
},
"expected": {
"contents": "```rust\nfn add(left: i32, right: i32) -> i32 { left + right }\n```\n\nAdds **two** numbers.\n",
"isMarkdown": true,
"range": {
"start": {
"line": 6,
"utf16Column": 2
},
"end": {
"line": 6,
"utf16Column": 5
}
}
},
"codeLanguage": "rust",
"codeLine": "fn add(left: i32, right: i32) -> i32 { left + right }"
},
{
"name": "language-marked-string",
"result": {
"contents": {
"language": "rust",
"value": "fn add(left: i32, right: i32) -> i32 { left + right }\n"
},
"range": {
"start": {
"line": 6,
"character": 2
},
"end": {
"line": 6,
"character": 5
}
}
},
"expected": {
"contents": "```rust\nfn add(left: i32, right: i32) -> i32 { left + right }\n```",
"isMarkdown": true,
"range": {
"start": {
"line": 6,
"utf16Column": 2
},
"end": {
"line": 6,
"utf16Column": 5
}
}
},
"codeLanguage": "rust",
"codeLine": "fn add(left: i32, right: i32) -> i32 { left + right }"
},
{
"name": "language-marked-string-with-backticks",
"result": {
"contents": {
"language": "rust",
"value": "let fence = \"```\";"
},
"range": {
"start": {
"line": 6,
"character": 2
},
"end": {
"line": 6,
"character": 5
}
}
},
"expected": {
"contents": "````rust\nlet fence = \"```\";\n````",
"isMarkdown": true,
"range": {
"start": {
"line": 6,
"utf16Column": 2
},
"end": {
"line": 6,
"utf16Column": 5
}
}
},
"codeLanguage": "rust",
"codeLine": "let fence = \"```\";"
},
{
"name": "markup-plaintext",
"result": {
"contents": {
"kind": "plaintext",
"value": "literal *text* and ```rust"
},
"range": {
"start": {
"line": 6,
"character": 2
},
"end": {
"line": 6,
"character": 5
}
}
},
"expected": {
"contents": "literal *text* and ```rust",
"isMarkdown": false,
"range": {
"start": {
"line": 6,
"utf16Column": 2
},
"end": {
"line": 6,
"utf16Column": 5
}
}
},
"codeLanguage": "",
"codeLine": ""
},
{
"name": "markup-markdown",
"result": {
"contents": {
"kind": "markdown",
"value": "```rust\nfn add(left: i32, right: i32) -> i32 { left + right }\n```\n\nAdds **two** numbers.\n"
},
"range": {
"start": {
"line": 6,
"character": 2
},
"end": {
"line": 6,
"character": 5
}
}
},
"expected": {
"contents": "```rust\nfn add(left: i32, right: i32) -> i32 { left + right }\n```\n\nAdds **two** numbers.\n",
"isMarkdown": true,
"range": {
"start": {
"line": 6,
"utf16Column": 2
},
"end": {
"line": 6,
"utf16Column": 5
}
}
},
"codeLanguage": "rust",
"codeLine": "fn add(left: i32, right: i32) -> i32 { left + right }"
},
{
"name": "marked-string-array",
"result": {
"contents": [
{
"language": "rust",
"value": "fn add(left: i32, right: i32) -> i32 { left + right }"
},
"Adds **two** numbers."
],
"range": {
"start": {
"line": 6,
"character": 2
},
"end": {
"line": 6,
"character": 5
}
}
},
"expected": {
"contents": "```rust\nfn add(left: i32, right: i32) -> i32 { left + right }\n```\n\nAdds **two** numbers.",
"isMarkdown": true,
"range": {
"start": {
"line": 6,
"utf16Column": 2
},
"end": {
"line": 6,
"utf16Column": 5
}
}
},
"codeLanguage": "rust",
"codeLine": "fn add(left: i32, right: i32) -> i32 { left + right }"
},
{
"name": "markdown-string-array",
"result": {
"contents": [
"**First** section.",
"`Second` section."
],
"range": {
"start": {
"line": 6,
"character": 2
},
"end": {
"line": 6,
"character": 5
}
}
},
"expected": {
"contents": "**First** section.\n\n`Second` section.",
"isMarkdown": true,
"range": {
"start": {
"line": 6,
"utf16Column": 2
},
"end": {
"line": 6,
"utf16Column": 5
}
}
},
"codeLanguage": "",
"codeLine": ""
},
{
"name": "null-hover",
"result": null,
"expected": null,
"codeLanguage": "",
"codeLine": ""
},
{
"name": "empty-marked-string-array",
"result": {
"contents": []
},
"expected": null,
"codeLanguage": "",
"codeLine": ""
}
]
}
Loading