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

@ -33,6 +33,7 @@ pub mod group_id;
pub mod macros;
pub mod prelude;
pub mod printer;
mod source_code;
use crate::formatter::Formatter;
use crate::group_id::UniqueGroupIdBuilder;
@ -47,6 +48,7 @@ pub use buffer::{
VecBuffer,
};
pub use builders::BestFitting;
pub use source_code::{SourceCode, SourceCodeSlice};
pub use crate::diagnostics::{ActualStart, FormatError, InvalidDocumentError, PrintError};
pub use format_element::{normalize_newlines, FormatElement, LINE_TERMINATORS};
@ -202,6 +204,9 @@ pub trait FormatContext {
/// Returns the formatting options
fn options(&self) -> &Self::Options;
/// Returns the source code from the document that gets formatted.
fn source_code(&self) -> SourceCode;
}
/// Options customizing how the source code should be formatted.
@ -219,11 +224,20 @@ pub trait FormatOptions {
#[derive(Debug, Default, Eq, PartialEq)]
pub struct SimpleFormatContext {
options: SimpleFormatOptions,
source_code: String,
}
impl SimpleFormatContext {
pub fn new(options: SimpleFormatOptions) -> Self {
Self { options }
Self {
options,
source_code: String::new(),
}
}
pub fn with_source_code(mut self, code: &str) -> Self {
self.source_code = String::from(code);
self
}
}
@ -233,9 +247,13 @@ impl FormatContext for SimpleFormatContext {
fn options(&self) -> &Self::Options {
&self.options
}
fn source_code(&self) -> SourceCode {
SourceCode::new(&self.source_code)
}
}
#[derive(Debug, Default, Eq, PartialEq)]
#[derive(Debug, Default, Eq, PartialEq, Clone)]
pub struct SimpleFormatOptions {
pub indent_style: IndentStyle,
pub line_width: LineWidth,
@ -302,15 +320,18 @@ where
Context: FormatContext,
{
pub fn print(&self) -> PrintResult<Printed> {
let source_code = self.context.source_code();
let print_options = self.context.options().as_print_options();
let printed = Printer::new(print_options).print(&self.document)?;
let printed = Printer::new(source_code, print_options).print(&self.document)?;
Ok(printed)
}
pub fn print_with_indent(&self, indent: u16) -> PrintResult<Printed> {
let source_code = self.context.source_code();
let print_options = self.context.options().as_print_options();
let printed = Printer::new(print_options).print_with_indent(&self.document, indent)?;
let printed =
Printer::new(source_code, print_options).print_with_indent(&self.document, indent)?;
Ok(printed)
}