-
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
Comments outside expression parentheses #7873
Conversation
2a1fa16
to
55b2da1
Compare
An alternative we could explore is storing the parentheses range on |
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.
How does this work for expressions where we use maybe_parenthesize
with a layout that doesn't preserve parentheses and a trailing (non-own line) comment?
Expr::BoolOp(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f), | ||
Expr::NamedExpr(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f), | ||
Expr::BinOp(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f), | ||
Expr::UnaryOp(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f), | ||
Expr::Lambda(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f), | ||
Expr::IfExp(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f), | ||
Expr::Dict(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f), | ||
Expr::Set(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f), | ||
Expr::ListComp(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f), | ||
Expr::SetComp(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f), | ||
Expr::DictComp(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f), | ||
Expr::GeneratorExp(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f), | ||
Expr::Await(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f), | ||
Expr::Yield(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f), | ||
Expr::YieldFrom(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f), | ||
Expr::Compare(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f), | ||
Expr::Call(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f), | ||
Expr::FormattedValue(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f), | ||
Expr::FString(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f), | ||
Expr::Constant(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f), | ||
Expr::Attribute(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f), | ||
Expr::Subscript(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f), | ||
Expr::Starred(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f), | ||
Expr::Name(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f), | ||
Expr::List(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f), | ||
Expr::Tuple(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f), | ||
Expr::Slice(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f), | ||
Expr::IpyEscapeCommand(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f), |
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.
Nit: The main benefit of FormatRuleWith
is that it wraps the object to format with how it gets formatted. I would probably lean towards calling the relevant format rules directly (fewer abstractions, easier to understand)
Expr::Call => FormatExprCall::default().fmt_fields(expr, f)
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 prefer this one for avoiding repeating the -> Format association.
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.
That's fair and up to you. It just requires a few more brain cycles to resolve FormatNodeRule::fmt_fields(expr.format().rule(), expr, f)
to FormatExprFString::default().fmt_fields(expr, f)
9addbb3
to
504c7af
Compare
) -> FormatResult<()> { | ||
// First part: Split the comments | ||
|
||
// TODO(konstin): This is copied from `parenthesized_range`, except that we don't have the |
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'm still not sure what to do about this, otherwise this PR is ready
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 don't mind the duplication. We could implement a helper that returns an Iterator over the parentheses (opening and closing) and reuse it in parenthesized_range
and here.
Could you update the test plan with the similarity index table to see if there are any changes. |
) -> FormatResult<()> { | ||
// First part: Split the comments | ||
|
||
// TODO(konstin): This is copied from `parenthesized_range`, except that we don't have the |
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 don't mind the duplication. We could implement a helper that returns an Iterator over the parentheses (opening and closing) and reuse it in parenthesized_range
and here.
) -> FormatResult<()> { | ||
// First part: Split the comments | ||
|
||
// TODO(konstin): This is copied from `parenthesized_range`, except that we don't have the |
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.
parenthesized_range
has a special case for call expressions like call(a)
to avoid counting the (
and )
as the parentheses of this node. Is it safe to not special-tread call expressions here and if so why?
(
call # sneaky
( # comments
a
) # more
)
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.
The problem is that the expression would be a
but a doesn't know its parent
// * the closing parenthesis | ||
// * outer trailing comments | ||
|
||
let fmt_fields = format_with(|f| match expression { |
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.
Nit: It could make sense to extract this into it's own struct FormatExprFields
to not make this function longer than absolutely necessary (It kind of breaks the reading flow, there's the comment above explaining what is happening, but it is then followed by this somewhat unrelated boilerplate)
with (a # trailing same line comment | ||
# trailing own line comment | ||
) as b: ... | ||
|
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.
What's the reason for removing this test case?
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.
It's a duplicate, sorry should have added a PR comment
a40819c
to
d61caea
Compare
Co-authored-by: Micha Reiser <micha@reiser.io>
Co-authored-by: Micha Reiser <micha@reiser.io>
Co-authored-by: Micha Reiser <micha@reiser.io>
d61caea
to
e4ef5b0
Compare
Added the similarity index table to the description, this had more impact than i had expected |
PR Check ResultsEcosystem✅ ecosystem check detected no changes. |
Summary
Fixes #7448
Fixes #7892
I've removed automatic dangling comment formatting, we're doing manual dangling comment formatting everywhere anyway (the assert-all-comments-formatted ensures this) and dangling comments would break the formatting there.
main:
PR:
Test Plan
New test file.