Skip to content

Commit

Permalink
Avoid unstable formatting in ellipsis-only body with trailing comment
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Dec 3, 2023
1 parent b358cbf commit f0cfb29
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ with True:
with True:
... # comment

with True:
...
# comment

match x:
case 1:
...
Expand Down Expand Up @@ -99,4 +103,4 @@ try:
except:
... # comment
finally:
... # comment
... # comment
4 changes: 3 additions & 1 deletion crates/ruff_python_formatter/src/statement/suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,9 @@ pub(crate) fn contains_only_an_ellipsis(body: &[Stmt], comments: &Comments) -> b
let [node] = body else {
return false;
};
value.is_ellipsis_literal_expr() && !comments.has_leading(node)
value.is_ellipsis_literal_expr()
&& !comments.has_leading(node)
&& !comments.has_trailing_own_line(node)
}
_ => false,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ with True:
with True:
... # comment
with True:
...
# comment
match x:
case 1:
...
Expand Down Expand Up @@ -105,7 +109,8 @@ try:
except:
... # comment
finally:
... # comment```
... # comment
```

## Output
```python
Expand Down Expand Up @@ -163,6 +168,10 @@ with True:
with True: ... # comment
with True:
...
# comment
match x:
case 1: ...
case 2:
Expand Down

0 comments on commit f0cfb29

Please sign in to comment.