Implement Rewrite for ast::Stmt
This commit is contained in:
parent
22837b0748
commit
fbd1398c92
30
src/expr.rs
30
src/expr.rs
@ -17,7 +17,8 @@ use rewrite::{Rewrite, RewriteContext};
|
||||
use lists::{write_list, itemize_list, ListFormatting, SeparatorTactic, ListTactic,
|
||||
DefinitiveListTactic, definitive_tactic, ListItem, format_fn_args};
|
||||
use string::{StringFormat, rewrite_string};
|
||||
use utils::{span_after, extra_offset, last_line_width, wrap_str, binary_search, first_line_width};
|
||||
use utils::{span_after, extra_offset, last_line_width, wrap_str, binary_search, first_line_width,
|
||||
semicolon_for_stmt};
|
||||
use visitor::FmtVisitor;
|
||||
use config::{StructLitStyle, MultilineStyle};
|
||||
use comment::{FindUncommented, rewrite_comment, contains_comment};
|
||||
@ -475,6 +476,33 @@ impl Rewrite for ast::Block {
|
||||
}
|
||||
}
|
||||
|
||||
impl Rewrite for ast::Stmt {
|
||||
fn rewrite(&self, context: &RewriteContext, _width: usize, offset: Indent) -> Option<String> {
|
||||
match self.node {
|
||||
ast::Stmt_::StmtDecl(ref decl, _) => {
|
||||
if let ast::Decl_::DeclLocal(ref local) = decl.node {
|
||||
local.rewrite(context, context.config.max_width, offset)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
ast::Stmt_::StmtExpr(ref ex, _) | ast::Stmt_::StmtSemi(ref ex, _) => {
|
||||
let suffix = if semicolon_for_stmt(self) {
|
||||
";"
|
||||
} else {
|
||||
""
|
||||
};
|
||||
|
||||
ex.rewrite(context,
|
||||
context.config.max_width - offset.width() - suffix.len(),
|
||||
offset)
|
||||
.map(|s| s + suffix)
|
||||
}
|
||||
ast::Stmt_::StmtMac(..) => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Abstraction over for, while and loop expressions
|
||||
struct Loop<'a> {
|
||||
cond: Option<&'a ast::Expr>,
|
||||
|
32
src/items.rs
32
src/items.rs
@ -12,7 +12,7 @@
|
||||
|
||||
use Indent;
|
||||
use utils::{format_mutability, format_visibility, contains_skip, span_after, end_typaram,
|
||||
wrap_str, last_line_width, semicolon_for_expr, semicolon_for_stmt};
|
||||
wrap_str, last_line_width, semicolon_for_expr};
|
||||
use lists::{write_list, itemize_list, ListItem, ListFormatting, SeparatorTactic,
|
||||
DefinitiveListTactic, definitive_tactic, format_item_list};
|
||||
use expr::{is_empty_block, is_simple_block_stmt, rewrite_assign_rhs};
|
||||
@ -479,7 +479,9 @@ impl<'a> FmtVisitor<'a> {
|
||||
.map(|s| s + suffix)
|
||||
.or_else(|| Some(self.snippet(e.span)))
|
||||
} else if let Some(ref stmt) = block.stmts.first() {
|
||||
self.rewrite_stmt(stmt)
|
||||
stmt.rewrite(&self.get_context(),
|
||||
self.config.max_width - self.block_indent.width(),
|
||||
self.block_indent)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
@ -496,32 +498,6 @@ impl<'a> FmtVisitor<'a> {
|
||||
None
|
||||
}
|
||||
|
||||
pub fn rewrite_stmt(&self, stmt: &ast::Stmt) -> Option<String> {
|
||||
match stmt.node {
|
||||
ast::Stmt_::StmtDecl(ref decl, _) => {
|
||||
if let ast::Decl_::DeclLocal(ref local) = decl.node {
|
||||
let context = self.get_context();
|
||||
local.rewrite(&context, self.config.max_width, self.block_indent)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
ast::Stmt_::StmtExpr(ref ex, _) | ast::Stmt_::StmtSemi(ref ex, _) => {
|
||||
let suffix = if semicolon_for_stmt(stmt) {
|
||||
";"
|
||||
} else {
|
||||
""
|
||||
};
|
||||
|
||||
ex.rewrite(&self.get_context(),
|
||||
self.config.max_width - self.block_indent.width() - suffix.len(),
|
||||
self.block_indent)
|
||||
.map(|s| s + suffix)
|
||||
}
|
||||
ast::Stmt_::StmtMac(..) => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn rewrite_args(&self,
|
||||
args: &[ast::Arg],
|
||||
explicit_self: Option<&ast::ExplicitSelf>,
|
||||
|
@ -41,12 +41,18 @@ impl<'a> FmtVisitor<'a> {
|
||||
if let ast::Decl_::DeclItem(ref item) = decl.node {
|
||||
self.visit_item(item);
|
||||
} else {
|
||||
let rewrite = self.rewrite_stmt(stmt);
|
||||
let rewrite = stmt.rewrite(&self.get_context(),
|
||||
self.config.max_width - self.block_indent.width(),
|
||||
self.block_indent);
|
||||
|
||||
self.push_rewrite(stmt.span, rewrite);
|
||||
}
|
||||
}
|
||||
ast::Stmt_::StmtExpr(..) | ast::Stmt_::StmtSemi(..) => {
|
||||
let rewrite = self.rewrite_stmt(stmt);
|
||||
let rewrite = stmt.rewrite(&self.get_context(),
|
||||
self.config.max_width - self.block_indent.width(),
|
||||
self.block_indent);
|
||||
|
||||
self.push_rewrite(stmt.span, rewrite);
|
||||
}
|
||||
ast::Stmt_::StmtMac(ref mac, _macro_style) => {
|
||||
|
@ -64,6 +64,8 @@ fn lots_of_space ()
|
||||
1
|
||||
}
|
||||
|
||||
fn mac() -> Vec<i32> { vec![] }
|
||||
|
||||
trait CoolTypes {
|
||||
fn dummy(&self) {
|
||||
}
|
||||
|
@ -52,6 +52,8 @@ fn aaaaaaaaaaaaaaaaa_looooooooooooooooooooooong_name() {
|
||||
|
||||
fn lots_of_space() { 1 }
|
||||
|
||||
fn mac() -> Vec<i32> { vec![] }
|
||||
|
||||
trait CoolTypes {
|
||||
fn dummy(&self) { }
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user