go_to_line.rs

  1use std::sync::Arc;
  2
  3use editor::{display_map::ToDisplayPoint, scroll::autoscroll::Autoscroll, Editor};
  4use gpui::{
  5    actions, elements::*, geometry::vector::Vector2F, AnyViewHandle, AppContext, Axis, Entity,
  6    View, ViewContext, ViewHandle,
  7};
  8use menu::{Cancel, Confirm};
  9use text::{Bias, Point};
 10use util::paths::FILE_ROW_COLUMN_DELIMITER;
 11use workspace::{Modal, Workspace};
 12
 13actions!(go_to_line, [Toggle]);
 14
 15pub fn init(cx: &mut AppContext) {
 16    cx.add_action(GoToLine::toggle);
 17    cx.add_action(GoToLine::confirm);
 18    cx.add_action(GoToLine::cancel);
 19}
 20
 21pub struct GoToLine {
 22    line_editor: ViewHandle<Editor>,
 23    active_editor: ViewHandle<Editor>,
 24    prev_scroll_position: Option<Vector2F>,
 25    cursor_point: Point,
 26    max_point: Point,
 27}
 28
 29pub enum Event {
 30    Dismissed,
 31}
 32
 33impl GoToLine {
 34    pub fn new(active_editor: ViewHandle<Editor>, cx: &mut ViewContext<Self>) -> Self {
 35        let line_editor = cx.add_view(|cx| {
 36            Editor::single_line(
 37                Some(Arc::new(|theme| theme.picker.input_editor.clone())),
 38                cx,
 39            )
 40        });
 41        cx.subscribe(&line_editor, Self::on_line_editor_event)
 42            .detach();
 43
 44        let (scroll_position, cursor_point, max_point) = active_editor.update(cx, |editor, cx| {
 45            let scroll_position = editor.scroll_position(cx);
 46            let buffer = editor.buffer().read(cx).snapshot(cx);
 47            (
 48                Some(scroll_position),
 49                editor.selections.newest(cx).head(),
 50                buffer.max_point(),
 51            )
 52        });
 53
 54        Self {
 55            line_editor,
 56            active_editor,
 57            prev_scroll_position: scroll_position,
 58            cursor_point,
 59            max_point,
 60        }
 61    }
 62
 63    fn toggle(workspace: &mut Workspace, _: &Toggle, cx: &mut ViewContext<Workspace>) {
 64        if let Some(editor) = workspace
 65            .active_item(cx)
 66            .and_then(|active_item| active_item.downcast::<Editor>())
 67        {
 68            workspace.toggle_modal(cx, |_, cx| cx.add_view(|cx| GoToLine::new(editor, cx)));
 69        }
 70    }
 71
 72    fn cancel(&mut self, _: &Cancel, cx: &mut ViewContext<Self>) {
 73        cx.emit(Event::Dismissed);
 74    }
 75
 76    fn confirm(&mut self, _: &Confirm, cx: &mut ViewContext<Self>) {
 77        self.prev_scroll_position.take();
 78        if let Some(point) = self.point_from_query(cx) {
 79            self.active_editor.update(cx, |active_editor, cx| {
 80                let snapshot = active_editor.snapshot(cx).display_snapshot;
 81                let point = snapshot.buffer_snapshot.clip_point(point, Bias::Left);
 82                active_editor.change_selections(Some(Autoscroll::center()), cx, |s| {
 83                    s.select_ranges([point..point])
 84                });
 85            });
 86        }
 87
 88        cx.emit(Event::Dismissed);
 89    }
 90
 91    fn on_line_editor_event(
 92        &mut self,
 93        _: ViewHandle<Editor>,
 94        event: &editor::Event,
 95        cx: &mut ViewContext<Self>,
 96    ) {
 97        match event {
 98            editor::Event::Blurred => cx.emit(Event::Dismissed),
 99            editor::Event::BufferEdited { .. } => {
100                if let Some(point) = self.point_from_query(cx) {
101                    self.active_editor.update(cx, |active_editor, cx| {
102                        let snapshot = active_editor.snapshot(cx).display_snapshot;
103                        let point = snapshot.buffer_snapshot.clip_point(point, Bias::Left);
104                        let display_point = point.to_display_point(&snapshot);
105                        let row = display_point.row();
106                        active_editor.highlight_rows(Some(row..row + 1));
107                        active_editor.request_autoscroll(Autoscroll::center(), cx);
108                    });
109                    cx.notify();
110                }
111            }
112            _ => {}
113        }
114    }
115
116    fn point_from_query(&self, cx: &ViewContext<Self>) -> Option<Point> {
117        let line_editor = self.line_editor.read(cx).text(cx);
118        let mut components = line_editor
119            .splitn(2, FILE_ROW_COLUMN_DELIMITER)
120            .map(str::trim)
121            .fuse();
122        let row = components.next().and_then(|row| row.parse::<u32>().ok())?;
123        let column = components.next().and_then(|col| col.parse::<u32>().ok());
124        Some(Point::new(
125            row.saturating_sub(1),
126            column.unwrap_or(0).saturating_sub(1),
127        ))
128    }
129}
130
131impl Entity for GoToLine {
132    type Event = Event;
133
134    fn release(&mut self, cx: &mut AppContext) {
135        let scroll_position = self.prev_scroll_position.take();
136        cx.update_window(self.active_editor.window_id(), |cx| {
137            self.active_editor.update(cx, |editor, cx| {
138                editor.highlight_rows(None);
139                if let Some(scroll_position) = scroll_position {
140                    editor.set_scroll_position(scroll_position, cx);
141                }
142            })
143        });
144    }
145}
146
147impl View for GoToLine {
148    fn ui_name() -> &'static str {
149        "GoToLine"
150    }
151
152    fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
153        let theme = &theme::current(cx).picker;
154
155        let label = format!(
156            "{}{FILE_ROW_COLUMN_DELIMITER}{} of {} lines",
157            self.cursor_point.row + 1,
158            self.cursor_point.column + 1,
159            self.max_point.row + 1
160        );
161
162        Flex::new(Axis::Vertical)
163            .with_child(
164                ChildView::new(&self.line_editor, cx)
165                    .contained()
166                    .with_style(theme.input_editor.container),
167            )
168            .with_child(
169                Label::new(label, theme.no_matches.label.clone())
170                    .contained()
171                    .with_style(theme.no_matches.container),
172            )
173            .contained()
174            .with_style(theme.container)
175            .constrained()
176            .with_max_width(500.0)
177            .into_any_named("go to line")
178    }
179
180    fn focus_in(&mut self, _: AnyViewHandle, cx: &mut ViewContext<Self>) {
181        cx.focus(&self.line_editor);
182    }
183}
184
185impl Modal for GoToLine {
186    fn dismiss_on_event(event: &Self::Event) -> bool {
187        matches!(event, Event::Dismissed)
188    }
189}