Debounce diagnostics status bar updates (#21463)

Closes https://github.com/zed-industries/zed/pull/20797

Release Notes:

- Fixed diagnostics status bar flashing when typing
This commit is contained in:
Kirill Bulatov 2024-12-03 17:27:59 +02:00 committed by GitHub
parent 1270ef3ea5
commit a0f2c0799e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,7 +1,9 @@
use std::time::Duration;
use editor::Editor; use editor::Editor;
use gpui::{ use gpui::{
EventEmitter, IntoElement, ParentElement, Render, Styled, Subscription, View, ViewContext, EventEmitter, IntoElement, ParentElement, Render, Styled, Subscription, Task, View,
WeakView, ViewContext, WeakView,
}; };
use language::Diagnostic; use language::Diagnostic;
use ui::{h_flex, prelude::*, Button, ButtonLike, Color, Icon, IconName, Label, Tooltip}; use ui::{h_flex, prelude::*, Button, ButtonLike, Color, Icon, IconName, Label, Tooltip};
@ -15,6 +17,7 @@ pub struct DiagnosticIndicator {
workspace: WeakView<Workspace>, workspace: WeakView<Workspace>,
current_diagnostic: Option<Diagnostic>, current_diagnostic: Option<Diagnostic>,
_observe_active_editor: Option<Subscription>, _observe_active_editor: Option<Subscription>,
diagnostics_update: Task<()>,
} }
impl Render for DiagnosticIndicator { impl Render for DiagnosticIndicator {
@ -126,6 +129,7 @@ impl DiagnosticIndicator {
workspace: workspace.weak_handle(), workspace: workspace.weak_handle(),
current_diagnostic: None, current_diagnostic: None,
_observe_active_editor: None, _observe_active_editor: None,
diagnostics_update: Task::ready(()),
} }
} }
@ -149,8 +153,17 @@ impl DiagnosticIndicator {
.min_by_key(|entry| (entry.diagnostic.severity, entry.range.len())) .min_by_key(|entry| (entry.diagnostic.severity, entry.range.len()))
.map(|entry| entry.diagnostic); .map(|entry| entry.diagnostic);
if new_diagnostic != self.current_diagnostic { if new_diagnostic != self.current_diagnostic {
self.current_diagnostic = new_diagnostic; self.diagnostics_update = cx.spawn(|diagnostics_indicator, mut cx| async move {
cx.background_executor()
.timer(Duration::from_millis(50))
.await;
diagnostics_indicator
.update(&mut cx, |diagnostics_indicator, cx| {
diagnostics_indicator.current_diagnostic = new_diagnostic;
cx.notify(); cx.notify();
})
.ok();
});
} }
} }
} }