use crate::utils::{
colon_spaces, contains_skip, count_newlines, first_line_ends_with, inner_attributes,
last_line_extendable, last_line_width, mk_sp, outer_attributes, ptr_vec_to_ref_vec,
- semicolon_for_expr, semicolon_for_stmt, wrap_str,
+ semicolon_for_expr, unicode_str_width, wrap_str,
};
use crate::vertical::rewrite_with_alignment;
use crate::visitor::FmtVisitor;
result
}
-impl Rewrite for ast::Stmt {
- fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
- skip_out_of_file_lines_range!(context, self.span());
-
- let result = match self.node {
- ast::StmtKind::Local(ref local) => local.rewrite(context, shape),
- ast::StmtKind::Expr(ref ex) | ast::StmtKind::Semi(ref ex) => {
- let suffix = if semicolon_for_stmt(context, self) {
- ";"
- } else {
- ""
- };
-
- let shape = shape.sub_width(suffix.len())?;
- format_expr(ex, ExprType::Statement, context, shape).map(|s| s + suffix)
- }
- ast::StmtKind::Mac(..) | ast::StmtKind::Item(..) => None,
- };
- result.and_then(|res| recover_comment_removed(res, self.span(), context))
- }
-}
-
// Rewrite condition if the given expression has one.
pub(crate) fn rewrite_cond(
context: &RewriteContext<'_>,
}
}
-pub(crate) fn stmt_is_if(stmt: &ast::Stmt) -> bool {
- match stmt.node {
- ast::StmtKind::Expr(ref e) => match e.node {
- ast::ExprKind::If(..) => true,
- _ => false,
- },
- _ => false,
- }
-}
-
pub(crate) fn is_unsafe_block(block: &ast::Block) -> bool {
if let ast::BlockCheckMode::Unsafe(..) = block.rules {
true
rhs_tactics: RhsTactics,
) -> Option<String> {
match orig_rhs {
- Some(ref new_str) if !new_str.contains('\n') && new_str.len() <= shape.width => {
+ Some(ref new_str)
+ if !new_str.contains('\n') && unicode_str_width(new_str) <= shape.width =>
+ {
Some(format!(" {}", new_str))
}
_ => {
}
}
+/// Returns true if formatting next_line_rhs is better on a new line when compared to the
+/// original's line formatting.
+///
+/// It is considered better if:
+/// 1. the tactic is ForceNextLineWithoutIndent
+/// 2. next_line_rhs doesn't have newlines
+/// 3. the original line has more newlines than next_line_rhs
+/// 4. the original formatting of the first line ends with `(`, `{`, or `[` and next_line_rhs
+/// doesn't
pub(crate) fn prefer_next_line(
orig_rhs: &str,
next_line_rhs: &str,