Skip to content

Commit

Permalink
Expand if-exp
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Sep 3, 2023
1 parent 3c74868 commit dcab12b
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,60 @@ def something():
if flat
else ValuesListIterable)
)


# Expanding leading and trailing comments.
before = (
# comment
0
if self.thing is None
else before - after
)

before = (
0
if self.thing is None
else before - after
# comment
)

before = ( # comment
0
if self.thing is None
else before - after
)

before = (
# comment
(0
if self.thing is None
else before - after
))

before = [
# comment
0
if self.thing is None
else before - after
]

before = (
# comment
0
if self.thing is None
else before - after
)

before = [
# comment
0
if self.thing is None
else before - after,
2
]

before = (
0
if self.thing is None
else before - after # comment
)
19 changes: 16 additions & 3 deletions crates/ruff_python_formatter/src/expression/expr_if_exp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,22 @@ impl FormatNodeRule<ExprIfExp> for FormatExprIfExp {
let comments = f.context().comments().clone();

let inner = format_with(|f: &mut PyFormatter| {
// We place `if test` and `else orelse` on a single line, so the `test` and `orelse` leading
// comments go on the line before the `if` or `else` instead of directly ahead `test` or
// `orelse`
// If the expression has any leading or trailing comments, always expand it, as in:
// ```
// (
// # comment
// 0
// if self.thing is None
// else before - after
// )
// ```
if comments.has_leading(item) || comments.has_trailing(item) {
expand_parent().fmt(f)?;
}

// We place `if test` and `else orelse` on a single line, so the `test` and `orelse`
// leading comments go on the line before the `if` or `else` instead of directly ahead
// `test` or `orelse`.
write!(
f,
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,63 @@ def something():
if flat
else ValuesListIterable)
)
# Expanding leading and trailing comments.
before = (
# comment
0
if self.thing is None
else before - after
)
before = (
0
if self.thing is None
else before - after
# comment
)
before = ( # comment
0
if self.thing is None
else before - after
)
before = (
# comment
(0
if self.thing is None
else before - after
))
before = [
# comment
0
if self.thing is None
else before - after
]
before = (
# comment
0
if self.thing is None
else before - after
)
before = [
# comment
0
if self.thing is None
else before - after,
2
]
before = (
0
if self.thing is None
else before - after # comment
)
```

## Output
Expand Down Expand Up @@ -240,6 +297,63 @@ def something():
else ValuesListIterable
)
)
# Expanding leading and trailing comments.
before = (
# comment
0
if self.thing is None
else before - after
)
before = (
0
if self.thing is None
else before - after
# comment
)
before = ( # comment
0
if self.thing is None
else before - after
)
before = (
# comment
0
if self.thing is None
else before - after
)
before = [
# comment
0
if self.thing is None
else before - after
]
before = (
# comment
0
if self.thing is None
else before - after
)
before = [
# comment
0
if self.thing is None
else before - after,
2,
]
before = (
0
if self.thing is None
else before - after # comment
)
```


Expand Down

0 comments on commit dcab12b

Please sign in to comment.