Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 70c8e36

Browse files
committed
Format a paren expr with double slash comment
1 parent efe24bd commit 70c8e36

File tree

1 file changed

+43
-13
lines changed

1 file changed

+43
-13
lines changed

src/expr.rs

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ use spanned::Spanned;
3939
use string::{rewrite_string, StringFormat};
4040
use types::{can_be_overflowed_type, rewrite_path, PathContext};
4141
use utils::{
42-
colon_spaces, contains_skip, count_newlines, first_line_ends_with, first_line_width,
43-
inner_attributes, last_line_extendable, last_line_width, mk_sp, outer_attributes,
44-
ptr_vec_to_ref_vec, semicolon_for_stmt, wrap_str,
42+
colon_spaces, contains_skip, count_newlines, first_line_ends_with, inner_attributes,
43+
last_line_extendable, last_line_width, mk_sp, outer_attributes, ptr_vec_to_ref_vec,
44+
semicolon_for_stmt, wrap_str,
4545
};
4646
use vertical::rewrite_with_alignment;
4747
use visitor::FmtVisitor;
@@ -1438,13 +1438,15 @@ fn rewrite_paren(
14381438
debug!("rewrite_paren, shape: {:?}", shape);
14391439

14401440
// Extract comments within parens.
1441+
let mut pre_span;
1442+
let mut post_span;
14411443
let mut pre_comment;
14421444
let mut post_comment;
14431445
let remove_nested_parens = context.config.remove_nested_parens();
14441446
loop {
14451447
// 1 = "(" or ")"
1446-
let pre_span = mk_sp(span.lo() + BytePos(1), subexpr.span.lo());
1447-
let post_span = mk_sp(subexpr.span.hi(), span.hi() - BytePos(1));
1448+
pre_span = mk_sp(span.lo() + BytePos(1), subexpr.span.lo());
1449+
post_span = mk_sp(subexpr.span.hi(), span.hi() - BytePos(1));
14481450
pre_comment = rewrite_missing_comment(pre_span, shape, context)?;
14491451
post_comment = rewrite_missing_comment(post_span, shape, context)?;
14501452

@@ -1460,20 +1462,48 @@ fn rewrite_paren(
14601462
break;
14611463
}
14621464

1463-
// 1 `(`
1464-
let sub_shape = shape.offset_left(1).and_then(|s| s.sub_width(1))?;
1465-
1465+
// 1 = `(` and `)`
1466+
let sub_shape = shape.offset_left(1)?.sub_width(1)?;
14661467
let subexpr_str = subexpr.rewrite(context, sub_shape)?;
1467-
debug!("rewrite_paren, subexpr_str: `{:?}`", subexpr_str);
1468-
1469-
// 2 = `()`
1470-
if subexpr_str.contains('\n') || first_line_width(&subexpr_str) + 2 <= shape.width {
1468+
let fits_single_line = !pre_comment.contains("//") && !post_comment.contains("//");
1469+
if fits_single_line {
14711470
Some(format!("({}{}{})", pre_comment, &subexpr_str, post_comment))
14721471
} else {
1473-
None
1472+
rewrite_paren_in_multi_line(context, subexpr, shape, pre_span, post_span)
14741473
}
14751474
}
14761475

1476+
fn rewrite_paren_in_multi_line(
1477+
context: &RewriteContext,
1478+
subexpr: &ast::Expr,
1479+
shape: Shape,
1480+
pre_span: Span,
1481+
post_span: Span,
1482+
) -> Option<String> {
1483+
let nested_indent = shape.indent.block_indent(context.config);
1484+
let nested_shape = Shape::indented(nested_indent, context.config);
1485+
let pre_comment = rewrite_missing_comment(pre_span, nested_shape, context)?;
1486+
let post_comment = rewrite_missing_comment(post_span, nested_shape, context)?;
1487+
let subexpr_str = subexpr.rewrite(context, nested_shape)?;
1488+
1489+
let mut result = String::with_capacity(subexpr_str.len() * 2);
1490+
result.push('(');
1491+
if !pre_comment.is_empty() {
1492+
result.push_str(&nested_indent.to_string_with_newline(context.config));
1493+
result.push_str(&pre_comment);
1494+
}
1495+
result.push_str(&nested_indent.to_string_with_newline(context.config));
1496+
result.push_str(&subexpr_str);
1497+
if !post_comment.is_empty() {
1498+
result.push_str(&nested_indent.to_string_with_newline(context.config));
1499+
result.push_str(&post_comment);
1500+
}
1501+
result.push_str(&shape.indent.to_string_with_newline(context.config));
1502+
result.push(')');
1503+
1504+
Some(result)
1505+
}
1506+
14771507
fn rewrite_index(
14781508
expr: &ast::Expr,
14791509
index: &ast::Expr,

0 commit comments

Comments
 (0)