Add blank lines to separate blocks of indented code#1515
Add blank lines to separate blocks of indented code#1515DonJayamanne merged 13 commits intomicrosoft:masterfrom
Conversation
c6a8904 to
058c828
Compare
brettcannon
left a comment
There was a problem hiding this comment.
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.
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__ |
|
|
||
|
|
||
| def _get_global_statement_blocks(source, lines): | ||
| """Gets a list of all global statement blocks. |
There was a problem hiding this comment.
"""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.
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.
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.
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.
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.
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: