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:
konsti 2023-07-20 11:30:39 +02:00 committed by GitHub
parent 92f471a666
commit 8c5f8a8aef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -747,16 +747,21 @@ 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);
}
let mut colon_token = SimpleTokenizer::new(
locator.contents(), locator.contents(),
TextRange::new(preceding.end(), following.start()), TextRange::new(preceding.end(), following.start()),
) )
.skip_trivia(); .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
@ -773,26 +778,20 @@ fn handle_trailing_end_of_line_condition_comment<'a>(
// ): // ):
// ... // ...
// ``` // ```
break; return CommentPlacement::Default(comment);
} }
SimpleTokenKind::RParen => { Some(token) => {
// Skip over any closing parentheses
}
SimpleTokenKind::Comma => {
// Skip over any trailing comma
}
kind => {
unreachable!( unreachable!(
"Only ')' or ':' should follow the condition but encountered {kind:?}" "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
/// ///
/// ```python /// ```python
@ -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 // The dict case
debug_assert!( debug_assert!(
matches!( matches!(
token, token,
SimpleToken { SimpleToken {
kind: SimpleTokenKind::LBrace | SimpleTokenKind::Comma | SimpleTokenKind::Colon, kind: SimpleTokenKind::LBrace
| SimpleTokenKind::Comma
| SimpleTokenKind::Colon,
.. ..
} }
), ),
"{token:?}", "{token:?}",
); );
break; }
} }
for token in tokens { for token in tokens {