Introduce SourceCodeSlice to reduce the size of FormatElement (#4622)

Introduce `SourceCodeSlice` to reduce the size of `FormatElement`
This commit is contained in:
Micha Reiser 2023-05-24 17:04:52 +02:00 committed by GitHub
parent 6943beee66
commit 86ced3516b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
26 changed files with 408 additions and 171 deletions

View file

@ -1,36 +1,34 @@
use std::rc::Rc;
use ruff_formatter::{FormatContext, SimpleFormatOptions};
use ruff_formatter::{FormatContext, SimpleFormatOptions, SourceCode};
use ruff_python_ast::source_code::Locator;
pub struct ASTFormatContext {
#[derive(Clone, Debug)]
pub struct ASTFormatContext<'source> {
options: SimpleFormatOptions,
contents: Rc<str>,
contents: &'source str,
}
impl ASTFormatContext {
pub fn new(options: SimpleFormatOptions, contents: &str) -> Self {
Self {
options,
contents: Rc::from(contents),
}
impl<'source> ASTFormatContext<'source> {
pub fn new(options: SimpleFormatOptions, contents: &'source str) -> Self {
Self { options, contents }
}
pub fn contents(&self) -> &'source str {
self.contents
}
pub fn locator(&self) -> Locator<'source> {
Locator::new(self.contents)
}
}
impl FormatContext for ASTFormatContext {
impl FormatContext for ASTFormatContext<'_> {
type Options = SimpleFormatOptions;
fn options(&self) -> &Self::Options {
&self.options
}
}
impl ASTFormatContext {
pub fn contents(&self) -> Rc<str> {
self.contents.clone()
}
pub fn locator(&self) -> Locator {
Locator::new(&self.contents)
fn source_code(&self) -> SourceCode {
SourceCode::new(self.contents)
}
}