From 83436213ade25ba3206bd2645671b2dd2f83c437 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Fri, 28 Apr 2023 14:02:44 +0200 Subject: [PATCH] Remove `Jump` internal action --- crates/diagnostics/src/diagnostics.rs | 5 ++++ crates/editor/src/editor.rs | 30 ++++++++++-------------- crates/editor/src/element.rs | 33 ++++++++++++++++++--------- crates/editor/src/items.rs | 6 ++--- crates/editor/src/scroll.rs | 16 ++++++------- crates/search/src/project_search.rs | 5 ++++ 6 files changed, 54 insertions(+), 41 deletions(-) diff --git a/crates/diagnostics/src/diagnostics.rs b/crates/diagnostics/src/diagnostics.rs index 53ac43be2e..f4b2451bbb 100644 --- a/crates/diagnostics/src/diagnostics.rs +++ b/crates/diagnostics/src/diagnostics.rs @@ -551,6 +551,11 @@ impl Item for ProjectDiagnosticsEditor { false } + fn added_to_workspace(&mut self, workspace: &mut Workspace, cx: &mut ViewContext) { + self.editor + .update(cx, |editor, cx| editor.added_to_workspace(workspace, cx)); + } + fn navigate(&mut self, data: Box, cx: &mut ViewContext) -> bool { self.editor .update(cx, |editor, cx| editor.navigate(data, cx)) diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 300feb173d..8a6c8a34fd 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -37,7 +37,7 @@ use gpui::{ executor, fonts::{self, HighlightStyle, TextStyle}, geometry::vector::Vector2F, - impl_actions, impl_internal_actions, + impl_actions, keymap_matcher::KeymapContext, platform::{CursorStyle, MouseButton}, serde_json::{self, json}, @@ -86,7 +86,7 @@ use std::{ pub use sum_tree::Bias; use theme::{DiagnosticStyle, Theme}; use util::{post_inc, RangeExt, ResultExt, TryFutureExt}; -use workspace::{ItemNavHistory, ViewId, Workspace, WorkspaceId}; +use workspace::{ItemNavHistory, ViewId, Workspace}; use crate::git::diff_hunk_to_display; @@ -104,13 +104,6 @@ pub struct SelectNext { pub replace_newest: bool, } -#[derive(Clone, Debug, PartialEq)] -pub struct Jump { - path: ProjectPath, - position: Point, - anchor: language::Anchor, -} - #[derive(Clone, Deserialize, PartialEq)] pub struct SelectToBeginningOfLine { #[serde(default)] @@ -282,8 +275,6 @@ impl_actions!( ] ); -impl_internal_actions!(editor, [Jump]); - enum DocumentHighlightRead {} enum DocumentHighlightWrite {} enum InputComposition {} @@ -377,7 +368,6 @@ pub fn init(cx: &mut AppContext) { cx.add_action(Editor::show_completions); cx.add_action(Editor::toggle_code_actions); cx.add_action(Editor::open_excerpts); - cx.add_action(Editor::jump); cx.add_action(Editor::toggle_soft_wrap); cx.add_action(Editor::reveal_in_finder); cx.add_action(Editor::copy_path); @@ -504,7 +494,7 @@ pub struct Editor { pending_rename: Option, searchable: bool, cursor_shape: CursorShape, - workspace_id: Option, + workspace: Option<(WeakViewHandle, i64)>, keymap_context_layers: BTreeMap, input_enabled: bool, read_only: bool, @@ -1277,7 +1267,7 @@ impl Editor { searchable: true, override_text_style: None, cursor_shape: Default::default(), - workspace_id: None, + workspace: None, keymap_context_layers: Default::default(), input_enabled: true, read_only: false, @@ -6751,10 +6741,14 @@ impl Editor { }); } - fn jump(workspace: &mut Workspace, action: &Jump, cx: &mut ViewContext) { - let editor = workspace.open_path(action.path.clone(), None, true, cx); - let position = action.position; - let anchor = action.anchor; + fn jump( + workspace: &mut Workspace, + path: ProjectPath, + position: Point, + anchor: language::Anchor, + cx: &mut ViewContext, + ) { + let editor = workspace.open_path(path, None, true, cx); cx.spawn(|_, mut cx| async move { let editor = editor .await? diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs index ef9769d1ee..1bfbc6117c 100644 --- a/crates/editor/src/element.rs +++ b/crates/editor/src/element.rs @@ -1435,18 +1435,15 @@ impl EditorElement { } => { let id = *id; let jump_icon = project::File::from_dyn(buffer.file()).map(|file| { - let jump_position = range + let jump_path = ProjectPath { + worktree_id: file.worktree_id(cx), + path: file.path.clone(), + }; + let jump_anchor = range .primary .as_ref() .map_or(range.context.start, |primary| primary.start); - let jump_action = crate::Jump { - path: ProjectPath { - worktree_id: file.worktree_id(cx), - path: file.path.clone(), - }, - position: language::ToPoint::to_point(&jump_position, buffer), - anchor: jump_position, - }; + let jump_position = language::ToPoint::to_point(&jump_anchor, buffer); enum JumpIcon {} MouseEventHandler::::new(id.into(), cx, |state, _| { @@ -1463,8 +1460,22 @@ impl EditorElement { .with_height(style.button_width) }) .with_cursor_style(CursorStyle::PointingHand) - .on_click(MouseButton::Left, move |_, _, cx| { - cx.dispatch_action(jump_action.clone()) + .on_click(MouseButton::Left, move |_, editor, cx| { + if let Some(workspace) = editor + .workspace + .as_ref() + .and_then(|(workspace, _)| workspace.upgrade(cx)) + { + workspace.update(cx, |workspace, cx| { + Editor::jump( + workspace, + jump_path.clone(), + jump_position, + jump_anchor, + cx, + ); + }); + } }) .with_tooltip::( id.into(), diff --git a/crates/editor/src/items.rs b/crates/editor/src/items.rs index 2eaf7b568e..83e971358d 100644 --- a/crates/editor/src/items.rs +++ b/crates/editor/src/items.rs @@ -794,7 +794,7 @@ impl Item for Editor { fn added_to_workspace(&mut self, workspace: &mut Workspace, cx: &mut ViewContext) { let workspace_id = workspace.database_id(); let item_id = cx.view_id(); - self.workspace_id = Some(workspace_id); + self.workspace = Some((workspace.weak_handle(), workspace.database_id())); fn serialize( buffer: ModelHandle, @@ -819,9 +819,9 @@ impl Item for Editor { serialize(buffer.clone(), workspace_id, item_id, cx); cx.subscribe(&buffer, |this, buffer, event, cx| { - if let Some(workspace_id) = this.workspace_id { + if let Some((_, workspace_id)) = this.workspace.as_ref() { if let language::Event::FileHandleChanged = event { - serialize(buffer, workspace_id, cx.view_id(), cx); + serialize(buffer, *workspace_id, cx.view_id(), cx); } } }) diff --git a/crates/editor/src/scroll.rs b/crates/editor/src/scroll.rs index ab8bbe0d5e..cda90c14c9 100644 --- a/crates/editor/src/scroll.rs +++ b/crates/editor/src/scroll.rs @@ -308,13 +308,9 @@ impl Editor { let map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); hide_hover(self, &HideHover, cx); - self.scroll_manager.set_scroll_position( - scroll_position, - &map, - local, - self.workspace_id, - cx, - ); + let workspace_id = self.workspace.as_ref().map(|workspace| workspace.1); + self.scroll_manager + .set_scroll_position(scroll_position, &map, local, workspace_id, cx); } pub fn scroll_position(&self, cx: &mut ViewContext) -> Vector2F { @@ -324,12 +320,13 @@ impl Editor { pub fn set_scroll_anchor(&mut self, scroll_anchor: ScrollAnchor, cx: &mut ViewContext) { hide_hover(self, &HideHover, cx); + let workspace_id = self.workspace.as_ref().map(|workspace| workspace.1); let top_row = scroll_anchor .top_anchor .to_point(&self.buffer().read(cx).snapshot(cx)) .row; self.scroll_manager - .set_anchor(scroll_anchor, top_row, true, self.workspace_id, cx); + .set_anchor(scroll_anchor, top_row, true, workspace_id, cx); } pub(crate) fn set_scroll_anchor_remote( @@ -338,12 +335,13 @@ impl Editor { cx: &mut ViewContext, ) { hide_hover(self, &HideHover, cx); + let workspace_id = self.workspace.as_ref().map(|workspace| workspace.1); let top_row = scroll_anchor .top_anchor .to_point(&self.buffer().read(cx).snapshot(cx)) .row; self.scroll_manager - .set_anchor(scroll_anchor, top_row, false, self.workspace_id, cx); + .set_anchor(scroll_anchor, top_row, false, workspace_id, cx); } pub fn scroll_screen(&mut self, amount: &ScrollAmount, cx: &mut ViewContext) { diff --git a/crates/search/src/project_search.rs b/crates/search/src/project_search.rs index 986a4b4bfb..ac478a8a2c 100644 --- a/crates/search/src/project_search.rs +++ b/crates/search/src/project_search.rs @@ -332,6 +332,11 @@ impl Item for ProjectSearchView { Some(Self::new(model, cx)) } + fn added_to_workspace(&mut self, workspace: &mut Workspace, cx: &mut ViewContext) { + self.results_editor + .update(cx, |editor, cx| editor.added_to_workspace(workspace, cx)); + } + fn set_nav_history(&mut self, nav_history: ItemNavHistory, cx: &mut ViewContext) { self.results_editor.update(cx, |editor, _| { editor.set_nav_history(Some(nav_history));