Stream: t-compiler/rust-analyzer

Topic: applying formatting in a rust-analyzer assist


Michael Bryan (Jan 02 2021 at 06:40, on Zulip):

I made an assist (#7131) and was wondering if I need to make sure the replacement code is formatted correctly.

The doc-comment currently looks like this:

// ```
// fn add(a: u32, b: u32) -> u32 { a + b }
// fn main() {
//     let x = add<|>(1, 2);
// }
// ```
// ->
// ```
// fn add(a: u32, b: u32) -> u32 { a + b }
// fn main() {
//     let x = { let a = 1; let b = 2; a + b };
// }
// ```

But the generate code comes out like this:

fn add(a: u32, b: u32) -> u32 { a + b }
fn main() {
    let x = {
let a = 1;
let b = 2;
a + b
};
}

If you ignore whitespace the two are syntactically identical, however the generated test fails saying the "text differs".

Should I leave the output as-is and just update my doc-test to expect the unformatted, or is there some sort of formatting routine I can call?

Lukas Wirth (Jan 02 2021 at 11:14, on Zulip):

You can control indentation of syntax nodes to some degree, see https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/assists/src/handlers/replace_if_let_with_match.rs for example

Lukas Wirth (Jan 02 2021 at 11:15, on Zulip):

As in you can set a nodes indentation level and read it out as well.

Michael Bryan (Jan 02 2021 at 20:14, on Zulip):

Thanks for the pointer, @Lukas Wirth! I ended up getting exactly the result I was looking for.

Now I just need to wait for someone more knowledgeable of rust-analyzer to review the PR and make suggestions on how I can generate simpler code for trivial cases.

Last update: Jul 29 2021 at 09:00UTC