mirror of
https://github.com/davidhalter/parso.git
synced 2026-05-24 17:28:53 +08:00
Move some stuff around to make it more flexible.
This commit is contained in:
@@ -7,6 +7,15 @@ class Normalizer(object):
|
|||||||
self._config = config
|
self._config = config
|
||||||
self.issues = []
|
self.issues = []
|
||||||
|
|
||||||
|
def visit(self, node):
|
||||||
|
try:
|
||||||
|
children = node.children
|
||||||
|
except AttributeError:
|
||||||
|
return self.visit_leaf(node)
|
||||||
|
else:
|
||||||
|
with self.visit_node(node):
|
||||||
|
return ''.join(self.visit(child) for child in children)
|
||||||
|
|
||||||
@contextmanager
|
@contextmanager
|
||||||
def visit_node(self):
|
def visit_node(self):
|
||||||
yield
|
yield
|
||||||
|
|||||||
@@ -347,7 +347,7 @@ class PEP8Normalizer(Normalizer):
|
|||||||
|
|
||||||
self._newline_count = 0
|
self._newline_count = 0
|
||||||
|
|
||||||
def normalize(self, leaf):
|
def visit_leaf(self, leaf):
|
||||||
for part in leaf._split_prefix():
|
for part in leaf._split_prefix():
|
||||||
if part.type == 'spacing':
|
if part.type == 'spacing':
|
||||||
# This part is used for the part call after for.
|
# This part is used for the part call after for.
|
||||||
|
|||||||
+3
-12
@@ -162,26 +162,17 @@ class NodeOrLeaf(object):
|
|||||||
"there's no default normalizer for this tree.")
|
"there's no default normalizer for this tree.")
|
||||||
return normalizer_config.create_normalizer()
|
return normalizer_config.create_normalizer()
|
||||||
|
|
||||||
def _will_be_normalize(self, normalizer_config=None):
|
def _normalize(self, normalizer_config=None):
|
||||||
"""
|
"""
|
||||||
TODO this is not public, yet.
|
TODO this is not public, yet.
|
||||||
The returned code will be normalized, e.g. PEP8 for Python.
|
The returned code will be normalized, e.g. PEP8 for Python.
|
||||||
"""
|
"""
|
||||||
normalizer = self._get_normalizer(normalizer_config)
|
normalizer = self._get_normalizer(normalizer_config)
|
||||||
return self._normalize(normalizer)
|
return normalizer.visit(self)
|
||||||
|
|
||||||
def _normalize(self, normalizer):
|
|
||||||
try:
|
|
||||||
children = self.children
|
|
||||||
except AttributeError:
|
|
||||||
return normalizer.normalize(self)
|
|
||||||
else:
|
|
||||||
with normalizer.visit_node(self):
|
|
||||||
return ''.join(child._normalize(normalizer) for child in children)
|
|
||||||
|
|
||||||
def _get_normalizer_issues(self, normalizer_config=None):
|
def _get_normalizer_issues(self, normalizer_config=None):
|
||||||
normalizer = self._get_normalizer(normalizer_config)
|
normalizer = self._get_normalizer(normalizer_config)
|
||||||
self._normalize(normalizer)
|
normalizer.visit(self)
|
||||||
return normalizer.issues
|
return normalizer.issues
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user