-
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
Handle bracketed comments on sequence patterns #6801
Conversation
4178cca
to
b1f7091
Compare
PR Check ResultsBenchmarkLinux
Windows
|
f221e7a
to
f457285
Compare
b1f7091
to
5666806
Compare
crates/ruff_python_formatter/src/pattern/pattern_match_sequence.rs
Outdated
Show resolved
Hide resolved
let close_parentheses_count = | ||
SimpleTokenizer::new(source, TextRange::new(elt.end(), elt.end())) | ||
.skip_trivia() | ||
.take_while(|token| token.kind() != SimpleTokenKind::Comma) | ||
.filter(|token| token.kind() == SimpleTokenKind::RParen) | ||
.count(); |
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 not able to follow this. Can you add an example/test cases for the 2 cases here?
if source[pattern.range()].starts_with('[') { | ||
SequenceType::List | ||
} else if source[pattern.range()].starts_with('(') { | ||
let Some(elt) = pattern.patterns.first() else { |
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 seems to be the same as is_tuple_parenthesized
. It may make sense to make the code reusable (as it is somewhat tricky). It would also be helpful to add an example of why the complex open > close
paren path is necessary. I already forgot and had to look it up on the PR 95f7882
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.
Haha ok. I can try. It felt difficult to DRY it up without making the types much more permissive.
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.
Yeah, don't try too hard.
crates/ruff_python_formatter/src/pattern/pattern_match_sequence.rs
Outdated
Show resolved
Hide resolved
SequenceType::List => empty_parenthesized("[", dangling, "]").fmt(f), | ||
SequenceType::TupleNoParens => { | ||
unreachable!("If empty, it should be either tuple or list") | ||
SequenceType::Tuple | SequenceType::TupleNoParens => { |
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 would an empty TupleNoParens
look like?
f457285
to
0fef07d
Compare
5666806
to
96d92a8
Compare
5d9d616
to
161e3c5
Compare
635c173
to
728e378
Compare
96d92a8
to
26f5d4b
Compare
26f5d4b
to
48d2cc9
Compare
Summary
This PR ensures that we handle bracketed comments on sequences, like
# comment
here:The handling is very similar to other, similar nodes, except that we do need some special logic to determine whether the sequence is parenthesized, similar to our logic for tuples.
Test Plan
cargo test