pub struct ModeStack { /* private fields */ }Expand description
Mode stack for push/pop mode switching.
Supports vim-style mode stacking where modes can be pushed and popped. For example, entering operator-pending mode pushes onto the stack, and completing/canceling the operation pops back.
§Generic Mode Support
All methods accept impl Into<ModeId>, allowing both ModeId and
any type implementing Mode to be used directly:
let mut stack = ModeStack::new(VimMode::Normal); // VimMode implements Mode
stack.push(VimMode::OperatorPending);§Example
use reovim_kernel::api::v1::{ModeId, ModeStack, ModuleId};
let module = ModuleId::new("editor");
let normal = ModeId::with_discriminant(module.clone(), "NORMAL", 0);
let insert = ModeId::with_discriminant(module.clone(), "INSERT", 1);
let op_pending = ModeId::with_discriminant(module, "OP-PEND", 5);
let mut stack = ModeStack::new(normal.clone());
assert_eq!(stack.current(), &normal);
// Push operator-pending mode
stack.push(op_pending.clone());
assert_eq!(stack.current(), &op_pending);
// Pop back to normal
assert_eq!(stack.pop(), Some(op_pending));
assert_eq!(stack.current(), &normal);
// Set directly to insert (replaces current)
stack.set(insert.clone());
assert_eq!(stack.current(), &insert);Implementations§
Source§impl ModeStack
impl ModeStack
Sourcepub fn new<M: Into<ModeId>>(initial: M) -> Self
pub fn new<M: Into<ModeId>>(initial: M) -> Self
Create a new mode stack with an initial mode.
Accepts any type that implements Into<ModeId>, including
ModeId itself and any type implementing Mode.
Sourcepub fn current(&self) -> &ModeId
pub fn current(&self) -> &ModeId
Get the current (top) mode.
§Panics
This function will never panic in normal use. It only panics if the internal invariant (stack has at least one element) is violated, which indicates a bug.
Sourcepub fn push<M: Into<ModeId>>(&mut self, mode: M)
pub fn push<M: Into<ModeId>>(&mut self, mode: M)
Push a new mode onto the stack.
Accepts any type that implements Into<ModeId>.
Sourcepub fn pop(&mut self) -> Option<ModeId>
pub fn pop(&mut self) -> Option<ModeId>
Pop the top mode from the stack.
Returns None if only one mode remains (cannot pop the base mode).
Sourcepub fn set<M: Into<ModeId>>(&mut self, mode: M)
pub fn set<M: Into<ModeId>>(&mut self, mode: M)
Set the current mode, replacing the top of the stack.
This is equivalent to pop + push, but works even when only one mode exists.
Accepts any type that implements Into<ModeId>.
Sourcepub fn home(&self) -> &ModeId
pub fn home(&self) -> &ModeId
Get the home (base) mode.
The home mode is the first mode pushed onto the stack and cannot be popped.
§Panics
This function will never panic in normal use. It only panics if the internal invariant (stack has at least one element) is violated, which indicates a bug.