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:
Dhruv Manilawala 2024-06-09 15:25:17 +05:30 committed by GitHub
parent 7509a48eab
commit 549cc1e437
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
28 changed files with 151 additions and 102 deletions

View file

@ -1,4 +1,5 @@
use ruff_python_parser::{parse_unchecked, Mode};
use ruff_python_trivia::CommentRanges;
use ruff_source_file::Locator;
use ruff_text_size::TextSize;
@ -8,9 +9,10 @@ fn block_comments_two_line_block_at_start() {
let source = "# line 1\n# line 2\n";
let parsed = parse_unchecked(source, Mode::Module);
let locator = Locator::new(source);
let comment_ranges = CommentRanges::from(parsed.tokens());
// act
let block_comments = parsed.comment_ranges().block_comments(&locator);
let block_comments = comment_ranges.block_comments(&locator);
// assert
assert_eq!(block_comments, vec![TextSize::new(0), TextSize::new(9)]);
@ -22,9 +24,10 @@ fn block_comments_indented_block() {
let source = " # line 1\n # line 2\n";
let parsed = parse_unchecked(source, Mode::Module);
let locator = Locator::new(source);
let comment_ranges = CommentRanges::from(parsed.tokens());
// act
let block_comments = parsed.comment_ranges().block_comments(&locator);
let block_comments = comment_ranges.block_comments(&locator);
// assert
assert_eq!(block_comments, vec![TextSize::new(4), TextSize::new(17)]);
@ -36,9 +39,10 @@ fn block_comments_single_line_is_not_a_block() {
let source = "\n";
let parsed = parse_unchecked(source, Mode::Module);
let locator = Locator::new(source);
let comment_ranges = CommentRanges::from(parsed.tokens());
// act
let block_comments = parsed.comment_ranges().block_comments(&locator);
let block_comments = comment_ranges.block_comments(&locator);
// assert
assert_eq!(block_comments, Vec::<TextSize>::new());
@ -50,9 +54,10 @@ fn block_comments_lines_with_code_not_a_block() {
let source = "x = 1 # line 1\ny = 2 # line 2\n";
let parsed = parse_unchecked(source, Mode::Module);
let locator = Locator::new(source);
let comment_ranges = CommentRanges::from(parsed.tokens());
// act
let block_comments = parsed.comment_ranges().block_comments(&locator);
let block_comments = comment_ranges.block_comments(&locator);
// assert
assert_eq!(block_comments, Vec::<TextSize>::new());
@ -64,9 +69,10 @@ fn block_comments_sequential_lines_not_in_block() {
let source = " # line 1\n # line 2\n";
let parsed = parse_unchecked(source, Mode::Module);
let locator = Locator::new(source);
let comment_ranges = CommentRanges::from(parsed.tokens());
// act
let block_comments = parsed.comment_ranges().block_comments(&locator);
let block_comments = comment_ranges.block_comments(&locator);
// assert
assert_eq!(block_comments, Vec::<TextSize>::new());
@ -83,9 +89,10 @@ fn block_comments_lines_in_triple_quotes_not_a_block() {
"#;
let parsed = parse_unchecked(source, Mode::Module);
let locator = Locator::new(source);
let comment_ranges = CommentRanges::from(parsed.tokens());
// act
let block_comments = parsed.comment_ranges().block_comments(&locator);
let block_comments = comment_ranges.block_comments(&locator);
// assert
assert_eq!(block_comments, Vec::<TextSize>::new());
@ -119,9 +126,10 @@ y = 2 # do not form a block comment
"#;
let parsed = parse_unchecked(source, Mode::Module);
let locator = Locator::new(source);
let comment_ranges = CommentRanges::from(parsed.tokens());
// act
let block_comments = parsed.comment_ranges().block_comments(&locator);
let block_comments = comment_ranges.block_comments(&locator);
// assert
assert_eq!(

View file

@ -1,7 +1,7 @@
use insta::assert_debug_snapshot;
use ruff_python_parser::{parse_unchecked, Mode};
use ruff_python_trivia::{lines_after, lines_before, SimpleToken, SimpleTokenizer};
use ruff_python_trivia::{lines_after, lines_before, CommentRanges, SimpleToken, SimpleTokenizer};
use ruff_python_trivia::{BackwardsTokenizer, SimpleTokenKind};
use ruff_text_size::{TextLen, TextRange, TextSize};
@ -23,7 +23,8 @@ impl TokenizationTestCase {
fn tokenize_reverse(&self) -> Vec<SimpleToken> {
let parsed = parse_unchecked(self.source, Mode::Module);
BackwardsTokenizer::new(self.source, self.range, parsed.comment_ranges()).collect()
let comment_ranges = CommentRanges::from(parsed.tokens());
BackwardsTokenizer::new(self.source, self.range, &comment_ranges).collect()
}
fn tokens(&self) -> &[SimpleToken] {