Add blank line after constants in stub file (#360)

Fixes #340
This commit is contained in:
Jelle Zijlstra 2018-06-16 11:53:45 -07:00 committed by Łukasz Langa
parent af8fd24068
commit 9db828c3de
2 changed files with 54 additions and 38 deletions

View File

@ -1390,44 +1390,8 @@ def _maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
before = 0 if depth else 1 before = 0 if depth else 1
else: else:
before = 1 if depth else 2 before = 1 if depth else 2
is_decorator = current_line.is_decorator if current_line.is_decorator or current_line.is_def or current_line.is_class:
if is_decorator or current_line.is_def or current_line.is_class: return self._maybe_empty_lines_for_class_or_def(current_line, before)
if not is_decorator:
self.previous_defs.append(depth)
if self.previous_line is None:
# Don't insert empty lines before the first line in the file.
return 0, 0
if self.previous_line.is_decorator:
return 0, 0
if self.previous_line.depth < current_line.depth and (
self.previous_line.is_class or self.previous_line.is_def
):
return 0, 0
if (
self.previous_line.is_comment
and self.previous_line.depth == current_line.depth
and before == 0
):
return 0, 0
if self.is_pyi:
if self.previous_line.depth > current_line.depth:
newlines = 1
elif current_line.is_class or self.previous_line.is_class:
if current_line.is_stub_class and self.previous_line.is_stub_class:
newlines = 0
else:
newlines = 1
else:
newlines = 0
else:
newlines = 2
if current_line.depth and newlines:
newlines -= 1
return newlines, 0
if ( if (
self.previous_line self.previous_line
@ -1446,6 +1410,50 @@ def _maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
return before, 0 return before, 0
def _maybe_empty_lines_for_class_or_def(
self, current_line: Line, before: int
) -> Tuple[int, int]:
if not current_line.is_decorator:
self.previous_defs.append(current_line.depth)
if self.previous_line is None:
# Don't insert empty lines before the first line in the file.
return 0, 0
if self.previous_line.is_decorator:
return 0, 0
if self.previous_line.depth < current_line.depth and (
self.previous_line.is_class or self.previous_line.is_def
):
return 0, 0
if (
self.previous_line.is_comment
and self.previous_line.depth == current_line.depth
and before == 0
):
return 0, 0
if self.is_pyi:
if self.previous_line.depth > current_line.depth:
newlines = 1
elif current_line.is_class or self.previous_line.is_class:
if current_line.is_stub_class and self.previous_line.is_stub_class:
# No blank line between classes with an emty body
newlines = 0
else:
newlines = 1
elif current_line.is_def and not self.previous_line.is_def:
# Blank line between a block of functions and a block of non-functions
newlines = 1
else:
newlines = 0
else:
newlines = 2
if current_line.depth and newlines:
newlines -= 1
return newlines, 0
@dataclass @dataclass
class LineGenerator(Visitor[Line]): class LineGenerator(Visitor[Line]):

View File

@ -1,3 +1,7 @@
X: int
def f(): ...
class C: class C:
... ...
@ -16,6 +20,10 @@ def g():
def h(): ... def h(): ...
# output # output
X: int
def f(): ...
class C: ... class C: ...
class B: ... class B: ...