Allow to temporarily toggle diagnostics in the editor (#30316)

* Adds a `diagnostics_max_severity: null` editor settings that has
previous hardcoded default, `warning`
* Make inline diagnostics to inherit this setting by default (can be
overridden with its own max_severity setting)
* Allows to toggle diagnostics in the editor menu and via new action,
`editor::ToggleDiagnostics`

Closes https://github.com/zed-industries/zed/issues/4686

Release Notes:

- N/A *or* Added/Fixed/Improved ...
This commit is contained in:
Kirill Bulatov 2025-05-09 00:47:32 +03:00 committed by GitHub
parent 9e5d115e72
commit a8312d623d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 279 additions and 96 deletions

View file

@ -129,6 +129,7 @@ use project::{
},
session::{Session, SessionEvent},
},
project_settings::DiagnosticSeverity,
};
pub use git::blame::BlameRenderer;
@ -141,8 +142,8 @@ use task::{ResolvedTask, RunnableTag, TaskTemplate, TaskVariables};
pub use lsp::CompletionContext;
use lsp::{
CodeActionKind, CompletionItemKind, CompletionTriggerKind, DiagnosticSeverity,
InsertTextFormat, InsertTextMode, LanguageServerId, LanguageServerName,
CodeActionKind, CompletionItemKind, CompletionTriggerKind, InsertTextFormat, InsertTextMode,
LanguageServerId, LanguageServerName,
};
use language::BufferSnapshot;
@ -628,7 +629,7 @@ struct InlineDiagnostic {
group_id: usize,
is_primary: bool,
start: Point,
severity: DiagnosticSeverity,
severity: lsp::DiagnosticSeverity,
}
pub enum MenuInlineCompletionsPolicy {
@ -860,6 +861,7 @@ pub struct Editor {
snippet_stack: InvalidationStack<SnippetState>,
select_syntax_node_history: SelectSyntaxNodeHistory,
ime_transaction: Option<TransactionId>,
pub diagnostics_max_severity: DiagnosticSeverity,
active_diagnostics: ActiveDiagnostic,
show_inline_diagnostics: bool,
inline_diagnostics_update: Task<()>,
@ -1506,6 +1508,15 @@ impl Editor {
display_map.is_none() || mode.is_minimap(),
"Providing a display map for a new editor is only intended for the minimap and might have unindended side effects otherwise!"
);
let full_mode = mode.is_full();
let diagnostics_max_severity = if full_mode {
EditorSettings::get_global(cx)
.diagnostics_max_severity
.unwrap_or(DiagnosticSeverity::Hint)
} else {
DiagnosticSeverity::Off
};
let style = window.text_style();
let font_size = style.font_size.to_pixels(window.rem_size());
let editor = cx.entity().downgrade();
@ -1539,7 +1550,7 @@ impl Editor {
.into_any()
}),
merge_adjacent: true,
..Default::default()
..FoldPlaceholder::default()
};
let display_map = display_map.unwrap_or_else(|| {
cx.new(|cx| {
@ -1551,6 +1562,7 @@ impl Editor {
FILE_HEADER_HEIGHT,
MULTI_BUFFER_EXCERPT_HEADER_HEIGHT,
fold_placeholder,
diagnostics_max_severity,
cx,
)
})
@ -1678,8 +1690,6 @@ impl Editor {
code_action_providers.push(Rc::new(project) as Rc<_>);
}
let full_mode = mode.is_full();
let mut this = Self {
focus_handle,
show_cursor_when_unfocused: false,
@ -1692,16 +1702,17 @@ impl Editor {
add_selections_state: None,
select_next_state: None,
select_prev_state: None,
selection_history: Default::default(),
autoclose_regions: Default::default(),
snippet_stack: Default::default(),
selection_history: SelectionHistory::default(),
autoclose_regions: Vec::new(),
snippet_stack: InvalidationStack::default(),
select_syntax_node_history: SelectSyntaxNodeHistory::default(),
ime_transaction: Default::default(),
ime_transaction: None,
active_diagnostics: ActiveDiagnostic::None,
show_inline_diagnostics: ProjectSettings::get_global(cx).diagnostics.inline.enabled,
inline_diagnostics_update: Task::ready(()),
inline_diagnostics: Vec::new(),
soft_wrap_mode_override,
diagnostics_max_severity,
hard_wrap: None,
completion_provider: project.clone().map(|project| Box::new(project) as _),
semantics_provider: project.clone().map(|project| Rc::new(project) as _),
@ -1725,7 +1736,7 @@ impl Editor {
placeholder_text: None,
highlight_order: 0,
highlighted_rows: HashMap::default(),
background_highlights: Default::default(),
background_highlights: TreeMap::default(),
gutter_highlights: TreeMap::default(),
scrollbar_marker_state: ScrollbarMarkerState::default(),
active_indent_guides_state: ActiveIndentGuidesState::default(),
@ -1733,21 +1744,21 @@ impl Editor {
context_menu: RefCell::new(None),
context_menu_options: None,
mouse_context_menu: None,
completion_tasks: Default::default(),
inline_blame_popover: Default::default(),
completion_tasks: Vec::new(),
inline_blame_popover: None,
signature_help_state: SignatureHelpState::default(),
auto_signature_help: None,
find_all_references_task_sources: Vec::new(),
next_completion_id: 0,
next_inlay_id: 0,
code_action_providers,
available_code_actions: Default::default(),
code_actions_task: Default::default(),
quick_selection_highlight_task: Default::default(),
debounced_selection_highlight_task: Default::default(),
document_highlights_task: Default::default(),
linked_editing_range_task: Default::default(),
pending_rename: Default::default(),
available_code_actions: None,
code_actions_task: None,
quick_selection_highlight_task: None,
debounced_selection_highlight_task: None,
document_highlights_task: None,
linked_editing_range_task: None,
pending_rename: None,
searchable: true,
cursor_shape: EditorSettings::get_global(cx)
.cursor_shape
@ -1765,9 +1776,9 @@ impl Editor {
jsx_tag_auto_close_enabled_in_any_buffer: false,
leader_id: None,
remote_id: None,
hover_state: Default::default(),
hover_state: HoverState::default(),
pending_mouse_down: None,
hovered_link_state: Default::default(),
hovered_link_state: None,
edit_prediction_provider: None,
active_inline_completion: None,
stale_inline_completion_in_menu: None,
@ -1786,7 +1797,7 @@ impl Editor {
gutter_dimensions: GutterDimensions::default(),
style: None,
show_cursor_names: false,
hovered_cursors: Default::default(),
hovered_cursors: HashMap::default(),
next_editor_action_id: EditorActionId::default(),
editor_actions: Rc::default(),
inline_completions_hidden_for_vim_mode: false,
@ -1808,7 +1819,7 @@ impl Editor {
.restore_unsaved_buffers,
blame: None,
blame_subscription: None,
tasks: Default::default(),
tasks: BTreeMap::default(),
breakpoint_store,
gutter_breakpoint_indicator: (None, None),
@ -15093,8 +15104,12 @@ impl Editor {
self.inline_diagnostics.clear();
}
pub fn diagnostics_enabled(&self) -> bool {
self.mode.is_full()
}
pub fn inline_diagnostics_enabled(&self) -> bool {
self.inline_diagnostics_enabled
self.diagnostics_enabled() && self.inline_diagnostics_enabled
}
pub fn show_inline_diagnostics(&self) -> bool {
@ -15111,6 +15126,43 @@ impl Editor {
self.refresh_inline_diagnostics(false, window, cx);
}
pub fn set_max_diagnostics_severity(&mut self, severity: DiagnosticSeverity, cx: &mut App) {
self.diagnostics_max_severity = severity;
self.display_map.update(cx, |display_map, _| {
display_map.diagnostics_max_severity = self.diagnostics_max_severity;
});
}
pub fn toggle_diagnostics(
&mut self,
_: &ToggleDiagnostics,
window: &mut Window,
cx: &mut Context<Editor>,
) {
if !self.diagnostics_enabled() {
return;
}
let new_severity = if self.diagnostics_max_severity == DiagnosticSeverity::Off {
EditorSettings::get_global(cx)
.diagnostics_max_severity
.filter(|severity| severity != &DiagnosticSeverity::Off)
.unwrap_or(DiagnosticSeverity::Hint)
} else {
DiagnosticSeverity::Off
};
self.set_max_diagnostics_severity(new_severity, cx);
if self.diagnostics_max_severity == DiagnosticSeverity::Off {
self.active_diagnostics = ActiveDiagnostic::None;
self.inline_diagnostics_update = Task::ready(());
self.inline_diagnostics.clear();
} else {
self.refresh_inline_diagnostics(false, window, cx);
}
cx.notify();
}
pub fn toggle_minimap(
&mut self,
_: &ToggleMinimap,
@ -15128,9 +15180,16 @@ impl Editor {
window: &mut Window,
cx: &mut Context<Self>,
) {
let max_severity = ProjectSettings::get_global(cx)
.diagnostics
.inline
.max_severity
.unwrap_or(self.diagnostics_max_severity);
if self.mode.is_minimap()
|| !self.inline_diagnostics_enabled
|| !self.inline_diagnostics_enabled()
|| !self.show_inline_diagnostics
|| max_severity == DiagnosticSeverity::Off
{
self.inline_diagnostics_update = Task::ready(());
self.inline_diagnostics.clear();
@ -18050,6 +18109,14 @@ impl Editor {
}
fn settings_changed(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let new_severity = if self.diagnostics_enabled() {
EditorSettings::get_global(cx)
.diagnostics_max_severity
.unwrap_or(DiagnosticSeverity::Hint)
} else {
DiagnosticSeverity::Off
};
self.set_max_diagnostics_severity(new_severity, cx);
self.tasks_update_task = Some(self.refresh_runnables(window, cx));
self.update_edit_prediction_settings(cx);
self.refresh_inline_completion(true, false, window, cx);
@ -20471,8 +20538,6 @@ impl Render for Editor {
EditorMode::Minimap { .. } => cx.theme().colors().editor_background.opacity(0.7),
};
let show_underlines = !self.mode.is_minimap();
EditorElement::new(
&cx.entity(),
EditorStyle {
@ -20485,7 +20550,7 @@ impl Render for Editor {
inlay_hints_style: make_inlay_hints_style(cx),
inline_completion_styles: make_suggestion_styles(cx),
unnecessary_code_fade: ThemeSettings::get_global(cx).unnecessary_code_fade,
show_underlines,
show_underlines: !self.mode.is_minimap(),
},
)
}
@ -20896,12 +20961,12 @@ fn inline_completion_edit_text(
edit_preview.highlight_edits(current_snapshot, &edits, include_deletions, cx)
}
pub fn diagnostic_style(severity: DiagnosticSeverity, colors: &StatusColors) -> Hsla {
pub fn diagnostic_style(severity: lsp::DiagnosticSeverity, colors: &StatusColors) -> Hsla {
match severity {
DiagnosticSeverity::ERROR => colors.error,
DiagnosticSeverity::WARNING => colors.warning,
DiagnosticSeverity::INFORMATION => colors.info,
DiagnosticSeverity::HINT => colors.info,
lsp::DiagnosticSeverity::ERROR => colors.error,
lsp::DiagnosticSeverity::WARNING => colors.warning,
lsp::DiagnosticSeverity::INFORMATION => colors.info,
lsp::DiagnosticSeverity::HINT => colors.info,
_ => colors.ignored,
}
}