mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-27 20:42:10 +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.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue