Implement session-global include_warnings in the diagnostic item (#21618)

Mikayla Maki created

Release Notes:

- Make the include warnings toggle in the diagnostic tab global for a
zed session.

Change summary

crates/diagnostics/src/diagnostics.rs       | 38 +++++++++++++++++++---
crates/diagnostics/src/diagnostics_tests.rs | 32 ++++++++++++++++--
2 files changed, 61 insertions(+), 9 deletions(-)

Detailed changes

crates/diagnostics/src/diagnostics.rs 🔗

@@ -16,8 +16,8 @@ use editor::{
 };
 use gpui::{
     actions, div, svg, AnyElement, AnyView, AppContext, Context, EventEmitter, FocusHandle,
-    FocusableView, HighlightStyle, InteractiveElement, IntoElement, Model, ParentElement, Render,
-    SharedString, Styled, StyledText, Subscription, Task, View, ViewContext, VisualContext,
+    FocusableView, Global, HighlightStyle, InteractiveElement, IntoElement, Model, ParentElement,
+    Render, SharedString, Styled, StyledText, Subscription, Task, View, ViewContext, VisualContext,
     WeakView, WindowContext,
 };
 use language::{
@@ -46,6 +46,9 @@ use workspace::{
 
 actions!(diagnostics, [Deploy, ToggleWarnings]);
 
+struct IncludeWarnings(bool);
+impl Global for IncludeWarnings {}
+
 pub fn init(cx: &mut AppContext) {
     ProjectDiagnosticsSettings::register(cx);
     cx.observe_new_views(ProjectDiagnosticsEditor::register)
@@ -117,6 +120,7 @@ impl ProjectDiagnosticsEditor {
 
     fn new_with_context(
         context: u32,
+        include_warnings: bool,
         project_handle: Model<Project>,
         workspace: WeakView<Workspace>,
         cx: &mut ViewContext<Self>,
@@ -186,19 +190,24 @@ impl ProjectDiagnosticsEditor {
             }
         })
         .detach();
+        cx.observe_global::<IncludeWarnings>(|this, cx| {
+            this.include_warnings = cx.global::<IncludeWarnings>().0;
+            this.update_all_excerpts(cx);
+        })
+        .detach();
 
         let project = project_handle.read(cx);
         let mut this = Self {
             project: project_handle.clone(),
             context,
             summary: project.diagnostic_summary(false, cx),
+            include_warnings,
             workspace,
             excerpts,
             focus_handle,
             editor,
             path_states: Default::default(),
             paths_to_update: Default::default(),
-            include_warnings: ProjectDiagnosticsSettings::get_global(cx).include_warnings,
             update_excerpts_task: None,
             _subscription: project_event_subscription,
         };
@@ -243,11 +252,13 @@ impl ProjectDiagnosticsEditor {
 
     fn new(
         project_handle: Model<Project>,
+        include_warnings: bool,
         workspace: WeakView<Workspace>,
         cx: &mut ViewContext<Self>,
     ) -> Self {
         Self::new_with_context(
             editor::DEFAULT_MULTIBUFFER_CONTEXT,
+            include_warnings,
             project_handle,
             workspace,
             cx,
@@ -259,8 +270,19 @@ impl ProjectDiagnosticsEditor {
             workspace.activate_item(&existing, true, true, cx);
         } else {
             let workspace_handle = cx.view().downgrade();
+
+            let include_warnings = match cx.try_global::<IncludeWarnings>() {
+                Some(include_warnings) => include_warnings.0,
+                None => ProjectDiagnosticsSettings::get_global(cx).include_warnings,
+            };
+
             let diagnostics = cx.new_view(|cx| {
-                ProjectDiagnosticsEditor::new(workspace.project().clone(), workspace_handle, cx)
+                ProjectDiagnosticsEditor::new(
+                    workspace.project().clone(),
+                    include_warnings,
+                    workspace_handle,
+                    cx,
+                )
             });
             workspace.add_item_to_active_pane(Box::new(diagnostics), None, true, cx);
         }
@@ -268,6 +290,7 @@ impl ProjectDiagnosticsEditor {
 
     fn toggle_warnings(&mut self, _: &ToggleWarnings, cx: &mut ViewContext<Self>) {
         self.include_warnings = !self.include_warnings;
+        cx.set_global(IncludeWarnings(self.include_warnings));
         self.update_all_excerpts(cx);
         cx.notify();
     }
@@ -740,7 +763,12 @@ impl Item for ProjectDiagnosticsEditor {
         Self: Sized,
     {
         Some(cx.new_view(|cx| {
-            ProjectDiagnosticsEditor::new(self.project.clone(), self.workspace.clone(), cx)
+            ProjectDiagnosticsEditor::new(
+                self.project.clone(),
+                self.include_warnings,
+                self.workspace.clone(),
+                cx,
+            )
         }))
     }
 

crates/diagnostics/src/diagnostics_tests.rs 🔗

@@ -151,7 +151,13 @@ async fn test_diagnostics(cx: &mut TestAppContext) {
 
     // Open the project diagnostics view while there are already diagnostics.
     let view = window.build_view(cx, |cx| {
-        ProjectDiagnosticsEditor::new_with_context(1, project.clone(), workspace.downgrade(), cx)
+        ProjectDiagnosticsEditor::new_with_context(
+            1,
+            true,
+            project.clone(),
+            workspace.downgrade(),
+            cx,
+        )
     });
     let editor = view.update(cx, |view, _| view.editor.clone());
 
@@ -459,7 +465,13 @@ async fn test_diagnostics_multiple_servers(cx: &mut TestAppContext) {
     let workspace = window.root(cx).unwrap();
 
     let view = window.build_view(cx, |cx| {
-        ProjectDiagnosticsEditor::new_with_context(1, project.clone(), workspace.downgrade(), cx)
+        ProjectDiagnosticsEditor::new_with_context(
+            1,
+            true,
+            project.clone(),
+            workspace.downgrade(),
+            cx,
+        )
     });
     let editor = view.update(cx, |view, _| view.editor.clone());
 
@@ -720,7 +732,13 @@ async fn test_random_diagnostics(cx: &mut TestAppContext, mut rng: StdRng) {
     let workspace = window.root(cx).unwrap();
 
     let mutated_view = window.build_view(cx, |cx| {
-        ProjectDiagnosticsEditor::new_with_context(1, project.clone(), workspace.downgrade(), cx)
+        ProjectDiagnosticsEditor::new_with_context(
+            1,
+            true,
+            project.clone(),
+            workspace.downgrade(),
+            cx,
+        )
     });
 
     workspace.update(cx, |workspace, cx| {
@@ -816,7 +834,13 @@ async fn test_random_diagnostics(cx: &mut TestAppContext, mut rng: StdRng) {
 
     log::info!("constructing reference diagnostics view");
     let reference_view = window.build_view(cx, |cx| {
-        ProjectDiagnosticsEditor::new_with_context(1, project.clone(), workspace.downgrade(), cx)
+        ProjectDiagnosticsEditor::new_with_context(
+            1,
+            true,
+            project.clone(),
+            workspace.downgrade(),
+            cx,
+        )
     });
     cx.run_until_parked();