mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-24 05:25:17 +00:00
Formatter: Small RParen refactoring (#5885)
## Summary A bit more consistency inspired by https://github.com/astral-sh/ruff/pull/5882#discussion_r1268182403 ## Test Plan Existing tests (refactoring)
This commit is contained in:
parent
92f471a666
commit
8c5f8a8aef
1 changed files with 56 additions and 59 deletions
|
@ -747,50 +747,49 @@ fn handle_trailing_end_of_line_condition_comment<'a>(
|
||||||
|
|
||||||
// If the preceding is the node before the `colon`
|
// If the preceding is the node before the `colon`
|
||||||
// `while true:` The node before the `colon` is the `true` constant.
|
// `while true:` The node before the `colon` is the `true` constant.
|
||||||
if preceding.ptr_eq(last_before_colon) {
|
if !preceding.ptr_eq(last_before_colon) {
|
||||||
let tokens = SimpleTokenizer::new(
|
return CommentPlacement::Default(comment);
|
||||||
locator.contents(),
|
}
|
||||||
TextRange::new(preceding.end(), following.start()),
|
let mut colon_token = SimpleTokenizer::new(
|
||||||
)
|
locator.contents(),
|
||||||
.skip_trivia();
|
TextRange::new(preceding.end(), following.start()),
|
||||||
|
)
|
||||||
|
.skip_trivia()
|
||||||
|
// Skip over any closing parentheses and any trailing comma
|
||||||
|
.skip_while(|token| {
|
||||||
|
token.kind == SimpleTokenKind::RParen || token.kind == SimpleTokenKind::Comma
|
||||||
|
});
|
||||||
|
|
||||||
for token in tokens {
|
match colon_token.next() {
|
||||||
match token.kind() {
|
Some(token) if token.kind == SimpleTokenKind::Colon => {
|
||||||
SimpleTokenKind::Colon => {
|
if comment.slice().start() > token.start() {
|
||||||
if comment.slice().start() > token.start() {
|
// Comment comes after the colon
|
||||||
// Comment comes after the colon
|
// ```python
|
||||||
// ```python
|
// while a: # comment
|
||||||
// while a: # comment
|
// ...
|
||||||
// ...
|
// ```
|
||||||
// ```
|
return CommentPlacement::dangling(enclosing_node, comment);
|
||||||
return CommentPlacement::dangling(enclosing_node, comment);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Comment comes before the colon
|
|
||||||
// ```python
|
|
||||||
// while (
|
|
||||||
// a # comment
|
|
||||||
// ):
|
|
||||||
// ...
|
|
||||||
// ```
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
SimpleTokenKind::RParen => {
|
|
||||||
// Skip over any closing parentheses
|
|
||||||
}
|
|
||||||
SimpleTokenKind::Comma => {
|
|
||||||
// Skip over any trailing comma
|
|
||||||
}
|
|
||||||
kind => {
|
|
||||||
unreachable!(
|
|
||||||
"Only ')' or ':' should follow the condition but encountered {kind:?}"
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Comment comes before the colon
|
||||||
|
// ```python
|
||||||
|
// while (
|
||||||
|
// a # comment
|
||||||
|
// ):
|
||||||
|
// ...
|
||||||
|
// ```
|
||||||
|
return CommentPlacement::Default(comment);
|
||||||
|
}
|
||||||
|
Some(token) => {
|
||||||
|
unreachable!(
|
||||||
|
"Only ')' or ':' should follow the condition but encountered {:?}",
|
||||||
|
token.kind
|
||||||
|
)
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
unreachable!("Expected trailing condition comment to be preceded by a token",)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
CommentPlacement::Default(comment)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Handles end of line comments after the `:` of an except clause
|
/// Handles end of line comments after the `:` of an except clause
|
||||||
|
@ -1173,35 +1172,33 @@ fn handle_dict_unpacking_comment<'a>(
|
||||||
locator.contents(),
|
locator.contents(),
|
||||||
TextRange::new(preceding_end, comment.slice().start()),
|
TextRange::new(preceding_end, comment.slice().start()),
|
||||||
)
|
)
|
||||||
.skip_trivia();
|
.skip_trivia()
|
||||||
|
.skip_while(|token| token.kind == SimpleTokenKind::RParen);
|
||||||
|
|
||||||
// if the remaining tokens from the previous node are exactly `**`,
|
// if the remaining tokens from the previous node are exactly `**`,
|
||||||
// re-assign the comment to the one that follows the stars
|
// re-assign the comment to the one that follows the stars
|
||||||
let mut count = 0;
|
let mut count = 0;
|
||||||
|
|
||||||
// we start from the preceding node but we skip its token
|
// we start from the preceding node but we skip its token
|
||||||
for token in tokens.by_ref() {
|
if let Some(token) = tokens.next() {
|
||||||
// Skip closing parentheses that are not part of the node range
|
|
||||||
if token.kind == SimpleTokenKind::RParen {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
// The Keyword case
|
// The Keyword case
|
||||||
if token.kind == SimpleTokenKind::Star {
|
if token.kind == SimpleTokenKind::Star {
|
||||||
count += 1;
|
count += 1;
|
||||||
break;
|
} else {
|
||||||
|
// The dict case
|
||||||
|
debug_assert!(
|
||||||
|
matches!(
|
||||||
|
token,
|
||||||
|
SimpleToken {
|
||||||
|
kind: SimpleTokenKind::LBrace
|
||||||
|
| SimpleTokenKind::Comma
|
||||||
|
| SimpleTokenKind::Colon,
|
||||||
|
..
|
||||||
|
}
|
||||||
|
),
|
||||||
|
"{token:?}",
|
||||||
|
);
|
||||||
}
|
}
|
||||||
// The dict case
|
|
||||||
debug_assert!(
|
|
||||||
matches!(
|
|
||||||
token,
|
|
||||||
SimpleToken {
|
|
||||||
kind: SimpleTokenKind::LBrace | SimpleTokenKind::Comma | SimpleTokenKind::Colon,
|
|
||||||
..
|
|
||||||
}
|
|
||||||
),
|
|
||||||
"{token:?}",
|
|
||||||
);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for token in tokens {
|
for token in tokens {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue