diff --git a/kakplugin/src/lib.rs b/kakplugin/src/lib.rs index 187797a..189eda6 100644 --- a/kakplugin/src/lib.rs +++ b/kakplugin/src/lib.rs @@ -317,3 +317,7 @@ where Ok(()) } + +pub fn reg(register: Register, keys: Option<&'_ str>) -> Result, KakError> { + response(format!("%reg{{{}}}", register.kak_expanded()), keys) +} diff --git a/src/xlookup.rs b/src/xlookup.rs index 298dfee..6e9c095 100644 --- a/src/xlookup.rs +++ b/src/xlookup.rs @@ -1,7 +1,8 @@ use crate::utils::get_hash; use evalexpr::{eval, Value}; use kakplugin::{ - get_selections, open_command_fifo, set_selections, types::Register, KakError, Selection, + get_selections, open_command_fifo, response, set_selections, types::Register, KakError, + Selection, }; use std::{ collections::{ @@ -15,11 +16,11 @@ use std::{ #[derive(clap::StructOpt, Debug)] pub struct Options { - #[clap(help = "Register with the lookup table")] + #[clap(help = "Register with the lookup table", default_value = "\"")] register: Register, } pub fn xlookup(options: &Options) -> Result { - let lookup_table = build_lookuptable(options.register)?; + let lookup_table = build_lookuptable(kakplugin::reg(options.register, None)?)?; let selections = get_selections(None)?; @@ -50,8 +51,7 @@ pub fn xlookup(options: &Options) -> Result { }) } -fn build_lookuptable(register: Register) -> Result, KakError> { - let mut selections = get_selections(Some(&format!("\"{register}z")))?; +fn build_lookuptable(mut selections: Vec) -> Result, KakError> { let mut iter = selections.array_chunks_mut(); let ret = iter.try_fold(BTreeMap::new(), |mut acc, [key, value]| { match acc.entry(get_hash(key, false, None, false)) { @@ -71,3 +71,32 @@ fn build_lookuptable(register: Register) -> Result, Kak Ok(ret) } } + +#[cfg(test)] +mod tests { + use super::*; + macro_rules! blt { + ($($x:expr),+ $(,)?) => { + build_lookuptable(vec![$($x.to_string()),+]) + } + } + macro_rules! hsh { + ($expr:expr) => { + get_hash(&$expr.to_string(), false, None, false) + }; + } + #[test] + fn test_build_lookuptable() { + // Must be an even number + assert!(blt!["1", "b", "c"].is_err()); + // Duplicate key + assert!(blt!["1", "b", "2", "c", "2", "d"].is_err()); + // Valid + assert!(blt!["1", "b", "2", "c"].is_ok()); + + let lt = blt!["1", "b", "2", "c"].unwrap(); + assert_eq!(lt.get(&hsh!("1")), Some(&String::from("b"))); + assert_eq!(lt.get(&hsh!("2")), Some(&String::from("c"))); + assert_eq!(lt.get(&hsh!("3")), None); + } +}