Skip to content

Commit

Permalink
Return Result from Shape methods
Browse files Browse the repository at this point in the history
  • Loading branch information
camsteffen committed Nov 27, 2024
1 parent b902add commit 9343d96
Show file tree
Hide file tree
Showing 19 changed files with 287 additions and 339 deletions.
33 changes: 14 additions & 19 deletions src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,22 @@ fn argument_shape(
shape: Shape,
context: &RewriteContext<'_>,
) -> Option<Shape> {
match context.config.indent_style() {
let shape = match context.config.indent_style() {
IndentStyle::Block => {
if combine {
shape.offset_left(left)
shape.offset_left_opt(left)?
} else {
Some(
shape
.block_indent(context.config.tab_spaces())
.with_max_width(context.config),
)
shape
.block_indent(context.config.tab_spaces())
.with_max_width(context.config)
}
}
IndentStyle::Visual => shape
.visual_indent(0)
.shrink_left(left)
.and_then(|s| s.sub_width(right)),
}
.shrink_left_opt(left)?
.sub_width_opt(right)?,
};
Some(shape)
}

fn format_derive(
Expand Down Expand Up @@ -127,8 +126,8 @@ fn format_derive(
context,
)?;
let one_line_shape = shape
.offset_left("[derive()]".len() + prefix.len())?
.sub_width("()]".len())?;
.offset_left_opt("[derive()]".len() + prefix.len())?
.sub_width_opt("()]".len())?;
let one_line_budget = one_line_shape.width;

let tactic = definitive_tactic(
Expand Down Expand Up @@ -297,7 +296,7 @@ impl Rewrite for ast::MetaItem {
&path,
list.iter(),
// 1 = "]"
shape.sub_width(1).max_width_error(shape.width, self.span)?,
shape.sub_width(1, self.span)?,
self.span,
context.config.attr_fn_like_width(),
Some(if has_trailing_comma {
Expand All @@ -310,9 +309,7 @@ impl Rewrite for ast::MetaItem {
ast::MetaItemKind::NameValue(ref lit) => {
let path = rewrite_path(context, PathContext::Type, &None, &self.path, shape)?;
// 3 = ` = `
let lit_shape = shape
.shrink_left(path.len() + 3)
.max_width_error(shape.width, self.span)?;
let lit_shape = shape.shrink_left(path.len() + 3, self.span)?;
// `rewrite_literal` returns `None` when `lit` exceeds max
// width. Since a literal is basically unformattable unless it
// is a string literal (and only if `format_strings` is set),
Expand Down Expand Up @@ -369,9 +366,7 @@ impl Rewrite for ast::Attribute {
}

// 1 = `[`
let shape = shape
.offset_left(prefix.len() + 1)
.max_width_error(shape.width, self.span)?;
let shape = shape.offset_left(prefix.len() + 1, self.span)?;
Ok(meta.rewrite_result(context, shape).map_or_else(
|_| snippet.to_owned(),
|rw| match &self.kind {
Expand Down
77 changes: 41 additions & 36 deletions src/chains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ use crate::config::{IndentStyle, StyleEdition};
use crate::expr::rewrite_call;
use crate::lists::extract_pre_comment;
use crate::macros::convert_try_mac;
use crate::rewrite::{Rewrite, RewriteContext, RewriteError, RewriteErrorExt, RewriteResult};
use crate::rewrite::{
ExceedsMaxWidthError, Rewrite, RewriteContext, RewriteError, RewriteErrorExt, RewriteResult,
};
use crate::shape::Shape;
use crate::source_map::SpanUtils;
use crate::utils::{
Expand Down Expand Up @@ -127,14 +129,15 @@ fn get_visual_style_child_shape(
shape: Shape,
offset: usize,
parent_overflowing: bool,
) -> Option<Shape> {
span: Span,
) -> Result<Shape, ExceedsMaxWidthError> {
if !parent_overflowing {
shape
.with_max_width(context.config)
.offset_left(offset)
.offset_left(offset, span)
.map(|s| s.visual_indent(0))
} else {
Some(shape.visual_indent(offset))
Ok(shape.visual_indent(offset))
}
}

Expand Down Expand Up @@ -280,9 +283,7 @@ impl Rewrite for ChainItem {
}

fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
let shape = shape
.sub_width(self.tries)
.max_width_error(shape.width, self.span)?;
let shape = shape.sub_width(self.tries, self.span)?;
let rewrite = match self.kind {
ChainItemKind::Parent {
ref expr,
Expand Down Expand Up @@ -559,9 +560,7 @@ impl Rewrite for Chain {
let full_span = self.parent.span.with_hi(children_span.hi());

// Decide how to layout the rest of the chain.
let child_shape = formatter
.child_shape(context, shape)
.max_width_error(shape.width, children_span)?;
let child_shape = formatter.child_shape(context, shape, children_span)?;

formatter.format_children(context, child_shape)?;
formatter.format_last_child(context, shape, child_shape)?;
Expand Down Expand Up @@ -590,7 +589,12 @@ trait ChainFormatter {
context: &RewriteContext<'_>,
shape: Shape,
) -> Result<(), RewriteError>;
fn child_shape(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<Shape>;
fn child_shape(
&self,
context: &RewriteContext<'_>,
shape: Shape,
span: Span,
) -> Result<Shape, ExceedsMaxWidthError>;
fn format_children(
&mut self,
context: &RewriteContext<'_>,
Expand Down Expand Up @@ -720,29 +724,23 @@ impl<'a> ChainFormatterShared<'a> {
&& self.rewrites.iter().all(|s| !s.contains('\n'))
&& one_line_budget > 0;
let last_shape = if all_in_one_line {
shape
.sub_width(last.tries)
.max_width_error(shape.width, last.span)?
shape.sub_width(last.tries, last.span)?
} else if extendable {
child_shape
.sub_width(last.tries)
.max_width_error(child_shape.width, last.span)?
child_shape.sub_width(last.tries, last.span)?
} else {
child_shape
.sub_width(shape.rhs_overhead(context.config) + last.tries)
.max_width_error(child_shape.width, last.span)?
child_shape.sub_width(shape.rhs_overhead(context.config) + last.tries, last.span)?
};

let mut last_subexpr_str = None;
if all_in_one_line || extendable {
// First we try to 'overflow' the last child and see if it looks better than using
// vertical layout.
let one_line_shape = if context.use_block_indent() {
last_shape.offset_left(almost_total)
last_shape.offset_left_opt(almost_total)
} else {
last_shape
.visual_indent(almost_total)
.sub_width(almost_total)
.sub_width_opt(almost_total)
};

if let Some(one_line_shape) = one_line_shape {
Expand All @@ -760,9 +758,10 @@ impl<'a> ChainFormatterShared<'a> {
// layout, just by looking at the overflowed rewrite. Now we rewrite the
// last child on its own line, and compare two rewrites to choose which is
// better.
let last_shape = child_shape
.sub_width(shape.rhs_overhead(context.config) + last.tries)
.max_width_error(child_shape.width, last.span)?;
let last_shape = child_shape.sub_width(
shape.rhs_overhead(context.config) + last.tries,
last.span,
)?;
match last.rewrite_result(context, last_shape) {
Ok(ref new_rw) if !could_fit_single_line => {
last_subexpr_str = Some(new_rw.clone());
Expand All @@ -787,9 +786,7 @@ impl<'a> ChainFormatterShared<'a> {
let last_shape = if context.use_block_indent() {
last_shape
} else {
child_shape
.sub_width(shape.rhs_overhead(context.config) + last.tries)
.max_width_error(child_shape.width, last.span)?
child_shape.sub_width(shape.rhs_overhead(context.config) + last.tries, last.span)?
};

let last_subexpr_str =
Expand Down Expand Up @@ -863,9 +860,7 @@ impl<'a> ChainFormatter for ChainFormatterBlock<'a> {
if let ChainItemKind::Comment(..) = item.kind {
break;
}
let shape = shape
.offset_left(root_rewrite.len())
.max_width_error(shape.width, item.span)?;
let shape = shape.offset_left(root_rewrite.len(), item.span)?;
match &item.rewrite_result(context, shape) {
Ok(rewrite) => root_rewrite.push_str(rewrite),
Err(_) => break,
Expand All @@ -883,9 +878,14 @@ impl<'a> ChainFormatter for ChainFormatterBlock<'a> {
Ok(())
}

fn child_shape(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<Shape> {
fn child_shape(
&self,
context: &RewriteContext<'_>,
shape: Shape,
_span: Span,
) -> Result<Shape, ExceedsMaxWidthError> {
let block_end = self.root_ends_with_block;
Some(get_block_child_shape(block_end, context, shape))
Ok(get_block_child_shape(block_end, context, shape))
}

fn format_children(
Expand Down Expand Up @@ -955,8 +955,7 @@ impl<'a> ChainFormatter for ChainFormatterVisual<'a> {
}
let child_shape = parent_shape
.visual_indent(self.offset)
.sub_width(self.offset)
.max_width_error(parent_shape.width, item.span)?;
.sub_width(self.offset, item.span)?;
let rewrite = item.rewrite_result(context, child_shape)?;
if filtered_str_fits(&rewrite, context.config.max_width(), shape) {
root_rewrite.push_str(&rewrite);
Expand All @@ -975,13 +974,19 @@ impl<'a> ChainFormatter for ChainFormatterVisual<'a> {
Ok(())
}

fn child_shape(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<Shape> {
fn child_shape(
&self,
context: &RewriteContext<'_>,
shape: Shape,
span: Span,
) -> Result<Shape, ExceedsMaxWidthError> {
get_visual_style_child_shape(
context,
shape,
self.offset,
// TODO(calebcartwright): self.shared.permissibly_overflowing_parent,
false,
span,
)
}

Expand Down
25 changes: 9 additions & 16 deletions src/closures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ pub(crate) fn rewrite_closure(
shape,
)?;
// 1 = space between `|...|` and body.
let body_shape = shape
.offset_left(extra_offset)
.max_width_error(shape.width, span)?;
let body_shape = shape.offset_left(extra_offset, span)?;

if let ast::ExprKind::Block(ref block, _) = body.kind {
// The body of the closure is an empty block.
Expand Down Expand Up @@ -294,16 +292,15 @@ fn rewrite_closure_fn_decl(
// 4 = "|| {".len(), which is overconservative when the closure consists of
// a single expression.
let nested_shape = shape
.shrink_left(binder.len() + const_.len() + immovable.len() + coro.len() + mover.len())
.and_then(|shape| shape.sub_width(4))
.max_width_error(shape.width, span)?;
.shrink_left(
binder.len() + const_.len() + immovable.len() + coro.len() + mover.len(),
span,
)?
.sub_width(4, span)?;

// 1 = |
let param_offset = nested_shape.indent + 1;
let param_shape = nested_shape
.offset_left(1)
.max_width_error(nested_shape.width, span)?
.visual_indent(0);
let param_shape = nested_shape.offset_left(1, span)?.visual_indent(0);
let ret_str = fn_decl.output.rewrite_result(context, param_shape)?;

let param_items = itemize_list(
Expand All @@ -328,9 +325,7 @@ fn rewrite_closure_fn_decl(
horizontal_budget,
);
let param_shape = match tactic {
DefinitiveListTactic::Horizontal => param_shape
.sub_width(ret_str.len() + 1)
.max_width_error(param_shape.width, span)?,
DefinitiveListTactic::Horizontal => param_shape.sub_width(ret_str.len() + 1, span)?,
_ => param_shape,
};

Expand Down Expand Up @@ -401,9 +396,7 @@ pub(crate) fn rewrite_last_closure(
return Err(RewriteError::Unknown);
}

let body_shape = shape
.offset_left(extra_offset)
.max_width_error(shape.width, expr.span)?;
let body_shape = shape.offset_left(extra_offset, expr.span)?;

// We force to use block for the body of the closure for certain kinds of expressions.
if is_block_closure_forced(context, body) {
Expand Down
Loading

0 comments on commit 9343d96

Please sign in to comment.