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