Make navigation history work with project diagnostics

Antonio Scandurra , Nathan Sobo , and Max Brunsfeld created

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
Co-Authored-By: Max Brunsfeld <max@zed.dev>

Change summary

crates/diagnostics/src/diagnostics.rs | 44 +++++++++++++++++++++++++---
crates/editor/src/editor.rs           | 10 +++++
crates/workspace/src/pane.rs          |  7 +---
3 files changed, 50 insertions(+), 11 deletions(-)

Detailed changes

crates/diagnostics/src/diagnostics.rs 🔗

@@ -15,7 +15,14 @@ use gpui::{
 use language::{Bias, Buffer, Diagnostic, DiagnosticEntry, Point, Selection, SelectionGoal};
 use postage::watch;
 use project::{Project, ProjectPath};
-use std::{any::TypeId, cmp::Ordering, mem, ops::Range, path::PathBuf, sync::Arc};
+use std::{
+    any::{Any, TypeId},
+    cmp::Ordering,
+    mem,
+    ops::Range,
+    path::PathBuf,
+    sync::Arc,
+};
 use util::TryFutureExt;
 use workspace::{ItemNavHistory, Workspace};
 
@@ -517,10 +524,19 @@ impl workspace::Item for ProjectDiagnostics {
     fn build_view(
         handle: ModelHandle<Self>,
         workspace: &Workspace,
-        _: ItemNavHistory,
+        nav_history: ItemNavHistory,
         cx: &mut ViewContext<Self::View>,
     ) -> Self::View {
-        ProjectDiagnosticsEditor::new(handle, workspace.weak_handle(), workspace.settings(), cx)
+        let diagnostics = ProjectDiagnosticsEditor::new(
+            handle,
+            workspace.weak_handle(),
+            workspace.settings(),
+            cx,
+        );
+        diagnostics
+            .editor
+            .update(cx, |editor, _| editor.set_nav_history(Some(nav_history)));
+        diagnostics
     }
 
     fn project_entry(&self) -> Option<project::ProjectEntry> {
@@ -543,6 +559,11 @@ impl workspace::ItemView for ProjectDiagnosticsEditor {
         None
     }
 
+    fn navigate(&mut self, data: Box<dyn Any>, cx: &mut ViewContext<Self>) {
+        self.editor
+            .update(cx, |editor, cx| editor.navigate(data, cx));
+    }
+
     fn is_dirty(&self, cx: &AppContext) -> bool {
         self.excerpts.read(cx).read(cx).is_dirty()
     }
@@ -587,12 +608,21 @@ impl workspace::ItemView for ProjectDiagnosticsEditor {
     where
         Self: Sized,
     {
-        Some(ProjectDiagnosticsEditor::new(
+        let diagnostics = ProjectDiagnosticsEditor::new(
             self.model.clone(),
             self.workspace.clone(),
             self.settings.clone(),
             cx,
-        ))
+        );
+        diagnostics.editor.update(cx, |editor, cx| {
+            let nav_history = self
+                .editor
+                .read(cx)
+                .nav_history()
+                .map(|nav_history| ItemNavHistory::new(nav_history.history(), &cx.handle()));
+            editor.set_nav_history(nav_history);
+        });
+        Some(diagnostics)
     }
 
     fn act_as_type(
@@ -609,6 +639,10 @@ impl workspace::ItemView for ProjectDiagnosticsEditor {
             None
         }
     }
+
+    fn deactivated(&mut self, cx: &mut ViewContext<Self>) {
+        self.editor.update(cx, |editor, cx| editor.deactivated(cx));
+    }
 }
 
 fn path_header_renderer(buffer: ModelHandle<Buffer>, build_settings: BuildSettings) -> RenderBlock {

crates/editor/src/editor.rs 🔗

@@ -470,7 +470,7 @@ impl Editor {
         clone.nav_history = self
             .nav_history
             .as_ref()
-            .map(|nav_history| nav_history.clone(&cx.handle()));
+            .map(|nav_history| ItemNavHistory::new(nav_history.history(), &cx.handle()));
         clone
     }
 
@@ -2459,6 +2459,14 @@ impl Editor {
         self.update_selections(vec![selection], Some(Autoscroll::Fit), cx);
     }
 
+    pub fn set_nav_history(&mut self, nav_history: Option<ItemNavHistory>) {
+        self.nav_history = nav_history;
+    }
+
+    pub fn nav_history(&self) -> Option<&ItemNavHistory> {
+        self.nav_history.as_ref()
+    }
+
     fn push_to_nav_history(
         &self,
         position: Anchor,

crates/workspace/src/pane.rs 🔗

@@ -551,11 +551,8 @@ impl ItemNavHistory {
         }
     }
 
-    pub fn clone<T: ItemView>(&self, item_view: &ViewHandle<T>) -> Self {
-        Self {
-            history: self.history.clone(),
-            item_view: Rc::new(item_view.downgrade()),
-        }
+    pub fn history(&self) -> Rc<RefCell<NavHistory>> {
+        self.history.clone()
     }
 
     pub fn push<D: 'static + Any>(&self, data: Option<D>) {