Formatter

Struct Formatter 

Source
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>

Source

pub fn new( interner: &'a ThreadedInterner, php_version: PHPVersion, settings: FormatSettings, ) -> Self

Creates a new Formatter instance.

§Arguments
  • interner - The interner to use for string interning.
  • php_version - The PHP version to target when formatting.
  • settings - The settings to use for formatting.
§Returns

A new Formatter instance configured with the given parameters.

Source

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.

Source

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.

Source

pub fn format(&self, source: &'a Source, program: &'a Program) -> String

Formats a PHP program.

This is a convenience method that combines build and print into a single operation.

§Arguments
  • source - The source to use for the program.
  • program - A Program struct representing the AST of the program to format.
§Returns

The formatted program as a string.

Source

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 - A Program struct representing the AST of the program to build.
§Returns

A Document representing the structured format of the program.

Source

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§

Source§

impl<'a> Debug for Formatter<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more