Skip to content

Commit

Permalink
feat: allow t! in place of template-expand (#958)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jtroo authored Apr 22, 2024
1 parent 2ff1385 commit 0eba84f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
3 changes: 2 additions & 1 deletion cfg_samples/kanata.kbd
Original file line number Diff line number Diff line change
Expand Up @@ -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
_ _ _ _ _ _ _ _ _ _ _ _ _ _
Expand Down
3 changes: 2 additions & 1 deletion docs/config.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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))
----

Expand Down
6 changes: 4 additions & 2 deletions parser/src/cfg/deftemplate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ pub fn expand_templates(mut toplevel_exprs: Vec<TopLevel>) -> Result<Vec<TopLeve
.collect();
visit_validate_all_atoms(&content, &mut |s| match s.t.as_str() {
"deftemplate" => 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;
Expand Down Expand Up @@ -175,7 +177,7 @@ fn expand(exprs: &mut Vec<SExpr>, 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;
Expand Down
38 changes: 38 additions & 0 deletions parser/src/cfg/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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#"
Expand Down

0 comments on commit 0eba84f

Please sign in to comment.