Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix status bar diagnostics flashing #20797

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions crates/diagnostics/src/items.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::time::Duration;

use editor::Editor;
use gpui::{
EventEmitter, IntoElement, ParentElement, Render, Styled, Subscription, View, ViewContext,
EventEmitter, IntoElement, ParentElement, Render, Styled, Subscription, Task, View, ViewContext,
WeakView,
};
use language::Diagnostic;
Expand All @@ -14,7 +16,9 @@ pub struct DiagnosticIndicator {
active_editor: Option<WeakView<Editor>>,
workspace: WeakView<Workspace>,
current_diagnostic: Option<Diagnostic>,
pending_diagnostic: Option<Diagnostic>,
_observe_active_editor: Option<Subscription>,
_diagnostic_update_task: Option<Task<()>>,
}

impl Render for DiagnosticIndicator {
Expand Down Expand Up @@ -125,7 +129,9 @@ impl DiagnosticIndicator {
active_editor: None,
workspace: workspace.weak_handle(),
current_diagnostic: None,
pending_diagnostic: None,
_observe_active_editor: None,
_diagnostic_update_task: None,
}
}

Expand All @@ -148,11 +154,45 @@ impl DiagnosticIndicator {
.filter(|entry| !entry.range.is_empty())
.min_by_key(|entry| (entry.diagnostic.severity, entry.range.len()))
.map(|entry| entry.diagnostic);
if new_diagnostic != self.current_diagnostic {
if new_diagnostic == self.current_diagnostic {
return;
}

// if we have a diagnostic, we should update the current diagnostic immediately
// otherwise we should debounce the update to avoid flashing
if new_diagnostic.is_some() {
self.current_diagnostic = new_diagnostic;
self._diagnostic_update_task = None;
cx.notify();
} else {
self.pending_diagnostic = new_diagnostic;
self._debounced_diagnostic_update(cx);
}
}

fn _debounced_diagnostic_update(&mut self, cx: &mut ViewContext<Self>) {
// do not schedule another task if one is already running
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the point of this, and all the rest new comments around?
The code around is quite self-descriptive and shows the behavior perfectly, so let's remove all the comments added here.

if self._diagnostic_update_task.is_some() {
return;
}

self._diagnostic_update_task = Some(cx.spawn(|this, mut cx| async move {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: this is badly readable, esp. in GitHub interface, could it be diagnostic_indicator or whatever that is?

cx.background_executor()
.timer(Duration::from_millis(50))
.await;

this.update(&mut cx, |this, cx| {
// check if the background update has been aborted
if this._diagnostic_update_task.is_none() {
return;
}
this._diagnostic_update_task = None;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks somewhat magical, as we're removing ourselves from the state during running.

Could we invert the approach?
No if self._diagnostic_update_task.is_some() check, but instead, we always, unconditionally, do self._diagnostic_update_task = Some(....

This way, we will always start waiting, replacing the previous wait/update.

this.current_diagnostic = this.pending_diagnostic.clone();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct me if I'm wrong, but pending_diagnostic can only be None?
As we do self.pending_diagnostic = new_diagnostic only if NOT new_diagnostic.is_some().

Why is this field needed even?

Feels like we need to always do

DEBOUNCE
self.current_diagnostic = new_diagnostic;

in a task and always replace the previous task?


Or, we can name this _diagnostics_cleanup_task and it seems that we only debounce the cleanup part of the update.

cx.notify();
})
.ok();
}));
}
}

impl EventEmitter<ToolbarItemEvent> for DiagnosticIndicator {}
Expand Down
Loading