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