From c2eaf6128e380e52d8487d42e952d909c051fa68 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Wed, 8 Jun 2022 15:08:11 +0200 Subject: [PATCH] Move `ProjectDiagnosticsEditor::jump` to `Editor::jump` --- crates/diagnostics/src/diagnostics.rs | 27 +---------------- crates/editor/src/editor.rs | 43 +++++++++++++++++++++++---- 2 files changed, 39 insertions(+), 31 deletions(-) diff --git a/crates/diagnostics/src/diagnostics.rs b/crates/diagnostics/src/diagnostics.rs index 2e7525cf14..a2f16fa0c4 100644 --- a/crates/diagnostics/src/diagnostics.rs +++ b/crates/diagnostics/src/diagnostics.rs @@ -28,7 +28,7 @@ use std::{ path::PathBuf, sync::Arc, }; -use util::{ResultExt, TryFutureExt}; +use util::TryFutureExt; use workspace::{ItemHandle as _, ItemNavHistory, Workspace}; actions!(diagnostics, [Deploy]); @@ -39,7 +39,6 @@ const CONTEXT_LINE_COUNT: u32 = 1; pub fn init(cx: &mut MutableAppContext) { cx.add_action(ProjectDiagnosticsEditor::deploy); - cx.add_action(ProjectDiagnosticsEditor::jump); items::init(cx); } @@ -195,30 +194,6 @@ impl ProjectDiagnosticsEditor { } } - fn jump(workspace: &mut Workspace, action: &Jump, cx: &mut ViewContext) { - let editor = workspace.open_path(action.path.clone(), true, cx); - let position = action.position; - let anchor = action.anchor; - cx.spawn_weak(|_, mut cx| async move { - let editor = editor.await.log_err()?.downcast::()?; - editor.update(&mut cx, |editor, cx| { - let buffer = editor.buffer().read(cx).as_singleton()?; - let buffer = buffer.read(cx); - let cursor = if buffer.can_resolve(&anchor) { - anchor.to_point(buffer) - } else { - buffer.clip_point(position, Bias::Left) - }; - editor.change_selections(Some(Autoscroll::Newest), cx, |s| { - s.select_ranges([cursor..cursor]); - }); - Some(()) - })?; - Some(()) - }) - .detach() - } - fn update_excerpts(&mut self, language_server_id: Option, cx: &mut ViewContext) { let mut paths = Vec::new(); self.paths_to_update.retain(|path, server_id| { diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 580635a35b..6e1b146bad 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -40,7 +40,7 @@ pub use multi_buffer::{ ToPoint, }; use ordered_float::OrderedFloat; -use project::{HoverBlock, Project, ProjectTransaction}; +use project::{HoverBlock, Project, ProjectPath, ProjectTransaction}; use selections_collection::{resolve_multiple, MutableSelectionsCollection, SelectionsCollection}; use serde::{Deserialize, Serialize}; use settings::Settings; @@ -86,6 +86,13 @@ pub struct HoverAt { point: Option, } +#[derive(Clone, Debug, PartialEq)] +pub struct Jump { + path: ProjectPath, + position: Point, + anchor: language::Anchor, +} + #[derive(Clone, Deserialize, PartialEq)] pub struct Input(pub String); @@ -216,7 +223,7 @@ impl_actions!( ] ); -impl_internal_actions!(editor, [Scroll, Select, HoverAt]); +impl_internal_actions!(editor, [Scroll, Select, HoverAt, Jump]); enum DocumentHighlightRead {} enum DocumentHighlightWrite {} @@ -306,6 +313,7 @@ pub fn init(cx: &mut MutableAppContext) { cx.add_action(Editor::hover); cx.add_action(Editor::hover_at); cx.add_action(Editor::open_excerpts); + cx.add_action(Editor::jump); cx.add_action(Editor::restart_language_server); cx.add_async_action(Editor::confirm_completion); cx.add_async_action(Editor::confirm_code_action); @@ -5826,6 +5834,30 @@ impl Editor { nav_history.borrow_mut().enable(); }); } + + fn jump(workspace: &mut Workspace, action: &Jump, cx: &mut ViewContext) { + let editor = workspace.open_path(action.path.clone(), true, cx); + let position = action.position; + let anchor = action.anchor; + cx.spawn_weak(|_, mut cx| async move { + let editor = editor.await.log_err()?.downcast::()?; + editor.update(&mut cx, |editor, cx| { + let buffer = editor.buffer().read(cx).as_singleton()?; + let buffer = buffer.read(cx); + let cursor = if buffer.can_resolve(&anchor) { + language::ToPoint::to_point(&anchor, buffer) + } else { + buffer.clip_point(position, Bias::Left) + }; + editor.change_selections(Some(Autoscroll::Newest), cx, |s| { + s.select_ranges([cursor..cursor]); + }); + Some(()) + })?; + Some(()) + }) + .detach() + } } impl EditorSnapshot { @@ -9647,9 +9679,10 @@ mod tests { [aaaa (bbbb] cccc)"}); - let excerpt_ranges = excerpt_ranges - .into_iter() - .map(|context| ExcerptRange { context, primary: None }); + let excerpt_ranges = excerpt_ranges.into_iter().map(|context| ExcerptRange { + context, + primary: None, + }); let buffer = cx.add_model(|cx| Buffer::new(0, initial_text, cx)); let multibuffer = cx.add_model(|cx| { let mut multibuffer = MultiBuffer::new(0);