Avoid hard line break after dangling open-parenthesis comments (#6380)

## Summary

Given:

```python
[  # comment
    first,
    second,
    third
]  # another comment
```

We were adding a hard line break as part of the formatting of `#
comment`, which led to the following formatting:

```python
[first, second, third]  # comment
  # another comment
```

Closes https://github.com/astral-sh/ruff/issues/6367.
This commit is contained in:
Charlie Marsh 2023-08-07 10:15:32 -04:00 committed by GitHub
parent 63692b3798
commit b763973357
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 82 additions and 18 deletions

View file

@ -4,7 +4,7 @@ use ruff_python_ast::node::AnyNodeRef;
use ruff_python_ast::Ranged;
use ruff_python_trivia::{first_non_trivia_token, SimpleToken, SimpleTokenKind, SimpleTokenizer};
use crate::comments::{dangling_comments, SourceComment};
use crate::comments::{dangling_open_parenthesis_comments, SourceComment};
use crate::context::{NodeLevel, WithNodeLevel};
use crate::prelude::*;
@ -117,7 +117,7 @@ where
}
/// Formats `content` enclosed by the `left` and `right` parentheses, along with any dangling
/// comments that on the parentheses themselves.
/// comments on the opening parenthesis itself.
pub(crate) fn parenthesized_with_dangling_comments<'content, 'ast, Content>(
left: &'static str,
comments: &'content [SourceComment],
@ -155,8 +155,8 @@ impl<'ast> Format<PyFormatContext<'ast>> for FormatParenthesized<'_, 'ast> {
} else {
group(&format_args![
text(self.left),
&line_suffix(&dangling_comments(self.comments)),
&group(&soft_block_indent(&Arguments::from(&self.content))),
&dangling_open_parenthesis_comments(self.comments),
&soft_block_indent(&Arguments::from(&self.content)),
text(self.right)
])
.fmt(f)
@ -319,10 +319,11 @@ impl<'ast> Format<PyFormatContext<'ast>> for FormatInParenthesesOnlyGroup<'_, 'a
#[cfg(test)]
mod tests {
use crate::expression::parentheses::is_expression_parenthesized;
use ruff_python_ast::node::AnyNodeRef;
use ruff_python_parser::parse_expression;
use crate::expression::parentheses::is_expression_parenthesized;
#[test]
fn test_has_parentheses() {
let expression = r#"(b().c("")).d()"#;