refactor rewrite_block
This commit is contained in:
parent
add045979b
commit
bb56224d2a
@ -183,7 +183,8 @@ fn rewrite_closure_with_block(
|
|||||||
None,
|
None,
|
||||||
shape,
|
shape,
|
||||||
false,
|
false,
|
||||||
)?;
|
)
|
||||||
|
.ok()?;
|
||||||
Some(format!("{prefix} {block}"))
|
Some(format!("{prefix} {block}"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
81
src/expr.rs
81
src/expr.rs
@ -22,7 +22,7 @@ use crate::macros::{rewrite_macro, MacroPosition};
|
|||||||
use crate::matches::rewrite_match;
|
use crate::matches::rewrite_match;
|
||||||
use crate::overflow::{self, IntoOverflowableItem, OverflowableItem};
|
use crate::overflow::{self, IntoOverflowableItem, OverflowableItem};
|
||||||
use crate::pairs::{rewrite_all_pairs, rewrite_pair, PairParts};
|
use crate::pairs::{rewrite_all_pairs, rewrite_pair, PairParts};
|
||||||
use crate::rewrite::{Rewrite, RewriteContext};
|
use crate::rewrite::{Rewrite, RewriteContext, RewriteError, RewriteErrorExt, RewriteResult};
|
||||||
use crate::shape::{Indent, Shape};
|
use crate::shape::{Indent, Shape};
|
||||||
use crate::source_map::{LineRangeUtils, SpanUtils};
|
use crate::source_map::{LineRangeUtils, SpanUtils};
|
||||||
use crate::spanned::Spanned;
|
use crate::spanned::Spanned;
|
||||||
@ -145,7 +145,7 @@ pub(crate) fn format_expr(
|
|||||||
// not the `ast::Block` node we're about to rewrite. To prevent dropping inner
|
// not the `ast::Block` node we're about to rewrite. To prevent dropping inner
|
||||||
// attributes call `rewrite_block` directly.
|
// attributes call `rewrite_block` directly.
|
||||||
// See https://github.com/rust-lang/rustfmt/issues/6158
|
// See https://github.com/rust-lang/rustfmt/issues/6158
|
||||||
rewrite_block(block, Some(&expr.attrs), opt_label, context, shape)?
|
rewrite_block(block, Some(&expr.attrs), opt_label, context, shape).ok()?
|
||||||
}
|
}
|
||||||
_ => anon_const.rewrite(context, shape)?,
|
_ => anon_const.rewrite(context, shape)?,
|
||||||
};
|
};
|
||||||
@ -155,7 +155,7 @@ pub(crate) fn format_expr(
|
|||||||
match expr_type {
|
match expr_type {
|
||||||
ExprType::Statement => {
|
ExprType::Statement => {
|
||||||
if is_unsafe_block(block) {
|
if is_unsafe_block(block) {
|
||||||
rewrite_block(block, Some(&expr.attrs), opt_label, context, shape)
|
rewrite_block(block, Some(&expr.attrs), opt_label, context, shape).ok()
|
||||||
} else if let rw @ Some(_) =
|
} else if let rw @ Some(_) =
|
||||||
rewrite_empty_block(context, block, Some(&expr.attrs), opt_label, "", shape)
|
rewrite_empty_block(context, block, Some(&expr.attrs), opt_label, "", shape)
|
||||||
{
|
{
|
||||||
@ -173,10 +173,11 @@ pub(crate) fn format_expr(
|
|||||||
shape,
|
shape,
|
||||||
true,
|
true,
|
||||||
)
|
)
|
||||||
|
.ok()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ExprType::SubExpression => {
|
ExprType::SubExpression => {
|
||||||
rewrite_block(block, Some(&expr.attrs), opt_label, context, shape)
|
rewrite_block(block, Some(&expr.attrs), opt_label, context, shape).ok()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -352,10 +353,10 @@ pub(crate) fn format_expr(
|
|||||||
// https://github.com/rust-dev-tools/fmt-rfcs/issues/152
|
// https://github.com/rust-dev-tools/fmt-rfcs/issues/152
|
||||||
ast::ExprKind::InlineAsm(..) => Some(context.snippet(expr.span).to_owned()),
|
ast::ExprKind::InlineAsm(..) => Some(context.snippet(expr.span).to_owned()),
|
||||||
ast::ExprKind::TryBlock(ref block) => {
|
ast::ExprKind::TryBlock(ref block) => {
|
||||||
if let rw @ Some(_) =
|
if let rw @ Ok(_) =
|
||||||
rewrite_single_line_block(context, "try ", block, Some(&expr.attrs), None, shape)
|
rewrite_single_line_block(context, "try ", block, Some(&expr.attrs), None, shape)
|
||||||
{
|
{
|
||||||
rw
|
rw.ok()
|
||||||
} else {
|
} else {
|
||||||
// 9 = `try `
|
// 9 = `try `
|
||||||
let budget = shape.width.saturating_sub(9);
|
let budget = shape.width.saturating_sub(9);
|
||||||
@ -368,7 +369,8 @@ pub(crate) fn format_expr(
|
|||||||
None,
|
None,
|
||||||
context,
|
context,
|
||||||
Shape::legacy(budget, shape.indent)
|
Shape::legacy(budget, shape.indent)
|
||||||
)?
|
)
|
||||||
|
.ok()?
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -378,7 +380,7 @@ pub(crate) fn format_expr(
|
|||||||
} else {
|
} else {
|
||||||
""
|
""
|
||||||
};
|
};
|
||||||
if let rw @ Some(_) = rewrite_single_line_block(
|
if let rw @ Ok(_) = rewrite_single_line_block(
|
||||||
context,
|
context,
|
||||||
format!("{kind} {mover}").as_str(),
|
format!("{kind} {mover}").as_str(),
|
||||||
block,
|
block,
|
||||||
@ -386,7 +388,7 @@ pub(crate) fn format_expr(
|
|||||||
None,
|
None,
|
||||||
shape,
|
shape,
|
||||||
) {
|
) {
|
||||||
rw
|
rw.ok()
|
||||||
} else {
|
} else {
|
||||||
// 6 = `async `
|
// 6 = `async `
|
||||||
let budget = shape.width.saturating_sub(6);
|
let budget = shape.width.saturating_sub(6);
|
||||||
@ -398,7 +400,8 @@ pub(crate) fn format_expr(
|
|||||||
None,
|
None,
|
||||||
context,
|
context,
|
||||||
Shape::legacy(budget, shape.indent)
|
Shape::legacy(budget, shape.indent)
|
||||||
)?
|
)
|
||||||
|
.ok()?
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -522,17 +525,19 @@ fn rewrite_single_line_block(
|
|||||||
attrs: Option<&[ast::Attribute]>,
|
attrs: Option<&[ast::Attribute]>,
|
||||||
label: Option<ast::Label>,
|
label: Option<ast::Label>,
|
||||||
shape: Shape,
|
shape: Shape,
|
||||||
) -> Option<String> {
|
) -> RewriteResult {
|
||||||
if let Some(block_expr) = stmt::Stmt::from_simple_block(context, block, attrs) {
|
if let Some(block_expr) = stmt::Stmt::from_simple_block(context, block, attrs) {
|
||||||
let expr_shape = shape.offset_left(last_line_width(prefix))?;
|
let expr_shape = shape
|
||||||
let expr_str = block_expr.rewrite(context, expr_shape)?;
|
.offset_left(last_line_width(prefix))
|
||||||
|
.max_width_error(shape.width, block_expr.span())?;
|
||||||
|
let expr_str = block_expr.rewrite_result(context, expr_shape)?;
|
||||||
let label_str = rewrite_label(label);
|
let label_str = rewrite_label(label);
|
||||||
let result = format!("{prefix}{label_str}{{ {expr_str} }}");
|
let result = format!("{prefix}{label_str}{{ {expr_str} }}");
|
||||||
if result.len() <= shape.width && !result.contains('\n') {
|
if result.len() <= shape.width && !result.contains('\n') {
|
||||||
return Some(result);
|
return Ok(result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None
|
Err(RewriteError::Unknown)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn rewrite_block_with_visitor(
|
pub(crate) fn rewrite_block_with_visitor(
|
||||||
@ -543,9 +548,9 @@ pub(crate) fn rewrite_block_with_visitor(
|
|||||||
label: Option<ast::Label>,
|
label: Option<ast::Label>,
|
||||||
shape: Shape,
|
shape: Shape,
|
||||||
has_braces: bool,
|
has_braces: bool,
|
||||||
) -> Option<String> {
|
) -> RewriteResult {
|
||||||
if let rw @ Some(_) = rewrite_empty_block(context, block, attrs, label, prefix, shape) {
|
if let Some(rw_str) = rewrite_empty_block(context, block, attrs, label, prefix, shape) {
|
||||||
return rw;
|
return Ok(rw_str);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut visitor = FmtVisitor::from_context(context);
|
let mut visitor = FmtVisitor::from_context(context);
|
||||||
@ -554,7 +559,7 @@ pub(crate) fn rewrite_block_with_visitor(
|
|||||||
match (block.rules, label) {
|
match (block.rules, label) {
|
||||||
(ast::BlockCheckMode::Unsafe(..), _) | (ast::BlockCheckMode::Default, Some(_)) => {
|
(ast::BlockCheckMode::Unsafe(..), _) | (ast::BlockCheckMode::Default, Some(_)) => {
|
||||||
let snippet = context.snippet(block.span);
|
let snippet = context.snippet(block.span);
|
||||||
let open_pos = snippet.find_uncommented("{")?;
|
let open_pos = snippet.find_uncommented("{").unknown_error()?;
|
||||||
visitor.last_pos = block.span.lo() + BytePos(open_pos as u32)
|
visitor.last_pos = block.span.lo() + BytePos(open_pos as u32)
|
||||||
}
|
}
|
||||||
(ast::BlockCheckMode::Default, None) => visitor.last_pos = block.span.lo(),
|
(ast::BlockCheckMode::Default, None) => visitor.last_pos = block.span.lo(),
|
||||||
@ -568,11 +573,15 @@ pub(crate) fn rewrite_block_with_visitor(
|
|||||||
.skipped_range
|
.skipped_range
|
||||||
.borrow_mut()
|
.borrow_mut()
|
||||||
.append(&mut visitor_context.skipped_range.borrow_mut());
|
.append(&mut visitor_context.skipped_range.borrow_mut());
|
||||||
Some(format!("{}{}{}", prefix, label_str, visitor.buffer))
|
Ok(format!("{}{}{}", prefix, label_str, visitor.buffer))
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Rewrite for ast::Block {
|
impl Rewrite for ast::Block {
|
||||||
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
|
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
|
||||||
|
self.rewrite_result(context, shape).ok()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
|
||||||
rewrite_block(self, None, None, context, shape)
|
rewrite_block(self, None, None, context, shape)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -583,7 +592,7 @@ fn rewrite_block(
|
|||||||
label: Option<ast::Label>,
|
label: Option<ast::Label>,
|
||||||
context: &RewriteContext<'_>,
|
context: &RewriteContext<'_>,
|
||||||
shape: Shape,
|
shape: Shape,
|
||||||
) -> Option<String> {
|
) -> RewriteResult {
|
||||||
rewrite_block_inner(block, attrs, label, true, context, shape)
|
rewrite_block_inner(block, attrs, label, true, context, shape)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -594,27 +603,24 @@ fn rewrite_block_inner(
|
|||||||
allow_single_line: bool,
|
allow_single_line: bool,
|
||||||
context: &RewriteContext<'_>,
|
context: &RewriteContext<'_>,
|
||||||
shape: Shape,
|
shape: Shape,
|
||||||
) -> Option<String> {
|
) -> RewriteResult {
|
||||||
let prefix = block_prefix(context, block, shape)?;
|
let prefix = block_prefix(context, block, shape).unknown_error()?;
|
||||||
|
|
||||||
// shape.width is used only for the single line case: either the empty block `{}`,
|
// shape.width is used only for the single line case: either the empty block `{}`,
|
||||||
// or an unsafe expression `unsafe { e }`.
|
// or an unsafe expression `unsafe { e }`.
|
||||||
if let rw @ Some(_) = rewrite_empty_block(context, block, attrs, label, &prefix, shape) {
|
if let Some(rw_str) = rewrite_empty_block(context, block, attrs, label, &prefix, shape) {
|
||||||
return rw;
|
return Ok(rw_str);
|
||||||
}
|
}
|
||||||
|
|
||||||
let result = rewrite_block_with_visitor(context, &prefix, block, attrs, label, shape, true);
|
let result_str =
|
||||||
if let Some(ref result_str) = result {
|
rewrite_block_with_visitor(context, &prefix, block, attrs, label, shape, true)?;
|
||||||
if allow_single_line && result_str.lines().count() <= 3 {
|
if allow_single_line && result_str.lines().count() <= 3 {
|
||||||
if let rw @ Some(_) =
|
if let rw @ Ok(_) = rewrite_single_line_block(context, &prefix, block, attrs, label, shape)
|
||||||
rewrite_single_line_block(context, &prefix, block, attrs, label, shape)
|
{
|
||||||
{
|
return rw;
|
||||||
return rw;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Ok(result_str)
|
||||||
result
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Rewrite the divergent block of a `let-else` statement.
|
/// Rewrite the divergent block of a `let-else` statement.
|
||||||
@ -623,7 +629,7 @@ pub(crate) fn rewrite_let_else_block(
|
|||||||
allow_single_line: bool,
|
allow_single_line: bool,
|
||||||
context: &RewriteContext<'_>,
|
context: &RewriteContext<'_>,
|
||||||
shape: Shape,
|
shape: Shape,
|
||||||
) -> Option<String> {
|
) -> RewriteResult {
|
||||||
rewrite_block_inner(block, None, None, allow_single_line, context, shape)
|
rewrite_block_inner(block, None, None, allow_single_line, context, shape)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1112,7 +1118,8 @@ impl<'a> Rewrite for ControlFlow<'a> {
|
|||||||
let block_str = {
|
let block_str = {
|
||||||
let old_val = context.is_if_else_block.replace(self.else_block.is_some());
|
let old_val = context.is_if_else_block.replace(self.else_block.is_some());
|
||||||
let result =
|
let result =
|
||||||
rewrite_block_with_visitor(context, "", self.block, None, None, block_shape, true);
|
rewrite_block_with_visitor(context, "", self.block, None, None, block_shape, true)
|
||||||
|
.ok();
|
||||||
context.is_if_else_block.replace(old_val);
|
context.is_if_else_block.replace(old_val);
|
||||||
result?
|
result?
|
||||||
};
|
};
|
||||||
|
@ -181,8 +181,7 @@ impl Rewrite for ast::Local {
|
|||||||
&& allow_single_line_let_else_block(assign_str_with_else_kw, block);
|
&& allow_single_line_let_else_block(assign_str_with_else_kw, block);
|
||||||
|
|
||||||
let mut rw_else_block =
|
let mut rw_else_block =
|
||||||
rewrite_let_else_block(block, allow_single_line, context, shape)
|
rewrite_let_else_block(block, allow_single_line, context, shape)?;
|
||||||
.unknown_error()?;
|
|
||||||
|
|
||||||
let single_line_else = !rw_else_block.contains('\n');
|
let single_line_else = !rw_else_block.contains('\n');
|
||||||
// +1 for the trailing `;`
|
// +1 for the trailing `;`
|
||||||
@ -191,8 +190,7 @@ impl Rewrite for ast::Local {
|
|||||||
if allow_single_line && single_line_else && else_block_exceeds_width {
|
if allow_single_line && single_line_else && else_block_exceeds_width {
|
||||||
// writing this on one line would exceed the available width
|
// writing this on one line would exceed the available width
|
||||||
// so rewrite the else block over multiple lines.
|
// so rewrite the else block over multiple lines.
|
||||||
rw_else_block =
|
rw_else_block = rewrite_let_else_block(block, false, context, shape)?;
|
||||||
rewrite_let_else_block(block, false, context, shape).unknown_error()?;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
result.push_str(&rw_else_block);
|
result.push_str(&rw_else_block);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user