From 5751303ea4423ca4758561616474ff018331d293 Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Tue, 7 Nov 2023 13:42:33 -0700 Subject: [PATCH] MOAR CODE --- crates/go_to_line2/src/go_to_line.rs | 163 ++++++++++++++++----------- crates/workspace2/src/modal_layer.rs | 8 +- 2 files changed, 105 insertions(+), 66 deletions(-) diff --git a/crates/go_to_line2/src/go_to_line.rs b/crates/go_to_line2/src/go_to_line.rs index c2e2f28ab2..d280f31de2 100644 --- a/crates/go_to_line2/src/go_to_line.rs +++ b/crates/go_to_line2/src/go_to_line.rs @@ -3,22 +3,27 @@ use gpui::{ VisualContext, }; use ui::modal; +use editor::{scroll::autoscroll::Autoscroll, Editor}; +use gpui::{ + actions, div, px, red, AppContext, Div, EventEmitter, ParentElement, Render, Styled, View, + ViewContext, VisualContext, +}; +use text::{Bias, Point}; +use ui::modal; +use util::paths::FILE_ROW_COLUMN_DELIMITER; use workspace::ModalRegistry; -actions!(Toggle); +actions!(Toggle, Cancel, Confirm); pub fn init(cx: &mut AppContext) { cx.register_action_type::(); cx.global_mut::() - .register_modal(Toggle, |_, cx| { - // if let Some(editor) = workspace - // .active_item(cx) - // .and_then(|active_item| active_item.downcast::()) - // { - // cx.build_view(|cx| GoToLine::new(editor, cx)) - // } - let view = cx.build_view(|_| GoToLine); - view + .register_modal(Toggle, |workspace, cx| { + let editor = workspace + .active_item(cx) + .and_then(|active_item| active_item.downcast::())?; + + Some(cx.build_view(|cx| GoToLine::new(editor, cx))) }); // cx.add_action(GoToLine::toggle); @@ -26,13 +31,96 @@ pub fn init(cx: &mut AppContext) { // cx.add_action(GoToLine::cancel); } -pub struct GoToLine; +pub struct GoToLine { + line_editor: View, + active_editor: View, +} + +pub enum Event { + Dismissed, +} + +impl EventEmitter for GoToLine { + type Event = Event; +} + +impl GoToLine { + pub fn new(active_editor: View, cx: &mut ViewContext) -> Self { + let line_editor = cx.build_view(|cx| Editor::single_line(cx)); + cx.subscribe(&line_editor, Self::on_line_editor_event) + .detach(); + + Self { + line_editor, + active_editor, + } + } + + fn on_line_editor_event( + &mut self, + _: View, + event: &editor::Event, + cx: &mut ViewContext, + ) { + match event { + 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(); + } + } + _ => {} + } + } + + fn point_from_query(&self, cx: &ViewContext) -> Option { + // todo!() + let line_editor = "2:2"; //self.line_editor.read(cx).text(cx); + let mut components = line_editor + .splitn(2, FILE_ROW_COLUMN_DELIMITER) + .map(str::trim) + .fuse(); + let row = components.next().and_then(|row| row.parse::().ok())?; + let column = components.next().and_then(|col| col.parse::().ok()); + Some(Point::new( + row.saturating_sub(1), + column.unwrap_or(0).saturating_sub(1), + )) + } + + fn cancel(&mut self, _: &Cancel, cx: &mut ViewContext) { + cx.emit(Event::Dismissed); + } + + fn confirm(&mut self, _: &Confirm, cx: &mut ViewContext) { + 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 Render for GoToLine { type Element = Div; fn render(&mut self, cx: &mut ViewContext) -> Self::Element { - modal(cx).child(div().m_4().bg(red()).w(px(100.0)).h(px(100.0))) + modal(cx).child(self.line_editor.clone()).child("blah blah") } } @@ -51,14 +139,6 @@ impl Render for GoToLine { // impl GoToLine { // pub fn new(active_editor: View, cx: &mut ViewContext) -> Self { -// // let line_editor = cx.build_view(|cx| { -// // Editor::single_line( -// // Some(Arc::new(|theme| theme.picker.input_editor.clone())), -// // cx, -// // ) -// // }); -// // cx.subscribe(&line_editor, Self::on_line_editor_event) -// // .detach(); // let (scroll_position, cursor_point, max_point) = active_editor.update(cx, |editor, cx| { // let scroll_position = editor.scroll_position(cx); @@ -101,49 +181,6 @@ impl Render for GoToLine { // cx.emit(Event::Dismissed); // } -// fn on_line_editor_event( -// &mut self, -// _: View, -// event: &editor::Event, -// cx: &mut ViewContext, -// ) { -// match event { -// 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(); -// } -// } -// _ => {} -// } -// } - -// fn point_from_query(&self, cx: &ViewContext) -> Option { -// return None; -// // todo!() -// // let line_editor = self.line_editor.read(cx).text(cx); -// // let mut components = line_editor -// // .splitn(2, FILE_ROW_COLUMN_DELIMITER) -// // .map(str::trim) -// // .fuse(); -// // let row = components.next().and_then(|row| row.parse::().ok())?; -// // let column = components.next().and_then(|col| col.parse::().ok()); -// // Some(Point::new( -// // row.saturating_sub(1), -// // column.unwrap_or(0).saturating_sub(1), -// // )) -// } -// } - // impl EventEmitter for GoToLine { // type Event = Event; // } diff --git a/crates/workspace2/src/modal_layer.rs b/crates/workspace2/src/modal_layer.rs index a0c3515245..298e5646b7 100644 --- a/crates/workspace2/src/modal_layer.rs +++ b/crates/workspace2/src/modal_layer.rs @@ -33,7 +33,7 @@ impl ModalRegistry { pub fn register_modal(&mut self, action: A, build_view: B) where V: Render, - B: Fn(&Workspace, &mut ViewContext) -> View + 'static, + B: Fn(&Workspace, &mut ViewContext) -> Option> + 'static, { let build_view = Arc::new(build_view); @@ -47,12 +47,14 @@ impl ModalRegistry { event: &A, phase: DispatchPhase, cx: &mut ViewContext| { - dbg!("GOT HERE"); if phase == DispatchPhase::Capture { return; } - let new_modal = (build_view)(workspace, cx); + let Some(new_modal) = (build_view)(workspace, cx) else { + return; + }; + workspace.modal_layer.update(cx, |modal_layer, _| { modal_layer.open_modal = Some(new_modal.into()); });