rustfmt icon indicating copy to clipboard operation
rustfmt copied to clipboard

Fix match enum OR comment indentation

Open MarcusGrass opened this issue 1 year ago • 2 comments

Fixes https://github.com/rust-lang/rustfmt/issues/6060

This was a bit tricky.

When the inner item is multiline, identation-count before the actual inner item start throws off the line count.

In the usual case, the inner item doesn't contain indentation, example:

fn simple(
    // pre-comment on a function!?
    i: i32,          // yes, it's possible!
    response: NoWay, // hose
)

in the above case, the inner item is i: i32, no comma, no indentation.

So when calculating whether the inner item is too long to add more spaces already, indentation needs to be removed beforehand, but only for inner items that have newlines in them. In the below case, the inner item is the entire:

msgs::DecodeError::UnknownRequiredFeature
        | msgs::DecodeError::InvalidValue
        | msgs::DecodeError::ShortRead => 0x4000 | 22

Newlines and indentation all.

If the line does become too long by the comment, it will be split and use the old logic in a branch, I added a test for that as well.

MarcusGrass avatar Feb 22 '24 08:02 MarcusGrass

Rebased to master since this had become a bit stale

MarcusGrass avatar Oct 12 '25 16:10 MarcusGrass

On hold, ran some diffs and it looks like here may be some problems with this one

MarcusGrass avatar Oct 24 '25 20:10 MarcusGrass