Use source type to determine parser mode for formatting (#8205)
## Summary This PR fixes the bug where if a Notebook contained IPython syntax, then the format command would fail. This was because the correct mode was not being used while parsing through the formatter code path. ## Test Plan This PR isn't the only requirement for Notebook formatting to start working with IPython escape commands. The following PR in the stack is required as well.
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
use std::fmt::Debug;
|
||||
|
||||
use ruff_python_ast::PySourceType;
|
||||
use ruff_python_parser::lexer::{lex, LexicalError};
|
||||
use ruff_python_parser::{Mode, Tok};
|
||||
use ruff_python_parser::{AsMode, Tok};
|
||||
use ruff_python_trivia::CommentRanges;
|
||||
use ruff_text_size::TextRange;
|
||||
|
||||
@@ -25,11 +26,12 @@ impl CommentRangesBuilder {
|
||||
/// Helper method to lex and extract comment ranges
|
||||
pub fn tokens_and_ranges(
|
||||
source: &str,
|
||||
source_type: PySourceType,
|
||||
) -> Result<(Vec<(Tok, TextRange)>, CommentRanges), LexicalError> {
|
||||
let mut tokens = Vec::new();
|
||||
let mut comment_ranges = CommentRangesBuilder::default();
|
||||
|
||||
for result in lex(source, Mode::Module) {
|
||||
for result in lex(source, source_type.as_mode()) {
|
||||
let (token, range) = result?;
|
||||
|
||||
comment_ranges.visit_token(&token, range);
|
||||
|
||||
Reference in New Issue
Block a user