-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
|
@@ -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 { | ||
|
@@ -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, | ||
} | ||
} | ||
|
||
|
@@ -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 | ||
if self._diagnostic_update_task.is_some() { | ||
return; | ||
} | ||
|
||
self._diagnostic_update_task = Some(cx.spawn(|this, mut cx| async move { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NIT: |
||
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? This way, we will always start waiting, replacing the previous wait/update. |
||
this.current_diagnostic = this.pending_diagnostic.clone(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct me if I'm wrong, but Why is this field needed even? Feels like we need to always do
in a task and always replace the previous task? Or, we can name this |
||
cx.notify(); | ||
}) | ||
.ok(); | ||
})); | ||
} | ||
} | ||
|
||
impl EventEmitter<ToolbarItemEvent> for DiagnosticIndicator {} | ||
|
There was a problem hiding this comment.
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.