mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-22 19:39:58 +00:00
Move has_comments
to CommentRanges
(#11495)
## Summary This PR moves the `has_comments` function from `Indexer` to `CommentRanges`. The main motivation is that the `CommentRanges` will now be built by the parser which is shared between the linter and the formatter. Thus, the `CommentRanges` will be removed from the `Indexer`. ## Test Plan `cargo test`
This commit is contained in:
parent
5bb9720a10
commit
f0046ab28e
10 changed files with 65 additions and 29 deletions
|
@ -6,7 +6,7 @@ use ruff_source_file::Locator;
|
|||
|
||||
use ruff_text_size::{Ranged, TextRange, TextSize};
|
||||
|
||||
use crate::is_python_whitespace;
|
||||
use crate::{has_leading_content, has_trailing_content, is_python_whitespace};
|
||||
|
||||
/// Stores the ranges of comments sorted by [`TextRange::start`] in increasing order. No two ranges are overlapping.
|
||||
#[derive(Clone, Default)]
|
||||
|
@ -49,6 +49,25 @@ impl CommentRanges {
|
|||
}
|
||||
}
|
||||
|
||||
/// Returns `true` if a statement or expression includes at least one comment.
|
||||
pub fn has_comments<T>(&self, node: &T, locator: &Locator) -> bool
|
||||
where
|
||||
T: Ranged,
|
||||
{
|
||||
let start = if has_leading_content(node.start(), locator) {
|
||||
node.start()
|
||||
} else {
|
||||
locator.line_start(node.start())
|
||||
};
|
||||
let end = if has_trailing_content(node.end(), locator) {
|
||||
node.end()
|
||||
} else {
|
||||
locator.line_end(node.end())
|
||||
};
|
||||
|
||||
self.intersects(TextRange::new(start, end))
|
||||
}
|
||||
|
||||
/// Given a [`CommentRanges`], determine which comments are grouped together
|
||||
/// in "comment blocks". A "comment block" is a sequence of consecutive
|
||||
/// own-line comments in which the comment hash (`#`) appears in the same
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue