Make Go To Line to respect column numbers

This commit is contained in:
Kirill Bulatov 2023-05-11 12:23:05 +03:00
parent e5bca9c871
commit 477bc8da05

View File

@ -1,8 +1,7 @@
use std::sync::Arc; use std::sync::Arc;
use editor::{ use editor::{
display_map::ToDisplayPoint, scroll::autoscroll::Autoscroll, DisplayPoint, Editor, display_map::ToDisplayPoint, scroll::autoscroll::Autoscroll, Editor, FILE_ROW_COLUMN_DELIMITER,
FILE_ROW_COLUMN_DELIMITER,
}; };
use gpui::{ use gpui::{
actions, elements::*, geometry::vector::Vector2F, AnyViewHandle, AppContext, Axis, Entity, actions, elements::*, geometry::vector::Vector2F, AnyViewHandle, AppContext, Axis, Entity,
@ -78,15 +77,16 @@ impl GoToLine {
fn confirm(&mut self, _: &Confirm, cx: &mut ViewContext<Self>) { fn confirm(&mut self, _: &Confirm, cx: &mut ViewContext<Self>) {
self.prev_scroll_position.take(); self.prev_scroll_position.take();
self.active_editor.update(cx, |active_editor, cx| { if let Some(point) = self.point_from_query(cx) {
if let Some(rows) = active_editor.highlighted_rows() { self.active_editor.update(cx, |active_editor, cx| {
let snapshot = active_editor.snapshot(cx).display_snapshot; 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| { active_editor.change_selections(Some(Autoscroll::center()), cx, |s| {
s.select_ranges([position..position]) s.select_ranges([point..point])
}); });
} });
}); }
cx.emit(Event::Dismissed); cx.emit(Event::Dismissed);
} }
@ -99,16 +99,7 @@ impl GoToLine {
match event { match event {
editor::Event::Blurred => cx.emit(Event::Dismissed), editor::Event::Blurred => cx.emit(Event::Dismissed),
editor::Event::BufferEdited { .. } => { editor::Event::BufferEdited { .. } => {
let line_editor = self.line_editor.read(cx).text(cx); if let Some(point) = self.point_from_query(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))
}) {
self.active_editor.update(cx, |active_editor, cx| { self.active_editor.update(cx, |active_editor, cx| {
let snapshot = active_editor.snapshot(cx).display_snapshot; let snapshot = active_editor.snapshot(cx).display_snapshot;
let point = snapshot.buffer_snapshot.clip_point(point, Bias::Left); 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 { impl Entity for GoToLine {