Add configuration option for wrapping of assignment right-hand side #4886
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.
This PR adds a new configuration option called
righthand_indentation_strategy
(WIP name) that allows configuring of how rustfmt will emit assignment statements if the value does not fit on a single line.There"s three options.
Heuristic
will use the same heuristics as the current rustfmt, and is the default.SameLineAsLHS
will always attempt to place the first part of the expression on the same line as the left-hand, as long as that does not exceed the maximum width.NewlineIndentRHS
does the opposite, instead preferring to always newline-indent the expression if it does not fit entirely on the same line as the lhs.A short example (where
|
represents the max width):There are better examples showing the exact differences in Configurations.md.
This PR addresses #3514, both the original request (
NewlineIndentRHS
) as well as the comment by @seanpianka (SameLineAsLHS
, which was also the configuration option I wanted myself). As far as I can see the changes should be backwards-compatible and always produce legal formatting.There are some things I didn"t fully know what to do with, so I"m counting on some guidance from you here. In particular:
Configuration name. I don"t like the current name, since it talks about indentation (we"re only doing different indentation in
NewlineIndentRHS
). Possibly something likeassignment_wrapping_behavior
?TODO Items. There"s several todo items in the code that relate to situations in which we"re only able to format the code in one way even though the user specifically asks for a different way. Consider the following example, with a tab width of 12 (extreme example):
This is how it"d look formatted with both
NewlineIndentRHS
andSameLineAsLHS
:If the user has
NewlineIndentRHS
, then we have two options. Either we can fail entirely (since we weren"t able to format within the width they wanted), or we can fall back to the same-line layout that does work. Right now I"ve opted to do the second (and the documentation is written with this approach in mind), but I can also see the argument for failing entirely. The current TODOs in the code refer to this situation (and the inverse, where we"re able to do things on the newline but not on the same line).Let me know your thoughts! As far as I can see, the changes should be isolated to just assigns and backwards compatible.