Expand description
Formatting of chained expressions, i.e., expressions that are chained by
dots: struct and enum field access, method calls, and try shorthand (?
).
Instead of walking these subexpressions one-by-one, as is our usual strategy for expression formatting, we collect maximal sequences of these expressions and handle them simultaneously.
Whenever possible, the entire chain is put on a single line. If that fails, we put each subexpression on a separate, much like the (default) function argument function argument strategy.
Depends on config options: chain_indent
is the indent to use for
blocks in the parent/root/base of the chain (and the rest of the chain’s
alignment).
E.g., let foo = { aaaa; bbb; ccc }.bar.baz();
, we would layout for the
following values of chain_indent
:
Block:
let foo = {
aaaa;
bbb;
ccc
}.bar
.baz();
Visual:
let foo = {
aaaa;
bbb;
ccc
}
.bar
.baz();
If the first item in the chain is a block expression, we align the dots with the braces. Block:
let a = foo.bar
.baz()
.qux
Visual:
let a = foo.bar
.baz()
.qux
Structs§
- Chain 🔒
- Chain
Formatter 🔒Block - Chain
Formatter 🔒Shared - Chain
Formatter 🔒Visual - Chain
Item 🔒 - An expression plus trailing
?
s to be formatted together. - SubExpr 🔒
- Information about an expression in a chain.
Enums§
Traits§
Functions§
- format_
chain_ 🔒item - format_
overflow_ 🔒style - Provides the original input contents from the span of a chain element with trailing spaces trimmed.
- get_
block_ 🔒child_ shape - get_
visual_ 🔒style_ child_ shape - rewrite_
chain 🔒 - should_
add_ 🔒parens - Whether a method call’s receiver needs parenthesis, like
- trim_
tries 🔒 - Removes try operators (
?
s) that appear in the given string. If removing them leaves an empty line, remove that line as well unless it is the first line (we need the first newline for detecting pre/post comment).