use {
reovim_driver_input::{
KeyCode, KeyEvent, KeyLookupState, KeySequence, ModeKeyResolver, ModeState, Modifiers,
ResolveContext, ResolveInput, ResolveResult,
},
reovim_kernel::api::v1::ModeId,
reovim_module_cmdline::CmdlineState,
};
use crate::modes::VimMode;
pub struct VimCommandLineResolver {
mode_id: ModeId,
}
impl VimCommandLineResolver {
#[must_use]
pub const fn new() -> Self {
Self {
mode_id: VimMode::COMMANDLINE_ID,
}
}
const fn is_insertable(key: &KeyEvent) -> Option<char> {
if key.modifiers.contains(Modifiers::CTRL) || key.modifiers.contains(Modifiers::ALT) {
return None;
}
match key.code {
KeyCode::Char(c) => Some(c),
_ => None,
}
}
}
impl Default for VimCommandLineResolver {
fn default() -> Self {
Self::new()
}
}
#[cfg_attr(coverage_nightly, coverage(off))]
impl ModeKeyResolver for VimCommandLineResolver {
fn resolve_with_keymap(
&self,
key: &KeyEvent,
_state: &mut ModeState,
input: &ResolveInput<'_>,
) -> ResolveResult {
if let Some(c) = Self::is_insertable(key) {
return ResolveResult::insert_char_to::<CmdlineState>(c);
}
let mut keys = KeySequence::new();
keys.push(*key);
let lookup_state = input.keymap.query(input.mode, &keys);
match lookup_state {
KeyLookupState::ExactWithLonger { exact, .. } | KeyLookupState::ExactOnly(exact) => {
ResolveResult::Execute(exact, ResolveContext::default())
}
KeyLookupState::PrefixOnly => {
ResolveResult::Pending
}
KeyLookupState::NotFound => {
ResolveResult::NotHandled
}
}
}
fn mode_id(&self) -> &ModeId {
&self.mode_id
}
fn inherits_from(&self) -> Option<&ModeId> {
None
}
fn reset(&mut self) {
}
}