mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-25 04:45:13 +00:00
perf(pycodestyle): Initialize Stylist from tokens (#3757)
This commit is contained in:
parent
000394f428
commit
f68c26a506
14 changed files with 200 additions and 172 deletions
|
@ -7,14 +7,15 @@ pub use generator::Generator;
|
|||
pub use indexer::Indexer;
|
||||
pub use locator::Locator;
|
||||
use rustpython_parser as parser;
|
||||
use rustpython_parser::ParseError;
|
||||
use rustpython_parser::{lexer, Mode, ParseError};
|
||||
pub 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 tokens: Vec<_> = lexer::lex(code, Mode::Module).collect();
|
||||
let stylist = Stylist::from_tokens(&tokens, &locator);
|
||||
let mut generator: Generator = (&stylist).into();
|
||||
generator.unparse_suite(&python_ast);
|
||||
Ok(generator.generate())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue