mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 18:28:24 +00:00
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:
parent
31032f4f70
commit
c2ec5f0bc9
4 changed files with 25 additions and 17 deletions
|
@ -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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue