Skip to content

Commit

Permalink
Gate behind preview
Browse files Browse the repository at this point in the history
  • Loading branch information
konstin committed Jul 12, 2024
1 parent b3d6adb commit 438001a
Show file tree
Hide file tree
Showing 7 changed files with 371 additions and 18 deletions.
8 changes: 7 additions & 1 deletion crates/ruff_python_formatter/src/statement/suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,13 @@ impl FormatSuite {
let existing_newlines =
lines_after_ignoring_end_of_line_trivia(end_of_def_or_class, f.context().source());
if existing_newlines < 2 {
empty_line().fmt(f)?;
if f.context().is_preview() {
empty_line().fmt(f)?;
} else {
if last_def_or_class.is_stmt_class_def() && f.options().source_type().is_stub() {
empty_line().fmt(f)?;
}
}
}
Ok(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,22 @@ def bar():
# leading 3 that already has an empty line
@deco3
# leading 4
@@ -106,7 +105,6 @@
# Another leading comment
def another_inline():
pass
-
else:
# More leading comments
def inline_after_else():
@@ -121,7 +119,6 @@
# Another leading comment
def another_top_level_quote_inline_inline():
pass
-
else:
# More leading comments
def top_level_quote_inline_after_else():
```

## Ruff Output
Expand Down Expand Up @@ -271,7 +287,6 @@ def main():
# Another leading comment
def another_inline():
pass
else:
# More leading comments
def inline_after_else():
Expand All @@ -286,7 +301,6 @@ if a:
# Another leading comment
def another_top_level_quote_inline_inline():
pass
else:
# More leading comments
def top_level_quote_inline_after_else():
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/cases/function2.py
---
## Input

```python
def f(
a,
**kwargs,
) -> A:
with cache_dir():
if something:
result = (
CliRunner().invoke(black.main, [str(src1), str(src2), "--diff", "--check"])
)
limited.append(-limited.pop()) # negate top
return A(
very_long_argument_name1=very_long_value_for_the_argument,
very_long_argument_name2=-very.long.value.for_the_argument,
**kwargs,
)
def g():
"Docstring."
def inner():
pass
print("Inner defs should breathe a little.")
def h():
def inner():
pass
print("Inner defs should breathe a little.")
if os.name == "posix":
import termios
def i_should_be_followed_by_only_one_newline():
pass
elif os.name == "nt":
try:
import msvcrt
def i_should_be_followed_by_only_one_newline():
pass
except ImportError:
def i_should_be_followed_by_only_one_newline():
pass
elif False:
class IHopeYouAreHavingALovelyDay:
def __call__(self):
print("i_should_be_followed_by_only_one_newline")
else:
def foo():
pass
with hmm_but_this_should_get_two_preceding_newlines():
pass
```

## Black Differences

```diff
--- Black
+++ Ruff
@@ -36,7 +36,6 @@
def i_should_be_followed_by_only_one_newline():
pass
-
elif os.name == "nt":
try:
import msvcrt
```

## Ruff Output

```python
def f(
a,
**kwargs,
) -> A:
with cache_dir():
if something:
result = CliRunner().invoke(
black.main, [str(src1), str(src2), "--diff", "--check"]
)
limited.append(-limited.pop()) # negate top
return A(
very_long_argument_name1=very_long_value_for_the_argument,
very_long_argument_name2=-very.long.value.for_the_argument,
**kwargs,
)
def g():
"Docstring."
def inner():
pass
print("Inner defs should breathe a little.")
def h():
def inner():
pass
print("Inner defs should breathe a little.")
if os.name == "posix":
import termios
def i_should_be_followed_by_only_one_newline():
pass
elif os.name == "nt":
try:
import msvcrt
def i_should_be_followed_by_only_one_newline():
pass
except ImportError:
def i_should_be_followed_by_only_one_newline():
pass
elif False:
class IHopeYouAreHavingALovelyDay:
def __call__(self):
print("i_should_be_followed_by_only_one_newline")
else:
def foo():
pass
with hmm_but_this_should_get_two_preceding_newlines():
pass
```

## Black Output

```python
def f(
a,
**kwargs,
) -> A:
with cache_dir():
if something:
result = CliRunner().invoke(
black.main, [str(src1), str(src2), "--diff", "--check"]
)
limited.append(-limited.pop()) # negate top
return A(
very_long_argument_name1=very_long_value_for_the_argument,
very_long_argument_name2=-very.long.value.for_the_argument,
**kwargs,
)
def g():
"Docstring."
def inner():
pass
print("Inner defs should breathe a little.")
def h():
def inner():
pass
print("Inner defs should breathe a little.")
if os.name == "posix":
import termios
def i_should_be_followed_by_only_one_newline():
pass
elif os.name == "nt":
try:
import msvcrt
def i_should_be_followed_by_only_one_newline():
pass
except ImportError:
def i_should_be_followed_by_only_one_newline():
pass
elif False:
class IHopeYouAreHavingALovelyDay:
def __call__(self):
print("i_should_be_followed_by_only_one_newline")
else:
def foo():
pass
with hmm_but_this_should_get_two_preceding_newlines():
pass
```
70 changes: 62 additions & 8 deletions crates/ruff_python_formatter/tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,6 @@ if True:
def a():
return 1
else:
pass
Expand All @@ -625,7 +624,6 @@ match True:
def a():
return 1
case 1:
def a():
Expand All @@ -636,7 +634,6 @@ try:
def a():
return 1
except RuntimeError:
def a():
Expand All @@ -647,7 +644,6 @@ try:
def a():
return 1
finally:
def a():
Expand All @@ -658,22 +654,18 @@ try:
def a():
return 1
except RuntimeError:
def a():
return 1
except ZeroDivisionError:
def a():
return 1
else:
def a():
return 1
finally:
def a():
Expand Down Expand Up @@ -724,3 +716,65 @@ def overload4():
def overload4(a: int): ...
```


## Preview changes
```diff
--- Stable
+++ Preview
@@ -277,6 +277,7 @@
def a():
return 1
+
else:
pass
@@ -285,6 +286,7 @@
def a():
return 1
+
case 1:
def a():
@@ -295,6 +297,7 @@
def a():
return 1
+
except RuntimeError:
def a():
@@ -305,6 +308,7 @@
def a():
return 1
+
finally:
def a():
@@ -315,18 +319,22 @@
def a():
return 1
+
except RuntimeError:
def a():
return 1
+
except ZeroDivisionError:
def a():
return 1
+
else:
def a():
return 1
+
finally:
def a():
```
Loading

0 comments on commit 438001a

Please sign in to comment.