From 47e0535f1c7efc87449473292420f76dbbbfee74 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Thu, 14 Sep 2023 21:57:05 +0300 Subject: [PATCH] Randomize inlay highlight range start --- crates/editor/src/display_map/inlay_map.rs | 17 ++++---- crates/editor/src/hover_popover.rs | 2 +- crates/editor/src/link_go_to_definition.rs | 46 +++++++++++----------- 3 files changed, 33 insertions(+), 32 deletions(-) diff --git a/crates/editor/src/display_map/inlay_map.rs b/crates/editor/src/display_map/inlay_map.rs index 53c833a1b008b76c8e62683357d3479363a387ff..5efa15a1c2b54704c80ad66cd532537abbf97aff 100644 --- a/crates/editor/src/display_map/inlay_map.rs +++ b/crates/editor/src/display_map/inlay_map.rs @@ -316,10 +316,10 @@ impl<'a> Iterator for InlayChunks<'a> { }; let next_inlay_highlight_endpoint; let offset_in_inlay = self.output_offset - self.transforms.start().0; - if let Some((style, inlay_range)) = inlay_highlight_style_and_range { - let range = inlay_range.highlight_start..inlay_range.highlight_end; + if let Some((style, highlight)) = inlay_highlight_style_and_range { + let range = &highlight.range; if offset_in_inlay.0 < range.start { - next_inlay_highlight_endpoint = range.start; + next_inlay_highlight_endpoint = range.start - offset_in_inlay.0; } else if offset_in_inlay.0 >= range.end { next_inlay_highlight_endpoint = usize::MAX; } else { @@ -1711,25 +1711,28 @@ mod tests { .filter_map(|i| { let (_, inlay) = &inlays[i]; let inlay_text_len = inlay.text.len(); + // TODO kb gen_range match inlay_text_len { 0 => None, 1 => Some(InlayHighlight { inlay: inlay.id, inlay_position: inlay.position, - highlight_start: 0, - highlight_end: 1, + range: 0..1, }), n => { let inlay_text = inlay.text.to_string(); let mut highlight_end = rng.gen_range(1..n); + let mut highlight_start = rng.gen_range(0..highlight_end); while !inlay_text.is_char_boundary(highlight_end) { highlight_end += 1; } + while !inlay_text.is_char_boundary(highlight_start) { + highlight_start -= 1; + } Some(InlayHighlight { inlay: inlay.id, inlay_position: inlay.position, - highlight_start: 0, - highlight_end, + range: highlight_start..highlight_end, }) } } diff --git a/crates/editor/src/hover_popover.rs b/crates/editor/src/hover_popover.rs index 1cec238e4a6d97a9d30ec8de1dbbd554506311ec..79de3786a7befa7281f53ed1753ea2368361daca 100644 --- a/crates/editor/src/hover_popover.rs +++ b/crates/editor/src/hover_popover.rs @@ -107,7 +107,7 @@ pub fn hover_at_inlay(editor: &mut Editor, inlay_hover: InlayHover, cx: &mut Vie let hover_popover = InfoPopover { project: project.clone(), - symbol_range: RangeInEditor::Inlay(inlay_hover.range), + symbol_range: RangeInEditor::Inlay(inlay_hover.range.clone()), blocks: vec![inlay_hover.tooltip], language: None, rendered_content: None, diff --git a/crates/editor/src/link_go_to_definition.rs b/crates/editor/src/link_go_to_definition.rs index 3797de5e57876148c8ade870bc554afa93426532..581bda7e58c78070ebeca40485fe45d3e9ed93bb 100644 --- a/crates/editor/src/link_go_to_definition.rs +++ b/crates/editor/src/link_go_to_definition.rs @@ -43,10 +43,10 @@ impl RangeInEditor { let point_after_start = range.start.cmp(point, &snapshot.buffer_snapshot).is_le(); point_after_start && range.end.cmp(point, &snapshot.buffer_snapshot).is_ge() } - (Self::Inlay(range), TriggerPoint::InlayHint(point, _, _)) => { - range.inlay == point.inlay - && range.highlight_start.cmp(&point.highlight_end).is_le() - && range.highlight_end.cmp(&point.highlight_end).is_ge() + (Self::Inlay(highlight), TriggerPoint::InlayHint(point, _, _)) => { + highlight.inlay == point.inlay + && highlight.range.contains(&point.range.start) + && highlight.range.contains(&point.range.end) } (Self::Inlay(_), TriggerPoint::Text(_)) | (Self::Text(_), TriggerPoint::InlayHint(_, _, _)) => false, @@ -66,13 +66,11 @@ pub enum GoToDefinitionLink { InlayHint(lsp::Location, LanguageServerId), } -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct InlayHighlight { pub inlay: InlayId, pub inlay_position: Anchor, - // TODO kb turn into Range - pub highlight_start: usize, - pub highlight_end: usize, + pub range: Range, } #[derive(Debug, Clone)] @@ -252,9 +250,8 @@ pub fn update_inlay_link_and_hover_points( range: InlayHighlight { inlay: hovered_hint.id, inlay_position: hovered_hint.position, - highlight_start: extra_shift_left, - highlight_end: hovered_hint.text.len() - + extra_shift_right, + range: extra_shift_left + ..hovered_hint.text.len() + extra_shift_right, }, }, cx, @@ -272,13 +269,14 @@ pub fn update_inlay_link_and_hover_points( hovered_offset, ) { - let range = InlayHighlight { + let highlight_start = + (part_range.start - hint_start).0 + extra_shift_left; + let highlight_end = + (part_range.end - hint_start).0 + extra_shift_right; + let highlight = InlayHighlight { inlay: hovered_hint.id, inlay_position: hovered_hint.position, - highlight_start: (part_range.start - hint_start).0 - + extra_shift_left, - highlight_end: (part_range.end - hint_start).0 - + extra_shift_right, + range: highlight_start..highlight_end, }; if let Some(tooltip) = hovered_hint_part.tooltip { hover_popover::hover_at_inlay( @@ -299,7 +297,7 @@ pub fn update_inlay_link_and_hover_points( kind: content.kind, }, }, - range, + range: highlight.clone(), }, cx, ); @@ -312,7 +310,7 @@ pub fn update_inlay_link_and_hover_points( update_go_to_definition_link( editor, Some(GoToDefinitionTrigger::InlayHint( - range, + highlight, location, language_server_id, )), @@ -433,8 +431,8 @@ pub fn show_link_definition( ) }) } - TriggerPoint::InlayHint(trigger_source, lsp_location, server_id) => Some(( - Some(RangeInEditor::Inlay(*trigger_source)), + TriggerPoint::InlayHint(highlight, lsp_location, server_id) => Some(( + Some(RangeInEditor::Inlay(highlight.clone())), vec![GoToDefinitionLink::InlayHint( lsp_location.clone(), *server_id, @@ -501,8 +499,8 @@ pub fn show_link_definition( ..snapshot.anchor_after(offset_range.end), ) } - TriggerPoint::InlayHint(inlay_coordinates, _, _) => { - RangeInEditor::Inlay(*inlay_coordinates) + TriggerPoint::InlayHint(highlight, _, _) => { + RangeInEditor::Inlay(highlight.clone()) } }); @@ -513,9 +511,9 @@ pub fn show_link_definition( style, cx, ), - RangeInEditor::Inlay(inlay_coordinates) => this + RangeInEditor::Inlay(highlight) => this .highlight_inlays::( - vec![inlay_coordinates], + vec![highlight], style, cx, ),