Make Go To Line to respect column numbers

Kirill Bulatov created

Change summary

crates/go_to_line/src/go_to_line.rs | 41 +++++++++++++++++-------------
1 file changed, 23 insertions(+), 18 deletions(-)

Detailed changes

crates/go_to_line/src/go_to_line.rs 🔗

@@ -1,8 +1,7 @@
 use std::sync::Arc;
 
 use editor::{
-    display_map::ToDisplayPoint, scroll::autoscroll::Autoscroll, DisplayPoint, Editor,
-    FILE_ROW_COLUMN_DELIMITER,
+    display_map::ToDisplayPoint, scroll::autoscroll::Autoscroll, Editor, FILE_ROW_COLUMN_DELIMITER,
 };
 use gpui::{
     actions, elements::*, geometry::vector::Vector2F, AnyViewHandle, AppContext, Axis, Entity,
@@ -78,15 +77,16 @@ impl GoToLine {
 
     fn confirm(&mut self, _: &Confirm, cx: &mut ViewContext<Self>) {
         self.prev_scroll_position.take();
-        self.active_editor.update(cx, |active_editor, cx| {
-            if let Some(rows) = active_editor.highlighted_rows() {
+        if let Some(point) = self.point_from_query(cx) {
+            self.active_editor.update(cx, |active_editor, cx| {
                 let snapshot = active_editor.snapshot(cx).display_snapshot;
-                let position = DisplayPoint::new(rows.start, 0).to_point(&snapshot);
+                let point = snapshot.buffer_snapshot.clip_point(point, Bias::Left);
                 active_editor.change_selections(Some(Autoscroll::center()), cx, |s| {
-                    s.select_ranges([position..position])
+                    s.select_ranges([point..point])
                 });
-            }
-        });
+            });
+        }
+
         cx.emit(Event::Dismissed);
     }
 
@@ -99,16 +99,7 @@ impl GoToLine {
         match event {
             editor::Event::Blurred => cx.emit(Event::Dismissed),
             editor::Event::BufferEdited { .. } => {
-                let line_editor = self.line_editor.read(cx).text(cx);
-                let mut components = line_editor
-                    .splitn(2, FILE_ROW_COLUMN_DELIMITER)
-                    .map(str::trim)
-                    .fuse();
-                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.saturating_sub(1), column.unwrap_or(0).saturating_sub(1))
-                }) {
+                if let Some(point) = self.point_from_query(cx) {
                     self.active_editor.update(cx, |active_editor, cx| {
                         let snapshot = active_editor.snapshot(cx).display_snapshot;
                         let point = snapshot.buffer_snapshot.clip_point(point, Bias::Left);
@@ -123,6 +114,20 @@ impl GoToLine {
             _ => {}
         }
     }
+
+    fn point_from_query(&self, cx: &ViewContext<Self>) -> Option<Point> {
+        let line_editor = self.line_editor.read(cx).text(cx);
+        let mut components = line_editor
+            .splitn(2, FILE_ROW_COLUMN_DELIMITER)
+            .map(str::trim)
+            .fuse();
+        let row = components.next().and_then(|row| row.parse::<u32>().ok())?;
+        let column = components.next().and_then(|col| col.parse::<u32>().ok());
+        Some(Point::new(
+            row.saturating_sub(1),
+            column.unwrap_or(0).saturating_sub(1),
+        ))
+    }
 }
 
 impl Entity for GoToLine {