Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

Fix toggling diagnostics off not allowing to toggle them back (#60963)

Counterfeit IQ0129767b65ce
diff --git a/crates/editor/src/diagnostics.rs b/crates/editor/src/diagnostics.rs
index a1e40bdb392..3e988781eb7 100644
--- a/crates/editor/src/diagnostics.rs
+++ b/crates/editor/src/diagnostics.rs
@@ -600,11 +600,14 @@ impl Editor {
#[cfg(test)]
mod tests {
use crate::{
- actions::{ToggleDiagnostics, ToggleInlineDiagnostics},
+ actions::{
+ ToggleCodeLens, ToggleDiagnostics, ToggleInlayHints, ToggleInlineDiagnostics,
+ ToggleSemanticHighlights,
+ },
editor_tests::init_test,
test::editor_test_context::EditorTestContext,
};
- use gpui::{TestAppContext, UpdateGlobal};
+ use gpui::{Action, TestAppContext, UpdateGlobal};
use indoc::indoc;
use language::DiagnosticSourceKind;
use lsp::LanguageServerId;
@@ -750,4 +753,138 @@ mod tests {
);
});
}
+
+ #[gpui::test]
+ async fn test_actions_gated_by_lsp_data(cx: &mut TestAppContext) {
+ init_test(cx, |_| {});
+ let mut cx = EditorTestContext::new(cx).await;
+ let lsp_data_actions: [&dyn Action; 4] = [
+ &ToggleDiagnostics,
+ &ToggleInlayHints,
+ &ToggleCodeLens,
+ &ToggleSemanticHighlights,
+ ];
+
+ cx.update_editor(|editor, _, cx| {
+ editor.enable_lsp_data = false;
+ cx.notify();
+ });
+ cx.update(|window, cx| {
+ for action in lsp_data_actions {
+ assert!(
+ !window.is_action_available(action, cx),
+ "{} should not be available when LSP data is disabled",
+ action.name()
+ );
+ }
+ });
+
+ cx.update_editor(|editor, _, cx| {
+ editor.enable_lsp_data = true;
+ cx.notify();
+ });
+ cx.update(|window, cx| {
+ for action in lsp_data_actions {
+ assert!(
+ window.is_action_available(action, cx),
+ "{} should be available again after re-enabling LSP data",
+ action.name()
+ );
+ }
+ });
+ }
+
+ #[gpui::test]
+ async fn test_toggle_diagnostics_remains_available_after_disabling_diagnostics(
+ cx: &mut TestAppContext,
+ ) {
+ init_test(cx, |_| {});
+ let mut cx = EditorTestContext::new(cx).await;
+
+ cx.dispatch_action(ToggleDiagnostics);
+ cx.update_editor(|editor, window, cx| {
+ assert!(
+ !editor.diagnostics_enabled(),
+ "diagnostics should be disabled after dispatching ToggleDiagnostics"
+ );
+ assert!(
+ window.is_action_available(&ToggleDiagnostics, cx),
+ "ToggleDiagnostics should still be available after disabling diagnostics, \
+ so the user can re-enable it"
+ );
+ });
+
+ cx.dispatch_action(ToggleDiagnostics);
+ cx.update_editor(|editor, window, cx| {
+ assert!(
+ editor.diagnostics_enabled(),
+ "diagnostics should be re-enabled after a second dispatch of ToggleDiagnostics"
+ );
+ assert!(
+ window.is_action_available(&ToggleDiagnostics, cx),
+ "ToggleDiagnostics should be available again after re-enabling diagnostics"
+ );
+ });
+ }
+
+ #[gpui::test]
+ async fn test_toggle_inline_diagnostics_availability(cx: &mut TestAppContext) {
+ init_test(cx, |_| {});
+ let mut cx = EditorTestContext::new(cx).await;
+
+ cx.dispatch_action(ToggleDiagnostics);
+ cx.update(|window, cx| {
+ assert!(
+ !window.is_action_available(&ToggleInlineDiagnostics, cx),
+ "ToggleInlineDiagnostics should not be available when diagnostics are disabled"
+ );
+ });
+
+ cx.dispatch_action(ToggleDiagnostics);
+ cx.update(|window, cx| {
+ assert!(
+ window.is_action_available(&ToggleInlineDiagnostics, cx),
+ "ToggleInlineDiagnostics should be available again after re-enabling diagnostics"
+ );
+ });
+
+ let initial_show = cx.update_editor(|editor, _, _| editor.show_inline_diagnostics());
+ cx.dispatch_action(ToggleInlineDiagnostics);
+ cx.update_editor(|editor, window, cx| {
+ assert_eq!(
+ editor.show_inline_diagnostics(),
+ !initial_show,
+ "inline diagnostics visibility should flip after dispatching ToggleInlineDiagnostics"
+ );
+ assert!(
+ window.is_action_available(&ToggleInlineDiagnostics, cx),
+ "ToggleInlineDiagnostics should still be available after toggling it, \
+ so the user can toggle it back"
+ );
+ });
+
+ cx.dispatch_action(ToggleInlineDiagnostics);
+ cx.update_editor(|editor, window, cx| {
+ assert_eq!(
+ editor.show_inline_diagnostics(),
+ initial_show,
+ "inline diagnostics visibility should flip back after a second dispatch of ToggleInlineDiagnostics"
+ );
+ assert!(
+ window.is_action_available(&ToggleInlineDiagnostics, cx),
+ "ToggleInlineDiagnostics should remain available after toggling it back"
+ );
+ });
+
+ cx.update_editor(|editor, _, cx| {
+ editor.disable_inline_diagnostics();
+ cx.notify();
+ });
+ cx.update(|window, cx| {
+ assert!(
+ !window.is_action_available(&ToggleInlineDiagnostics, cx),
+ "ToggleInlineDiagnostics should not be available in editors with inline diagnostics permanently disabled"
+ );
+ });
+ }
}
diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs
index 6f8fec7b376..682ffcbe27f 100644
--- a/crates/editor/src/editor.rs
+++ b/crates/editor/src/editor.rs
@@ -10793,7 +10793,7 @@ impl Editor {
self.read_scroll_position_from_db(item_id, workspace_id, window, cx);
}
- fn lsp_data_enabled(&self) -> bool {
+ pub(crate) fn lsp_data_enabled(&self) -> bool {
self.enable_lsp_data && self.mode().is_full()
}
diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs
index bfc39e3f465..e3e1c145626 100644
--- a/crates/editor/src/element.rs
+++ b/crates/editor/src/element.rs
@@ -483,12 +483,12 @@ impl EditorElement {
register_action(editor, window, Editor::toggle_line_numbers);
register_action(editor, window, Editor::toggle_relative_line_numbers);
register_action(editor, window, Editor::toggle_indent_guides);
- register_action(editor, window, Editor::toggle_inlay_hints);
register_action(editor, window, Editor::toggle_inline_values);
- register_action(editor, window, Editor::toggle_code_lens_action);
- register_action(editor, window, Editor::toggle_semantic_highlights);
register_action(editor, window, Editor::toggle_edit_predictions);
- if editor.read(cx).diagnostics_enabled() {
+ if editor.read(cx).lsp_data_enabled() {
+ register_action(editor, window, Editor::toggle_inlay_hints);
+ register_action(editor, window, Editor::toggle_code_lens_action);
+ register_action(editor, window, Editor::toggle_semantic_highlights);
register_action(editor, window, Editor::toggle_diagnostics);
}
if editor.read(cx).inline_diagnostics_enabled() {