Skip to main content

ModeStack

Struct ModeStack 

Source
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

Source

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.

Source

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.

Source

pub fn push<M: Into<ModeId>>(&mut self, mode: M)

Push a new mode onto the stack.

Accepts any type that implements Into<ModeId>.

Source

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).

Source

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>.

Source

pub const fn depth(&self) -> usize

Get the stack depth.

Source

pub const fn is_base(&self) -> bool

Check if we’re in the base mode (stack depth is 1).

Source

pub fn as_slice(&self) -> &[ModeId]

Get all modes in the stack (bottom to top).

Source

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.

Source

pub fn contains(&self, mode_id: &ModeId) -> bool

Check if a mode is anywhere in the stack.

Trait Implementations§

Source§

impl Clone for ModeStack

Source§

fn clone(&self) -> ModeStack

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ModeStack

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.