-
Notifications
You must be signed in to change notification settings - Fork 889
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
Support formatting macro calls with simple brace delimited arguments #6327
Conversation
Thanks for the PR, but I'm going to close this. At the moment it's an intentional decision to not format brace delimited macros. |
@ytmimi Sincerely, let me say a big "thank you!" from me and (I am sure) everybody else working in a Rust codebase. I understand being an OSS maintainer can be a difficult and thankless job. A well-maintained formatter is a difference-maker for Rust's ergonomics. Thank you! I want to take 60 seconds to sell you on this PR. A quick search (pasted below) shows over 25% of open issues pertain to macro formatting. Many are over a year old. Macro formatting is a common pain point for your users. There are special cases already cooked into rustfmt for paren and bracket-delimited macros. Generic formatting inside macro invocations is hard, but there is an 80/20 of value in supporting common patterns (like It won't format everything, but this PR smoothes a particularly sharp corner in the UX. I know you have more context on why this is intentionally un-addressed. I don't want to speculate too much... Could you please explain why? Whatever outcome with this PR, you are doing valuable work - I look forward to seeing rustfmt continue to grow!
|
@johnislarry Thanks for the kind words, it really means a lot. I agree that macro formatting is a common pain point, and it's also one of the trickier things rustfmt has to try and handle. Apart from some special casing, rustfmt can only generally format macro calls if the tokens inside those macro calls parse as valid rust code. Macros are allowed to contain any valid rust tokens, which might not necessarily form valid rust syntax, so rustfmt often runs into issues when attempting to parse those tokens. There are also cases where parsing succeeds but it's ambiguous and then rustfmt ends up butchering the formatting (#5709 comes to mind). To combat some of those issues, rustfmt special cases brace delimited macros |
@ytmimi I hear what you are saying. Formatting macro calls makes rustfmt a harder engineering problem than (for example) prettier. Sometimes difficulty (or lack of perfection) is used as an excuse to delay a solution. And issues can go unresolved for years due to this mindset. I can certainly relate to this. This PR preserves the brace-delimited escape hatch. It only adds an extra case for Respectfully, I want to ask: Why are you not ready to change this default? |
I don't think that's the case based on the example you provided in the description nor does this hold for the test cases in the PR. You're formatting macros that use braces
Personally, I'm a fan of the flexibility that the escape hatch of not formatting |
This PR falls back to the original behavior, except for when the macro body is this There are many other existing test cases (that still pass) where brace-delimited macros remain un-formatted. I apologize for not being clear about this - maybe this change was perceived as bigger than it is? This is where the original behavior is used as a fallback: https://github.com/rust-lang/rustfmt/pull/6327/files#diff-315c02cd05738da173861537577d159833f70f79cfda8cd7cf1a0d7a28ace31bR368-R374 Most brace-delimited macros will still take this fallback code path.
I can appreciate that this approach makes rustfmt simpler to implement. The problem is that it makes it a less-effective code formatter. (Defined by % of lines where it gives up) I have to add: I really appreciate your willingness to engage in a discussion about this! |
Sorry, but that doesn't change the fact that this formats As I mentioned above, the current behavior around |
Understood - I hope some day there's a better solution here. Good luck! |
Rustfmt gives up when it sees a macro with a brace-delimited argument. This PR implements formatting for a subset of these: key-value maps. For example:
now has its indentation fixed:
If the macro argument can't be parsed as key-value pairs, then it falls back to the old behavior.
I added a couple test cases and
cargo test
passes locally.This is my first rustfmt PR so I hope it's in the right direction - please let me know what I need to do to get this merged!