-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Avoid hard line break after dangling open-parenthesis comments #6380
Conversation
|
||
Ok(()) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 ResultsBenchmarkLinux
Windows
|
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(I think this should be investigated separately from this PR.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will put up a separate PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Follow-up here: #6389
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.