Open
Description
opened on Nov 13, 2019
Assist (and, once we have more of those, fixits and refactorings) need to produce a lot of syntax trees.
This is done with the help of make
module at the moment. Here's how a moderately-complex let statement is produced:
let match_expr = {
let happy_arm = make::match_arm(
once(
make::tuple_struct_pat(
path,
once(make::bind_pat(make::name("it")).into()),
)
.into(),
),
make::expr_path(make::path_from_name_ref(make::name_ref("it"))).into(),
);
let sad_arm = make::match_arm(
once(make::placeholder_pat().into()),
early_expression.into(),
);
make::expr_match(cond_expr, make::match_arm_list(vec![happy_arm, sad_arm]))
};
let let_stmt = make::let_stmt(
make::bind_pat(make::name(&bound_ident.syntax().to_string())).into(),
Some(match_expr.into()),
);
It would be sweet if the above code could be condensed to the following:
let let_stmt = magic! {
let $bound_ident = match $cond_expr {
$path(it) => it,
_ => $early_expression,
}
};
There exists a trivial solution: use format!
, produce a string, re-parse string back into an AST. This actually works great! This is the solution employed by IntelliJ Rust(example), and I don't remember having any problems with it.
However, adding more type-safety seems like a good thing to try! So, the magic!
macro should ideally produce the code above, and not some formatting-based thing.
Activity