mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 18:28:24 +00:00
Build CommentRanges
outside the parser (#11792)
## Summary This PR updates the parser to remove building the `CommentRanges` and instead it'll be built by the linter and the formatter when it's required. For the linter, it'll be built and owned by the `Indexer` while for the formatter it'll be built from the `Tokens` struct and passed as an argument. ## Test Plan `cargo insta test`
This commit is contained in:
parent
7509a48eab
commit
549cc1e437
28 changed files with 151 additions and 102 deletions
|
@ -3,7 +3,9 @@
|
|||
|
||||
use ruff_python_ast::Stmt;
|
||||
use ruff_python_parser::{TokenKind, Tokens};
|
||||
use ruff_python_trivia::{has_leading_content, has_trailing_content, is_python_whitespace};
|
||||
use ruff_python_trivia::{
|
||||
has_leading_content, has_trailing_content, is_python_whitespace, CommentRanges,
|
||||
};
|
||||
use ruff_source_file::Locator;
|
||||
use ruff_text_size::{Ranged, TextRange, TextSize};
|
||||
|
||||
|
@ -19,6 +21,9 @@ pub struct Indexer {
|
|||
|
||||
/// The range of all multiline strings in the source document.
|
||||
multiline_ranges: MultilineRanges,
|
||||
|
||||
/// The range of all comments in the source document.
|
||||
comment_ranges: CommentRanges,
|
||||
}
|
||||
|
||||
impl Indexer {
|
||||
|
@ -28,6 +33,8 @@ impl Indexer {
|
|||
let mut fstring_ranges_builder = FStringRangesBuilder::default();
|
||||
let mut multiline_ranges_builder = MultilineRangesBuilder::default();
|
||||
let mut continuation_lines = Vec::new();
|
||||
let mut comment_ranges = Vec::new();
|
||||
|
||||
// Token, end
|
||||
let mut prev_end = TextSize::default();
|
||||
let mut line_start = TextSize::default();
|
||||
|
@ -64,19 +71,38 @@ impl Indexer {
|
|||
// the closing delimiter, since the token itself can span multiple lines.
|
||||
line_start = locator.line_start(token.end());
|
||||
}
|
||||
TokenKind::Comment => {
|
||||
comment_ranges.push(token.range());
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
prev_end = token.end();
|
||||
}
|
||||
|
||||
// TODO(dhruvmanila): This is temporary until Ruff becomes error resilient. To understand
|
||||
// why this is required, refer to https://github.com/astral-sh/ruff/pull/11457#issuecomment-2144990269
|
||||
// which was released at the time of this writing. Now we can't just revert that behavior,
|
||||
// so we need to visit the remaining tokens if there are any for the comment ranges.
|
||||
for token in tokens.after(prev_end) {
|
||||
if token.kind() == TokenKind::Comment {
|
||||
comment_ranges.push(token.range());
|
||||
}
|
||||
}
|
||||
|
||||
Self {
|
||||
continuation_lines,
|
||||
fstring_ranges: fstring_ranges_builder.finish(),
|
||||
multiline_ranges: multiline_ranges_builder.finish(),
|
||||
comment_ranges: CommentRanges::new(comment_ranges),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the byte offset ranges of comments.
|
||||
pub const fn comment_ranges(&self) -> &CommentRanges {
|
||||
&self.comment_ranges
|
||||
}
|
||||
|
||||
/// Returns the byte offset ranges of f-strings.
|
||||
pub const fn fstring_ranges(&self) -> &FStringRanges {
|
||||
&self.fstring_ranges
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue