diff --git a/crates/ruff/src/rules/flake8_simplify/rules/ast_if.rs b/crates/ruff/src/rules/flake8_simplify/rules/ast_if.rs index 383ad69952..c2bf0b629a 100644 --- a/crates/ruff/src/rules/flake8_simplify/rules/ast_if.rs +++ b/crates/ruff/src/rules/flake8_simplify/rules/ast_if.rs @@ -721,10 +721,16 @@ pub(crate) fn if_with_same_arms(checker: &mut Checker, locator: &Locator, stmt_i // ...and the same comments let first_comments = checker .indexer() - .comments_in_range(body_range(¤t_branch, locator), locator); + .comment_ranges() + .comments_in_range(body_range(¤t_branch, locator)) + .iter() + .map(|range| locator.slice(*range)); let second_comments = checker .indexer() - .comments_in_range(body_range(following_branch, locator), locator); + .comment_ranges() + .comments_in_range(body_range(following_branch, locator)) + .iter() + .map(|range| locator.slice(*range)); if !first_comments.eq(second_comments) { continue; } diff --git a/crates/ruff/src/rules/isort/comments.rs b/crates/ruff/src/rules/isort/comments.rs index 1bfe1cacee..b492ce6309 100644 --- a/crates/ruff/src/rules/isort/comments.rs +++ b/crates/ruff/src/rules/isort/comments.rs @@ -1,7 +1,7 @@ use std::borrow::Cow; -use ruff_python_ast::{PySourceType, Ranged}; -use ruff_python_parser::{lexer, AsMode, Tok}; +use ruff_python_ast::Ranged; +use ruff_python_index::Indexer; use ruff_source_file::Locator; use ruff_text_size::TextRange; @@ -21,20 +21,15 @@ impl Ranged for Comment<'_> { pub(crate) fn collect_comments<'a>( range: TextRange, locator: &'a Locator, - source_type: PySourceType, + indexer: &'a Indexer, ) -> Vec> { - let contents = locator.slice(range); - lexer::lex_starts_at(contents, source_type.as_mode(), range.start()) - .flatten() - .filter_map(|(tok, range)| { - if let Tok::Comment(value) = tok { - Some(Comment { - value: value.into(), - range, - }) - } else { - None - } + indexer + .comment_ranges() + .comments_in_range(range) + .iter() + .map(|range| Comment { + value: locator.slice(*range).into(), + range: *range, }) .collect() } diff --git a/crates/ruff/src/rules/isort/rules/organize_imports.rs b/crates/ruff/src/rules/isort/rules/organize_imports.rs index acc9a0ee31..8b72f149ab 100644 --- a/crates/ruff/src/rules/isort/rules/organize_imports.rs +++ b/crates/ruff/src/rules/isort/rules/organize_imports.rs @@ -106,7 +106,7 @@ pub(crate) fn organize_imports( let comments = comments::collect_comments( TextRange::new(range.start(), locator.full_line_end(range.end())), locator, - source_type, + indexer, ); let trailing_line_end = if block.trailer.is_none() { diff --git a/crates/ruff_python_index/src/indexer.rs b/crates/ruff_python_index/src/indexer.rs index 222e58fca0..f6cc333423 100644 --- a/crates/ruff_python_index/src/indexer.rs +++ b/crates/ruff_python_index/src/indexer.rs @@ -99,18 +99,6 @@ impl Indexer { &self.comment_ranges } - /// Returns the comments in the given range as source code slices - pub fn comments_in_range<'a>( - &'a self, - range: TextRange, - locator: &'a Locator, - ) -> impl Iterator { - self.comment_ranges - .comments_in_range(range) - .iter() - .map(move |comment_range| locator.slice(*comment_range)) - } - /// Returns the line start positions of continuations (backslash). pub fn continuation_line_starts(&self) -> &[TextSize] { &self.continuation_lines