Skip to content

Commit

Permalink
refactor(chains): encapsulate shared code, prep for overflows
Browse files Browse the repository at this point in the history
  • Loading branch information
calebcartwright committed Aug 1, 2022
1 parent 3fa81c6 commit 23ef4b7
Showing 1 changed file with 77 additions and 22 deletions.
99 changes: 77 additions & 22 deletions src/chains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,60 @@ use crate::utils::{
rewrite_ident, trimmed_last_line_width, wrap_str,
};

/// Provides the original input contents from the span
/// of a chain element with trailing spaces trimmed.
fn format_overflow_style(span: Span, context: &RewriteContext<'_>) -> Option<String> {
context.snippet_provider.span_to_snippet(span).map(|s| {
s.lines()
.map(|l| l.trim_end())
.collect::<Vec<_>>()
.join("\n")
})
}

fn format_chain_item(
item: &ChainItem,
context: &RewriteContext<'_>,
rewrite_shape: Shape,
allow_overflow: bool,
) -> Option<String> {
if allow_overflow {
item.rewrite(context, rewrite_shape)
.or_else(|| format_overflow_style(item.span, context))
} else {
item.rewrite(context, rewrite_shape)
}
}

fn get_block_child_shape(
prev_ends_with_block: bool,
context: &RewriteContext<'_>,
shape: Shape,
) -> Shape {
if prev_ends_with_block {
shape.block_indent(0)
} else {
shape.block_indent(context.config.tab_spaces())
}
.with_max_width(context.config)
}

fn get_visual_style_child_shape(
context: &RewriteContext<'_>,
shape: Shape,
offset: usize,
parent_overflowing: bool,
) -> Option<Shape> {
if !parent_overflowing {
shape
.with_max_width(context.config)
.offset_left(offset)
.map(|s| s.visual_indent(0))
} else {
Some(shape.visual_indent(offset))
}
}

pub(crate) fn rewrite_chain(
expr: &ast::Expr,
context: &RewriteContext<'_>,
Expand Down Expand Up @@ -498,6 +552,8 @@ struct ChainFormatterShared<'a> {
// The number of children in the chain. This is not equal to `self.children.len()`
// because `self.children` will change size as we process the chain.
child_count: usize,
// Whether elements are allowed to overflow past the max_width limit
allow_overflow: bool,
}

impl<'a> ChainFormatterShared<'a> {
Expand All @@ -507,6 +563,8 @@ impl<'a> ChainFormatterShared<'a> {
rewrites: Vec::with_capacity(chain.children.len() + 1),
fits_single_line: false,
child_count: chain.children.len(),
// TODO(calebcartwright)
allow_overflow: false,
}
}

Expand All @@ -519,6 +577,14 @@ impl<'a> ChainFormatterShared<'a> {
}
}

fn format_children(&mut self, context: &RewriteContext<'_>, child_shape: Shape) -> Option<()> {
for item in &self.children[..self.children.len() - 1] {
let rewrite = format_chain_item(item, context, child_shape, self.allow_overflow)?;
self.rewrites.push(rewrite);
}
Some(())
}

// Rewrite the last child. The last child of a chain requires special treatment. We need to
// know whether 'overflowing' the last child make a better formatting:
//
Expand Down Expand Up @@ -731,22 +797,12 @@ impl<'a> ChainFormatter for ChainFormatterBlock<'a> {
}

fn child_shape(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<Shape> {
Some(
if self.root_ends_with_block {
shape.block_indent(0)
} else {
shape.block_indent(context.config.tab_spaces())
}
.with_max_width(context.config),
)
let block_end = self.root_ends_with_block;
Some(get_block_child_shape(block_end, context, shape))
}

fn format_children(&mut self, context: &RewriteContext<'_>, child_shape: Shape) -> Option<()> {
for item in &self.shared.children[..self.shared.children.len() - 1] {
let rewrite = item.rewrite(context, child_shape)?;
self.shared.rewrites.push(rewrite);
}
Some(())
self.shared.format_children(context, child_shape)
}

fn format_last_child(
Expand Down Expand Up @@ -828,18 +884,17 @@ impl<'a> ChainFormatter for ChainFormatterVisual<'a> {
}

fn child_shape(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<Shape> {
shape
.with_max_width(context.config)
.offset_left(self.offset)
.map(|s| s.visual_indent(0))
get_visual_style_child_shape(
context,
shape,
self.offset,
// TODO(calebcartwright): self.shared.permissibly_overflowing_parent,
false,
)
}

fn format_children(&mut self, context: &RewriteContext<'_>, child_shape: Shape) -> Option<()> {
for item in &self.shared.children[..self.shared.children.len() - 1] {
let rewrite = item.rewrite(context, child_shape)?;
self.shared.rewrites.push(rewrite);
}
Some(())
self.shared.format_children(context, child_shape)
}

fn format_last_child(
Expand Down

0 comments on commit 23ef4b7

Please sign in to comment.