-
Notifications
You must be signed in to change notification settings - Fork 898
Feature align dict colon #1032
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
base: main
Are you sure you want to change the base?
Feature align dict colon #1032
Conversation
Bump! |
+1 |
@lizawang this PR is blocked by merge conflicts. |
I don't see anything standing out besides resolving merge conflicts. @bwendling. @EeyoreLee , @char101 any thoughts ? |
@Spitfire1900 - Thanks for your invitation. I think this feature should be considered cause the feature-request has 42 upvotes. A code review is essential before merging, e.g., the function |
.. 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 isn't legal Python. The opening bracket should go on the previous line.
} | ||
|
||
``NEW_ALIGNMENT_AFTER_COMMENTLINE`` | ||
Make it optional to start a new alignmetn block for assignment |
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.
s/alignmetn/alignment/
@@ -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.
Probably don't need this change in the commit.
@@ -54,6 +54,14 @@ def SetGlobalStyle(style): | |||
_STYLE_HELP = dict( | |||
ALIGN_CLOSING_BRACKET_WITH_VISUAL_INDENT=textwrap.dedent("""\ | |||
Align closing bracket with visual indentation."""), | |||
ALIGN_DICT_COLON=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.
The other two knobs aren't added here?
@@ -394,6 +399,186 @@ def _AlignTrailingComments(final_lines): | |||
final_lines_index += 1 | |||
|
|||
|
|||
def _AlignDictColon(final_lines): | |||
"""Align colons in a dict to the same column""" | |||
"""NOTE One (nested) dict/list is one logical line!""" |
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.
Add this line as part of the previous docstring.
prefix = line_tok.formatted_whitespace_prefix | ||
newline = prefix.startswith('\n') | ||
if newline: | ||
# if comments inbetween, save, reset and continue to caluclate new alignment |
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 comment to 80-columns.
this_line = line | ||
|
||
line_tokens = this_line.tokens | ||
for open_index in range(len(line_tokens)): |
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.
Use enumerate
?
line_tok = line_tokens[open_index] | ||
|
||
# check each time if the detected dict is the dict we aim for | ||
if line_tok.value == '{' and line_tok.next_token.formatted_whitespace_prefix.startswith( |
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 put the conditional in parentheses and reformat to 80-columns.
@@ -394,6 +399,186 @@ def _AlignTrailingComments(final_lines): | |||
final_lines_index += 1 | |||
|
|||
|
|||
def _AlignDictColon(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.
I know that this is similar to the _AlignTrailingComments
function, but it's also much longer. Could you split some of the code out into separate functions to make it more readable?
Thanks for reviewing @bwendling 👍 |
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.
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_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.
Finally
In the end, the knobs are added into the style.py and tests for each one are also updated in yapftests folder.