From 703f1c3be086844405be3950a513e17b6eb4bed3 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Fri, 1 Apr 2022 14:02:49 +0200 Subject: [PATCH] Introduce `workspace::Item::reload` to manually trigger a reload --- crates/diagnostics/src/diagnostics.rs | 8 ++++++++ crates/editor/src/items.rs | 25 +++++++++++++++++++++++++ crates/search/src/project_search.rs | 9 +++++++++ crates/workspace/src/workspace.rs | 15 +++++++++++++++ 4 files changed, 57 insertions(+) diff --git a/crates/diagnostics/src/diagnostics.rs b/crates/diagnostics/src/diagnostics.rs index 56de434cf4..da50e99f1e 100644 --- a/crates/diagnostics/src/diagnostics.rs +++ b/crates/diagnostics/src/diagnostics.rs @@ -478,6 +478,14 @@ impl workspace::Item for ProjectDiagnosticsEditor { self.editor.save(project, cx) } + fn reload( + &mut self, + project: ModelHandle, + cx: &mut ViewContext, + ) -> Task> { + self.editor.reload(project, cx) + } + fn can_save_as(&self, _: &AppContext) -> bool { false } diff --git a/crates/editor/src/items.rs b/crates/editor/src/items.rs index 79b25f8f60..67d5aee773 100644 --- a/crates/editor/src/items.rs +++ b/crates/editor/src/items.rs @@ -371,6 +371,31 @@ impl Item for Editor { }) } + fn reload( + &mut self, + project: ModelHandle, + cx: &mut ViewContext, + ) -> Task> { + let buffer = self.buffer().clone(); + let buffers = self.buffer.read(cx).all_buffers(); + let reload_buffers = + project.update(cx, |project, cx| project.reload_buffers(buffers, true, cx)); + cx.spawn(|this, mut cx| async move { + let transaction = reload_buffers.log_err().await; + this.update(&mut cx, |editor, cx| { + editor.request_autoscroll(Autoscroll::Fit, cx) + }); + buffer.update(&mut cx, |buffer, _| { + if let Some(transaction) = transaction { + if !buffer.is_singleton() { + buffer.push_transaction(&transaction.0); + } + } + }); + Ok(()) + }) + } + fn should_activate_item_on_event(event: &Event) -> bool { matches!(event, Event::Activate) } diff --git a/crates/search/src/project_search.rs b/crates/search/src/project_search.rs index 65bb07ae46..745f23154f 100644 --- a/crates/search/src/project_search.rs +++ b/crates/search/src/project_search.rs @@ -280,6 +280,15 @@ impl Item for ProjectSearchView { unreachable!("save_as should not have been called") } + fn reload( + &mut self, + project: ModelHandle, + cx: &mut ViewContext, + ) -> Task> { + self.results_editor + .update(cx, |editor, cx| editor.reload(project, cx)) + } + fn clone_on_split(&self, cx: &mut ViewContext) -> Option where Self: Sized, diff --git a/crates/workspace/src/workspace.rs b/crates/workspace/src/workspace.rs index 9929cd9a51..073808e71a 100644 --- a/crates/workspace/src/workspace.rs +++ b/crates/workspace/src/workspace.rs @@ -237,6 +237,11 @@ pub trait Item: View { abs_path: PathBuf, cx: &mut ViewContext, ) -> Task>; + fn reload( + &mut self, + project: ModelHandle, + cx: &mut ViewContext, + ) -> Task>; fn should_activate_item_on_event(_: &Self::Event) -> bool { false } @@ -380,6 +385,8 @@ pub trait ItemHandle: 'static + fmt::Debug { abs_path: PathBuf, cx: &mut MutableAppContext, ) -> Task>; + fn reload(&self, project: ModelHandle, cx: &mut MutableAppContext) + -> Task>; fn act_as_type(&self, type_id: TypeId, cx: &AppContext) -> Option; fn to_followable_item_handle(&self, cx: &AppContext) -> Option>; } @@ -531,6 +538,14 @@ impl ItemHandle for ViewHandle { self.update(cx, |item, cx| item.save_as(project, abs_path, cx)) } + fn reload( + &self, + project: ModelHandle, + cx: &mut MutableAppContext, + ) -> Task> { + self.update(cx, |item, cx| item.reload(project, cx)) + } + fn is_dirty(&self, cx: &AppContext) -> bool { self.read(cx).is_dirty(cx) }