-
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
Format binary expressions #4862
Conversation
Current dependencies on/for this PR:
This comment was auto-generated by Graphite. |
) | ||
|
||
|
||
# Black breaks the right side first for the following expressions: |
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.
Not all of these are handled correctly yet because the format implementation of the right hand side is missing
write!( | ||
f, | ||
[ | ||
left.format(), |
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 implementation heavily relies on recursion. We may want to explore to manually unroll left-balanced binary expressions because this could stack-overflow for very deep binary expressions (but so could any of our analyzer infrastructure).
} | ||
} | ||
|
||
fn is_expression_parenthesized(expr: &Expr, contents: &str) -> bool { | ||
// Search backwards to avoid ambiguity with `(a, )` and because it's faster | ||
matches!( |
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 is unfortunate. We now have to inspect the source code for every expression that we format but I haven't been able to come up with a better approach. Let's see what the benchmark says to this.
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 are the alternatives? In the initial prototype, I think I handled these similarly to comments -- so, for every parenthesis, I associated it with the child it contains.
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 considered extracting the tokenized ranges from the token stream and either:
- Store them in a vec and perform a binary search: I don't think this is faster because it trades the few character iterations with a binary search for every expression (except maybe if there are None).
- Storing the
parenthesized
in aTrivia
struct and look it up by node. Again, I don't think this would be faster because we trade the iterating over a few characters vs performing a hash map lookup (while fast, iterating over 1- characters is super fast too)
# Has many lines. Many, many lines. | ||
# Many, many, many lines. | ||
-"""Module docstring. | ||
+( |
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.
We'll need to handle docstrings manually in the Suite
formatting but that's not part of this PR
f66044f
to
8070eef
Compare
0b45066
to
364f656
Compare
PR Check ResultsEcosystem✅ ecosystem check detected no changes. BenchmarkLinux
Windows
|
364f656
to
5ee366e
Compare
use rustpython_parser::ast::ExprList; | ||
|
||
#[derive(Default)] | ||
pub struct FormatExprList; | ||
|
||
impl FormatNodeRule<ExprList> for FormatExprList { | ||
fn fmt_fields(&self, item: &ExprList, f: &mut PyFormatter) -> FormatResult<()> { | ||
write!(f, [verbatim_text(item.range)]) | ||
let ExprList { |
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 is not a full implementation, but it is good enough to test the right side breaking of binary expressions
aaaaaaaaaaaaaa | ||
+ {a for x in bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb} | ||
) | ||
# Wraps it in parentheses if it needs to break both left and right |
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 new lines before the comment get removed. I'll look into this tomorrow as a separate PR
5ee366e
to
5899fa9
Compare
28014f5
to
5dde064
Compare
5dde064
to
5645338
Compare
Pull request was closed
* Format Binary Expressions * Extract NeedsParentheses trait
Summary
This PR implements basic formatting for binary expressions. It does not yet handle the special case where black breaks the right side first for call expressions, tuples, lists, ...
This PR also adds the infrastructure to preserve parentheses around expressions (depending on the context)
Test Plan
cargo test