Skip to content

Commit

Permalink
impl rewrite_result for ChainItem
Browse files Browse the repository at this point in the history
  • Loading branch information
ding-young committed Jul 29, 2024
1 parent 57c532b commit 983aa18
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
19 changes: 12 additions & 7 deletions src/chains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ 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, RewriteResult};
use crate::rewrite::{Rewrite, RewriteContext, RewriteError, RewriteErrorExt, RewriteResult};
use crate::shape::Shape;
use crate::source_map::SpanUtils;
use crate::utils::{
Expand Down Expand Up @@ -269,7 +269,13 @@ impl ChainItemKind {
impl Rewrite for ChainItem {
// TODO impl rewrite_result after rebase
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
let shape = shape.sub_width(self.tries)?;
self.rewrite_result(context, shape).ok()
}

fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
let shape = shape
.sub_width(self.tries)
.max_width_error(shape.width, self.span)?;
let rewrite = match self.kind {
ChainItemKind::Parent {
ref expr,
Expand All @@ -278,10 +284,9 @@ impl Rewrite for ChainItem {
ChainItemKind::Parent {
ref expr,
parens: false,
} => expr.rewrite(context, shape)?,
} => expr.rewrite_result(context, shape)?,
ChainItemKind::MethodCall(ref segment, ref types, ref exprs) => {
Self::rewrite_method_call(segment.ident, types, exprs, self.span, context, shape)
.ok()?
Self::rewrite_method_call(segment.ident, types, exprs, self.span, context, shape)?
}
ChainItemKind::StructField(ident) => format!(".{}", rewrite_ident(context, ident)),
ChainItemKind::TupleField(ident, nested) => format!(
Expand All @@ -295,10 +300,10 @@ impl Rewrite for ChainItem {
),
ChainItemKind::Await => ".await".to_owned(),
ChainItemKind::Comment(ref comment, _) => {
rewrite_comment(comment, false, shape, context.config).ok()?
rewrite_comment(comment, false, shape, context.config)?
}
};
Some(format!("{rewrite}{}", "?".repeat(self.tries)))
Ok(format!("{rewrite}{}", "?".repeat(self.tries)))
}
}

Expand Down
27 changes: 15 additions & 12 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pub(crate) fn format_expr(
let callee_str = callee.rewrite(context, shape)?;
rewrite_call(context, &callee_str, args, inner_span, shape).ok()
}
ast::ExprKind::Paren(ref subexpr) => rewrite_paren(context, subexpr, shape, expr.span),
ast::ExprKind::Paren(ref subexpr) => rewrite_paren(context, subexpr, shape, expr.span).ok(),
ast::ExprKind::Binary(op, ref lhs, ref rhs) => {
// FIXME: format comments between operands and operator
rewrite_all_pairs(expr, shape, context)
Expand Down Expand Up @@ -1484,7 +1484,7 @@ pub(crate) fn rewrite_paren(
mut subexpr: &ast::Expr,
shape: Shape,
mut span: Span,
) -> Option<String> {
) -> RewriteResult {
debug!("rewrite_paren, shape: {:?}", shape);

// Extract comments within parens.
Expand All @@ -1497,8 +1497,8 @@ pub(crate) fn rewrite_paren(
// 1 = "(" or ")"
pre_span = mk_sp(span.lo() + BytePos(1), subexpr.span().lo());
post_span = mk_sp(subexpr.span.hi(), span.hi() - BytePos(1));
pre_comment = rewrite_missing_comment(pre_span, shape, context).ok()?;
post_comment = rewrite_missing_comment(post_span, shape, context).ok()?;
pre_comment = rewrite_missing_comment(pre_span, shape, context)?;
post_comment = rewrite_missing_comment(post_span, shape, context)?;

// Remove nested parens if there are no comments.
if let ast::ExprKind::Paren(ref subsubexpr) = subexpr.kind {
Expand All @@ -1513,11 +1513,14 @@ pub(crate) fn rewrite_paren(
}

// 1 = `(` and `)`
let sub_shape = shape.offset_left(1)?.sub_width(1)?;
let subexpr_str = subexpr.rewrite(context, sub_shape)?;
let sub_shape = shape
.offset_left(1)
.and_then(|s| s.sub_width(1))
.max_width_error(shape.width, span)?;
let subexpr_str = subexpr.rewrite_result(context, sub_shape)?;
let fits_single_line = !pre_comment.contains("//") && !post_comment.contains("//");
if fits_single_line {
Some(format!("({pre_comment}{subexpr_str}{post_comment})"))
Ok(format!("({pre_comment}{subexpr_str}{post_comment})"))
} else {
rewrite_paren_in_multi_line(context, subexpr, shape, pre_span, post_span)
}
Expand All @@ -1529,12 +1532,12 @@ fn rewrite_paren_in_multi_line(
shape: Shape,
pre_span: Span,
post_span: Span,
) -> Option<String> {
) -> RewriteResult {
let nested_indent = shape.indent.block_indent(context.config);
let nested_shape = Shape::indented(nested_indent, context.config);
let pre_comment = rewrite_missing_comment(pre_span, nested_shape, context).ok()?;
let post_comment = rewrite_missing_comment(post_span, nested_shape, context).ok()?;
let subexpr_str = subexpr.rewrite(context, nested_shape)?;
let pre_comment = rewrite_missing_comment(pre_span, nested_shape, context)?;
let post_comment = rewrite_missing_comment(post_span, nested_shape, context)?;
let subexpr_str = subexpr.rewrite_result(context, nested_shape)?;

let mut result = String::with_capacity(subexpr_str.len() * 2);
result.push('(');
Expand All @@ -1551,7 +1554,7 @@ fn rewrite_paren_in_multi_line(
result.push_str(&shape.indent.to_string_with_newline(context.config));
result.push(')');

Some(result)
Ok(result)
}

fn rewrite_index(
Expand Down

0 comments on commit 983aa18

Please sign in to comment.