From 98d514f5bf2c9a53519f462e084791551dc8f69b Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Mon, 22 Jan 2024 15:21:10 -0700 Subject: [PATCH] Fix off-by-one highlighting in hover tooltip 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. --- crates/editor/src/hover_popover.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/crates/editor/src/hover_popover.rs b/crates/editor/src/hover_popover.rs index f311f20ae6d0faf9bc42193245fa42ae14cc5fac..a4bc1c5cdebe49f90e7f8eaa13add0de25ff498a 100644 --- a/crates/editor/src/hover_popover.rs +++ b/crates/editor/src/hover_popover.rs @@ -394,6 +394,27 @@ 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,