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

Release Notes:

- Make the include warnings toggle in the diagnostic tab global for a
zed session.
This commit is contained in:
Mikayla Maki 2024-12-05 16:32:17 -08:00 committed by GitHub
parent aff17322f3
commit cf4e847c62
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 61 additions and 9 deletions

View file

@ -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,
)
}))
}

View file

@ -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();