Change Editor::set_highlighted_row to take a row range

Max Brunsfeld and Antonio Scandurra created

Co-Authored-By: Antonio Scandurra <me@as-cii.com>

Change summary

crates/editor/src/editor.rs         | 12 ++++----
crates/editor/src/element.rs        | 39 +++++++++++++++++++-----------
crates/go_to_line/src/go_to_line.rs |  5 ++-
3 files changed, 33 insertions(+), 23 deletions(-)

Detailed changes

crates/editor/src/editor.rs 🔗

@@ -376,7 +376,7 @@ pub struct Editor {
     blinking_paused: bool,
     mode: EditorMode,
     placeholder_text: Option<Arc<str>>,
-    highlighted_row: Option<u32>,
+    highlighted_rows: Option<Range<u32>>,
 }
 
 pub struct EditorSnapshot {
@@ -505,7 +505,7 @@ impl Editor {
             blinking_paused: false,
             mode: EditorMode::Full,
             placeholder_text: None,
-            highlighted_row: None,
+            highlighted_rows: None,
         };
         let selection = Selection {
             id: post_inc(&mut this.next_selection_id),
@@ -3546,12 +3546,12 @@ impl Editor {
             .update(cx, |map, cx| map.set_wrap_width(width, cx))
     }
 
-    pub fn set_highlighted_row(&mut self, row: Option<u32>) {
-        self.highlighted_row = row;
+    pub fn set_highlighted_rows(&mut self, rows: Option<Range<u32>>) {
+        self.highlighted_rows = rows;
     }
 
-    pub fn highlighted_row(&mut self) -> Option<u32> {
-        self.highlighted_row
+    pub fn highlighted_rows(&self) -> Option<Range<u32>> {
+        self.highlighted_rows.clone()
     }
 
     fn next_blink_epoch(&mut self) -> usize {

crates/editor/src/element.rs 🔗

@@ -263,12 +263,16 @@ impl EditorElement {
                 }
             }
 
-            if let Some(highlighted_row) = layout.highlighted_row {
+            if let Some(highlighted_rows) = &layout.highlighted_rows {
                 let origin = vec2f(
                     bounds.origin_x(),
-                    bounds.origin_y() + (layout.line_height * highlighted_row as f32) - scroll_top,
+                    bounds.origin_y() + (layout.line_height * highlighted_rows.start as f32)
+                        - scroll_top,
+                );
+                let size = vec2f(
+                    bounds.width(),
+                    layout.line_height * highlighted_rows.len() as f32,
                 );
-                let size = vec2f(bounds.width(), layout.line_height);
                 cx.scene.push_quad(Quad {
                     bounds: RectF::new(origin, size),
                     background: Some(style.highlighted_line_background),
@@ -640,15 +644,20 @@ impl EditorElement {
                     .to_display_point(snapshot)
                     .row();
 
-                let anchor_x = text_x + if rows.contains(&anchor_row) {
-                    line_layouts[(anchor_row - rows.start) as usize]
-                        .x_for_index(block.column() as usize)
-                } else {
-                    layout_line(anchor_row, snapshot, style, cx.text_layout_cache)
-                        .x_for_index(block.column() as usize)
-                };
+                let anchor_x = text_x
+                    + if rows.contains(&anchor_row) {
+                        line_layouts[(anchor_row - rows.start) as usize]
+                            .x_for_index(block.column() as usize)
+                    } else {
+                        layout_line(anchor_row, snapshot, style, cx.text_layout_cache)
+                            .x_for_index(block.column() as usize)
+                    };
 
-                let mut element = block.render(&BlockContext { cx, anchor_x, line_number_x, });
+                let mut element = block.render(&BlockContext {
+                    cx,
+                    anchor_x,
+                    line_number_x,
+                });
                 element.layout(
                     SizeConstraint {
                         min: Vector2F::zero(),
@@ -750,9 +759,9 @@ impl Element for EditorElement {
 
         let mut selections = HashMap::default();
         let mut active_rows = BTreeMap::new();
-        let mut highlighted_row = None;
+        let mut highlighted_rows = None;
         self.update_view(cx.app, |view, cx| {
-            highlighted_row = view.highlighted_row();
+            highlighted_rows = view.highlighted_rows();
             let display_map = view.display_map.update(cx, |map, cx| map.snapshot(cx));
 
             let local_selections = view
@@ -831,7 +840,7 @@ impl Element for EditorElement {
             snapshot,
             style: self.settings.style.clone(),
             active_rows,
-            highlighted_row,
+            highlighted_rows,
             line_layouts,
             line_number_layouts,
             blocks,
@@ -962,7 +971,7 @@ pub struct LayoutState {
     style: EditorStyle,
     snapshot: EditorSnapshot,
     active_rows: BTreeMap<u32, bool>,
-    highlighted_row: Option<u32>,
+    highlighted_rows: Option<Range<u32>>,
     line_layouts: Vec<text_layout::Line>,
     line_number_layouts: Vec<Option<text_layout::Line>>,
     blocks: Vec<(u32, ElementBox)>,

crates/go_to_line/src/go_to_line.rs 🔗

@@ -143,8 +143,9 @@ impl GoToLine {
                         let snapshot = active_editor.snapshot(cx).display_snapshot;
                         let point = snapshot.buffer_snapshot.clip_point(point, Bias::Left);
                         let display_point = point.to_display_point(&snapshot);
+                        let row = display_point.row();
                         active_editor.select_ranges([point..point], Some(Autoscroll::Center), cx);
-                        active_editor.set_highlighted_row(Some(display_point.row()));
+                        active_editor.set_highlighted_rows(Some(row..row + 1));
                         Some(active_editor.newest_selection(&snapshot.buffer_snapshot))
                     });
                     cx.notify();
@@ -162,7 +163,7 @@ impl Entity for GoToLine {
         let line_selection = self.line_selection.take();
         let restore_state = self.restore_state.take();
         self.active_editor.update(cx, |editor, cx| {
-            editor.set_highlighted_row(None);
+            editor.set_highlighted_rows(None);
             if let Some((line_selection, restore_state)) = line_selection.zip(restore_state) {
                 let newest_selection =
                     editor.newest_selection::<usize>(&editor.buffer().read(cx).read(cx));