Fix off-by-one highlighting in hover tooltip (#4206)

rust analyzer has a tendency to return markdown of the form:

```rust
 // <-- note the leading space
blah blah blah
```

This is clearly defectuous, so we used to .trim() the output.

Unfortunately we trim after applying syntax highlighting, so that causes
the output to look goofy.

Fix this by updating the highlighting when we trim.

[[PR Description]]

Release Notes:

- Fixed a bug where syntax highlighting was off in hover tooltips
This commit is contained in:
Conrad Irwin 2024-01-22 23:12:08 -07:00 committed by GitHub
commit ce0833eadf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -394,6 +394,26 @@ async fn parse_blocks(
}
}
let leading_space = text.chars().take_while(|c| c.is_whitespace()).count();
if leading_space > 0 {
highlights = highlights
.into_iter()
.map(|(range, style)| {
(
range.start.saturating_sub(leading_space)
..range.end.saturating_sub(leading_space),
style,
)
})
.collect();
region_ranges = region_ranges
.into_iter()
.map(|range| {
range.start.saturating_sub(leading_space)..range.end.saturating_sub(leading_space)
})
.collect();
}
ParsedMarkdown {
text: text.trim().to_string(),
highlights,