impl rewrite_result for ChainItem

This commit is contained in:
ding-young 2024-07-29 14:45:24 +09:00 committed by Yacin Tmimi
parent 3d468e2d92
commit b8a5b211ef
2 changed files with 27 additions and 20 deletions

View file

@ -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::{
@ -267,9 +267,14 @@ 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,
@ -278,10 +283,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!(
@ -295,10 +299,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)))
}
}

View file

@ -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)
@ -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.
@ -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 {
@ -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)
}
@ -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('(');
@ -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(