tidy tidy

Conrad Irwin created

Change summary

crates/go_to_line2/src/go_to_line.rs | 267 +++++++----------------------
crates/workspace2/src/workspace2.rs  |   8 
2 files changed, 69 insertions(+), 206 deletions(-)

Detailed changes

crates/go_to_line2/src/go_to_line.rs 🔗

@@ -1,9 +1,10 @@
-use editor::Editor;
+use editor::{display_map::ToDisplayPoint, scroll::autoscroll::Autoscroll, Editor};
 use gpui::{
     actions, div, AppContext, Div, EventEmitter, ParentElement, Render, SharedString,
-    StatefulInteractivity, StatelessInteractive, Styled, View, ViewContext, VisualContext,
+    StatefulInteractivity, StatelessInteractive, Styled, Subscription, View, ViewContext,
+    VisualContext, WindowContext,
 };
-use text::Point;
+use text::{Bias, Point};
 use theme::ActiveTheme;
 use ui::{h_stack, modal, v_stack, Label, LabelColor};
 use util::paths::FILE_ROW_COLUMN_DELIMITER;
@@ -23,39 +24,17 @@ pub fn init(cx: &mut AppContext) {
 
                     Some(cx.build_view(|cx| GoToLine::new(editor, cx)))
                 });
-            dbg!("hey!");
         },
     )
     .detach();
