use super::{KeyToken, LeaderConfig, NormalCommand, motion::Motion};
use bevy::prelude::Resource;
#[derive(Clone, Debug, Default, Resource)]
pub struct VimConfig {
pub keymaps: KeymapSet,
pub options: VimOptions,
pub motions: MotionConfig,
pub leader: LeaderConfig,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct VimOptions {
pub ignore_case: bool,
pub smart_case: bool,
pub wrap_scan: bool,
pub timeout_len_ms: u64,
}
impl Default for VimOptions {
fn default() -> Self {
Self {
ignore_case: true,
smart_case: true,
wrap_scan: true,
timeout_len_ms: 1_000,
}
}
}
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct MotionConfig {}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct KeymapSet {
mappings: Vec<Keymap>,
}
impl KeymapSet {
#[must_use]
pub fn mappings(&self) -> &[Keymap] {
&self.mappings
}
}
impl Default for KeymapSet {
fn default() -> Self {
Self {
mappings: default_normal_keymaps(),
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Keymap {
pub modes: ModeSet,
pub lhs: KeySequence,
pub rhs: KeyAction,
pub remap: bool,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ModeSet {
normal: bool,
visual: bool,
}
impl ModeSet {
#[must_use]
pub const fn normal() -> Self {
Self {
normal: true,
visual: false,
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct KeySequence(Vec<KeyToken>);
impl KeySequence {
#[must_use]
pub fn new(tokens: impl Into<Vec<KeyToken>>) -> Self {
Self(tokens.into())
}
#[must_use]
pub fn as_slice(&self) -> &[KeyToken] {
&self.0
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum KeyAction {
Builtin(BuiltinAction),
Command(NormalCommand),
Ex(String),
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum BuiltinAction {
RepeatSearchForward,
RepeatSearchBackward,
RepeatCharSearch,
RepeatCharSearchReversed,
Motion(Motion),
}
fn default_normal_keymaps() -> Vec<Keymap> {
[
('h', Motion::Left),
('j', Motion::Down),
('k', Motion::Up),
('l', Motion::Right),
]
.into_iter()
.map(|(key, motion)| Keymap {
modes: ModeSet::normal(),
lhs: KeySequence::new([KeyToken::Char(key)]),
rhs: KeyAction::Builtin(BuiltinAction::Motion(motion)),
remap: false,
})
.collect()
}