Skip to content

Commit

Permalink
Avoid hard line break after dangling inner-parenthesis comments
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Aug 7, 2023
1 parent 63692b3 commit b312f42
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,9 @@
[ # end-of-line comment
1
]

[ # inner comment
first,
second,
third
] # outer comment
46 changes: 46 additions & 0 deletions crates/ruff_python_formatter/src/comments/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,52 @@ impl Format<PyFormatContext<'_>> for FormatDanglingComments<'_> {
}
}

/// Formats the dangling comments within a parenthesized expression, for example:
/// ```python
/// [ # comment
/// 1,
/// 2,
/// 3,
/// ]
/// ```
pub(crate) fn dangling_open_parenthesis_comments(
comments: &[SourceComment],
) -> FormatDanglingOpenParenthesisComments {
FormatDanglingOpenParenthesisComments { comments }
}

pub(crate) struct FormatDanglingOpenParenthesisComments<'a> {
comments: &'a [SourceComment],
}

impl Format<PyFormatContext<'_>> for FormatDanglingOpenParenthesisComments<'_> {
fn fmt(&self, f: &mut Formatter<PyFormatContext>) -> FormatResult<()> {
for comment in self
.comments
.iter()
.filter(|comment| comment.is_unformatted())
{
debug_assert!(
comment.line_position().is_end_of_line(),
"Expected dangling comment to be at the end of the line"
);

write!(
f,
[line_suffix(&format_args![
space(),
space(),
format_comment(comment)
])]
)?;

comment.mark_formatted();
}

Ok(())
}
}

/// Formats the content of the passed comment.
///
/// * Adds a whitespace between `#` and the comment text except if the first character is a `#`, `:`, `'`, or `!`
Expand Down
5 changes: 3 additions & 2 deletions crates/ruff_python_formatter/src/comments/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ use std::rc::Rc;
use ruff_python_ast::{Mod, Ranged};

pub(crate) use format::{
dangling_comments, dangling_node_comments, leading_alternate_branch_comments, leading_comments,
leading_node_comments, trailing_comments, trailing_node_comments,
dangling_comments, dangling_node_comments, dangling_open_parenthesis_comments,
leading_alternate_branch_comments, leading_comments, leading_node_comments, trailing_comments,
trailing_node_comments,
};
use ruff_formatter::{SourceCode, SourceCodeSlice};
use ruff_python_ast::node::AnyNodeRef;
Expand Down
11 changes: 6 additions & 5 deletions crates/ruff_python_formatter/src/expression/parentheses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;

Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()"#;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,19 @@ print( "111" ) # type: ignore
```diff
--- Black
+++ Ruff
@@ -1,10 +1,8 @@
@@ -1,12 +1,6 @@
# This is a regression test. Issue #3737
-a = ( # type: ignore
+a = int( # type: ignore # type: ignore
int( # type: ignore
- int( # type: ignore
- int( # type: ignore
- int(6) # type: ignore
- )
+ int(6) # type: ignore
)
)
- )
-)
+a = int(int(int(6))) # type: ignore # type: ignore # type: ignore # type: ignore
b = int(6)
```

Expand All @@ -53,11 +54,7 @@ print( "111" ) # type: ignore
```py
# This is a regression test. Issue #3737
a = int( # type: ignore # type: ignore
int( # type: ignore
int(6) # type: ignore
)
)
a = int(int(int(6))) # type: ignore # type: ignore # type: ignore # type: ignore
b = int(6)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ c1 = [ # trailing open bracket
[ # end-of-line comment
1
]
[ # inner comment
first,
second,
third
] # outer comment
```

## Output
Expand Down Expand Up @@ -94,6 +100,8 @@ c1 = [ # trailing open bracket
]
[1] # end-of-line comment
[first, second, third] # inner comment # outer comment
```


Expand Down

0 comments on commit b312f42

Please sign in to comment.