-
Notifications
You must be signed in to change notification settings - Fork 890
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
features to align assignment operators and dictionary colons #1020
base: main
Are you sure you want to change the base?
features to align assignment operators and dictionary colons #1020
Conversation
I don't understand why this doesn't get more attention. Undoubtedly one of the top missing features of python/yapf when compared with Go fmt. Hopefully shipped soon. |
Fantastic. Thanks @lizawang! Have been hunting everywhere for this. Until the merge, am working with your branch directly. |
@lizawang Please run YAPF over your changes. This is looking good. However, this change is massive and very hard to review. Could you split it up into one PR per knob? |
@gwelymernans Hello, by run Yapf over my changes, do you mean to format yapf codes with the new alignment changes? |
This reverts commit 506612e. change formatting back to original.
@lizawang Don't apply your changes to YAPF, but we do still require that YAPF is "clean" when it's ran on itself. So I normally do something like:
to see what changes. |
Hello all yapf founders!
I have some new features I want to share with the yapf community.
This is also a response to issue #194 and issue #346.
All the alignment features added are referred to the yapf function _AlignTrailingComments(final_lines).
The knobs
The main functions for all the knobs are added in reformatter.py.
knob: ALIGN_ASSIGNMENT
This is an option to align the assignment and augmented assignment operators of different variable assignment lines in the same block.
If there is any blank line in between:
If there is any comment line in between:
If there is any if/ while/ for line or function definition def line in between:
If there is any object(e.g. a dictionary, a list, a argument list, a set, a function call) that with its items on newlines after '=' in between:
If there is any variable assignment lines with different indentation in between:
knob: ALIGN_ARGUMENT_ASSIGNMENT
This is an option to align the assignment operators of each block inside the argument list of a function call or a function definition.
The condition : it is only effective if all arguments inside the argument list are placed on separate lines.
The case when the typed argument name is too long that it has newlines, we can increase the column limit, otherwise it will be aligned with the last line of the unwrapped typed argument name that has the assignment operator:
New block of arguments starts with new alignment. There are 3 cases featured as an indication for new alignment block:
knob: ALIGN_DICT_COLON
This is an option to align colons inside dictionary.
knob: NEW_ALIGNMENT_AFTER_COMMENTLINE
This option is made under the consideration that some programmers may prefer to remain the alignment with comment lines inside.
Preparations
To make the alignment functions possible, some other changes are made to other files.
subtype_assigner.py: a change is made to include the subtype TYPED_NAME_ARG_LIST to all tokens inside typed argument name. This is to recognize all tokens before '=' of very long typed name in arguments with typed names, e.g., a typed argument as below:
format_token.py: necessary boolean functions are added, is_assign, is_augassign, is_dict_key, is_dict_key_start, is_argassign, is_argname, is_argname_start.
Finally
In the end, the knobs are added into the style.py and tests for each one are also updated in yapftests folder.