mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-11 22:28:27 +00:00
Remove unnecessary Comment#slice
calls (#6997)
This commit is contained in:
parent
34e8de738e
commit
b404e54f33
11 changed files with 23 additions and 39 deletions
|
@ -40,9 +40,7 @@ impl Format<PyFormatContext<'_>> 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<PyFormatContext<'_>> 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<PyFormatContext<'_>> 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<PyFormatContext<'_>> 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()))
|
||||
]
|
||||
)?;
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -39,13 +39,13 @@ impl FormatNodeRule<ExprSlice> 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
|
||||
|
|
|
@ -212,7 +212,7 @@ impl Format<PyFormatContext<'_>> 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<PyFormatContext<'_>> for FormatStringContinuation<'_> {
|
|||
comment.line_position().is_end_of_line()
|
||||
&& !locator.contains_line_break(TextRange::new(
|
||||
token_range.end(),
|
||||
comment.slice().start(),
|
||||
comment.start(),
|
||||
))
|
||||
});
|
||||
|
||||
|
|
|
@ -38,8 +38,7 @@ impl FormatNodeRule<Comprehension> 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);
|
||||
|
|
|
@ -127,7 +127,7 @@ impl FormatNodeRule<Parameters> 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");
|
||||
|
|
|
@ -42,7 +42,7 @@ impl FormatNodeRule<StmtFor> 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);
|
||||
|
|
|
@ -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());
|
||||
|
|
|
@ -26,7 +26,7 @@ impl FormatNodeRule<StmtWhile> 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);
|
||||
|
|
|
@ -223,7 +223,7 @@ impl FormatRule<Suite, PyFormatContext<'_>> 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()
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue