Skip to content

Commit

Permalink
Respect own-line leading comments before parenthesized nodes (#6820)
Browse files Browse the repository at this point in the history
## Summary

This PR ensures that if an expression has an own-line leading comment
_before_ its open parentheses, we render it as such.

For example, given:

```python
[ # foo
    # bar
    ( # baz
        1
    )
]
```

On `main`, we format as:

```python
[  # foo
    (
        # bar
        # baz
        1
    )
]
```

As of this PR, we format as:

```python
[  # foo
    # bar
    (  # baz
        1
    )
]
```

## Test Plan

`cargo test`
  • Loading branch information
charliermarsh authored Aug 25, 2023
1 parent 59e7089 commit 813d7da
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,11 @@
second,
third
] # outer comment

[ # inner comment
# own-line comment
( # end-of-line comment
# own-line comment
first,
),
] # outer comment
31 changes: 18 additions & 13 deletions crates/ruff_python_formatter/src/expression/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use itertools::Itertools;
use std::cmp::Ordering;

use ruff_formatter::{
Expand All @@ -9,6 +10,7 @@ use ruff_python_ast::visitor::preorder::{walk_expr, PreorderVisitor};
use ruff_python_ast::{Expr, ExpressionRef, Operator};

use crate::builders::parenthesize_if_expands;
use crate::comments::leading_comments;
use crate::context::{NodeLevel, WithNodeLevel};
use crate::expression::parentheses::{
is_expression_parenthesized, optional_parentheses, parenthesized, NeedsParentheses,
Expand Down Expand Up @@ -107,8 +109,6 @@ impl FormatRule<Expr, PyFormatContext<'_>> for FormatExpr {
};

if parenthesize {
let comments = f.context().comments().clone();

// Any comments on the open parenthesis of a `node`.
//
// For example, `# comment` in:
Expand All @@ -117,18 +117,23 @@ impl FormatRule<Expr, PyFormatContext<'_>> for FormatExpr {
// foo.bar
// )
// ```
let open_parenthesis_comment = comments
.leading(expression)
.first()
.filter(|comment| comment.line_position().is_end_of_line());

parenthesized("(", &format_expr, ")")
.with_dangling_comments(
open_parenthesis_comment
.map(std::slice::from_ref)
.unwrap_or_default(),
let comments = f.context().comments().clone();
let leading = comments.leading(expression);
if let Some((index, open_parenthesis_comment)) = leading
.iter()
.find_position(|comment| comment.line_position().is_end_of_line())
{
write!(
f,
[
leading_comments(&leading[..index]),
parenthesized("(", &format_expr, ")")
.with_dangling_comments(std::slice::from_ref(open_parenthesis_comment))
]
)
.fmt(f)
} else {
parenthesized("(", &format_expr, ")").fmt(f)
}
} else {
let level = match f.context().node_level() {
NodeLevel::TopLevel | NodeLevel::CompoundStatement => NodeLevel::Expression(None),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,9 +372,8 @@ func(
)
func(
(
# outer comment
# inner comment
# outer comment
( # inner comment
[]
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ c1 = [ # trailing open bracket
second,
third
] # outer comment
[ # inner comment
# own-line comment
( # end-of-line comment
# own-line comment
first,
),
] # outer comment
```

## Output
Expand Down Expand Up @@ -110,6 +118,14 @@ c1 = [ # trailing open bracket
second,
third,
] # outer comment
[ # inner comment
# own-line comment
( # end-of-line comment
# own-line comment
first,
),
] # outer comment
```


Expand Down

0 comments on commit 813d7da

Please sign in to comment.