-
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
Feature align assignment #1030
base: main
Are you sure you want to change the base?
Feature align assignment #1030
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has way too many reformatting changes in it. Please make sure that the new options aren't enabled by default. Also please make sure that you're using the correct formatting style for YAPF.
.. code-block:: python | ||
|
||
fields = | ||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This opening bracket should be on the first line.
@@ -2,6 +2,10 @@ | |||
# All notable changes to this project will be documented in this file. | |||
# This project adheres to [Semantic Versioning](http://semver.org/). | |||
|
|||
## [0.41.1] 2022-08-30 | |||
### Added | |||
- Add 4 new knobs to align assignment operators and dictionary colons. They are align_assignment, align_argument_assignment, align_dict_colon and new_alignment_after_commentline. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please reflow this to 80-columns.
@@ -89,7 +89,7 @@ def Visit_classdef(self, node): # pylint: disable=invalid-name | |||
if len(node.children) > 4: | |||
# opening '(' | |||
_SetUnbreakable(node.children[2]) | |||
# ':' | |||
# ':' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know why this is here...
@@ -240,6 +240,7 @@ def Visit_argument(self, node): # pylint: disable=invalid-name | |||
# argument ::= | |||
# test [comp_for] | test '=' test | |||
self._ProcessArgLists(node) | |||
#TODO add a subtype to each argument? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove any TODO comments.
|
||
@property | ||
def is_augassign(self): | ||
augassigns = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use a frozenset
and move this out of the method into the class itself.
@@ -394,6 +398,172 @@ def _AlignTrailingComments(final_lines): | |||
final_lines_index += 1 | |||
|
|||
|
|||
def _AlignAssignment(final_lines): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function is very long. Please split it up into smaller functions so that it's more readable.
it will start new block alignment."""), | ||
NEW_ALIGNMENT_AFTER_COMMENTLINE=textwrap.dedent("""\ | ||
Start new assignment or colon alignment when there is a newline comment in between.""" | ||
), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please don't leave the ending paren in that place. :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm also not a fan of this knob's name. Maybe something like ALIGN_ASSIGNMENT_AFTER_COMMENTS
.
@@ -54,6 +54,13 @@ def SetGlobalStyle(style): | |||
_STYLE_HELP = dict( | |||
ALIGN_CLOSING_BRACKET_WITH_VISUAL_INDENT=textwrap.dedent("""\ | |||
Align closing bracket with visual indentation."""), | |||
ALIGN_ASSIGNMENT=textwrap.dedent("""\ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add the examples to these knobs.
Also the knobs should be alphabetized.
@@ -15,10 +15,11 @@ | |||
|
|||
import unittest | |||
|
|||
from lib2to3 import pytree | |||
from lib2to3 import pytree, pygram |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you need this change.
from lib2to3.pgen2 import token | ||
|
||
from yapf.yapflib import format_token | ||
from yapf.pytree import subtype_assigner |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or this change.
Hello all yapf founders!
I have some new features I want to share with the yapf community.
The alignment feature added is 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: 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.
Finally
In the end, the knobs are added into the style.py and tests for each one are also updated in yapftests folder.