Crate rome_formatter

Source
Expand description

Infrastructure for code formatting

This module defines FormatElement, an IR to format code documents and provides a mean to print such a document to a string. Objects that know how to format themselves implement the Format trait.

§Formatting Traits

  • Format: Implemented by objects that can be formatted.
  • FormatRule: Rule that knows how to format an object of another type. Necessary in the situation where it’s necessary to implement Format on an object from another crate. This module defines the FormatRefWithRule and FormatOwnedWithRule structs to pass an item with its corresponding rule.
  • FormatWithRule implemented by objects that know how to format another type. Useful for implementing some reusable formatting logic inside of this module if the type itself doesn’t implement Format

§Formatting Macros

This crate defines two macros to construct the IR. These are inspired by Rust’s fmt macros

  • format!: Formats a formatable object
  • format_args!: Concatenates a sequence of Format objects.
  • write!: Writes a sequence of formatable objects into an output buffer.

Re-exports§

pub use crate::diagnostics::ActualStart;
pub use crate::diagnostics::FormatError;
pub use crate::diagnostics::InvalidDocumentError;
pub use crate::diagnostics::PrintError;
pub use format_element::normalize_newlines;
pub use format_element::FormatElement;
pub use format_element::LINE_TERMINATORS;
pub use group_id::GroupId;

Modules§

comments
Types for extracting and representing comments of a syntax tree.
diagnostics
format_element
formatter
group_id
macros
prelude
printed_tokens
printer
separated
token
trivia
Provides builders for comments and skipped token trivia.

Macros§

best_fitting
Provides multiple different alternatives and the printer picks the first one that fits. Use this as last resort because it requires that the printer must try all variants in the worst case. The passed variants must be in the following order:
dbg_write
Writes formatted data into the given buffer and prints all written elements for a quick and dirty debugging.
format
Creates the Format IR for a value.
format_args
Constructs the parameters for other formatting macros.
write
Writes formatted data into a buffer.

Structs§

Argument
Mono-morphed type to format an object. Used by the crate::format!, crate::format_args!, and crate::write! macros.
Arguments
Sequence of objects that should be formatted in the specified order.
BestFitting
The first variant is the most flat, and the last is the most expanded variant. See best_fitting! macro for a more in-detail documentation
FormatOwnedWithRule
Formats the item with the specified rule.
FormatRefWithRule
Formats the referenced item with the specified rule.
FormatState
This structure stores the state that is relevant for the formatting of the whole document.
FormatStateSnapshot
FormatToken
Default implementation for formatting a token
Formatted
Inspect
Buffer that allows you inspecting elements as they get written to the formatter.
LineWidth
Validated value for the line_width formatter options
LineWidthFromIntError
Error type returned when converting a u16 to a LineWidth fails
PreambleBuffer
This struct wraps an existing buffer and emits a preamble text when the first text is written.
Printed
RemoveSoftLinesBuffer
A Buffer that removes any soft line breaks.
SimpleFormatContext
SimpleFormatOptions
SourceMarker
Lightweight sourcemap marker between source and output tokens
TransformSourceMap
A source map for mapping positions of a pre-processed tree back to the locations in the source tree.
TransformSourceMapBuilder
Builder for creating a source map.
VecBuffer
Vector backed Buffer implementation.

Enums§

BufferSnapshot
Snapshot of a buffer state that can be restored at a later point.
IndentStyle
ParseLineWidthError
Error type returned when parsing a LineWidth from a string fails

Traits§

Buffer
A trait for writing or formatting into FormatElement-accepting buffers or streams.
BufferExtensions
CstFormatContext
The CstFormatContext is an extension of the CST unaware FormatContext and must be implemented by every language.
Format
Formatting trait for types that can create a formatted representation. The rome_formatter equivalent to std::fmt::Display.
FormatContext
Context object storing data relevant when formatting an object.
FormatLanguage
Entry point for formatting a SyntaxNode for a specific language.
FormatOptions
Options customizing how the source code should be formatted.
FormatRule
Rule that knows how to format an object of type T.
FormatRuleWithOptions
Rule that supports customizing how it formats an object of type T.
FormatWithRule
Trait for an object that formats an object with a specified rule.

Functions§

format
The format function takes an Arguments struct and returns the resulting formatting IR.
format_node
Formats a syntax node file based on its features.
format_range
Formats a range within a file, supported by Rome
format_sub_tree
Formats a single node within a file, supported by Rome.
write
The write function takes a target buffer and an Arguments struct that can be precompiled with the format_args! macro.

Type Aliases§

FormatResult
Public return type of the formatter
PrintResult