1use std::sync::Arc;
2
3use editor::{display_map::ToDisplayPoint, scroll::autoscroll::Autoscroll, DisplayPoint, 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 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 self.active_editor.update(cx, |active_editor, cx| {
79 if let Some(rows) = active_editor.highlighted_rows() {
80 let snapshot = active_editor.snapshot(cx).display_snapshot;
81 let position = DisplayPoint::new(rows.start, 0).to_point(&snapshot);
82 active_editor.change_selections(Some(Autoscroll::center()), cx, |s| {
83 s.select_ranges([position..position])
84 });
85 }
86 });
87 cx.emit(Event::Dismissed);
88 }
89
90 fn on_line_editor_event(
91 &mut self,
92 _: ViewHandle<Editor>,
93 event: &editor::Event,
94 cx: &mut ViewContext<Self>,
95 ) {
96 match event {
97 editor::Event::Blurred => cx.emit(Event::Dismissed),
98 editor::Event::BufferEdited { .. } => {
99 let line_editor = self.line_editor.read(cx).text(cx);
100 let mut components = line_editor.trim().split(&[',', ':'][..]);
101 let row = components.next().and_then(|row| row.parse::<u32>().ok());
102 let column = components.next().and_then(|row| row.parse::<u32>().ok());
103 if let Some(point) = row.map(|row| {
104 Point::new(
105 row.saturating_sub(1),
106 column.map(|column| column.saturating_sub(1)).unwrap_or(0),
107 )
108 }) {
109 self.active_editor.update(cx, |active_editor, cx| {
110 let snapshot = active_editor.snapshot(cx).display_snapshot;
111 let point = snapshot.buffer_snapshot.clip_point(point, Bias::Left);
112 let display_point = point.to_display_point(&snapshot);
113 let row = display_point.row();
114 active_editor.highlight_rows(Some(row..row + 1));
115 active_editor.request_autoscroll(Autoscroll::center(), cx);
116 });
117 cx.notify();
118 }
119 }
120 _ => {}
121 }
122 }
123}
124
125impl Entity for GoToLine {
126 type Event = Event;
127
128 fn release(&mut self, cx: &mut AppContext) {
129 let scroll_position = self.prev_scroll_position.take();
130 cx.update_window(self.active_editor.window_id(), |cx| {
131 self.active_editor.update(cx, |editor, cx| {
132 editor.highlight_rows(None);
133 if let Some(scroll_position) = scroll_position {
134 editor.set_scroll_position(scroll_position, cx);
135 }
136 })
137 });
138 }
139}
140
141impl View for GoToLine {
142 fn ui_name() -> &'static str {
143 "GoToLine"
144 }
145
146 fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
147 let theme = &cx.global::<Settings>().theme.picker;
148
149 let label = format!(
150 "{},{} of {} lines",
151 self.cursor_point.row + 1,
152 self.cursor_point.column + 1,
153 self.max_point.row + 1
154 );
155
156 Flex::new(Axis::Vertical)
157 .with_child(
158 ChildView::new(&self.line_editor, cx)
159 .contained()
160 .with_style(theme.input_editor.container),
161 )
162 .with_child(
163 Label::new(label, theme.no_matches.label.clone())
164 .contained()
165 .with_style(theme.no_matches.container),
166 )
167 .contained()
168 .with_style(theme.container)
169 .constrained()
170 .with_max_width(500.0)
171 .into_any_named("go to line")
172 }
173
174 fn focus_in(&mut self, _: AnyViewHandle, cx: &mut ViewContext<Self>) {
175 cx.focus(&self.line_editor);
176 }
177}
178
179impl Modal for GoToLine {
180 fn dismiss_on_event(event: &Self::Event) -> bool {
181 matches!(event, Event::Dismissed)
182 }
183}