vim: Display pending keys in Vim mode indicator (#13195)
This changes the mode indicator to now show pending keys and not just pending operators. Release Notes: - Added pending keys to the mode indicator in Vim mode. Demo: https://github.com/zed-industries/zed/assets/1185253/4fc4ffd9-2ba7-4e2c-b2c3-cd19b40cb640
This commit is contained in:
parent
3a26a4809d
commit
195a270e18
2 changed files with 71 additions and 11 deletions
|
@ -539,6 +539,7 @@ pub struct Window {
|
||||||
pub(crate) focus: Option<FocusId>,
|
pub(crate) focus: Option<FocusId>,
|
||||||
focus_enabled: bool,
|
focus_enabled: bool,
|
||||||
pending_input: Option<PendingInput>,
|
pending_input: Option<PendingInput>,
|
||||||
|
pending_input_observers: SubscriberSet<(), AnyObserver>,
|
||||||
prompt: Option<RenderablePromptHandle>,
|
prompt: Option<RenderablePromptHandle>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -810,6 +811,7 @@ impl Window {
|
||||||
focus: None,
|
focus: None,
|
||||||
focus_enabled: true,
|
focus_enabled: true,
|
||||||
pending_input: None,
|
pending_input: None,
|
||||||
|
pending_input_observers: SubscriberSet::new(),
|
||||||
prompt: None,
|
prompt: None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -3128,16 +3130,20 @@ impl<'a> WindowContext<'a> {
|
||||||
let Some(currently_pending) = cx.window.pending_input.take() else {
|
let Some(currently_pending) = cx.window.pending_input.take() else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
cx.replay_pending_input(currently_pending)
|
cx.pending_input_changed();
|
||||||
|
cx.replay_pending_input(currently_pending);
|
||||||
})
|
})
|
||||||
.log_err();
|
.log_err();
|
||||||
}));
|
}));
|
||||||
|
|
||||||
self.window.pending_input = Some(currently_pending);
|
self.window.pending_input = Some(currently_pending);
|
||||||
|
self.pending_input_changed();
|
||||||
|
|
||||||
self.propagate_event = false;
|
self.propagate_event = false;
|
||||||
|
|
||||||
return;
|
return;
|
||||||
} else if let Some(currently_pending) = self.window.pending_input.take() {
|
} else if let Some(currently_pending) = self.window.pending_input.take() {
|
||||||
|
self.pending_input_changed();
|
||||||
if bindings
|
if bindings
|
||||||
.iter()
|
.iter()
|
||||||
.all(|binding| !currently_pending.used_by_binding(binding))
|
.all(|binding| !currently_pending.used_by_binding(binding))
|
||||||
|
@ -3173,6 +3179,13 @@ impl<'a> WindowContext<'a> {
|
||||||
self.dispatch_keystroke_observers(event, None);
|
self.dispatch_keystroke_observers(event, None);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn pending_input_changed(&mut self) {
|
||||||
|
self.window
|
||||||
|
.pending_input_observers
|
||||||
|
.clone()
|
||||||
|
.retain(&(), |callback| callback(self));
|
||||||
|
}
|
||||||
|
|
||||||
fn dispatch_key_down_up_event(
|
fn dispatch_key_down_up_event(
|
||||||
&mut self,
|
&mut self,
|
||||||
event: &dyn Any,
|
event: &dyn Any,
|
||||||
|
@ -3230,6 +3243,14 @@ impl<'a> WindowContext<'a> {
|
||||||
.has_pending_keystrokes()
|
.has_pending_keystrokes()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the currently pending input keystrokes that might result in a multi-stroke key binding.
|
||||||
|
pub fn pending_input_keystrokes(&self) -> Option<&[Keystroke]> {
|
||||||
|
self.window
|
||||||
|
.pending_input
|
||||||
|
.as_ref()
|
||||||
|
.map(|pending_input| pending_input.keystrokes.as_slice())
|
||||||
|
}
|
||||||
|
|
||||||
fn replay_pending_input(&mut self, currently_pending: PendingInput) {
|
fn replay_pending_input(&mut self, currently_pending: PendingInput) {
|
||||||
let node_id = self
|
let node_id = self
|
||||||
.window
|
.window
|
||||||
|
@ -4037,6 +4058,20 @@ impl<'a, V: 'static> ViewContext<'a, V> {
|
||||||
subscription
|
subscription
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Register a callback to be invoked when the window's pending input changes.
|
||||||
|
pub fn observe_pending_input(
|
||||||
|
&mut self,
|
||||||
|
mut callback: impl FnMut(&mut V, &mut ViewContext<V>) + 'static,
|
||||||
|
) -> Subscription {
|
||||||
|
let view = self.view.downgrade();
|
||||||
|
let (subscription, activate) = self.window.pending_input_observers.insert(
|
||||||
|
(),
|
||||||
|
Box::new(move |cx| view.update(cx, |view, cx| callback(view, cx)).is_ok()),
|
||||||
|
);
|
||||||
|
activate();
|
||||||
|
subscription
|
||||||
|
}
|
||||||
|
|
||||||
/// Register a listener to be called when the given focus handle receives focus.
|
/// Register a listener to be called when the given focus handle receives focus.
|
||||||
/// Returns a subscription and persists until the subscription is dropped.
|
/// Returns a subscription and persists until the subscription is dropped.
|
||||||
pub fn on_focus(
|
pub fn on_focus(
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use gpui::{div, Element, Render, Subscription, ViewContext};
|
use gpui::{div, Element, Render, Subscription, ViewContext};
|
||||||
|
use itertools::Itertools;
|
||||||
use workspace::{item::ItemHandle, ui::prelude::*, StatusItemView};
|
use workspace::{item::ItemHandle, ui::prelude::*, StatusItemView};
|
||||||
|
|
||||||
use crate::{state::Mode, Vim};
|
use crate::{state::Mode, Vim};
|
||||||
|
@ -7,29 +8,33 @@ use crate::{state::Mode, Vim};
|
||||||
pub struct ModeIndicator {
|
pub struct ModeIndicator {
|
||||||
pub(crate) mode: Option<Mode>,
|
pub(crate) mode: Option<Mode>,
|
||||||
pub(crate) operators: String,
|
pub(crate) operators: String,
|
||||||
_subscription: Subscription,
|
pending_keys: Option<String>,
|
||||||
|
_subscriptions: Vec<Subscription>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ModeIndicator {
|
impl ModeIndicator {
|
||||||
/// Construct a new mode indicator in this window.
|
/// Construct a new mode indicator in this window.
|
||||||
pub fn new(cx: &mut ViewContext<Self>) -> Self {
|
pub fn new(cx: &mut ViewContext<Self>) -> Self {
|
||||||
let _subscription = cx.observe_global::<Vim>(|this, cx| this.update_mode(cx));
|
let _subscriptions = vec![
|
||||||
|
cx.observe_global::<Vim>(|this, cx| this.update_mode(cx)),
|
||||||
|
cx.observe_pending_input(|this, cx| {
|
||||||
|
this.update_pending_keys(cx);
|
||||||
|
cx.notify();
|
||||||
|
}),
|
||||||
|
];
|
||||||
|
|
||||||
let mut this = Self {
|
let mut this = Self {
|
||||||
mode: None,
|
mode: None,
|
||||||
operators: "".to_string(),
|
operators: "".to_string(),
|
||||||
_subscription,
|
pending_keys: None,
|
||||||
|
_subscriptions,
|
||||||
};
|
};
|
||||||
this.update_mode(cx);
|
this.update_mode(cx);
|
||||||
this
|
this
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_mode(&mut self, cx: &mut ViewContext<Self>) {
|
fn update_mode(&mut self, cx: &mut ViewContext<Self>) {
|
||||||
// Vim doesn't exist in some tests
|
if let Some(vim) = self.vim(cx) {
|
||||||
let Some(vim) = cx.try_global::<Vim>() else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
|
|
||||||
if vim.enabled {
|
|
||||||
self.mode = Some(vim.state().mode);
|
self.mode = Some(vim.state().mode);
|
||||||
self.operators = self.current_operators_description(&vim);
|
self.operators = self.current_operators_description(&vim);
|
||||||
} else {
|
} else {
|
||||||
|
@ -37,6 +42,24 @@ impl ModeIndicator {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn update_pending_keys(&mut self, cx: &mut ViewContext<Self>) {
|
||||||
|
if self.vim(cx).is_some() {
|
||||||
|
self.pending_keys = cx.pending_input_keystrokes().map(|keystrokes| {
|
||||||
|
keystrokes
|
||||||
|
.iter()
|
||||||
|
.map(|keystroke| format!("{}", keystroke))
|
||||||
|
.join(" ")
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
self.pending_keys = None;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn vim<'a>(&self, cx: &'a mut ViewContext<Self>) -> Option<&'a Vim> {
|
||||||
|
// In some tests Vim isn't enabled, so we use try_global.
|
||||||
|
cx.try_global::<Vim>().filter(|vim| vim.enabled)
|
||||||
|
}
|
||||||
|
|
||||||
fn current_operators_description(&self, vim: &Vim) -> String {
|
fn current_operators_description(&self, vim: &Vim) -> String {
|
||||||
vim.state()
|
vim.state()
|
||||||
.pre_count
|
.pre_count
|
||||||
|
@ -61,7 +84,9 @@ impl Render for ModeIndicator {
|
||||||
return div().into_any();
|
return div().into_any();
|
||||||
};
|
};
|
||||||
|
|
||||||
Label::new(format!("{} -- {} --", self.operators, mode))
|
let pending = self.pending_keys.as_ref().unwrap_or(&self.operators);
|
||||||
|
|
||||||
|
Label::new(format!("{} -- {} --", pending, mode))
|
||||||
.size(LabelSize::Small)
|
.size(LabelSize::Small)
|
||||||
.line_height_style(LineHeightStyle::UiLabel)
|
.line_height_style(LineHeightStyle::UiLabel)
|
||||||
.into_any_element()
|
.into_any_element()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue