From b404e54f336b822b09a07a586ee2ca241eec94fe Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Tue, 29 Aug 2023 20:44:11 -0400 Subject: [PATCH] Remove unnecessary `Comment#slice` calls (#6997) --- .../ruff_python_formatter/src/comments/format.rs | 12 ++++-------- .../src/comments/placement.rs | 15 +++++---------- .../ruff_python_formatter/src/comments/visitor.rs | 14 ++++---------- .../src/expression/expr_slice.rs | 4 ++-- .../src/expression/string.rs | 4 ++-- .../src/other/comprehension.rs | 3 +-- .../ruff_python_formatter/src/other/parameters.rs | 2 +- .../src/statement/stmt_for.rs | 2 +- .../src/statement/stmt_try.rs | 2 +- .../src/statement/stmt_while.rs | 2 +- .../ruff_python_formatter/src/statement/suite.rs | 2 +- 11 files changed, 23 insertions(+), 39 deletions(-) diff --git a/crates/ruff_python_formatter/src/comments/format.rs b/crates/ruff_python_formatter/src/comments/format.rs index 4084d76a28..fcfa3b6811 100644 --- a/crates/ruff_python_formatter/src/comments/format.rs +++ b/crates/ruff_python_formatter/src/comments/format.rs @@ -40,9 +40,7 @@ impl Format> for FormatLeadingComments<'_> { .iter() .filter(|comment| comment.is_unformatted()) { - let slice = comment.slice(); - - let lines_after_comment = lines_after(slice.end(), f.context().source()); + let lines_after_comment = lines_after(comment.end(), f.context().source()); write!( f, [format_comment(comment), empty_lines(lines_after_comment)] @@ -83,7 +81,7 @@ impl Format> for FormatLeadingAlternateBranchComments<'_> { if let Some(first_leading) = self.comments.first() { // Leading comments only preserves the lines after the comment but not before. // Insert the necessary lines. - if lines_before(first_leading.slice().start(), f.context().source()) > 1 { + if lines_before(first_leading.start(), f.context().source()) > 1 { write!(f, [empty_line()])?; } @@ -133,12 +131,10 @@ impl Format> for FormatTrailingComments<'_> { .iter() .filter(|comment| comment.is_unformatted()) { - let slice = trailing.slice(); - has_trailing_own_line_comment |= trailing.line_position().is_own_line(); if has_trailing_own_line_comment { - let lines_before_comment = lines_before(slice.start(), f.context().source()); + let lines_before_comment = lines_before(trailing.start(), f.context().source()); // A trailing comment at the end of a body or list // ```python @@ -223,7 +219,7 @@ impl Format> for FormatDanglingComments<'_> { f, [ format_comment(comment), - empty_lines(lines_after(comment.slice().end(), f.context().source())) + empty_lines(lines_after(comment.end(), f.context().source())) ] )?; diff --git a/crates/ruff_python_formatter/src/comments/placement.rs b/crates/ruff_python_formatter/src/comments/placement.rs index 0db12e13c8..c506054b46 100644 --- a/crates/ruff_python_formatter/src/comments/placement.rs +++ b/crates/ruff_python_formatter/src/comments/placement.rs @@ -647,11 +647,10 @@ fn handle_parameters_separator_comment<'a>( locator: &Locator, ) -> CommentPlacement<'a> { let (slash, star) = find_parameter_separators(locator.contents(), parameters); - let comment_range = comment.slice().range(); let placement = assign_argument_separator_comment_placement( slash.as_ref(), star.as_ref(), - comment_range, + comment.range(), comment.line_position(), ); if placement.is_some() { @@ -695,9 +694,7 @@ fn handle_trailing_binary_expression_left_or_operator_comment<'a>( .expect("Expected a token for the operator") .start(); - let comment_range = comment.slice().range(); - - if comment_range.end() < operator_offset { + if comment.end() < operator_offset { // ```python // a = ( // 5 @@ -816,8 +813,7 @@ fn handle_module_level_own_line_comment_before_class_or_function_comment<'a>( } // Make the comment a leading comment if there's no empty line between the comment and the function / class header - if max_empty_lines(locator.slice(TextRange::new(comment.slice().end(), following.start()))) == 0 - { + if max_empty_lines(locator.slice(TextRange::new(comment.end(), following.start()))) == 0 { CommentPlacement::leading(following, comment) } else { // Otherwise attach the comment as trailing comment to the previous statement @@ -872,8 +868,7 @@ fn handle_slice_comments<'a>( return CommentPlacement::dangling(comment.enclosing_node(), comment); } - let assignment = - assign_comment_in_slice(comment.slice().range(), locator.contents(), expr_slice); + let assignment = assign_comment_in_slice(comment.range(), locator.contents(), expr_slice); let node = match assignment { ExprSliceCommentSection::Lower => lower, ExprSliceCommentSection::Upper => upper, @@ -1558,7 +1553,7 @@ fn handle_comprehension_comment<'a>( // b in c // ] // ``` - if comment.slice().end() < comprehension.target.start() { + if comment.end() < comprehension.target.start() { return if is_own_line { // own line comments are correctly assigned as leading the target CommentPlacement::Default(comment) diff --git a/crates/ruff_python_formatter/src/comments/visitor.rs b/crates/ruff_python_formatter/src/comments/visitor.rs index c6aa5abb7c..b4b94ae293 100644 --- a/crates/ruff_python_formatter/src/comments/visitor.rs +++ b/crates/ruff_python_formatter/src/comments/visitor.rs @@ -1,17 +1,16 @@ use std::iter::Peekable; -use ruff_python_ast::{Mod, Stmt}; -use ruff_text_size::{Ranged, TextRange, TextSize}; - use ruff_formatter::{SourceCode, SourceCodeSlice}; use ruff_python_ast::node::AnyNodeRef; -use ruff_python_index::CommentRanges; -use ruff_source_file::Locator; +use ruff_python_ast::{Mod, Stmt}; // The interface is designed to only export the members relevant for iterating nodes in // pre-order. #[allow(clippy::wildcard_imports)] use ruff_python_ast::visitor::preorder::*; +use ruff_python_index::CommentRanges; use ruff_python_trivia::is_python_whitespace; +use ruff_source_file::Locator; +use ruff_text_size::{Ranged, TextRange, TextSize}; use crate::comments::node_key::NodeRefEqualityKey; use crate::comments::placement::place_comment; @@ -212,11 +211,6 @@ impl<'a> DecoratedComment<'a> { self.parent } - /// Returns the slice into the source code. - pub(super) fn slice(&self) -> &SourceCodeSlice { - &self.slice - } - /// Returns the comment's preceding node. /// /// The direct child node (ignoring lists) of the [`enclosing_node`](DecoratedComment::enclosing_node) that precedes this comment. diff --git a/crates/ruff_python_formatter/src/expression/expr_slice.rs b/crates/ruff_python_formatter/src/expression/expr_slice.rs index 01a49f852f..8d1dfc627c 100644 --- a/crates/ruff_python_formatter/src/expression/expr_slice.rs +++ b/crates/ruff_python_formatter/src/expression/expr_slice.rs @@ -39,13 +39,13 @@ impl FormatNodeRule for FormatExprSlice { let slice_dangling_comments = comments.dangling(item.as_any_node_ref()); // Put the dangling comments (where the nodes are missing) into buckets let first_colon_partition_index = - slice_dangling_comments.partition_point(|x| x.slice().start() < first_colon.start()); + slice_dangling_comments.partition_point(|x| x.start() < first_colon.start()); let (dangling_lower_comments, dangling_upper_step_comments) = slice_dangling_comments.split_at(first_colon_partition_index); let (dangling_upper_comments, dangling_step_comments) = if let Some(second_colon) = &second_colon { let second_colon_partition_index = dangling_upper_step_comments - .partition_point(|x| x.slice().start() < second_colon.start()); + .partition_point(|x| x.start() < second_colon.start()); dangling_upper_step_comments.split_at(second_colon_partition_index) } else { // Without a second colon they remaining dangling comments belong between the first diff --git a/crates/ruff_python_formatter/src/expression/string.rs b/crates/ruff_python_formatter/src/expression/string.rs index e6178ef0c3..c4e53a1064 100644 --- a/crates/ruff_python_formatter/src/expression/string.rs +++ b/crates/ruff_python_formatter/src/expression/string.rs @@ -212,7 +212,7 @@ impl Format> for FormatStringContinuation<'_> { // ) // ``` let leading_comments_end = dangling_comments - .partition_point(|comment| comment.slice().start() <= token_range.start()); + .partition_point(|comment| comment.start() <= token_range.start()); let (leading_part_comments, rest) = dangling_comments.split_at(leading_comments_end); @@ -227,7 +227,7 @@ impl Format> for FormatStringContinuation<'_> { comment.line_position().is_end_of_line() && !locator.contains_line_break(TextRange::new( token_range.end(), - comment.slice().start(), + comment.start(), )) }); diff --git a/crates/ruff_python_formatter/src/other/comprehension.rs b/crates/ruff_python_formatter/src/other/comprehension.rs index 0a58fbfc38..23224f56c8 100644 --- a/crates/ruff_python_formatter/src/other/comprehension.rs +++ b/crates/ruff_python_formatter/src/other/comprehension.rs @@ -38,8 +38,7 @@ impl FormatNodeRule for FormatComprehension { let comments = f.context().comments().clone(); let dangling_item_comments = comments.dangling(item); let (before_target_comments, before_in_comments) = dangling_item_comments.split_at( - dangling_item_comments - .partition_point(|comment| comment.slice().end() < target.start()), + dangling_item_comments.partition_point(|comment| comment.end() < target.start()), ); let trailing_in_comments = comments.dangling(iter); diff --git a/crates/ruff_python_formatter/src/other/parameters.rs b/crates/ruff_python_formatter/src/other/parameters.rs index 2e6c451ae4..cb5666fbed 100644 --- a/crates/ruff_python_formatter/src/other/parameters.rs +++ b/crates/ruff_python_formatter/src/other/parameters.rs @@ -127,7 +127,7 @@ impl FormatNodeRule for FormatParameters { let assignment = assign_argument_separator_comment_placement( slash.as_ref(), star.as_ref(), - comment.slice().range(), + comment.range(), comment.line_position(), ) .expect("Unexpected dangling comment type in function parameters"); diff --git a/crates/ruff_python_formatter/src/statement/stmt_for.rs b/crates/ruff_python_formatter/src/statement/stmt_for.rs index 66a3012e81..a66cc1888b 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_for.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_for.rs @@ -42,7 +42,7 @@ impl FormatNodeRule for FormatStmtFor { let dangling_comments = comments.dangling(item); let body_start = body.first().map_or(iter.end(), Stmt::start); let or_else_comments_start = - dangling_comments.partition_point(|comment| comment.slice().end() < body_start); + dangling_comments.partition_point(|comment| comment.end() < body_start); let (trailing_condition_comments, or_else_comments) = dangling_comments.split_at(or_else_comments_start); diff --git a/crates/ruff_python_formatter/src/statement/stmt_try.rs b/crates/ruff_python_formatter/src/statement/stmt_try.rs index 63bbcd4096..3cbf7ff7f8 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_try.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_try.rs @@ -119,7 +119,7 @@ fn format_case<'a>( Ok(if let Some(last) = body.last() { let case_comments_start = - dangling_comments.partition_point(|comment| comment.slice().end() <= last.end()); + dangling_comments.partition_point(|comment| comment.end() <= last.end()); let (case_comments, rest) = dangling_comments.split_at(case_comments_start); let partition_point = case_comments.partition_point(|comment| comment.line_position().is_own_line()); diff --git a/crates/ruff_python_formatter/src/statement/stmt_while.rs b/crates/ruff_python_formatter/src/statement/stmt_while.rs index b9212831dd..af667a6cf5 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_while.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_while.rs @@ -26,7 +26,7 @@ impl FormatNodeRule for FormatStmtWhile { let body_start = body.first().map_or(test.end(), Stmt::start); let or_else_comments_start = - dangling_comments.partition_point(|comment| comment.slice().end() < body_start); + dangling_comments.partition_point(|comment| comment.end() < body_start); let (trailing_condition_comments, or_else_comments) = dangling_comments.split_at(or_else_comments_start); diff --git a/crates/ruff_python_formatter/src/statement/suite.rs b/crates/ruff_python_formatter/src/statement/suite.rs index bb29230c1e..e266554d74 100644 --- a/crates/ruff_python_formatter/src/statement/suite.rs +++ b/crates/ruff_python_formatter/src/statement/suite.rs @@ -223,7 +223,7 @@ impl FormatRule> for FormatSuite { // the leading comment. This is why the suite handling counts the lines before the // start of the next statement or before the first leading comments for compound statements. let start = if let Some(first_leading) = comments.leading(following).first() { - first_leading.slice().start() + first_leading.start() } else { following.start() };