mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-28 04:45:01 +00:00
Handle trailing body end-of-line comments (#4811)
### Summary This PR adds custom logic to handle end-of-line comments of the last statement in a body. For example: ```python while True: if something.changed: do.stuff() # trailing comment b ``` The `# trailing comment` is a trailing comment of the `do.stuff()` expression statement. We incorrectly attached the comment as a trailing comment of the enclosing `while` statement because the comment is between the end of the while statement (the `while` statement ends right after `do.stuff()`) and before the `b` statement. This PR fixes the placement to correctly attach these comments to the last statement in a body (recursively). ## Test Plan I reviewed the snapshots and they now look correct. This may appear odd because a lot comments have now disappeared. This is the expected result because we use `verbatim` formatting for the block statements (like `while`) and that means that it only formats the inner content of the block, but not any trailing comments. The comments were visible before, because they were associated with the block statement (e.g. `while`).
This commit is contained in:
parent
e82160a83a
commit
cb6788ab5f
8 changed files with 132 additions and 9 deletions
|
@ -18,6 +18,7 @@ pub(super) fn place_comment<'a>(
|
|||
.or_else(|comment| handle_match_comment(comment, locator))
|
||||
.or_else(|comment| handle_in_between_bodies_comment(comment, locator))
|
||||
.or_else(|comment| handle_trailing_body_comment(comment, locator))
|
||||
.or_else(handle_trailing_end_of_line_body_comment)
|
||||
.or_else(|comment| handle_positional_only_arguments_separator_comment(comment, locator))
|
||||
.or_else(|comment| {
|
||||
handle_trailing_binary_expression_left_or_operator_comment(comment, locator)
|
||||
|
@ -401,6 +402,41 @@ fn handle_trailing_body_comment<'a>(
|
|||
}
|
||||
}
|
||||
|
||||
/// Handles end of line comments of the last statement in an indented body:
|
||||
///
|
||||
/// ```python
|
||||
/// while True:
|
||||
/// if something.changed:
|
||||
/// do.stuff() # trailing comment
|
||||
/// ```
|
||||
fn handle_trailing_end_of_line_body_comment(comment: DecoratedComment<'_>) -> CommentPlacement<'_> {
|
||||
// Must be an end of line comment
|
||||
if comment.text_position().is_own_line() {
|
||||
return CommentPlacement::Default(comment);
|
||||
}
|
||||
|
||||
// Must be *after* a statement
|
||||
let Some(preceding) = comment.preceding_node() else {
|
||||
return CommentPlacement::Default(comment);
|
||||
};
|
||||
|
||||
// Recursively get the last child of statements with a body.
|
||||
let last_children = std::iter::successors(last_child_in_body(preceding), |parent| {
|
||||
last_child_in_body(*parent)
|
||||
});
|
||||
|
||||
if let Some(last_child) = last_children.last() {
|
||||
CommentPlacement::trailing(last_child, comment)
|
||||
} else {
|
||||
// End of line comment of a statement that has no body. This is not what we're looking for.
|
||||
// ```python
|
||||
// a # trailing comment
|
||||
// b
|
||||
// ```
|
||||
CommentPlacement::Default(comment)
|
||||
}
|
||||
}
|
||||
|
||||
/// Attaches comments for the positional-only arguments separator `/` as trailing comments to the
|
||||
/// enclosing [`Arguments`] node.
|
||||
///
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue