Skip to content

Commit

Permalink
Consistent CommentPlacement conversion signatures (#6231)
Browse files Browse the repository at this point in the history
**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.
  • Loading branch information
konstin authored Aug 1, 2023
1 parent ecfdd8d commit c6986ac
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
30 changes: 15 additions & 15 deletions crates/ruff_python_formatter/src/comments/placement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use ruff_python_ast::{
};
use ruff_text_size::TextRange;

use ruff_python_ast::node::{AnyNodeRef, AstNode};
use ruff_python_ast::node::AnyNodeRef;
use ruff_python_ast::whitespace::indentation;
use ruff_python_trivia::{
indentation_at_offset, PythonWhitespace, SimpleToken, SimpleTokenKind, SimpleTokenizer,
Expand Down Expand Up @@ -420,7 +420,7 @@ fn handle_match_comment<'a>(
// ```
// Attach the `comment` as leading comment to the next case.
if comment_indentation <= match_case_indentation {
CommentPlacement::leading(next_case.into(), comment)
CommentPlacement::leading(next_case, comment)
} else {
// Otherwise, delegate to `handle_trailing_body_comment`
// ```python
Expand Down Expand Up @@ -450,7 +450,7 @@ fn handle_match_comment<'a>(
// # Trailing match comment
// ```
// This is a trailing comment of the last case.
CommentPlacement::trailing(match_case.into(), comment)
CommentPlacement::trailing(match_case, comment)
} else {
// Delegate to `handle_trailing_body_comment` because it's either a trailing indent
// for the last statement in the `case` body or a comment for the parent of the `match`
Expand Down Expand Up @@ -628,7 +628,7 @@ fn handle_trailing_binary_expression_left_or_operator_comment<'a>(
// 3
// )
// ```
CommentPlacement::trailing(AnyNodeRef::from(binary_expression.left.as_ref()), comment)
CommentPlacement::trailing(binary_expression.left.as_ref(), comment)
} else if comment.line_position().is_end_of_line() {
// Is the operator on its own line.
if locator.contains_line_break(TextRange::new(
Expand All @@ -645,7 +645,7 @@ fn handle_trailing_binary_expression_left_or_operator_comment<'a>(
// 3
// )
// ```
CommentPlacement::dangling(binary_expression.into(), comment)
CommentPlacement::dangling(binary_expression, comment)
} else {
// ```python
// a = (
Expand Down Expand Up @@ -806,14 +806,14 @@ fn handle_slice_comments<'a>(

if let Some(node) = node {
if comment.slice().start() < node.start() {
CommentPlacement::leading(node.as_ref().into(), comment)
CommentPlacement::leading(node.as_ref(), comment)
} else {
// If a trailing comment is an end of line comment that's fine because we have a node
// ahead of it
CommentPlacement::trailing(node.as_ref().into(), comment)
CommentPlacement::trailing(node.as_ref(), comment)
}
} else {
CommentPlacement::dangling(expr_slice.as_any_node_ref(), comment)
CommentPlacement::dangling(expr_slice, comment)
}
}

Expand Down Expand Up @@ -955,7 +955,7 @@ fn handle_attribute_comment<'a>(
// ```
CommentPlacement::trailing(comment.enclosing_node(), comment)
} else {
CommentPlacement::dangling(attribute.into(), comment)
CommentPlacement::dangling(attribute, comment)
}
}

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

let else_token = find_only_token_in_range(
Expand All @@ -1009,7 +1009,7 @@ fn handle_expr_if_comment<'a>(
if else_token.range.start() < comment.slice().start()
&& comment.slice().start() < orelse.start()
{
return CommentPlacement::leading(orelse.as_ref().into(), comment);
return CommentPlacement::leading(orelse.as_ref(), comment);
}

CommentPlacement::Default(comment)
Expand Down Expand Up @@ -1043,7 +1043,7 @@ fn handle_trailing_expression_starred_star_end_of_line_comment<'a>(
return CommentPlacement::Default(comment);
}

CommentPlacement::leading(starred.as_any_node_ref(), comment)
CommentPlacement::leading(starred, comment)
}

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

Expand Down Expand Up @@ -1219,12 +1219,12 @@ fn handle_comprehension_comment<'a>(
);
if is_own_line {
if last_end < comment.slice().start() && comment.slice().start() < if_token.start() {
return CommentPlacement::dangling((if_node).into(), comment);
return CommentPlacement::dangling(if_node, comment);
}
} else if if_token.start() < comment.slice().start()
&& comment.slice().start() < if_node.range().start()
{
return CommentPlacement::dangling((if_node).into(), comment);
return CommentPlacement::dangling(if_node, comment);
}
last_end = if_node.range().end();
}
Expand Down
12 changes: 6 additions & 6 deletions crates/ruff_python_formatter/src/comments/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,26 +620,26 @@ pub(super) enum CommentPlacement<'a> {
impl<'a> CommentPlacement<'a> {
/// Makes `comment` a [leading comment](self#leading-comments) of `node`.
#[inline]
pub(super) fn leading(node: AnyNodeRef<'a>, comment: impl Into<SourceComment>) -> Self {
pub(super) fn leading(node: impl Into<AnyNodeRef<'a>>, comment: DecoratedComment) -> Self {
Self::Leading {
node,
node: node.into(),
comment: comment.into(),
}
}

/// Makes `comment` a [dangling comment](self::dangling-comments) of `node`.
pub(super) fn dangling(node: AnyNodeRef<'a>, comment: impl Into<SourceComment>) -> Self {
pub(super) fn dangling(node: impl Into<AnyNodeRef<'a>>, comment: DecoratedComment) -> Self {
Self::Dangling {
node,
node: node.into(),
comment: comment.into(),
}
}

/// Makes `comment` a [trailing comment](self::trailing-comments) of `node`.
#[inline]
pub(super) fn trailing(node: AnyNodeRef<'a>, comment: impl Into<SourceComment>) -> Self {
pub(super) fn trailing(node: impl Into<AnyNodeRef<'a>>, comment: DecoratedComment) -> Self {
Self::Trailing {
node,
node: node.into(),
comment: comment.into(),
}
}
Expand Down

0 comments on commit c6986ac

Please sign in to comment.