mirror of
https://github.com/astral-sh/ruff
synced 2026-01-21 13:30:49 -05:00
The primary motivation is that we can now robustly detect `\` continuations due to the addition of `Tok::NonLogicalNewline`. This PR generalizes the approach we took to comments (track all lines that contain any comments), and applies it to continuations too.
22 lines
713 B
Rust
22 lines
713 B
Rust
mod generator;
|
|
mod indexer;
|
|
mod locator;
|
|
mod stylist;
|
|
|
|
pub(crate) use generator::Generator;
|
|
pub(crate) use indexer::Indexer;
|
|
pub(crate) use locator::Locator;
|
|
use rustpython_parser::error::ParseError;
|
|
use rustpython_parser::parser;
|
|
pub(crate) use stylist::{LineEnding, Stylist};
|
|
|
|
/// Run round-trip source code generation on a given Python code.
|
|
pub fn round_trip(code: &str, source_path: &str) -> Result<String, ParseError> {
|
|
let locator = Locator::new(code);
|
|
let python_ast = parser::parse_program(code, source_path)?;
|
|
let stylist = Stylist::from_contents(code, &locator);
|
|
let mut generator: Generator = (&stylist).into();
|
|
generator.unparse_suite(&python_ast);
|
|
Ok(generator.generate())
|
|
}
|