Make ruff::source_code::{Generator, Locator, Stylist} private

This commit is contained in:
Martin Fischer
2023-01-14 20:21:30 +01:00
committed by Charlie Marsh
parent d77675f30d
commit a81ac6705d
3 changed files with 19 additions and 11 deletions

View File

@@ -25,12 +25,14 @@ impl<'a> Locator<'a> {
self.rope.get_or_init(|| Rope::from_str(self.contents))
}
#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn slice_source_code_at(&self, location: &Location) -> Cow<'_, str> {
let rope = self.get_or_init_rope();
let offset = rope.line_to_char(location.row() - 1) + location.column();
Cow::from(rope.slice(offset..))
}
#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn slice_source_code_until(&self, location: &Location) -> Cow<'_, str> {
let rope = self.get_or_init_rope();
let offset = rope.line_to_char(location.row() - 1) + location.column();

View File

@@ -2,6 +2,18 @@ mod generator;
mod locator;
mod stylist;
pub use generator::Generator;
pub use locator::Locator;
pub use stylist::{LineEnding, Stylist};
pub(crate) use generator::Generator;
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())
}