mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-28 04:45:01 +00:00
Bool expression comment placement (#7269)
This commit is contained in:
parent
c21b960fc7
commit
1e6df19a35
12 changed files with 776 additions and 145 deletions
|
@ -205,6 +205,9 @@ fn handle_enclosed_comment<'a>(
|
|||
locator,
|
||||
)
|
||||
}
|
||||
AnyNodeRef::ExprBoolOp(_) | AnyNodeRef::ExprCompare(_) => {
|
||||
handle_trailing_binary_like_comment(comment, locator)
|
||||
}
|
||||
AnyNodeRef::Keyword(keyword) => handle_keyword_comment(comment, keyword, locator),
|
||||
AnyNodeRef::PatternKeyword(pattern_keyword) => {
|
||||
handle_pattern_keyword_comment(comment, pattern_keyword, locator)
|
||||
|
@ -836,6 +839,47 @@ fn handle_trailing_binary_expression_left_or_operator_comment<'a>(
|
|||
}
|
||||
}
|
||||
|
||||
/// Attaches comments between two bool or compare expression operands to the preceding operand if the comment is before the operator.
|
||||
///
|
||||
/// ```python
|
||||
/// a = (
|
||||
/// 5 > 3
|
||||
/// # trailing comment
|
||||
/// and 3 == 3
|
||||
/// )
|
||||
/// ```
|
||||
fn handle_trailing_binary_like_comment<'a>(
|
||||
comment: DecoratedComment<'a>,
|
||||
locator: &Locator,
|
||||
) -> CommentPlacement<'a> {
|
||||
debug_assert!(
|
||||
comment.enclosing_node().is_expr_bool_op() || comment.enclosing_node().is_expr_compare()
|
||||
);
|
||||
|
||||
// Only if there's a preceding node (in which case, the preceding node is `left` or middle node).
|
||||
let (Some(left_operand), Some(right_operand)) =
|
||||
(comment.preceding_node(), comment.following_node())
|
||||
else {
|
||||
return CommentPlacement::Default(comment);
|
||||
};
|
||||
|
||||
let between_operands_range = TextRange::new(left_operand.end(), right_operand.start());
|
||||
|
||||
let mut tokens = SimpleTokenizer::new(locator.contents(), between_operands_range)
|
||||
.skip_trivia()
|
||||
.skip_while(|token| token.kind == SimpleTokenKind::RParen);
|
||||
let operator_offset = tokens
|
||||
.next()
|
||||
.expect("Expected a token for the operator")
|
||||
.start();
|
||||
|
||||
if comment.end() < operator_offset {
|
||||
CommentPlacement::trailing(left_operand, comment)
|
||||
} else {
|
||||
CommentPlacement::Default(comment)
|
||||
}
|
||||
}
|
||||
|
||||
/// Handles own line comments on the module level before a class or function statement.
|
||||
/// A comment only becomes the leading comment of a class or function if it isn't separated by an empty
|
||||
/// line from the class. Comments that are separated by at least one empty line from the header of the
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue