@@ -74,6 +74,60 @@ use crate::utils::{
74
74
rewrite_ident, trimmed_last_line_width, wrap_str,
75
75
};
76
76
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
+
77
131
pub(crate) fn rewrite_chain(
78
132
expr: &ast::Expr,
79
133
context: &RewriteContext<'_>,
@@ -498,6 +552,8 @@ struct ChainFormatterShared<'a> {
498
552
// The number of children in the chain. This is not equal to `self.children.len()`
499
553
// because `self.children` will change size as we process the chain.
500
554
child_count: usize,
555
+ // Whether elements are allowed to overflow past the max_width limit
556
+ allow_overflow: bool,
501
557
}
502
558
503
559
impl<'a> ChainFormatterShared<'a> {
@@ -507,6 +563,8 @@ impl<'a> ChainFormatterShared<'a> {
507
563
rewrites: Vec::with_capacity(chain.children.len() + 1),
508
564
fits_single_line: false,
509
565
child_count: chain.children.len(),
566
+ // TODO(calebcartwright)
567
+ allow_overflow: false,
510
568
}
511
569
}
512
570
@@ -519,6 +577,14 @@ impl<'a> ChainFormatterShared<'a> {
519
577
}
520
578
}
521
579
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
+
522
588
// Rewrite the last child. The last child of a chain requires special treatment. We need to
523
589
// know whether 'overflowing' the last child make a better formatting:
524
590
//
@@ -731,22 +797,12 @@ impl<'a> ChainFormatter for ChainFormatterBlock<'a> {
731
797
}
732
798
733
799
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))
742
802
}
743
803
744
804
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)
750
806
}
751
807
752
808
fn format_last_child(
@@ -828,18 +884,17 @@ impl<'a> ChainFormatter for ChainFormatterVisual<'a> {
828
884
}
829
885
830
886
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
+ )
835
894
}
836
895
837
896
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)
843
898
}
844
899
845
900
fn format_last_child(
0 commit comments