Skip to content

Fix handling of code that is annotated with rustfmt::skip. #3113

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

Merged
merged 2 commits into from
Oct 24, 2018

Conversation

scampi
Copy link
Contributor

@scampi scampi commented Oct 18, 2018

A rustfmt::skip'ed block is indented although original lines are
returned. In order to resolve this, the leading whitespaces are trimmed
on each line while retaining the layout; this leaves the skipped code
to be indented as necessary by the caller.

Close #3105

@topecongiro
Copy link
Contributor

Hmm, actually I am not sure if rustfmt is allowed to change the indentation of items under rustfmt_skip.
cc @nrc

@scampi
Copy link
Contributor Author

scampi commented Oct 22, 2018

@topecongiro at first I thought keeping the layout was enough but indeed I realise now there can be situations where even indenting the code with the rest would be unwanted.

Maybe a better fix would be not to change the indentation at all:

  • the source tests added in the PR should remain as is, i.e., the skipped methods shouldn't be indented with the rest
  • the method from the original issue shouldn't right-shift either

what do you think ?

@otavio
Copy link
Contributor

otavio commented Oct 23, 2018

@scampi, I agree. If something is marked as rustfmt::skip it must be kept unchanged.

@scampi
Copy link
Contributor Author

scampi commented Oct 23, 2018

@topecongiro @otavio thanks for your input. Just proposing the changes now to know if that is a good direction. The solution I have found is to keep track of skipped lines which allows caller code to act on those accordingly. Those lines are stored as a list of ranges.

In addition to skipped lines, this list could also be used to keep track of lines that could not be formatted and where the original lines were used instead, e.g., the issue #3045.

@nrc
Copy link
Member

nrc commented Oct 24, 2018

Hmm, actually I am not sure if rustfmt is allowed to change the indentation of items under rustfmt_skip.

I think we should not change the indentation at all, since it is possible to make the lines overrun the max width. The user can change the indentation manually and then we'll preserve it.

@nrc
Copy link
Member

nrc commented Oct 24, 2018

I'm unclear now - is this intended to fit rustfmt::skiped code? And how is that related to macros?

@scampi
Copy link
Contributor Author

scampi commented Oct 24, 2018

Taking the shuffle_done! macro definition as example, the method push_skipped_with_span is called on simd_shuffle32, which is annotated with rustfmt::skip. Because the formatting is skipped, the original lines, with the leading whitespaces, are returned.

https://github.com/rust-lang-nursery/rustfmt/blob/4789f65041d428a597f91cea9ad90b548b93cb28/src/visitor.rs#L592

That simd_shuffle32 method is within a macro definition, which body is formatted with MacroBranch::rewrite:

https://github.com/rust-lang-nursery/rustfmt/blob/4789f65041d428a597f91cea9ad90b548b93cb28/src/macros.rs#L1326-L1336

The problem occurs at this point: the following piece is indenting the macro's body.

https://github.com/rust-lang-nursery/rustfmt/blob/4789f65041d428a597f91cea9ad90b548b93cb28/src/macros.rs#L1344-L1349

Since the original (skipped) lines were returned, the simd_shuffle32 method is once again indented, therefore right-shifting that method.

A rustfmt::skip'ed block is indented although original lines are
returned. In order to resolve this, the leading whitespaces are trimmed
on each line while retaining the layout; this leaves the skipped code
to be indented as necessary by the caller.
… indentation which would cause code right-shifting
@nrc nrc merged commit fbeabe9 into rust-lang:master Oct 24, 2018
@nrc
Copy link
Member

nrc commented Oct 24, 2018

Thank you!

@scampi scampi deleted the issue3105 branch October 25, 2018 06:53
@topecongiro
Copy link
Contributor

@scampi My apologies for the late reply. Thank you for your work and updates!

@scampi
Copy link
Contributor Author

scampi commented Oct 26, 2018

no worries @topecongiro

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

skipped code in macro defintion is right shifted
4 participants