Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
- Standalone form feed characters at the module level are no longer removed (#4021)
- Additional cases of immediately nested tuples, lists, and dictionaries are now
indented less (#4012)
- Allow empty lines at the beginning of all blocks, except immediately before a
docstring (#4060)
- Fix crash in preview mode when using a short `--line-length` (#4086)

### Configuration
Expand Down
14 changes: 5 additions & 9 deletions src/black/lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,18 +689,14 @@ def _maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
return 0, 1
return before, 1

# In preview mode, always allow blank lines, except right before a function
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw a few lines up we have and current_line.is_triple_quoted_string. Should that also use is_docstring? Seems preferable to have consistent behaviour for single quote docstrings as well

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I don't really understand why we sometimes use one and sometimes the other. I'll merge this first and then maybe make another preview style change harmonizing between these two.

# docstring
is_empty_first_line_ok = (
Preview.allow_empty_first_line_before_new_block_or_comment
in current_line.mode
Preview.allow_empty_first_line_in_block in current_line.mode
and (
# If it's a standalone comment
current_line.leaves[0].type == STANDALONE_COMMENT
# If it opens a new block
or current_line.opens_block
# If it's a triple quote comment (but not at the start of a funcdef)
not is_docstring(current_line.leaves[0])
or (
is_docstring(current_line.leaves[0])
and self.previous_line
self.previous_line
and self.previous_line.leaves[0]
and self.previous_line.leaves[0].parent
and not is_funcdef(self.previous_line.leaves[0].parent)
Expand Down
2 changes: 1 addition & 1 deletion src/black/mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ class Preview(Enum):
accept_raw_docstrings = auto()
fix_power_op_line_length = auto()
hug_parens_with_braces_and_square_brackets = auto()
allow_empty_first_line_before_new_block_or_comment = auto()
allow_empty_first_line_in_block = auto()
single_line_format_skip_with_multiple_comments = auto()
long_case_block_line_splitting = auto()
allow_form_feeds = auto()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ def baz():
if x:
a = 123

def quux():

new_line = here


class Cls:

def method(self):

pass

# output

def foo():
Expand Down Expand Up @@ -104,3 +115,14 @@ def baz():
# OK
if x:
a = 123


def quux():

new_line = here


class Cls:
def method(self):

pass
1 change: 1 addition & 0 deletions tests/data/cases/preview_form_feeds.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ def foo():

# form feeds are prohibited inside blocks, or on a line with nonwhitespace
def bar(a=1, b: bool = False):

pass


Expand Down