-
-    // // cx.window_global()
-    // // cx.window_global::<Workspace>
-    // // cx.window_global::<ActiveEditor>()
-    // Workspace::on_init(|workspace, cx| {
-    //     workspace.on_open_item()
-    // });
-
-    // Editor::on_init(|editor, cx|{
-
-    // })
-
-    // Editor::register_action(|_editor, _: &Toggle, cx| {
-    //     dbg!("HEY!");
-    //     // let editor = cx.view();
-    //     // cx.update_window(cx.window().handle(), |cx, view| {
-    //     //     let workspace = view.downcast::<Workspace>();
-    //     // })
-    //     // workspace.show_modal(cx.build_view(|cx| GoToLine::new(editor, cx)))
-    // })
-    // cx.global_mut::<ModalRegistry>()
-    //     .register_modal(Toggle, |workspace, cx| {
-    //     });
 }
 
 pub struct GoToLine {
     line_editor: View<Editor>,
-    #[allow(unused)] // todo!()
     active_editor: View<Editor>,
+    current_text: SharedString,
+    prev_scroll_position: Option<gpui::Point<f32>>,
+    _subscriptions: Vec<Subscription>,
 }
 
 pub enum Event {
@@ -69,20 +48,45 @@ impl EventEmitter for GoToLine {
 impl GoToLine {
     pub fn new(active_editor: View<Editor>, cx: &mut ViewContext<Self>) -> Self {
         let line_editor = cx.build_view(|cx| {
-            let mut editor = Editor::single_line(cx);
-            editor.set_placeholder_text("Find something", cx);
+            let editor = Editor::single_line(cx);
             editor.focus(cx);
             editor
         });
-        cx.subscribe(&line_editor, Self::on_line_editor_event)
-            .detach();
+        let line_editor_change = cx.subscribe(&line_editor, Self::on_line_editor_event);
+
+        let editor = active_editor.read(cx);
+        let cursor = editor.selections.last::<Point>(cx).head();
+        let last_line = editor.buffer().read(cx).snapshot(cx).max_point().row;
+        let scroll_position = active_editor.update(cx, |editor, cx| editor.scroll_position(cx));
+
+        let current_text = format!(
+            "line {} of {} (column {})",
+            cursor.row + 1,
+            last_line + 1,
+            cursor.column + 1,
+        );
 
         Self {
             line_editor,
             active_editor,
+            current_text: current_text.into(),
+            prev_scroll_position: Some(scroll_position),
+            _subscriptions: vec![line_editor_change, cx.on_release(Self::release)],
         }
     }
 
+    fn release(&mut self, cx: &mut WindowContext) {
+        let scroll_position = self.prev_scroll_position.take();
+        self.active_editor.update(cx, |editor, cx| {
+            editor.focus(cx);
+            editor.highlight_rows(None);
+            if let Some(scroll_position) = scroll_position {
+                editor.set_scroll_position(scroll_position, cx);
+            }
+            cx.notify();
+        })
+    }
+
     fn on_line_editor_event(
         &mut self,
         _: View<Editor>,
@@ -90,28 +94,28 @@ impl GoToLine {
         cx: &mut ViewContext<Self>,
     ) {
         match event {
+            // todo!() this isn't working...
             editor::Event::Blurred => cx.emit(Event::Dismissed),
-            editor::Event::BufferEdited { .. } => {
-                //  if let Some(point) = self.point_from_query(cx) {
-                // todo!()
-                // self.active_editor.update(cx, |active_editor, cx| {
-                //     let snapshot = active_editor.snapshot(cx).display_snapshot;
-                //     let point = snapshot.buffer_snapshot.clip_point(point, Bias::Left);
-                //     let display_point = point.to_display_point(&snapshot);
-                //     let row = display_point.row();
-                //     active_editor.highlight_rows(Some(row..row + 1));
-                //     active_editor.request_autoscroll(Autoscroll::center(), cx);
-                // });
-                //       cx.notify();
-                //  }
-            }
+            editor::Event::BufferEdited { .. } => self.highlight_current_line(cx),
             _ => {}
         }
     }
 
-    #[allow(unused)]
+    fn highlight_current_line(&mut self, cx: &mut ViewContext<Self>) {
+        if let Some(point) = self.point_from_query(cx) {
+            self.active_editor.update(cx, |active_editor, cx| {
+                let snapshot = active_editor.snapshot(cx).display_snapshot;
+                let point = snapshot.buffer_snapshot.clip_point(point, Bias::Left);
+                let display_point = point.to_display_point(&snapshot);
+                let row = display_point.row();
+                active_editor.highlight_rows(Some(row..row + 1));
+                active_editor.request_autoscroll(Autoscroll::center(), cx);
+            });
+            cx.notify();
+        }
+    }
+
     fn point_from_query(&self, cx: &ViewContext<Self>) -> Option<Point> {
-        // todo!()
         let line_editor = self.line_editor.read(cx).text(cx);
         let mut components = line_editor
             .splitn(2, FILE_ROW_COLUMN_DELIMITER)
@@ -126,26 +130,22 @@ impl GoToLine {
     }
 
     fn cancel(&mut self, _: &menu::Cancel, cx: &mut ViewContext<Self>) {
-        println!("CANCLE");
         cx.emit(Event::Dismissed);
     }
 
-    fn confirm(&mut self, _: &menu::Confirm, _cx: &mut ViewContext<Self>) {
-        // // if let Some(point) = self.point_from_query(cx) {
-        // //     self.active_editor.update(cx, |active_editor, cx| {
-        // //         let snapshot = active_editor.snapshot(cx).display_snapshot;
-        // //         let point = snapshot.buffer_snapshot.clip_point(point, Bias::Left);
-        // //         active_editor.change_selections(Some(Autoscroll::center()), cx, |s| {
-        // //             s.select_ranges([point..point])
-        // //         });
-        // //     });
-        // // }
-
-        // cx.emit(Event::Dismissed);
-    }
+    fn confirm(&mut self, _: &menu::Confirm, cx: &mut ViewContext<Self>) {
+        if let Some(point) = self.point_from_query(cx) {
+            self.active_editor.update(cx, |active_editor, cx| {
+                let snapshot = active_editor.snapshot(cx).display_snapshot;
+                let point = snapshot.buffer_snapshot.clip_point(point, Bias::Left);
+                active_editor.change_selections(Some(Autoscroll::center()), cx, |s| {
+                    s.select_ranges([point..point])
+                });
+            });
+            self.prev_scroll_position.take();
+        }
 
-    fn status_text(&self) -> SharedString {
-        "Default text".into()
+        cx.emit(Event::Dismissed);
     }
 }
 
@@ -188,145 +188,8 @@ impl Render for GoToLine {
                             .justify_between()
                             .px_2()
                             .py_1()
-                            .child(Label::new(self.status_text()).color(LabelColor::Muted)),
+                            .child(Label::new(self.current_text.clone()).color(LabelColor::Muted)),
                     ),
             )
     }
 }
-
-// pub struct GoToLine {
-//     //line_editor: View<Editor>,
-//     active_editor: View<Editor>,
-//     prev_scroll_position: Option<gpui::Point<Pixels>>,
-//     cursor_point: Point,
-//     max_point: Point,
-//     has_focus: bool,
-// }
-
-// pub enum Event {
-//     Dismissed,
-// }
-
-// impl GoToLine {
-//     pub fn new(active_editor: View<Editor>, cx: &mut ViewContext<Self>) -> Self {
-
-//         let (scroll_position, cursor_point, max_point) = active_editor.update(cx, |editor, cx| {
-//             let scroll_position = editor.scroll_position(cx);
-//             let buffer = editor.buffer().read(cx).snapshot(cx);
-//             (
-//                 Some(scroll_position),
-//                 editor.selections.newest(cx).head(),
-//                 buffer.max_point(),
-//             )
-//         });
-
-//         cx.on_release(|_, on_release| {}).detach();
-
-//         Self {
-//             //line_editor,
-//             active_editor,
-//             prev_scroll_position: scroll_position,
-//             cursor_point,
-//             max_point,
-//             has_focus: false,
-//         }
-//     }
-
-//     fn cancel(&mut self, _: &Cancel, cx: &mut ViewContext<Self>) {
-//         cx.emit(Event::Dismissed);
-//     }
-
-//     fn confirm(&mut self, _: &Confirm, cx: &mut ViewContext<Self>) {
-//         self.prev_scroll_position.take();
-//         if let Some(point) = self.point_from_query(cx) {
-//             self.active_editor.update(cx, |active_editor, cx| {
-//                 let snapshot = active_editor.snapshot(cx).display_snapshot;
-//                 let point = snapshot.buffer_snapshot.clip_point(point, Bias::Left);
-//                 active_editor.change_selections(Some(Autoscroll::center()), cx, |s| {
-//                     s.select_ranges([point..point])
-//                 });
-//             });
-//         }
-
-//         cx.emit(Event::Dismissed);
-//     }
-
-// impl EventEmitter for GoToLine {
-//     type Event = Event;
-// }
-
-// impl Entity for GoToLine {
-//     fn release(&mut self, cx: &mut AppContext) {
-//         let scroll_position = self.prev_scroll_position.take();
-//         self.active_editor.window().update(cx, |cx| {
-//             self.active_editor.update(cx, |editor, cx| {
-//                 editor.highlight_rows(None);
-//                 if let Some(scroll_position) = scroll_position {
-//                     editor.set_scroll_position(scroll_position, cx);
-//                 }
-//             })
-//         });
-//     }
-// }
-
-// impl Render for GoToLine {
-//     type Element = Div<Self>;
-
-//     fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
-//         // todo!()
-//         div()
-//     }
-// }
-
-// impl View for GoToLine {
-//     fn ui_name() -> &'static str {
-//         "GoToLine"
-//     }
-
-//     fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
-//         let theme = &theme::current(cx).picker;
-
-//         let label = format!(
-//             "{}{FILE_ROW_COLUMN_DELIMITER}{} of {} lines",
-//             self.cursor_point.row + 1,
-//             self.cursor_point.column + 1,
-//             self.max_point.row + 1
-//         );
-
-//         Flex::new(Axis::Vertical)
-//             .with_child(
-//                 ChildView::new(&self.line_editor, cx)
-//                     .contained()
-//                     .with_style(theme.input_editor.container),
-//             )
-//             .with_child(
-//                 Label::new(label, theme.no_matches.label.clone())
-//                     .contained()
-//                     .with_style(theme.no_matches.container),
-//             )
-//             .contained()
-//             .with_style(theme.container)
-//             .constrained()
-//             .with_max_width(500.0)
-//             .into_any_named("go to line")
-//     }
-
-//     fn focus_in(&mut self, _: AnyView, cx: &mut ViewContext<Self>) {
-//         self.has_focus = true;
-//         cx.focus(&self.line_editor);
-//     }
-
-//     fn focus_out(&mut self, _: AnyView, _: &mut ViewContext<Self>) {
-//         self.has_focus = false;
-//     }
-// }
-
-// impl Modal for GoToLine {
-//     fn has_focus(&self) -> bool {
-//         self.has_focus
-//     }
-
-//     fn dismiss_on_event(event: &Self::Event) -> bool {
-//         matches!(event, Event::Dismissed)
-//     }
-// }

crates/workspace2/src/workspace2.rs 🔗

@@ -37,10 +37,10 @@ use futures::{
 };
 use gpui::{
     div, point, size, AnyModel, AnyView, AnyWeakView, AppContext, AsyncAppContext,
-    AsyncWindowContext, Bounds, Component, Context, Div, Entity, EntityId, EventEmitter,
-    FocusHandle, GlobalPixels, Model, ModelContext, ParentElement, Point, Render, Size,
-    StatefulInteractive, Styled, Subscription, Task, View, ViewContext, VisualContext, WeakView,
-    WindowBounds, WindowContext, WindowHandle, WindowOptions,
+    AsyncWindowContext, Bounds, Component, Div, Entity, EntityId, EventEmitter, FocusHandle,
+    GlobalPixels, Model, ModelContext, ParentElement, Point, Render, Size, StatefulInteractive,
+    Styled, Subscription, Task, View, ViewContext, VisualContext, WeakView, WindowBounds,
+    WindowContext, WindowHandle, WindowOptions,
 };
 use item::{FollowableItem, FollowableItemHandle, Item, ItemHandle, ItemSettings, ProjectItem};
 use itertools::Itertools;