Skip to content
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

Skip over parentheses when detecting in keyword #8054

Merged
merged 1 commit into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,6 @@
c # negative decimal
]

# Parenthesized targets and iterators.
[x for (x) in y]
[x for x in (y)]
27 changes: 7 additions & 20 deletions crates/ruff_python_formatter/src/other/comprehension.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use ruff_formatter::{format_args, write, Buffer, FormatError, FormatResult};
use ruff_formatter::{format_args, write, Buffer, FormatResult};
use ruff_python_ast::{Comprehension, Expr};
use ruff_python_trivia::{SimpleToken, SimpleTokenKind, SimpleTokenizer};
use ruff_python_trivia::{find_only_token_in_range, SimpleTokenKind};
use ruff_text_size::{Ranged, TextRange};

use crate::comments::{leading_comments, trailing_comments, SourceComment};
Expand Down Expand Up @@ -42,27 +42,14 @@ impl FormatNodeRule<Comprehension> for FormatComprehension {
dangling_item_comments.partition_point(|comment| comment.end() < target.start()),
);

let maybe_in_token = SimpleTokenizer::new(
f.context().source(),
let in_token = find_only_token_in_range(
TextRange::new(target.end(), iter.start()),
)
.skip_trivia()
.next();

let Some(
in_keyword @ SimpleToken {
kind: SimpleTokenKind::In,
..
},
) = maybe_in_token
else {
return Err(FormatError::syntax_error(
"Expected `in` keyword between the `target` and `iter`.",
));
};
SimpleTokenKind::In,
f.context().source(),
);

let (before_in_comments, dangling_comments) = dangling_comments.split_at(
dangling_comments.partition_point(|comment| comment.end() < in_keyword.start()),
dangling_comments.partition_point(|comment| comment.end() < in_token.start()),
);

let (trailing_in_comments, dangling_if_comments) = dangling_comments
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ aaaaaaaaaaaaaaaaaaaaa = [
c # negative decimal
]

# Parenthesized targets and iterators.
[x for (x) in y]
[x for x in (y)]
```

## Output
Expand Down Expand Up @@ -247,6 +250,10 @@ aaaaaaaaaaaaaaaaaaaaa = [
for components in b # pylint: disable=undefined-loop-variable # integer 1 may only have decimal 01-09
+ c # negative decimal
]

# Parenthesized targets and iterators.
[x for (x) in y]
[x for x in (y)]
```


Expand Down