From f2d61f3ea575622fe1cc46e4d55144885b22f878 Mon Sep 17 00:00:00 2001 From: Hans Date: Fri, 12 Apr 2024 12:39:57 +0800 Subject: [PATCH] Add feature to display commands for vim mode (#10349) Release Notes: - Added the current operator stack to the Vim status bar at the bottom of the editor. #4447 This commit introduces a new feature that displays the current partial command in the vim mode, similar to the behavior in Vim plugin. This helps users keep track of the commands they're entering. --- crates/vim/src/mode_indicator.rs | 14 +++++++++++++- crates/vim/src/vim.rs | 11 +++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/crates/vim/src/mode_indicator.rs b/crates/vim/src/mode_indicator.rs index a093eab9a2..a7033bc220 100644 --- a/crates/vim/src/mode_indicator.rs +++ b/crates/vim/src/mode_indicator.rs @@ -6,6 +6,7 @@ use crate::{state::Mode, Vim}; /// The ModeIndicator displays the current mode in the status bar. pub struct ModeIndicator { pub(crate) mode: Option, + pub(crate) operators: String, _subscription: Subscription, } @@ -15,6 +16,7 @@ impl ModeIndicator { let _subscription = cx.observe_global::(|this, cx| this.update_mode(cx)); let mut this = Self { mode: None, + operators: "".to_string(), _subscription, }; this.update_mode(cx); @@ -29,10 +31,20 @@ impl ModeIndicator { if vim.enabled { self.mode = Some(vim.state().mode); + self.operators = self.current_operators_description(&vim); } else { self.mode = None; } } + + fn current_operators_description(&self, vim: &Vim) -> String { + vim.state() + .operator_stack + .iter() + .map(|item| item.id()) + .collect::>() + .join("") + } } impl Render for ModeIndicator { @@ -41,7 +53,7 @@ impl Render for ModeIndicator { return div().into_any(); }; - Label::new(format!("-- {} --", mode)) + Label::new(format!("{} -- {} --", self.operators, mode)) .size(LabelSize::Small) .into_any_element() } diff --git a/crates/vim/src/vim.rs b/crates/vim/src/vim.rs index 55f961425c..565e34f9a2 100644 --- a/crates/vim/src/vim.rs +++ b/crates/vim/src/vim.rs @@ -517,6 +517,17 @@ impl Vim { ) { self.start_recording(cx) }; + // Since these operations can only be entered with pre-operators, + // we need to clear the previous operators when pushing, + // so that the current stack is the most correct + if matches!( + operator, + Operator::AddSurrounds { .. } + | Operator::ChangeSurrounds { .. } + | Operator::DeleteSurrounds + ) { + self.clear_operator(cx); + }; self.update_state(|state| state.operator_stack.push(operator)); self.sync_vim_settings(cx); }