Highlight the selected line when typing in the go to line dialog

Nathan Sobo created

Change summary

crates/editor/src/element.rs        | 18 ++++++++++++++++++
crates/editor/src/lib.rs            | 15 +++++++++++----
crates/go_to_line/src/lib.rs        | 14 +++++++++++++-
crates/theme/src/lib.rs             |  2 ++
crates/zed/assets/themes/_base.toml |  1 +
crates/zed/assets/themes/black.toml |  1 +
crates/zed/assets/themes/dark.toml  |  1 +
crates/zed/assets/themes/light.toml |  1 +
8 files changed, 48 insertions(+), 5 deletions(-)

Detailed changes

crates/editor/src/element.rs 🔗

@@ -263,6 +263,20 @@ impl EditorElement {
                     });
                 }
             }
+
+            if let Some(highlighted_row) = layout.highlighted_row {
+                let origin = vec2f(
+                    bounds.origin_x(),
+                    bounds.origin_y() + (layout.line_height * highlighted_row as f32) - scroll_top,
+                );
+                let size = vec2f(bounds.width(), layout.line_height);
+                cx.scene.push_quad(Quad {
+                    bounds: RectF::new(origin, size),
+                    background: Some(style.highlighted_line_background),
+                    border: Border::default(),
+                    corner_radius: 0.,
+                });
+            }
         }
 
         // Draw block backgrounds
@@ -729,7 +743,9 @@ impl Element for EditorElement {
 
         let mut selections = HashMap::new();
         let mut active_rows = BTreeMap::new();
+        let mut highlighted_row = None;
         self.update_view(cx.app, |view, cx| {
+            highlighted_row = view.highlighted_row();
             for selection_set_id in view.active_selection_sets(cx).collect::<Vec<_>>() {
                 let mut set = Vec::new();
                 for selection in view.selections_in_range(
@@ -786,6 +802,7 @@ impl Element for EditorElement {
             snapshot,
             style: self.settings.style.clone(),
             active_rows,
+            highlighted_row,
             line_layouts,
             line_number_layouts,
             block_layouts,
@@ -915,6 +932,7 @@ pub struct LayoutState {
     style: EditorStyle,
     snapshot: Snapshot,
     active_rows: BTreeMap<u32, bool>,
+    highlighted_row: Option<u32>,
     line_layouts: Vec<text_layout::Line>,
     line_number_layouts: Vec<Option<text_layout::Line>>,
     block_layouts: Vec<(Range<u32>, BlockStyle)>,

crates/editor/src/lib.rs 🔗

@@ -351,6 +351,7 @@ pub struct Editor {
     blinking_paused: bool,
     mode: EditorMode,
     placeholder_text: Option<Arc<str>>,
+    highlighted_row: Option<u32>,
 }
 
 pub struct Snapshot {
@@ -485,6 +486,7 @@ impl Editor {
             blinking_paused: false,
             mode: EditorMode::Full,
             placeholder_text: None,
+            highlighted_row: None,
         }
     }
 
@@ -3248,15 +3250,19 @@ impl Editor {
             .text()
     }
 
-    // pub fn font_size(&self) -> f32 {
-    //     self.settings.font_size
-    // }
-
     pub fn set_wrap_width(&self, width: f32, cx: &mut MutableAppContext) -> bool {
         self.display_map
             .update(cx, |map, cx| map.set_wrap_width(Some(width), cx))
     }
 
+    pub fn set_highlighted_row(&mut self, row: Option<u32>) {
+        self.highlighted_row = row;
+    }
+
+    pub fn highlighted_row(&mut self) -> Option<u32> {
+        self.highlighted_row
+    }
+
     fn next_blink_epoch(&mut self) -> usize {
         self.blink_epoch += 1;
         self.blink_epoch
@@ -3426,6 +3432,7 @@ impl EditorSettings {
                     background: Default::default(),
                     gutter_background: Default::default(),
                     active_line_background: Default::default(),
+                    highlighted_line_background: Default::default(),
                     line_number: Default::default(),
                     line_number_active: Default::default(),
                     selection: Default::default(),

crates/go_to_line/src/lib.rs 🔗

@@ -95,11 +95,17 @@ impl GoToLine {
                 let mut components = line_editor.trim().split(':');
                 let row = components.next().and_then(|row| row.parse::<u32>().ok());
                 let column = components.next().and_then(|row| row.parse::<u32>().ok());
-                if let Some(point) = row.map(|row| Point::new(row, column.unwrap_or(0))) {
+                if let Some(point) = row.map(|row| {
+                    Point::new(
+                        row.saturating_sub(1),
+                        column.map(|column| column.saturating_sub(1)).unwrap_or(0),
+                    )
+                }) {
                     self.active_editor.update(cx, |active_editor, cx| {
                         let buffer = active_editor.buffer().read(cx);
                         let point = buffer.clip_point(point, Bias::Left);
                         active_editor.select_ranges([point..point], Some(Autoscroll::Center), cx);
+                        active_editor.set_highlighted_row(Some(point.row));
                     });
                     cx.notify();
                 }
@@ -111,6 +117,12 @@ impl GoToLine {
 
 impl Entity for GoToLine {
     type Event = Event;
+
+    fn release(&mut self, cx: &mut MutableAppContext) {
+        self.active_editor.update(cx, |editor, cx| {
+            editor.set_highlighted_row(None);
+        })
+    }
 }
 
 impl View for GoToLine {

crates/theme/src/lib.rs 🔗

@@ -223,6 +223,7 @@ pub struct EditorStyle {
     pub selection: SelectionStyle,
     pub gutter_background: Color,
     pub active_line_background: Color,
+    pub highlighted_line_background: Color,
     pub line_number: Color,
     pub line_number_active: Color,
     pub guest_selections: Vec<SelectionStyle>,
@@ -286,6 +287,7 @@ impl InputEditorStyle {
             selection: self.selection,
             gutter_background: Default::default(),
             active_line_background: Default::default(),
+            highlighted_line_background: Default::default(),
             line_number: Default::default(),
             line_number_active: Default::default(),
             guest_selections: Default::default(),

crates/zed/assets/themes/_base.toml 🔗

@@ -231,6 +231,7 @@ text = "$text.1"
 background = "$surface.1"
 gutter_background = "$surface.1"
 active_line_background = "$state.active_line"
+highlighted_line_background = "$state.highlighted_line"
 line_number = "$text.2.color"
 line_number_active = "$text.0.color"
 selection = "$selection.host"