Add a way to toggle inlay hints with modifiers #2 (#25766)

https://github.com/zed-industries/zed/pull/25752 with fixes on top

* Ensures no flickering happens for all modifiers `: false` case
* Dismisses the toggled state on focus out
* Reworks cache state so that "enabled" and "toggled by modifiers" are
different states with their own lifecycle

Release Notes:

- N/A
This commit is contained in:
Kirill Bulatov 2025-02-28 10:03:25 +02:00 committed by GitHub
parent bb3aef15eb
commit 7440833ff1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 166 additions and 26 deletions

View file

@ -1031,6 +1031,7 @@ pub enum GotoDefinitionKind {
#[derive(Debug, Clone)]
enum InlayHintRefreshReason {
ModifiersChanged(bool),
Toggle(bool),
SettingsChange(InlayHintSettings),
NewLinesShown,
@ -1042,6 +1043,7 @@ enum InlayHintRefreshReason {
impl InlayHintRefreshReason {
fn description(&self) -> &'static str {
match self {
Self::ModifiersChanged(_) => "modifiers changed",
Self::Toggle(_) => "toggle",
Self::SettingsChange(_) => "settings change",
Self::NewLinesShown => "new lines shown",
@ -3668,7 +3670,7 @@ impl Editor {
cx: &mut Context<Self>,
) {
self.refresh_inlay_hints(
InlayHintRefreshReason::Toggle(!self.inlay_hint_cache.enabled),
InlayHintRefreshReason::Toggle(!self.inlay_hints_enabled()),
cx,
);
}
@ -3690,21 +3692,44 @@ impl Editor {
| InlayHintRefreshReason::ExcerptsRemoved(_)
);
let (invalidate_cache, required_languages) = match reason {
InlayHintRefreshReason::ModifiersChanged(enabled) => {
match self.inlay_hint_cache.modifiers_override(enabled) {
Some(enabled) => {
if enabled {
(InvalidationStrategy::RefreshRequested, None)
} else {
self.splice_inlays(
&self
.visible_inlay_hints(cx)
.iter()
.map(|inlay| inlay.id)
.collect::<Vec<InlayId>>(),
Vec::new(),
cx,
);
return;
}
}
None => return,
}
}
InlayHintRefreshReason::Toggle(enabled) => {
self.inlay_hint_cache.enabled = enabled;
if enabled {
(InvalidationStrategy::RefreshRequested, None)
if self.inlay_hint_cache.toggle(enabled) {
if enabled {
(InvalidationStrategy::RefreshRequested, None)
} else {
self.splice_inlays(
&self
.visible_inlay_hints(cx)
.iter()
.map(|inlay| inlay.id)
.collect::<Vec<InlayId>>(),
Vec::new(),
cx,
);
return;
}
} else {
self.inlay_hint_cache.clear();
self.splice_inlays(
&self
.visible_inlay_hints(cx)
.iter()
.map(|inlay| inlay.id)
.collect::<Vec<InlayId>>(),
Vec::new(),
cx,
);
return;
}
}
@ -15839,11 +15864,12 @@ impl Editor {
&mut self,
event: FocusOutEvent,
_window: &mut Window,
_cx: &mut Context<Self>,
cx: &mut Context<Self>,
) {
if event.blurred != self.focus_handle {
self.last_focused_descendant = Some(event.blurred);
}
self.refresh_inlay_hints(InlayHintRefreshReason::ModifiersChanged(false), cx);
}
pub fn handle_blur(&mut self, window: &mut Window, cx: &mut Context<Self>) {