Skip to content

fix: formatting macro with braces delimiter - #7031

Open
dswij wants to merge 2 commits into
rust-lang:mainfrom
dswij:issue-6747
Open

fix: formatting macro with braces delimiter#7031
dswij wants to merge 2 commits into
rust-lang:mainfrom
dswij:issue-6747

Conversation

@dswij

@dswij dswij commented Aug 16, 2026

Copy link
Copy Markdown
Member

Fixes #6747.

This PR makes it so that trim_left_preserve_layout does nothing when the first line contains a string start.

e.g., this will now not be formatted

macro! {"
...
"};

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.

@rustbot rustbot added the S-waiting-on-review Status: awaiting review from the assignee but also interested parties. label Aug 16, 2026
Comment thread src/macros.rs
Comment thread src/utils.rs
Comment thread src/utils.rs Outdated
Comment thread src/utils.rs Outdated
Comment thread tests/source/issue_6747.rs Outdated
Comment thread src/utils.rs Outdated
Comment thread src/utils.rs Outdated
@ytmimi

ytmimi commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: awaiting review from the assignee but also interested parties. labels Aug 19, 2026

@ytmimi ytmimi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to revert unnecessary changes

View changes since this review

@ytmimi ytmimi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for continuing to look at this. Here's my next round of review.

View changes since this review

Comment thread src/utils.rs
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,
/// ),
/// }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was the leading foo! removed?

Comment thread src/utils.rs
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,
/// ),
///}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why was the leading foo! removed?

Comment thread src/utils.rs
Some(first_line + "\n" + &trimmed_lines)
}

pub(crate) fn trim_left_preserve_layout_lines(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/utils.rs
}

/// Indent each line according to the specified `indent`.
/// Similar to `[::trim_left_preserve_layout]` but for macros.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to expand on the docs here and mention why we need an independent function for macros.

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

Labels

S-waiting-on-author Status: awaiting some action (such as code changes or more information) from the author.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Strings incorrectly unindented with braced macro

3 participants