[Issue #4108] post_comment_alignment config option#6930
Open
Ripper53 wants to merge 25 commits into
Open
Conversation
Contributor
|
I don't think it makes sense to support a bespoke |
This comment has been minimized.
This comment has been minimized.
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.
Issue
Add config to format like from this issue: #4108
Currently, this code:
Is formatted into this:
This can confuse the reader into thinking that the two comments are related or follow each other, even when they are unrelated.
Add a configuration option to allow code to be formatted with one space between the code and comment.
Changes
Add a config option of
post_comment_alignmentthat has two options:SingleSpaceSameIndentThe
SingleSpacebehavior adds one space between the code and comment.The
SameIndentbehavior keeps each item in the item list aligned. This does not preserve the behavior from before, because it was inconsistent. For example, the new implementation fixes this issue: #4108 (comment)I have edited preexisting tests to match the behavior of the new default option
SingleSpace.write_listnow takes a concrete type ofVec<T>and takes ownership, which allows the use ofiterandinto_iterwithout cloning unlike the previous solution.Removed
align_commentsfromListFormattingstruct. It was previously used to figure out if a comment should be aligned or not, but now the new config option is used.Discussion
I believe there should be a structure that is the single source of truth about the transformations of a comment. For example, how do we know if a comment should be turned into block style or wrapped? Right now, that logic exists in more than a single place, for example, in my rewrite of the
write_listfunction and in the preexistingrewrite_commentfunction.More general discussion:
I also do not like the use of primitive types everywhere. For example, a comment is treated and passed around as
&str, when it could easily be a newtype. So could comment openers and line starters. Plus, I don't think boolean parameters should be used at all in function parameters. Even for configuration, I would prefer a newtype like so:Then, functions like
commen_stylecan change their boolean parameter toNormalizeComments:Call site looks much more readable:
vs.
This also reduces the likelihood of logic errors with the incorrect boolean being supplied to the function. The newtype
NormalizeCommentsis a helpful abstraction that carries meaning with it instead of being a generic boolean that can be mixed and matched with other booleans.