Avoid hard line break after dangling open-parenthesis comments#6380
Avoid hard line break after dangling open-parenthesis comments#6380charliermarsh merged 1 commit intomainfrom
Conversation
|
|
||
| Ok(()) | ||
| } | ||
| } |
There was a problem hiding this comment.
A dedicated formatter for these kinds of open-parenthetical methods, rather than using the generic dangling_comments formatter which always hard-breaks after comments.
44a1b29 to
125504c
Compare
125504c to
5fef49b
Compare
PR Check ResultsBenchmarkLinuxWindows |
5fef49b to
7332d43
Compare
| @@ -155,7 +155,7 @@ impl<'ast> Format<PyFormatContext<'ast>> for FormatParenthesized<'_, 'ast> { | |||
| } else { | |||
| group(&format_args![ | |||
There was a problem hiding this comment.
Do we need this outer group anymore? There's nothing anymore that expands the parent (dangling comments always rendered a hard line break, forcing the group to expand). Or should parenthetical_comments render an expand_parent?
| ) | ||
| - ) | ||
| -) | ||
| +a = int(int(int(6))) # type: ignore # type: ignore # type: ignore # type: ignore |
There was a problem hiding this comment.
IMO: The old formatting expressed the intent of the user better.
We, so far, have taken the stand that trailing comments should remain as closely as possible to what they originally commented (see internal notion). Meaning they should, by default, expand their parent. I think this should also help that ignore comments move to far away, changing what they suppress.
There was a problem hiding this comment.
I will look into how we can preserve this while still fixing the bug in the PR summary, but FWIW, Black also does this formatting, unless the comments are type ignores: https://black.vercel.app/?version=main&state=_Td6WFoAAATm1rRGAgAhARYAAAB0L-Wj4AFIAH1dAD2IimZxl1N_WlOfrjryFgvD4ScVsKPztqdHDGJUg5knO0JCdpUeAEufiQmt7zAZacPYBA_60Y2o5_0gUa0qrRrUebiwy-1h9dFYjpJKI3bTmqTD_atbbMhmkqzeioikfpq_PQL5C6EgvdfH6suhrNVSpATyrIAX7v50BhYnAAAAAGQFt7kMk4ZRAAGZAckCAAAEz53MscRn-wIAAAAABFla.
There was a problem hiding this comment.
(I think this should be investigated separately from this PR.)
There was a problem hiding this comment.
This change seems to do what's described but it is a deviation from Black:
--- a/crates/ruff_python_formatter/src/comments/format.rs
+++ b/crates/ruff_python_formatter/src/comments/format.rs
@@ -259,11 +259,10 @@ impl Format<PyFormatContext<'_>> for FormatDanglingOpenParenthesisComments<'_> {
write!(
f,
- [line_suffix(&format_args![
- space(),
- space(),
- format_comment(comment)
- ])]
+ [
+ line_suffix(&format_args![space(), space(), format_comment(comment)]),
+ expand_parent()
+ ]
)?;There was a problem hiding this comment.
Will put up a separate PR.
There was a problem hiding this comment.
One feature that Ruff's architecture enables is that we can move comments. This is much harder in Black. I think we should use it and be opinionated about where comments should be placed and how they get formatted.
7332d43 to
b312f42
Compare
b312f42 to
9b2557c
Compare
…l-sh#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 astral-sh#6367.
Summary
Given:
We were adding a hard line break as part of the formatting of
# comment, which led to the following formatting:Closes #6367.