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 9b61f73 commit 5fef49b
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 5 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
40 changes: 40 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,46 @@ impl Format<PyFormatContext<'_>> for FormatDanglingComments<'_> {
}
}

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

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

impl Format<PyFormatContext<'_>> for FormatParentheticalComments<'_> {
fn fmt(&self, f: &mut Formatter<PyFormatContext>) -> FormatResult<()> {
let mut first = true;

for comment in self
.comments
.iter()
.filter(|comment| comment.is_unformatted())
{
if first {
write!(f, [space(), space()])?;
}

write!(f, [format_comment(comment)])?;

comment.mark_formatted();

first = false;
}

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
2 changes: 1 addition & 1 deletion crates/ruff_python_formatter/src/comments/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ 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,
leading_node_comments, parenthetical_comments, trailing_comments, trailing_node_comments,
};
use ruff_formatter::{SourceCode, SourceCodeSlice};
use ruff_python_ast::node::AnyNodeRef;
Expand Down
9 changes: 5 additions & 4 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::{parenthetical_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,7 +155,7 @@ impl<'ast> Format<PyFormatContext<'ast>> for FormatParenthesized<'_, 'ast> {
} else {
group(&format_args![
text(self.left),
&line_suffix(&dangling_comments(self.comments)),
&line_suffix(&parenthetical_comments(self.comments)),
&group(&soft_block_indent(&Arguments::from(&self.content))),
text(self.right)
])
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 @@ -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 5fef49b

Please sign in to comment.