Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/action.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#[cfg(feature = "derive")]
#[allow(dead_code)]
#[derive(Debug, keymap::KeyMap, Hash, PartialEq, Eq, Clone)]
pub(crate) enum Action {
/// Jump over obstacles
Expand Down
2 changes: 1 addition & 1 deletion keymap_derive/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const DOC_IDENT: &str = "doc";

pub(crate) struct Item<'a> {
pub variant: &'a Variant,
/// Raw string representations of the keys (e.g., ["ctrl-c", "@any"]).
/// Raw string representations of the keys (e.g., ["ctrl-c", "@any", "g g"]).
pub keys: Vec<String>,
/// Fully parsed nodes for each key sequence. Used for inspecting
/// key groups (like @any, @digit) during Key Group Capturing.
Expand Down
109 changes: 60 additions & 49 deletions keymap_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,36 +93,48 @@ fn impl_keymap_config(name: &Ident, items: &Vec<Item>) -> proc_macro2::TokenStre
.collect::<Vec<_>>();
let doc = &item.description;

// Find the index of a key group (like @any, @digit, etc.) in the parsed nodes.
// For simplicity, we only check the first key mapped to this variant.
// If the first key contains a group, the matched node at that index will be a `Char`.
// `char_idx` is the position of a key group node (e.g. `@any`, `@digit`) within
// the first key sequence of this variant. It is `None` when no group is present.
//
// Example: `#[key("@digit")]` → sequence is `[@digit]` → char_idx = Some(0)
// `#[key("d")]` → sequence is `[d]` → char_idx = None
//
// Only the first key sequence is inspected because all keys for a given variant
// must share the same group position (they map to the same field type).
// At runtime, `char_idx` tells `extract_via_trait` which node to pass to
// `KeyGroupValue::from_keymap_node` when binding the matched character/digit.
let mut char_idx: Option<usize> = None;
if let Some(first_node_seq) = item.nodes.first() {
for (idx, node) in first_node_seq.iter().enumerate() {
if let ::keymap_parser::node::Key::Group(_) = node.key {
if let keymap_parser::node::Key::Group(_) = node.key {
char_idx = Some(idx);
}
}
}

let extract_char = if let Some(idx) = char_idx {
quote! {
match keys.get(#idx).map(|n| &n.key) {
Some(::keymap_parser::node::Key::Char(c)) => *c,
_ => Default::default(),
// Generates an expression for extracting a value at the key group index using the
// `KeyGroupValue` trait. This works for any type that implements the trait, including
// type aliases, because the trait bound is resolved at monomorphisation time rather
// than by inspecting the token string of the type.
let extract_via_trait = |ty: &syn::Type| -> proc_macro2::TokenStream {
if let Some(idx) = char_idx {
quote! {
match keys.get(#idx) {
Some(node) => <#ty as ::keymap::KeyGroupValue>::from_keymap_node(node),
None => Default::default(),
}
}
} else {
quote! { Default::default() }
}
} else {
quote! { Default::default() }
};

let variant_expr = match &item.variant.fields {
Fields::Unit => quote! { #name::#ident },
Fields::Unnamed(fields) => {
let defaults = fields.unnamed.iter().map(|f| {
let ty_str = quote!(#f).to_string();
if ty_str == "char" {
extract_char.clone()
if char_idx.is_some() {
extract_via_trait(&f.ty)
} else {
quote! { Default::default() }
}
Expand All @@ -131,33 +143,18 @@ fn impl_keymap_config(name: &Ident, items: &Vec<Item>) -> proc_macro2::TokenStre
}
Fields::Named(fields) => {
let defaults = fields.named.iter().map(|f| {
let name = f.ident.as_ref().unwrap();
let ty_str = quote!(#f).to_string();
if ty_str.contains("char") {
quote! { #name: #extract_char }
let field_name = f.ident.as_ref().unwrap();
if char_idx.is_some() {
let expr = extract_via_trait(&f.ty);
quote! { #field_name: #expr }
} else {
quote! { #name: Default::default() }
quote! { #field_name: Default::default() }
}
});
quote! { #name::#ident { #(#defaults),* } }
}
};

let variant_expr_default = match &item.variant.fields {
Fields::Unit => quote! { #name::#ident },
Fields::Unnamed(fields) => {
let defaults = fields.unnamed.iter().map(|_| quote! { Default::default() });
quote! { #name::#ident(#(#defaults),*) }
}
Fields::Named(fields) => {
let defaults = fields.named.iter().map(|f| {
let name = &f.ident;
quote! { #name: Default::default() }
});
quote! { #name::#ident { #(#defaults),* } }
}
};

let variant_pat = match &item.variant.fields {
Fields::Unit => quote! { #name::#ident },
Fields::Unnamed(_) => quote! { #name::#ident(..) },
Expand All @@ -170,24 +167,36 @@ fn impl_keymap_config(name: &Ident, items: &Vec<Item>) -> proc_macro2::TokenStre
#variant_pat => #variant_name_str,
});

match_arms_deserialize.push(quote! {
#variant_name_str => Ok(#variant_expr_default),
});
if !item.ignore {
match_arms_bind.push(quote! {
#variant_pat => #variant_expr,
});

match_arms_bind.push(quote! {
#variant_pat => #variant_expr,
});
let variant_expr_default = match &item.variant.fields {
Fields::Unit => quote! { #name::#ident },
Fields::Unnamed(fields) => {
let defaults = fields.unnamed.iter().map(|_| quote! { Default::default() });
quote! { #name::#ident(#(#defaults),*) }
}
Fields::Named(fields) => {
let defaults = fields.named.iter().map(|f| {
let name = &f.ident;
quote! { #name: Default::default() }
});
quote! { #name::#ident { #(#defaults),* } }
}
};

// keymap_item
match_arms.push(quote! {
#variant_pat => ::keymap::Item::new(
vec![#(#keys),*],
#doc.to_string()
),
});
match_arms_deserialize.push(quote! {
#variant_name_str => Ok(#variant_expr_default),
});
match_arms.push(quote! {
#variant_pat => ::keymap::Item::new(
vec![#(#keys),*],
#doc.to_string()
),
});

// keymap_config
if !item.ignore {
entries.push(quote! {
(
#variant_expr_default,
Expand Down Expand Up @@ -250,6 +259,7 @@ fn impl_keymap_config(name: &Ident, items: &Vec<Item>) -> proc_macro2::TokenStre
fn keymap_item(&self) -> ::keymap::Item {
match self {
#(#match_arms)*
_ => ::core::unreachable!("ignored variant has no keymap"),
}
}

Expand All @@ -259,6 +269,7 @@ fn impl_keymap_config(name: &Ident, items: &Vec<Item>) -> proc_macro2::TokenStre
{
match self {
#(#match_arms_bind)*
_ => ::core::unreachable!("ignored variant cannot be bound"),
}
}
}
Expand Down
62 changes: 58 additions & 4 deletions keymap_derive/tests/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,27 @@ enum Action {
Jump(char),
}

#[allow(dead_code)]
#[derive(Debug, PartialEq, Eq, Clone)]
enum NoDefault {
A,
B,
}

#[allow(dead_code)]
#[derive(Debug, PartialEq, Eq, keymap_derive::KeyMap, Clone)]
enum IgnoreTest {
/// Active variant
#[key("a")]
Active,
/// Ignored variant (should NOT appear in keymap_config)
#[key(ignore)]
Ignored,
/// Ignored with field (should NOT require Default)
#[key(ignore)]
IgnoredWithData(NoDefault),
}

#[cfg(test)]
mod tests {
use keymap_dev::{Error, Item, KeyMap, KeyMapConfig, ToKeyMap};
Expand All @@ -47,9 +68,10 @@ mod tests {
(Action::Delete, "d d"),
(Action::Delete, "delete"),
]
.map(|(action, input)| {
.iter()
.for_each(|(action, input)| {
let key = keymap_parser::parse_seq(input).unwrap();
assert_eq!(&action, config.get_item_by_keymaps(&key).unwrap().0);
assert_eq!(action, config.get_item_by_keymaps(&key).unwrap().0);
});
}

Expand All @@ -61,9 +83,10 @@ mod tests {
(Action::Delete, "x"), // @lower
(Action::Digit('\0'), "1"), // @digit
]
.map(|(action, input)| {
.iter()
.for_each(|(action, input)| {
let key = keymap_parser::parse_seq(input).unwrap();
assert_eq!(&action, config.get_item_by_keymaps(&key).unwrap().0);
assert_eq!(action, config.get_item_by_keymaps(&key).unwrap().0);
});
}

Expand Down Expand Up @@ -159,4 +182,35 @@ mod tests {
let bound_action = config.get_bound_seq(&keys).unwrap();
assert_eq!(bound_action, Action::Create);
}

#[test]
fn test_key_ignore_not_in_config() {
let config = IgnoreTest::keymap_config();

assert_eq!(config.items.len(), 1);
assert_eq!(
config.items[0],
(
IgnoreTest::Active,
Item::new(
["a"].map(ToString::to_string).to_vec(),
"Active variant".to_string()
)
)
);
}

#[test]
fn test_ignored_variant_no_default_required() {
let config = IgnoreTest::keymap_config();

assert!(!config
.items
.iter()
.any(|(v, _)| matches!(v, IgnoreTest::Ignored)));
assert!(!config
.items
.iter()
.any(|(v, _)| matches!(v, IgnoreTest::IgnoredWithData(_))));
}
}
98 changes: 98 additions & 0 deletions keymap_derive/tests/derive_uint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
extern crate keymap_dev as keymap;

#[derive(Debug, PartialEq, Eq, keymap_derive::KeyMap, Clone)]
enum Action {
#[key("enter")]
Create,
#[key("@digit")]
Digit(char),
}

#[derive(Debug, PartialEq, Eq, keymap_derive::KeyMap, Clone)]
enum DigitAction {
#[key("@digit")]
DigitU8(u8),
#[key("@any")]
DigitAny(char),
}

#[derive(Debug, PartialEq, Eq, keymap_derive::KeyMap, Clone)]
enum DigitAction2 {
#[key("@digit")]
DigitU16(u16),
#[key("a")]
Letter(char),
}

// Type alias — the old string-based approach would fail here.
type MyDigit = u32;

#[derive(Debug, PartialEq, Eq, keymap_derive::KeyMap, Clone)]
enum AliasAction {
#[key("@digit")]
Count(MyDigit),
#[key("@any")]
Any(char),
}

#[cfg(test)]
mod tests {
use keymap_dev::{Error, KeyMap, KeyMapConfig, ToKeyMap};

use super::*;

struct Wrapper(keymap_parser::Node);
impl ToKeyMap for Wrapper {
fn to_keymap(&self) -> Result<KeyMap, Error> {
Ok(self.0.clone())
}
}

#[test]
fn test_digit_char() {
let config = Action::keymap_config();
let keys = keymap_parser::parse_seq("1")
.unwrap()
.into_iter()
.map(Wrapper)
.collect::<Vec<_>>();
let bound = config.get_bound_seq(&keys).unwrap();
assert_eq!(bound, Action::Digit('1'));
}

#[test]
fn test_digit_u8() {
let config = DigitAction::keymap_config();
let keys = keymap_parser::parse_seq("5")
.unwrap()
.into_iter()
.map(Wrapper)
.collect::<Vec<_>>();
let bound = config.get_bound_seq(&keys).unwrap();
assert_eq!(bound, DigitAction::DigitU8(5));
}

#[test]
fn test_digit_u16() {
let config = DigitAction2::keymap_config();
let keys = keymap_parser::parse_seq("7")
.unwrap()
.into_iter()
.map(Wrapper)
.collect::<Vec<_>>();
let bound = config.get_bound_seq(&keys).unwrap();
assert_eq!(bound, DigitAction2::DigitU16(7));
}

#[test]
fn test_digit_type_alias() {
let config = AliasAction::keymap_config();
let keys = keymap_parser::parse_seq("3")
.unwrap()
.into_iter()
.map(Wrapper)
.collect::<Vec<_>>();
let bound = config.get_bound_seq(&keys).unwrap();
assert_eq!(bound, AliasAction::Count(3));
}
}
Loading
Loading