Skip to content

Commit 23ef4b7

Browse files
refactor(chains): encapsulate shared code, prep for overflows
1 parent 3fa81c6 commit 23ef4b7

File tree

1 file changed

+77
-22
lines changed

1 file changed

+77
-22
lines changed

src/chains.rs

+77-22
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,60 @@ use crate::utils::{
7474
rewrite_ident, trimmed_last_line_width, wrap_str,
7575
};
7676

77+
/// Provides the original input contents from the span
78+
/// of a chain element with trailing spaces trimmed.
79+
fn format_overflow_style(span: Span, context: &RewriteContext<'_>) -> Option<String> {
80+
context.snippet_provider.span_to_snippet(span).map(|s| {
81+
s.lines()
82+
.map(|l| l.trim_end())
83+
.collect::<Vec<_>>()
84+
.join("\n")
85+
})
86+
}
87+
88+
fn format_chain_item(
89+
item: &ChainItem,
90+
context: &RewriteContext<'_>,
91+
rewrite_shape: Shape,
92+
allow_overflow: bool,
93+
) -> Option<String> {
94+
if allow_overflow {
95+
item.rewrite(context, rewrite_shape)
96+
.or_else(|| format_overflow_style(item.span, context))
97+
} else {
98+
item.rewrite(context, rewrite_shape)
99+
}
100+
}
101+
102+
fn get_block_child_shape(
103+
prev_ends_with_block: bool,
104+
context: &RewriteContext<'_>,
105+
shape: Shape,
106+
) -> Shape {
107+
if prev_ends_with_block {
108+
shape.block_indent(0)
109+
} else {
110+
shape.block_indent(context.config.tab_spaces())
111+
}
112+
.with_max_width(context.config)
113+
}
114+
115+
fn get_visual_style_child_shape(
116+
context: &RewriteContext<'_>,
117+
shape: Shape,
118+
offset: usize,
119+
parent_overflowing: bool,
120+
) -> Option<Shape> {
121+
if !parent_overflowing {
122+
shape
123+
.with_max_width(context.config)
124+
.offset_left(offset)
125+
.map(|s| s.visual_indent(0))
126+
} else {
127+
Some(shape.visual_indent(offset))
128+
}
129+
}
130+
77131
pub(crate) fn rewrite_chain(
78132
expr: &ast::Expr,
79133
context: &RewriteContext<'_>,
@@ -498,6 +552,8 @@ struct ChainFormatterShared<'a> {
498552
// The number of children in the chain. This is not equal to `self.children.len()`
499553
// because `self.children` will change size as we process the chain.
500554
child_count: usize,
555+
// Whether elements are allowed to overflow past the max_width limit
556+
allow_overflow: bool,
501557
}
502558

503559
impl<'a> ChainFormatterShared<'a> {
@@ -507,6 +563,8 @@ impl<'a> ChainFormatterShared<'a> {
507563
rewrites: Vec::with_capacity(chain.children.len() + 1),
508564
fits_single_line: false,
509565
child_count: chain.children.len(),
566+
// TODO(calebcartwright)
567+
allow_overflow: false,
510568
}
511569
}
512570

@@ -519,6 +577,14 @@ impl<'a> ChainFormatterShared<'a> {
519577
}
520578
}
521579

580+
fn format_children(&mut self, context: &RewriteContext<'_>, child_shape: Shape) -> Option<()> {
581+
for item in &self.children[..self.children.len() - 1] {
582+
let rewrite = format_chain_item(item, context, child_shape, self.allow_overflow)?;
583+
self.rewrites.push(rewrite);
584+
}
585+
Some(())
586+
}
587+
522588
// Rewrite the last child. The last child of a chain requires special treatment. We need to
523589
// know whether 'overflowing' the last child make a better formatting:
524590
//
@@ -731,22 +797,12 @@ impl<'a> ChainFormatter for ChainFormatterBlock<'a> {
731797
}
732798

733799
fn child_shape(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<Shape> {
734-
Some(
735-
if self.root_ends_with_block {
736-
shape.block_indent(0)
737-
} else {
738-
shape.block_indent(context.config.tab_spaces())
739-
}
740-
.with_max_width(context.config),
741-
)
800+
let block_end = self.root_ends_with_block;
801+
Some(get_block_child_shape(block_end, context, shape))
742802
}
743803

744804
fn format_children(&mut self, context: &RewriteContext<'_>, child_shape: Shape) -> Option<()> {
745-
for item in &self.shared.children[..self.shared.children.len() - 1] {
746-
let rewrite = item.rewrite(context, child_shape)?;
747-
self.shared.rewrites.push(rewrite);
748-
}
749-
Some(())
805+
self.shared.format_children(context, child_shape)
750806
}
751807

752808
fn format_last_child(
@@ -828,18 +884,17 @@ impl<'a> ChainFormatter for ChainFormatterVisual<'a> {
828884
}
829885

830886
fn child_shape(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<Shape> {
831-
shape
832-
.with_max_width(context.config)
833-
.offset_left(self.offset)
834-
.map(|s| s.visual_indent(0))
887+
get_visual_style_child_shape(
888+
context,
889+
shape,
890+
self.offset,
891+
// TODO(calebcartwright): self.shared.permissibly_overflowing_parent,
892+
false,
893+
)
835894
}
836895

837896
fn format_children(&mut self, context: &RewriteContext<'_>, child_shape: Shape) -> Option<()> {
838-
for item in &self.shared.children[..self.shared.children.len() - 1] {
839-
let rewrite = item.rewrite(context, child_shape)?;
840-
self.shared.rewrites.push(rewrite);
841-
}
842-
Some(())
897+
self.shared.format_children(context, child_shape)
843898
}
844899

845900
fn format_last_child(

0 commit comments

Comments
 (0)