pub struct Formatter<'a> { /* private fields */ }Expand description
Formatter for PHP code.
The Formatter is the main entry point for formatting PHP code. It allows for:
- Building an AST representation of the code
- Converting that AST into a document model
- Printing the document as a formatted string
Implementations§
Source§impl<'a> Formatter<'a>
impl<'a> Formatter<'a>
Sourcepub fn new(
interner: &'a ThreadedInterner,
php_version: PHPVersion,
settings: FormatSettings,
) -> Self
pub fn new( interner: &'a ThreadedInterner, php_version: PHPVersion, settings: FormatSettings, ) -> Self
Sourcepub fn format_code(
&self,
name: &'a str,
code: &'a str,
) -> Result<String, ParseError>
pub fn format_code( &self, name: &'a str, code: &'a str, ) -> Result<String, ParseError>
Formats PHP code provided as a string.
This method parses the provided code string into an AST and then formats it. It’s a convenient way to format code snippets without manually creating a Source object.
§Arguments
name- A name for the code snippet (used for error reporting).code- The PHP code to format as a string.
§Returns
A Result containing either the formatted code as a string or a ParseError if the code couldn’t be parsed.
Sourcepub fn format_source(&self, source: &'a Source) -> Result<String, ParseError>
pub fn format_source(&self, source: &'a Source) -> Result<String, ParseError>
Formats PHP code from a Source object.
This method parses the provided Source into an AST and then formats it. This is useful when you already have a Source object but not a parsed AST.
§Arguments
source- The Source object containing the PHP code to format.
§Returns
A Result containing either the formatted code as a string or a ParseError if the code couldn’t be parsed.
Sourcepub fn build(&self, source: &'a Source, program: &'a Program) -> Document<'a>
pub fn build(&self, source: &'a Source, program: &'a Program) -> Document<'a>
Builds a document model from a program AST.
This method converts the AST into a document model that represents the logical structure of the formatted code.
§Arguments
source- The source to use for the program.program- AProgramstruct representing the AST of the program to build.
§Returns
A Document representing the structured format of the program.
Sourcepub fn print(
&self,
document: Document<'a>,
capacity_hint: Option<usize>,
) -> String
pub fn print( &self, document: Document<'a>, capacity_hint: Option<usize>, ) -> String
Prints a document model as a formatted string.
This method takes a document model and renders it as formatted text according to the formatter settings.
§Arguments
document- The document model to print.capacity_hint- An optional hint for pre-allocating the output buffer size. When available (e.g., from source code), this improves performance.
§Returns
The formatted document as a string.
Trait Implementations§
Auto Trait Implementations§
impl<'a> Freeze for Formatter<'a>
impl<'a> !RefUnwindSafe for Formatter<'a>
impl<'a> Send for Formatter<'a>
impl<'a> Sync for Formatter<'a>
impl<'a> Unpin for Formatter<'a>
impl<'a> !UnwindSafe for Formatter<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more