mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-08 20:58:05 +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
|
@ -239,7 +239,6 @@ pub struct Parsed<T> {
|
|||
syntax: T,
|
||||
tokens: Tokens,
|
||||
errors: Vec<ParseError>,
|
||||
comment_ranges: CommentRanges,
|
||||
}
|
||||
|
||||
impl<T> Parsed<T> {
|
||||
|
@ -258,11 +257,6 @@ impl<T> Parsed<T> {
|
|||
&self.errors
|
||||
}
|
||||
|
||||
/// Returns the comment ranges for the parsed output.
|
||||
pub fn comment_ranges(&self) -> &CommentRanges {
|
||||
&self.comment_ranges
|
||||
}
|
||||
|
||||
/// Consumes the [`Parsed`] output and returns the contained syntax node.
|
||||
pub fn into_syntax(self) -> T {
|
||||
self.syntax
|
||||
|
@ -313,7 +307,6 @@ impl Parsed<Mod> {
|
|||
syntax: module,
|
||||
tokens: self.tokens,
|
||||
errors: self.errors,
|
||||
comment_ranges: self.comment_ranges,
|
||||
}),
|
||||
Mod::Expression(_) => None,
|
||||
}
|
||||
|
@ -333,7 +326,6 @@ impl Parsed<Mod> {
|
|||
syntax: expression,
|
||||
tokens: self.tokens,
|
||||
errors: self.errors,
|
||||
comment_ranges: self.comment_ranges,
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
@ -518,6 +510,18 @@ impl Deref for Tokens {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<&Tokens> for CommentRanges {
|
||||
fn from(tokens: &Tokens) -> Self {
|
||||
let mut ranges = vec![];
|
||||
for token in tokens {
|
||||
if token.kind() == TokenKind::Comment {
|
||||
ranges.push(token.range());
|
||||
}
|
||||
}
|
||||
CommentRanges::new(ranges)
|
||||
}
|
||||
}
|
||||
|
||||
/// Control in the different modes by which a source file can be parsed.
|
||||
///
|
||||
/// The mode argument specifies in what way code must be parsed.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue