Skip to content

Quasy quoting for assists #2227

Open
Open

Description

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-assistsBroken WindowBugs / technical debt to be addressed immediatelyE-hardS-actionableSomeone could pick this issue up and work on it right nowfunA technically challenging issue with high impact

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions