Skip to content

Commit c6986ac

Browse files
authored
Consistent CommentPlacement conversion signatures (#6231)
**Summary** Allow passing any node to `CommentPlacement::{leading, trailing, dangling}` without manually converting. Conversely, Restrict the comment to the only type we actually pass. **Test Plan** No changes.
1 parent ecfdd8d commit c6986ac

File tree

2 files changed

+21
-21
lines changed

2 files changed

+21
-21
lines changed

crates/ruff_python_formatter/src/comments/placement.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use ruff_python_ast::{
66
};
77
use ruff_text_size::TextRange;
88

9-
use ruff_python_ast::node::{AnyNodeRef, AstNode};
9+
use ruff_python_ast::node::AnyNodeRef;
1010
use ruff_python_ast::whitespace::indentation;
1111
use ruff_python_trivia::{
1212
indentation_at_offset, PythonWhitespace, SimpleToken, SimpleTokenKind, SimpleTokenizer,
@@ -420,7 +420,7 @@ fn handle_match_comment<'a>(
420420
// ```
421421
// Attach the `comment` as leading comment to the next case.
422422
if comment_indentation <= match_case_indentation {
423-
CommentPlacement::leading(next_case.into(), comment)
423+
CommentPlacement::leading(next_case, comment)
424424
} else {
425425
// Otherwise, delegate to `handle_trailing_body_comment`
426426
// ```python
@@ -450,7 +450,7 @@ fn handle_match_comment<'a>(
450450
// # Trailing match comment
451451
// ```
452452
// This is a trailing comment of the last case.
453-
CommentPlacement::trailing(match_case.into(), comment)
453+
CommentPlacement::trailing(match_case, comment)
454454
} else {
455455
// Delegate to `handle_trailing_body_comment` because it's either a trailing indent
456456
// for the last statement in the `case` body or a comment for the parent of the `match`
@@ -628,7 +628,7 @@ fn handle_trailing_binary_expression_left_or_operator_comment<'a>(
628628
// 3
629629
// )
630630
// ```
631-
CommentPlacement::trailing(AnyNodeRef::from(binary_expression.left.as_ref()), comment)
631+
CommentPlacement::trailing(binary_expression.left.as_ref(), comment)
632632
} else if comment.line_position().is_end_of_line() {
633633
// Is the operator on its own line.
634634
if locator.contains_line_break(TextRange::new(
@@ -645,7 +645,7 @@ fn handle_trailing_binary_expression_left_or_operator_comment<'a>(
645645
// 3
646646
// )
647647
// ```
648-
CommentPlacement::dangling(binary_expression.into(), comment)
648+
CommentPlacement::dangling(binary_expression, comment)
649649
} else {
650650
// ```python
651651
// a = (
@@ -806,14 +806,14 @@ fn handle_slice_comments<'a>(
806806

807807
if let Some(node) = node {
808808
if comment.slice().start() < node.start() {
809-
CommentPlacement::leading(node.as_ref().into(), comment)
809+
CommentPlacement::leading(node.as_ref(), comment)
810810
} else {
811811
// If a trailing comment is an end of line comment that's fine because we have a node
812812
// ahead of it
813-
CommentPlacement::trailing(node.as_ref().into(), comment)
813+
CommentPlacement::trailing(node.as_ref(), comment)
814814
}
815815
} else {
816-
CommentPlacement::dangling(expr_slice.as_any_node_ref(), comment)
816+
CommentPlacement::dangling(expr_slice, comment)
817817
}
818818
}
819819

@@ -955,7 +955,7 @@ fn handle_attribute_comment<'a>(
955955
// ```
956956
CommentPlacement::trailing(comment.enclosing_node(), comment)
957957
} else {
958-
CommentPlacement::dangling(attribute.into(), comment)
958+
CommentPlacement::dangling(attribute, comment)
959959
}
960960
}
961961

@@ -997,7 +997,7 @@ fn handle_expr_if_comment<'a>(
997997
);
998998
// Between `if` and `test`
999999
if if_token.range.start() < comment.slice().start() && comment.slice().start() < test.start() {
1000-
return CommentPlacement::leading(test.as_ref().into(), comment);
1000+
return CommentPlacement::leading(test.as_ref(), comment);
10011001
}
10021002

10031003
let else_token = find_only_token_in_range(
@@ -1009,7 +1009,7 @@ fn handle_expr_if_comment<'a>(
10091009
if else_token.range.start() < comment.slice().start()
10101010
&& comment.slice().start() < orelse.start()
10111011
{
1012-
return CommentPlacement::leading(orelse.as_ref().into(), comment);
1012+
return CommentPlacement::leading(orelse.as_ref(), comment);
10131013
}
10141014

10151015
CommentPlacement::Default(comment)
@@ -1043,7 +1043,7 @@ fn handle_trailing_expression_starred_star_end_of_line_comment<'a>(
10431043
return CommentPlacement::Default(comment);
10441044
}
10451045

1046-
CommentPlacement::leading(starred.as_any_node_ref(), comment)
1046+
CommentPlacement::leading(starred, comment)
10471047
}
10481048

