Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
editor: Fix incorrect inlay hint selection behavior (#60912)
Finn Eitreim78c0f2522a42
diff --git a/crates/editor/src/display_map.rs b/crates/editor/src/display_map.rsindex 9093fcfecf6..554fe1a5ab2 100644--- a/crates/editor/src/display_map.rs+++ b/crates/editor/src/display_map.rs@@ -1680,6 +1680,36 @@ impl DisplaySnapshot {self.display_point_converter().map(range)}+ /// Converts a non-empty buffer range into one contiguous display range.+ /// Inlays at either boundary are excluded, while inlays between selected+ /// buffer characters are included.+ pub fn contiguous_display_point_range_for_buffer_range(+ &self,+ range: Range<MultiBufferOffset>,+ ) -> Option<Range<DisplayPoint>> {+ if range.is_empty() {+ return None;+ }++ let buffer = self.buffer_snapshot();+ let first_character_end =+ buffer.clip_offset((range.start + 1usize).min(range.end), Bias::Right);+ let last_character_start = buffer.clip_offset(+ range.end.saturating_sub_usize(1).max(range.start),+ Bias::Left,+ );++ let mut converter = self.display_point_converter();+ let first_ranges = converter.map(range.start..first_character_end);+ let start = first_ranges.first()?.start;+ if first_character_end == range.end {+ return Some(start..first_ranges.last()?.end);+ }++ let last_ranges = converter.map(last_character_start..range.end);+ Some(start..last_ranges.last()?.end)+ }+/// Returns a converter that maps buffer offset ranges to `DisplayPoint`/// ranges (as in [`Self::isomorphic_display_point_ranges_for_buffer_range`])/// while reusing cursor state across calls. Use this when converting many@@ -4175,6 +4205,62 @@ pub mod tests {assert_eq!(ranges.len(), 1);assert_eq!(ranges[0].start, DisplayPoint::new(DisplayRow(0), 10));assert_eq!(ranges[0].end, DisplayPoint::new(DisplayRow(0), 14));++ map.update(cx, |map, cx| {+ map.splice_inlays(+ &[InlayId::Hint(0)],+ vec![+ Inlay::mock_hint(1, buffer_snapshot.anchor_after(MultiBufferOffset(5)), "L"),+ Inlay::mock_hint(2, buffer_snapshot.anchor_before(MultiBufferOffset(5)), "R"),+ Inlay::mock_hint(3, buffer_snapshot.anchor_after(MultiBufferOffset(7)), "I"),+ ],+ cx,+ );+ });+ let snapshot = map.update(cx, |map, cx| map.snapshot(cx));++ assert_eq!(+ snapshot.contiguous_display_point_range_for_buffer_range(+ MultiBufferOffset(4)..MultiBufferOffset(5),+ ),+ Some(DisplayPoint::new(DisplayRow(0), 4)..DisplayPoint::new(DisplayRow(0), 5)),+ );+ assert_eq!(+ snapshot.contiguous_display_point_range_for_buffer_range(+ MultiBufferOffset(5)..MultiBufferOffset(6),+ ),+ Some(DisplayPoint::new(DisplayRow(0), 7)..DisplayPoint::new(DisplayRow(0), 8)),+ );+ assert_eq!(+ snapshot.contiguous_display_point_range_for_buffer_range(+ MultiBufferOffset(4)..MultiBufferOffset(6),+ ),+ Some(DisplayPoint::new(DisplayRow(0), 4)..DisplayPoint::new(DisplayRow(0), 8)),+ );+ assert_eq!(+ snapshot.contiguous_display_point_range_for_buffer_range(+ MultiBufferOffset(6)..MultiBufferOffset(7),+ ),+ Some(DisplayPoint::new(DisplayRow(0), 8)..DisplayPoint::new(DisplayRow(0), 9)),+ );+ assert_eq!(+ snapshot.contiguous_display_point_range_for_buffer_range(+ MultiBufferOffset(7)..MultiBufferOffset(8),+ ),+ Some(DisplayPoint::new(DisplayRow(0), 10)..DisplayPoint::new(DisplayRow(0), 11)),+ );+ assert_eq!(+ snapshot.contiguous_display_point_range_for_buffer_range(+ MultiBufferOffset(4)..MultiBufferOffset(9),+ ),+ Some(DisplayPoint::new(DisplayRow(0), 4)..DisplayPoint::new(DisplayRow(0), 12)),+ );+ assert_eq!(+ snapshot.contiguous_display_point_range_for_buffer_range(+ MultiBufferOffset(5)..MultiBufferOffset(5),+ ),+ None,+ );}#[test]diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rsindex fe06b33f431..83e8f702b4a 100644--- a/crates/editor/src/element.rs+++ b/crates/editor/src/element.rs@@ -59,7 +59,7 @@ use language::{use markdown::Markdown;use multi_buffer::{Anchor, ExpandExcerptDirection, ExpandInfo, MultiBufferOffset, MultiBufferPoint,- MultiBufferRow, RowInfo,+ MultiBufferRow, RowInfo, ToOffset,};use project::{@@ -167,10 +167,28 @@ impl SelectionLayout {is_local: bool,user_name: Option<SharedString>,) -> Self {- let point_selection = selection.map(|p| p.to_point(map.buffer_snapshot()));+ let buffer_snapshot = map.buffer_snapshot();+ let point_selection = selection.map(|p| p.to_point(buffer_snapshot));let display_selection = point_selection.map(|p| p.to_display_point(map));let mut range = display_selection.range();let mut head = display_selection.head();+ if !line_mode {+ let offset_range = point_selection.start.to_offset(buffer_snapshot)+ ..point_selection.end.to_offset(buffer_snapshot);+ if let Some(contiguous_range) =+ map.contiguous_display_point_range_for_buffer_range(offset_range)+ {+ range = contiguous_range;+ // Keep the cursor attached to the highlight boundary; the+ // anchor-bias display position may sit on the far side of a+ // boundary inlay the highlight excludes.+ head = if selection.reversed {+ range.start+ } else {+ range.end+ };+ }+ }let mut active_rows = map.prev_line_boundary(point_selection.start).1.row()..map.next_line_boundary(point_selection.end).1.row();@@ -10673,13 +10691,13 @@ fn compute_auto_height_layout(mod tests {use super::*;use crate::{- Editor, HighlightKey, MultiBuffer, NavigationOverlayKey, NavigationOverlayLabel,- NavigationTargetOverlay, SelectionEffects,- display_map::{BlockPlacement, BlockProperties},+ Editor, FoldPlaceholder, HighlightKey, Inlay, MultiBuffer, NavigationOverlayKey,+ NavigationOverlayLabel, NavigationTargetOverlay, SelectionEffects,+ display_map::{BlockPlacement, BlockProperties, DisplayMap},editor_tests::{init_test, update_test_language_settings},};- use gpui::{TestAppContext, VisualTestContext};- use language::{Buffer, language_settings, tree_sitter_python};+ use gpui::{TestAppContext, VisualTestContext, font};+ use language::{Buffer, SelectionGoal, language_settings, tree_sitter_python};use log::info;use rand::{RngCore, rngs::StdRng};use std::num::NonZeroU32;@@ -10782,6 +10800,121 @@ mod tests {}}+ // Regression test for https://github.com/zed-industries/zed/issues/48141.+ #[gpui::test]+ fn test_selection_layout_around_inlay(cx: &mut TestAppContext) {+ init_test(cx, |_| {});++ let snapshot = cx.update(|cx| {+ let buffer = MultiBuffer::build_simple("abcd", cx);+ let buffer_snapshot = buffer.read(cx).snapshot(cx);+ let display_map = cx.new(|cx| {+ DisplayMap::new(+ buffer,+ font("Helvetica"),+ px(14.0),+ None,+ 1,+ 1,+ FoldPlaceholder::test(),+ project::project_settings::DiagnosticSeverity::Warning,+ cx,+ )+ });+ display_map.update(cx, |display_map, cx| {+ display_map.splice_inlays(+ &[],+ vec![+ Inlay::mock_hint(+ 0,+ buffer_snapshot.anchor_before(MultiBufferOffset(1)),+ "hint ",+ ),+ Inlay::mock_hint(+ 1,+ buffer_snapshot.anchor_after(MultiBufferOffset(3)),+ "!!",+ ),+ ],+ cx,+ );+ display_map.snapshot(cx)+ })+ });++ let layout = |range: Range<MultiBufferOffset>, reversed, cursor_offset| {+ SelectionLayout::new(+ Selection {+ id: 0,+ start: range.start,+ end: range.end,+ reversed,+ goal: SelectionGoal::None,+ },+ false,+ cursor_offset,+ CursorShape::Bar,+ &snapshot,+ true,+ true,+ None,+ )+ };+ let display_point = |column| DisplayPoint::new(DisplayRow(0), column);++ let ending_at_inlay = layout(MultiBufferOffset(0)..MultiBufferOffset(1), false, false);+ assert_eq!(ending_at_inlay.range, display_point(0)..display_point(1));+ assert_eq!(ending_at_inlay.head, display_point(1));++ let starting_at_inlay = layout(MultiBufferOffset(1)..MultiBufferOffset(2), false, false);+ assert_eq!(starting_at_inlay.range, display_point(6)..display_point(7));+ assert_eq!(starting_at_inlay.head, display_point(7));++ let spanning_inlay = layout(MultiBufferOffset(0)..MultiBufferOffset(2), false, false);+ assert_eq!(spanning_inlay.range, display_point(0)..display_point(7));+ assert_eq!(spanning_inlay.head, display_point(7));++ let reversed = layout(MultiBufferOffset(0)..MultiBufferOffset(2), true, false);+ assert_eq!(reversed.range, display_point(0)..display_point(7));+ assert_eq!(reversed.head, display_point(0));++ // A reversed selection starting at a right-anchored hint: the+ // anchor-bias cursor position would sit before the hint, detached from+ // the highlight, so the head snaps to the highlight boundary instead.+ let reversed_at_right_anchored_inlay =+ layout(MultiBufferOffset(3)..MultiBufferOffset(4), true, false);+ assert_eq!(+ reversed_at_right_anchored_inlay.range,+ display_point(10)..display_point(11)+ );+ assert_eq!(reversed_at_right_anchored_inlay.head, display_point(10));++ // An empty selection is a typing position, so it keeps the anchor-bias+ // display position rather than snapping around boundary inlays.+ let empty = layout(MultiBufferOffset(1)..MultiBufferOffset(1), false, false);+ assert!(empty.range.is_empty());+ assert_eq!(empty.head, display_point(6));++ let vim_ending_at_inlay = layout(MultiBufferOffset(0)..MultiBufferOffset(1), false, true);+ assert_eq!(+ vim_ending_at_inlay.range,+ display_point(0)..display_point(1)+ );+ assert_eq!(vim_ending_at_inlay.head, display_point(0));++ let vim_spanning_inlay = layout(MultiBufferOffset(0)..MultiBufferOffset(2), false, true);+ assert_eq!(vim_spanning_inlay.range, display_point(0)..display_point(7));+ assert_eq!(vim_spanning_inlay.head, display_point(6));++ let vim_reversed_at_right_anchored_inlay =+ layout(MultiBufferOffset(3)..MultiBufferOffset(4), true, true);+ assert_eq!(+ vim_reversed_at_right_anchored_inlay.range,+ display_point(10)..display_point(11)+ );+ assert_eq!(vim_reversed_at_right_anchored_inlay.head, display_point(10));+ }+#[gpui::test]async fn test_soft_wrap_editor_width_auto_height_editor(cx: &mut TestAppContext) {init_test(cx, |_| {});