mirror of
https://github.com/davidhalter/parso.git
synced 2025-12-15 17:07:13 +08:00
Fix most newline issues.
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
|
from functools import total_ordering
|
||||||
|
|
||||||
|
|
||||||
class Normalizer(object):
|
class Normalizer(object):
|
||||||
@@ -15,7 +16,8 @@ class Normalizer(object):
|
|||||||
|
|
||||||
def add_issue(self, code, message, node):
|
def add_issue(self, code, message, node):
|
||||||
issue = Issue(node, code, message)
|
issue = Issue(node, code, message)
|
||||||
self.issues.append(issue)
|
if issue not in self.issues:
|
||||||
|
self.issues.append(issue)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
@@ -53,6 +55,16 @@ class Issue(object):
|
|||||||
self.message = message
|
self.message = message
|
||||||
self.start_pos = node.start_pos
|
self.start_pos = node.start_pos
|
||||||
|
|
||||||
|
def __eq__(self, other):
|
||||||
|
return self.start_pos == other.start_pos and self.code == other.code
|
||||||
|
|
||||||
|
def __ne__(self, other):
|
||||||
|
return not self.__eq__(other)
|
||||||
|
|
||||||
|
def __hash__(self):
|
||||||
|
return hash((self.code, self.start_pos))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Rule(object):
|
class Rule(object):
|
||||||
error_code = None
|
error_code = None
|
||||||
|
|||||||
@@ -141,9 +141,11 @@ class PEP8Normalizer(Normalizer):
|
|||||||
def __init__(self, config):
|
def __init__(self, config):
|
||||||
super(PEP8Normalizer, self).__init__(config)
|
super(PEP8Normalizer, self).__init__(config)
|
||||||
self._previous_leaf = None
|
self._previous_leaf = None
|
||||||
|
self._actual_previous_leaf = None
|
||||||
self._on_newline = True
|
self._on_newline = True
|
||||||
self._newline_count = 0
|
self._newline_count = 0
|
||||||
self._wanted_newline_count = None
|
self._wanted_newline_count = None
|
||||||
|
self._max_new_lines_in_prefix = 0
|
||||||
self._new_statement = True
|
self._new_statement = True
|
||||||
self._implicit_indentation_possible = False
|
self._implicit_indentation_possible = False
|
||||||
# The top of stack of the indentation nodes.
|
# The top of stack of the indentation nodes.
|
||||||
@@ -264,6 +266,9 @@ class PEP8Normalizer(Normalizer):
|
|||||||
return int(suite_node.parent is None) + 1
|
return int(suite_node.parent is None) + 1
|
||||||
|
|
||||||
def _reset_newlines(self, spacing, actual_leaf, is_comment=False):
|
def _reset_newlines(self, spacing, actual_leaf, is_comment=False):
|
||||||
|
self._max_new_lines_in_prefix = \
|
||||||
|
max(self._max_new_lines_in_prefix, self._newline_count)
|
||||||
|
|
||||||
wanted = self._wanted_newline_count
|
wanted = self._wanted_newline_count
|
||||||
if wanted is not None:
|
if wanted is not None:
|
||||||
# Need to substract one
|
# Need to substract one
|
||||||
@@ -280,6 +285,27 @@ class PEP8Normalizer(Normalizer):
|
|||||||
else:
|
else:
|
||||||
self._wanted_newline_count = None
|
self._wanted_newline_count = None
|
||||||
|
|
||||||
|
if not is_comment:
|
||||||
|
wanted = self._get_wanted_blank_lines_count()
|
||||||
|
actual = self._max_new_lines_in_prefix - 1
|
||||||
|
|
||||||
|
val = actual_leaf.value
|
||||||
|
needs_lines = (
|
||||||
|
val == '@' and actual_leaf.parent.type == 'decorator'
|
||||||
|
or val == 'class'
|
||||||
|
or val == 'async' and actual_leaf.get_next_leaf() == 'def'
|
||||||
|
or val == 'def' and self._actual_previous_leaf != 'async'
|
||||||
|
)
|
||||||
|
if needs_lines and actual < wanted:
|
||||||
|
# The first leaf should not be added.
|
||||||
|
if self._actual_previous_leaf is not None:
|
||||||
|
code = 302 if wanted == 2 else 301
|
||||||
|
message = "expected %s blank line, found %s" \
|
||||||
|
% (wanted, actual)
|
||||||
|
self.add_issue(code, message, spacing)
|
||||||
|
|
||||||
|
self._max_new_lines_in_prefix = 0
|
||||||
|
|
||||||
self._newline_count = 0
|
self._newline_count = 0
|
||||||
|
|
||||||
def normalize(self, leaf):
|
def normalize(self, leaf):
|
||||||
@@ -306,6 +332,9 @@ class PEP8Normalizer(Normalizer):
|
|||||||
|
|
||||||
if not self._new_statement:
|
if not self._new_statement:
|
||||||
self._reset_newlines(part, leaf)
|
self._reset_newlines(part, leaf)
|
||||||
|
self._max_blank_lines = 0
|
||||||
|
|
||||||
|
self._actual_previous_leaf = leaf
|
||||||
|
|
||||||
return x
|
return x
|
||||||
|
|
||||||
|
|||||||
@@ -146,14 +146,14 @@ if a():
|
|||||||
a()
|
a()
|
||||||
|
|
||||||
|
|
||||||
#: E301+3:4
|
#: E301+2
|
||||||
def a():
|
def a():
|
||||||
x = 1
|
x = 1
|
||||||
def b():
|
def b():
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
#: E301+3:4 E301+5:8
|
#: E301+2 E301+4
|
||||||
def a():
|
def a():
|
||||||
x = 2
|
x = 2
|
||||||
def b():
|
def b():
|
||||||
@@ -162,7 +162,7 @@ def a():
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
#: E301+4 E301+6
|
#: E301+2 E301+4 E301+5
|
||||||
def a():
|
def a():
|
||||||
x = 1
|
x = 1
|
||||||
class C:
|
class C:
|
||||||
|
|||||||
Reference in New Issue
Block a user