-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add blank lines to separate blocks of indented code #1515
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
Add blank lines to separate blocks of indented code #1515
Conversation
c6a8904
to
058c828
Compare
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.
My changes are all rather minor, so I'm pre-approving this.
ast.NodeVisitor.generic_visit(self, node) | ||
|
||
|
||
def _tokenize(source): |
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 a comment mentioning why you're using an undocumented API.
self.line_numbers_with_statements = [] | ||
|
||
def generic_visit(self, node): | ||
node_type = type(node).__name__ |
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.
Dead line.
|
||
|
||
def _get_global_statement_blocks(source, lines): | ||
"""Gets a list of all global statement blocks. |
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.
"""Return a list of all global statement blocks.
The list comprises of 3-item tuples that contain the starting line number, ending line number,
and whether the statement is a single line.
"""
previous_statement = statement_ranges[-1] | ||
previous_statement_is_oneline = previous_statement[2] | ||
if previous_statement_is_oneline and current_statement_is_oneline: | ||
statement_ranges[-1] = (previous_statement[0], end_line_number, True) |
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.
Parentheses are unnecessary.
|
||
statement_ranges = [] | ||
for index, line_number in enumerate(visitor.line_numbers_with_statements): | ||
remaining_line_numbers = visitor.line_numbers_with_statements[index + 1:] |
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.
You can drop the spaces in index + 1
.
if len(line.strip()) == 0 and token.tok_name[toknum] == 'NL' and spos[0] == epos[0]) | ||
|
||
for line_number in reversed(list(newlines_indexes_to_remove)): | ||
del lines[line_number - 1] |
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.
You can drop the spaces in line_number - 1
.
|
||
# Step 2: Add blank lines between each global statement block. | ||
# A consequtive single lines blocks of code will be treated as a single statement, | ||
# just to ensure we do not unnecessarily add too many blank 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.
Extra leading spaces in the comment.
|
||
global_statement_ranges = _get_global_statement_blocks(source, lines) | ||
|
||
for line_number in (start_line for start_line, _, _ in reversed(global_statement_ranges) if start_line > 1): |
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.
filter(lambda x: x > 1, map(operator.itemgetter(0), reversed(global_statement_ranges))
is another option. 😁
Codecov Report
@@ Coverage Diff @@
## master #1515 +/- ##
==========================================
- Coverage 71.43% 71.33% -0.11%
==========================================
Files 273 273
Lines 12692 12700 +8
Branches 2282 2282
==========================================
- Hits 9066 9059 -7
- Misses 3492 3500 +8
- Partials 134 141 +7
Continue to review full report at Codecov.
|
Fixes #259
This pull request: