Expand description
Infrastructure for code formatting
This module defines FormatElement, an IR to format code documents and provides a means 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. Useful in the situation where it’s necessary to implement Format on an object from another crate. This module defines theFormatRefWithRuleandFormatOwnedWithRulestructs to pass an item with its corresponding rule.FormatWithRuleimplemented 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 formattable objectformat_args!: Concatenates a sequence of Format objects.write!: Writes a sequence of formattable 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::FormatElement;pub use format_element::LINE_TERMINATORS;pub use format_element::normalize_newlines;pub use group_id::GroupId;
Modules§
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
- A convenience wrapper for representing a formattable argument.
- Arguments
- Sequence of objects that should be formatted in the specified order.
- Best
Fitting - 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 - Format
Owned With Rule - Formats the
itemwith the specified rule. - Format
RefWith Rule - Formats the referenced
itemwith the specified rule. - Format
State - This structure stores the state that is relevant for the formatting of the whole document.
- Formatted
- Indent
Width - The visual width of an indentation.
- Inspect
- Buffer that allows you inspecting elements as they get written to the formatter.
- Line
Width - The maximum visual width to which the formatter should try to limit a line.
- Printed
- Printed
Range - Remove
Soft Lines Buffer - A Buffer that removes any soft line breaks or
if_group_breakselements. - Simple
Format Context - Simple
Format Options - Source
Code - The source code of a document that gets formatted
- Source
Code Slice - A slice into the source text of a document.
- Source
Marker - Lightweight sourcemap marker between source and output tokens
- VecBuffer
- Vector backed
Bufferimplementation.
Enums§
- Buffer
Snapshot - Snapshot of a buffer state that can be restored at a later point.
- Indent
Style
Traits§
- Buffer
- A trait for writing or formatting into
FormatElement-accepting buffers or streams. - Buffer
Extensions - Format
- Formatting trait for types that can create a formatted representation. The
ruff_formatterequivalent tostd::fmt::Display. - Format
Context - Context object storing data relevant when formatting an object.
- Format
Options - Options customizing how the source code should be formatted.
- Format
Rule - Rule that knows how to format an object of type
T. - Format
Rule With Options - Rule that supports customizing how it formats an object of type
T. - Format
With Rule - Trait for an object that formats an object with a specified rule.
Functions§
- format
- The
formatfunction takes anArgumentsstruct and returns the resulting formatting IR. - write
- The
writefunction takes a target buffer and anArgumentsstruct that can be precompiled with theformat_args!macro.
Type Aliases§
- Format
Result - Public return type of the formatter
- Print
Result