fix: formatting macro with braces delimiter - #7031
Open
dswij wants to merge 2 commits into
Open
Conversation
dswij
commented
Aug 16, 2026
ytmimi
reviewed
Aug 19, 2026
ytmimi
reviewed
Aug 19, 2026
ytmimi
reviewed
Aug 19, 2026
ytmimi
reviewed
Aug 19, 2026
ytmimi
reviewed
Aug 19, 2026
Contributor
|
@rustbot author |
ytmimi
requested changes
Aug 28, 2026
ytmimi
requested changes
Sep 2, 2026
Comment on lines
614
to
+643
| @@ -615,43 +607,73 @@ | |||
| buffer | |||
| } | |||
|
|
|||
| /// Indent each line according to the specified `indent`. | |||
| /// Similar to `[::trim_left_preserve_layout]` but for macros. | |||
| pub(crate) fn trim_left_preserve_layout_macros( | |||
| orig: &str, | |||
| indent: Indent, | |||
| config: &Config, | |||
| ) -> Option<String> { | |||
| let mut lines = LineClasses::new(orig); | |||
| let (first_line_kind, first_line) = lines.next()?; | |||
|
|
|||
| // If a macro delimiter and a string start(`"`) is in the same line, then skip trimming | |||
| // altogether. | |||
| if first_line_kind == FullCodeCharKind::StartString { | |||
| return Some(orig.to_string()); | |||
| } | |||
|
|
|||
| let trimmed_lines = trim_left_preserve_layout_lines(lines, indent, config)?; | |||
| Some(first_line + "\n" + &trimmed_lines) | |||
| } | |||
|
|
|||
| /// Trim each line based on the minimum indentation of the snippet and | |||
| /// indent line based on `Indent`. First line and lines within a string are skipped | |||
| /// | |||
| /// e.g. | |||
| /// | |||
| /// ```rust,compile_fail | |||
| /// foo!{ | |||
| /// x, | |||
| /// y, | |||
| /// foo( | |||
| /// a, | |||
| /// b, | |||
| /// c, | |||
| /// ), | |||
| /// } | |||
| /// { | |||
| /// x, | |||
| /// y, | |||
| /// foo( | |||
| /// a, | |||
| /// b, | |||
| /// c, | |||
| /// ), | |||
| /// } | |||
Contributor
There was a problem hiding this comment.
Why was the leading foo! removed?
Comment on lines
-628
to
+657
| /// c, | ||
| /// ), | ||
| /// } | ||
| /// { | ||
| /// x, | ||
| /// y, | ||
| /// foo( | ||
| /// a, | ||
| /// b, | ||
| /// c, | ||
| /// ), | ||
| /// } | ||
| /// ``` | ||
| /// | ||
| /// will become | ||
| /// | ||
| /// ```rust,compile_fail | ||
| /// foo!{ | ||
| /// x, | ||
| /// y, | ||
| /// foo( | ||
| /// a, | ||
| /// b, | ||
| /// c, | ||
| /// ), | ||
| /// } | ||
| /// { | ||
| /// x, | ||
| /// y, | ||
| /// foo( | ||
| /// a, | ||
| /// b, | ||
| /// c, | ||
| /// ), | ||
| ///} |
Contributor
There was a problem hiding this comment.
why was the leading foo! removed?
| Some(first_line + "\n" + &trimmed_lines) | ||
| } | ||
|
|
||
| pub(crate) fn trim_left_preserve_layout_lines( |
Contributor
There was a problem hiding this comment.
Not that it really matters, but I don't think trim_left_preserve_layout_lines needs to be pub(crate) Since it's just an internal helper function for trim_left_preserve_layout and trim_left_preserve_layout_macros.
| } | ||
|
|
||
| /// Indent each line according to the specified `indent`. | ||
| /// Similar to `[::trim_left_preserve_layout]` but for macros. |
Contributor
There was a problem hiding this comment.
It would be good to expand on the docs here and mention why we need an independent function for macros.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #6747.
This PR makes it so that
trim_left_preserve_layoutdoes nothing when the first line contains a string start.e.g., this will now not be formatted
I'm not too satisfied with this PR; I think the formatting for macros with braces
{,}needs to be reworked. I'm opening this PR as a starting point for that goal.