10491049
/// Handles trailing own line comments before the `as` keyword of a with item and
@@ -1188,7 +1188,7 @@ fn handle_comprehension_comment<'a>(
11881188
CommentPlacement::Default(comment)
11891189
} else {
11901190
// after the `in` but same line, turn into trailing on the `in` token
1191-
CommentPlacement::dangling((&comprehension.iter).into(), comment)
1191+
CommentPlacement::dangling(&comprehension.iter, comment)
11921192
};
11931193
}
11941194

@@ -1219,12 +1219,12 @@ fn handle_comprehension_comment<'a>(
12191219
);
12201220
if is_own_line {
12211221
if last_end < comment.slice().start() && comment.slice().start() < if_token.start() {
1222-
return CommentPlacement::dangling((if_node).into(), comment);
1222+
return CommentPlacement::dangling(if_node, comment);
12231223
}
12241224
} else if if_token.start() < comment.slice().start()
12251225
&& comment.slice().start() < if_node.range().start()
12261226
{
1227-
return CommentPlacement::dangling((if_node).into(), comment);
1227+
return CommentPlacement::dangling(if_node, comment);
12281228
}
12291229
last_end = if_node.range().end();
12301230
}

crates/ruff_python_formatter/src/comments/visitor.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -620,26 +620,26 @@ pub(super) enum CommentPlacement<'a> {
620620
impl<'a> CommentPlacement<'a> {
621621
/// Makes `comment` a [leading comment](self#leading-comments) of `node`.
622622
#[inline]
623-
pub(super) fn leading(node: AnyNodeRef<'a>, comment: impl Into<SourceComment>) -> Self {
623+
pub(super) fn leading(node: impl Into<AnyNodeRef<'a>>, comment: DecoratedComment) -> Self {
624624
Self::Leading {
625-
node,
625+
node: node.into(),
626626
comment: comment.into(),
627627
}
628628
}
629629

630630
/// Makes `comment` a [dangling comment](self::dangling-comments) of `node`.
631-
pub(super) fn dangling(node: AnyNodeRef<'a>, comment: impl Into<SourceComment>) -> Self {
631+
pub(super) fn dangling(node: impl Into<AnyNodeRef<'a>>, comment: DecoratedComment) -> Self {
632632
Self::Dangling {
633-
node,
633+
node: node.into(),
634634
comment: comment.into(),
635635
}
636636
}
637637

638638
/// Makes `comment` a [trailing comment](self::trailing-comments) of `node`.
639639
#[inline]
640-
pub(super) fn trailing(node: AnyNodeRef<'a>, comment: impl Into<SourceComment>) -> Self {
640+
pub(super) fn trailing(node: impl Into<AnyNodeRef<'a>>, comment: DecoratedComment) -> Self {
641641
Self::Trailing {
642-
node,
642+
node: node.into(),
643643
comment: comment.into(),
644644
}
645645
}

0 commit comments

Comments
 (0)