Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rustfmt-ify expansion of macro #10548

Closed
emi2k01 opened this issue Oct 15, 2021 · 6 comments · Fixed by #12014
Closed

Rustfmt-ify expansion of macro #10548

emi2k01 opened this issue Oct 15, 2021 · 6 comments · Fixed by #12014
Assignees
Labels
A-ide general IDE features C-feature Category: feature request E-unknown It's unclear if the issue is E-hard or E-easy without digging in

Comments

@emi2k01
Copy link

emi2k01 commented Oct 15, 2021

Just like cargo-expand, it'd be cool if Rust Analyzer had an option to format the expansion of macros.

@lilyball
Copy link

Either that, or allow me to run Format Document on the resulting editor. Trying to do so right now gives me a "Cannot edit in read-only editor" error (in VSCode). Probably formatting the initial expansion is better though.

@Veykril
Copy link
Member

Veykril commented Dec 17, 2021

The problem here is that macros don't necessarily expand to something on its own that rustfmt can format(to my knowledge) so this wouldn't always work as we don't expand macros into the source file.

@Veykril Veykril added A-ide general IDE features E-unknown It's unclear if the issue is E-hard or E-easy without digging in labels Dec 17, 2021
@bjorn3
Copy link
Member

bjorn3 commented Dec 17, 2021

It should be possible to wrap the macro output in for example fn foo() { ... } or fn foo() { let ... = (); } depending on the context in which the macro expanded and then pass it to rustfmt. Macros never expand to raw tokenstreams.

@lilyball
Copy link

I did just realize yesterday that the expansion does still include $crate, which isn't legal outside of macros, and unfortunately rustfmt cares.

> echo 'fn foo() { $crate::print("Bar"); }' | rustfmt
error: expected expression, found `$`
 --> <stdin>:1:12
  |
1 | fn foo() { $crate::print("Bar"); }
  |            ^ expected expression

@Veykril
Copy link
Member

Veykril commented Dec 19, 2021

Mmh ye that is a problem since we can't always replace them with the crate name its referring to since the crate in question isn't necessarily in the nameable dependencies of the current crate. We could replace $crate with some special valid identifier, rust rustfmt and then replace them it back in the output again I suppose?

@lnicola
Copy link
Member

lnicola commented Dec 19, 2021

CC #7532

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ide general IDE features C-feature Category: feature request E-unknown It's unclear if the issue is E-hard or E-easy without digging in
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants