From 0eba84fd5dc4785d7af9bfdbd9321fe9ace747d2 Mon Sep 17 00:00:00 2001 From: jtroo Date: Sun, 21 Apr 2024 22:18:35 -0700 Subject: [PATCH] feat: allow `t!` in place of `template-expand` (#958) Use `t!` as a short form of `template-expand`. The alternative of allowing the template name in the first atom was considered. However, such an implementation would be more difficult and complex. Allowing a shorter prefix atom is a simple and quick implementation. If one wanted to do the work in the future to allow even shorter template expansion by adding syntax to remove the prefix atom altogether, the added code should not conflict with that work. For now though, the simpler implementation will be added for easier maintenance and hopefully less risk of introducing bugs. --- cfg_samples/kanata.kbd | 3 ++- docs/config.adoc | 3 ++- parser/src/cfg/deftemplate.rs | 6 ++++-- parser/src/cfg/tests.rs | 38 +++++++++++++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 4 deletions(-) diff --git a/cfg_samples/kanata.kbd b/cfg_samples/kanata.kbd index 18c35f152..df6570126 100644 --- a/cfg_samples/kanata.kbd +++ b/cfg_samples/kanata.kbd @@ -1192,7 +1192,8 @@ If you need help, please feel welcome to ask in the GitHub discussions. (defvar chord-timeout 200) (template-expand left-hand-chords qwerty a s d f qwa qws qwd qwf) -(template-expand left-hand-chords dvorak a o e u dva dvo dve dvu) +;; You can use t! as a short form of template-expand +(t! left-hand-chords dvorak a o e u dva dvo dve dvu) (deflayer template-example _ _ _ _ _ _ _ _ _ _ _ _ _ _ diff --git a/docs/config.adoc b/docs/config.adoc index d05112637..e184df1b6 100644 --- a/docs/config.adoc +++ b/docs/config.adoc @@ -3129,7 +3129,8 @@ you wouldn't want this. Rather than retyping the code with `fork` and ;; Templates are a simple text substitution, so the above is exactly equivalent to: ;; (defalias fn1 (fork 1 (multi (unmod ralt lalt) f1) (lalt ralt))) (defalias fn2 (template-expand alt-fork 2 f2)) -(defalias fn3 (template-expand alt-fork 3 f3)) +;; You can use t! as a short form of template-expand +(defalias fn3 (t! alt-fork 3 f3)) (deflayer default (@fn1 @fn2 @fn3)) ---- diff --git a/parser/src/cfg/deftemplate.rs b/parser/src/cfg/deftemplate.rs index 55f06dc54..2707634f3 100644 --- a/parser/src/cfg/deftemplate.rs +++ b/parser/src/cfg/deftemplate.rs @@ -113,7 +113,9 @@ pub fn expand_templates(mut toplevel_exprs: Vec) -> Result err_span!(s, "deftemplate is not allowed within deftemplate"), - "template-expand" => err_span!(s, "template-expand is not allowed within deftemplate"), + "template-expand" | "t!" => { + err_span!(s, "template-expand is not allowed within deftemplate") + } s => { if let Some(count) = var_usage_counts.get_mut(s) { *count += 1; @@ -175,7 +177,7 @@ fn expand(exprs: &mut Vec, templates: &[Template]) -> Result<()> { SExpr::List(l) => { if !matches!( l.t.first().and_then(|expr| expr.atom(None)), - Some("template-expand") + Some("template-expand") | Some("t!") ) { expand(&mut l.t, templates)?; continue; diff --git a/parser/src/cfg/tests.rs b/parser/src/cfg/tests.rs index cc1b0c927..2cbd015bb 100644 --- a/parser/src/cfg/tests.rs +++ b/parser/src/cfg/tests.rs @@ -1645,6 +1645,44 @@ fn parse_template_7() { .expect("parses"); } +#[test] +fn parse_template_8() { + let _lk = lock(&CFG_PARSE_LOCK); + + let source = r#" +(deftemplate home-row (version) + a s d f g h + (if-in-list $version (v0 v3 (concat v (((1)))) v4) (concat j)) + (if-in-list $version ((concat v 0) (concat v (2)) v3 v4) (tap-hold 200 200 (concat j) (if-in-list $version (v0 v3 v4 v2) (concat "k")))) + k l ; ' +) +(deftemplate var () (defvar num 200)) +(defsrc + (t! home-row v1) +) +(deflayer base + (t! home-row v2) +) +(t! var) +(defalias a (tap-dance $num (a))) +"#; + let mut s = ParserState::default(); + parse_cfg_raw_string( + source, + &mut s, + &PathBuf::from("test"), + &mut FileContentProvider { + get_file_content_fn: &mut |_| unimplemented!(), + }, + DEF_LOCAL_KEYS, + Err("env vars not implemented".into()), + ) + .map_err(|e| { + eprintln!("{:?}", miette::Error::from(e)); + }) + .expect("parses"); +} + #[test] fn test_deflayermap() { let source = r#"