Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove newline after code block open #3035

Merged
merged 27 commits into from Jun 11, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
df60609
black will remove newlines between `def` statement and its function c…
saroad2 Apr 25, 2022
13f2aaf
Test the new ability
saroad2 Apr 25, 2022
8d6c373
Add change to CHANGES.md
saroad2 Apr 25, 2022
b991571
Added a few test cases
saroad2 Apr 25, 2022
549a094
Added more tests
saroad2 Apr 25, 2022
c30a951
Update CHANGES.md according to suggestion
saroad2 Apr 25, 2022
17516d7
remove trailing newline in `def` statement is now a preview ability
saroad2 Apr 25, 2022
bb97364
Add myself to AUTHORS.md since it's my forth PR in *Black*
saroad2 Apr 25, 2022
466767d
Merge branch 'main' into remove_newline_after_def
saroad2 Apr 29, 2022
2922f44
Merge branch 'master' into remove_newline_after_def
saroad2 May 8, 2022
10a299c
Merge branch 'main' into remove_newline_after_def
saroad2 May 9, 2022
a9eaf0f
Merge branch 'main' into remove_newline_after_def
saroad2 May 18, 2022
f2a5b65
Merge branch 'main' into remove_newline_after_def
saroad2 May 18, 2022
700c51d
Move new test cases to preview directory
saroad2 May 18, 2022
fd1da5c
*Black* now removes newlines after EVERY open block statement
saroad2 May 18, 2022
c845c0c
Add unit tests for removing newlines
saroad2 May 18, 2022
f8bbdbb
Add new feature to future_style.md
saroad2 May 19, 2022
6fe0a98
Fix change in CHANGES.md
saroad2 May 19, 2022
4fd907a
Combine all new test cases to one case.
saroad2 May 19, 2022
b2a015c
trim down long description in the future_style.md document
saroad2 May 19, 2022
b6e720c
Merge branch 'main' into remove_newline_after_def
saroad2 May 25, 2022
e496d90
Rename `remove_def_trailing_newline` to `remove_block_trailing_newline`
saroad2 May 25, 2022
860941b
Update docs/the_black_code_style/future_style.md
saroad2 May 26, 2022
f44edba
Fix linting issue
saroad2 May 26, 2022
24b48ca
Add new preview feature test for `match`-`case` blocks
saroad2 May 26, 2022
92ce12c
Fix failing test
saroad2 May 26, 2022
ee5888b
Add `case` and `match` to code blocks list
saroad2 May 26, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -12,6 +12,7 @@

- Fix unstable formatting involving `# fmt: skip` comments without internal spaces
(#2970)
- trailing newlines are removed right after `def` statement (#3035)
saroad2 marked this conversation as resolved.
Show resolved Hide resolved

### Preview style

Expand Down
6 changes: 6 additions & 0 deletions src/black/lines.py
Expand Up @@ -513,6 +513,12 @@ def _maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
):
return before, 1

if (
self.previous_line
and self.previous_line.is_def
and depth == self.previous_line.depth + 1
):
return 0, 0
return before, 0

def _maybe_empty_lines_for_class_or_def(
Expand Down
8 changes: 8 additions & 0 deletions tests/data/newline_after_def1.py
@@ -0,0 +1,8 @@
def foo():

print("The newline above me should be deleted!")

# output

def foo():
print("The newline above me should be deleted!")
10 changes: 10 additions & 0 deletions tests/data/newline_after_def2.py
@@ -0,0 +1,10 @@
def foo():



print("All the newlines above me should be deleted!")

# output

def foo():
print("All the newlines above me should be deleted!")
12 changes: 12 additions & 0 deletions tests/data/newline_after_def3.py
@@ -0,0 +1,12 @@
def f():

print("No newline above me!")

print("There is a newline above me, and that's OK!")

# output

def f():
print("No newline above me!")

print("There is a newline above me, and that's OK!")
12 changes: 12 additions & 0 deletions tests/data/newline_after_def4.py
@@ -0,0 +1,12 @@
def foo():

# There is a comment here

print("The newline above me should not be deleted!")

# output

def foo():
# There is a comment here

print("The newline above me should not be deleted!")
10 changes: 10 additions & 0 deletions tests/data/newline_after_def5.py
@@ -0,0 +1,10 @@
class Foo:
def bar(self):

print("The newline above me should be deleted!")

# output

class Foo:
def bar(self):
print("The newline above me should be deleted!")
1 change: 0 additions & 1 deletion tests/test_black.py
Expand Up @@ -1451,7 +1451,6 @@ def test_newline_comment_interaction(self) -> None:
black.assert_stable(source, output, mode=DEFAULT_MODE)

def test_bpo_2142_workaround(self) -> None:

Copy link
Collaborator

Choose a reason for hiding this comment

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

Haha, I like it!

# https://bugs.python.org/issue2142

source, _ = read_data("missing_final_newline.py")
Expand Down
5 changes: 5 additions & 0 deletions tests/test_format.py
Expand Up @@ -50,6 +50,11 @@
"function2",
"function_trailing_comma",
"import_spacing",
"newline_after_def1",
"newline_after_def2",
"newline_after_def3",
"newline_after_def4",
"newline_after_def5",
"power_op_spacing",
"remove_parens",
"slices",
Expand Down