mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-26 13:24:40 +00:00
Make Locator available in AST-to-CST conversion pass (#3194)
This commit is contained in:
parent
198b301baf
commit
eb15371453
2 changed files with 316 additions and 136 deletions
|
@ -1,7 +1,8 @@
|
|||
use anyhow::Result;
|
||||
use ruff_formatter::{format, Formatted, IndentStyle, SimpleFormatOptions};
|
||||
use rustpython_parser::lexer::LexResult;
|
||||
|
||||
use ruff_formatter::{format, Formatted, IndentStyle, SimpleFormatOptions};
|
||||
|
||||
use crate::attachment::attach;
|
||||
use crate::context::ASTFormatContext;
|
||||
use crate::core::locator::Locator;
|
||||
|
@ -35,7 +36,10 @@ pub fn fmt(contents: &str) -> Result<Formatted<ASTFormatContext>> {
|
|||
let python_ast = ruff_rustpython::parse_program_tokens(tokens, "<filename>")?;
|
||||
|
||||
// Convert to a CST.
|
||||
let mut python_cst: Vec<Stmt> = python_ast.into_iter().map(Into::into).collect();
|
||||
let mut python_cst: Vec<Stmt> = python_ast
|
||||
.into_iter()
|
||||
.map(|stmt| (stmt, &locator).into())
|
||||
.collect();
|
||||
|
||||
// Attach trivia.
|
||||
attach(&mut python_cst, trivia);
|
||||
|
@ -57,15 +61,16 @@ pub fn fmt(contents: &str) -> Result<Formatted<ASTFormatContext>> {
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::fmt::{Formatter, Write};
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
|
||||
use anyhow::Result;
|
||||
use similar::TextDiff;
|
||||
|
||||
use ruff_testing_macros::fixture;
|
||||
|
||||
use crate::fmt;
|
||||
use ruff_testing_macros::fixture;
|
||||
use similar::TextDiff;
|
||||
use std::fmt::{Formatter, Write};
|
||||
|
||||
#[fixture(
|
||||
pattern = "resources/test/fixtures/black/**/*.py",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue