Skip to content

Format and preserve attributes on ast::Stmt #1933

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 25 additions & 35 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,10 @@ pub fn format_expr(
ExprType::Statement => {
if is_unsafe_block(block) {
block.rewrite(context, shape)
} else {
} else if let rw @ Some(_) = rewrite_empty_block(context, block, shape) {
// Rewrite block without trying to put it in a single line.
if let rw @ Some(_) = rewrite_empty_block(context, block, shape) {
return rw;
}
rw
} else {
let prefix = try_opt!(block_prefix(context, block, shape));
rewrite_block_with_visitor(context, &prefix, block, shape)
}
Expand Down Expand Up @@ -181,17 +180,11 @@ pub fn format_expr(
)
}
}
ast::ExprKind::Yield(ref opt_expr) => {
if let Some(ref expr) = *opt_expr {
rewrite_unary_prefix(context, "yield ", &**expr, shape)
} else {
wrap_str(
"yield".to_string(),
context.config.max_width(),
shape,
)
}
}
ast::ExprKind::Yield(ref opt_expr) => if let Some(ref expr) = *opt_expr {
rewrite_unary_prefix(context, "yield ", &**expr, shape)
} else {
wrap_str("yield".to_string(), context.config.max_width(), shape)
},
ast::ExprKind::Closure(capture, ref fn_decl, ref body, _) => {
rewrite_closure(capture, fn_decl, body, expr.span, context, shape)
}
Expand Down Expand Up @@ -293,17 +286,17 @@ pub fn format_expr(
shape,
),
ast::ExprKind::Catch(ref block) => {
if let rewrite @ Some(_) = rewrite_single_line_block(context, "do catch ", block, shape)
{
return rewrite;
if let rw @ Some(_) = rewrite_single_line_block(context, "do catch ", block, shape) {
rw
} else {
// 9 = `do catch `
let budget = shape.width.checked_sub(9).unwrap_or(0);
Some(format!(
"{}{}",
"do catch ",
try_opt!(block.rewrite(&context, Shape::legacy(budget, shape.indent)))
))
}
// 9 = `do catch `
let budget = shape.width.checked_sub(9).unwrap_or(0);
Some(format!(
"{}{}",
"do catch ",
try_opt!(block.rewrite(&context, Shape::legacy(budget, shape.indent)))
))
}
};

Expand Down Expand Up @@ -883,16 +876,13 @@ impl Rewrite for ast::Stmt {
""
};

format_expr(
ex,
match self.node {
ast::StmtKind::Expr(_) => ExprType::SubExpression,
ast::StmtKind::Semi(_) => ExprType::Statement,
_ => unreachable!(),
},
context,
try_opt!(shape.sub_width(suffix.len())),
).map(|s| s + suffix)
let expr_type = match self.node {
ast::StmtKind::Expr(_) => ExprType::SubExpression,
ast::StmtKind::Semi(_) => ExprType::Statement,
_ => unreachable!(),
};
let shape = try_opt!(shape.sub_width(suffix.len()));
format_expr(ex, expr_type, context, shape).map(|s| s + suffix)
}
ast::StmtKind::Mac(..) | ast::StmtKind::Item(..) => None,
};
Expand Down
2 changes: 1 addition & 1 deletion src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ impl<'a> FmtVisitor<'a> {
) {
let vis = utils::format_visibility(vis);
// 4 = `use `, 1 = `;`
let rw = Shape::indented(self.block_indent, self.config)
let rw = self.shape()
.offset_left(vis.len() + 4)
.and_then(|shape| shape.sub_width(1))
.and_then(|shape| match vp.node {
Expand Down
40 changes: 23 additions & 17 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,23 @@ impl Rewrite for ast::Local {

skip_out_of_file_lines_range!(context, self.span);

let mut result = "let ".to_owned();
if contains_skip(&self.attrs) {
return None;
}

let attrs_str = try_opt!(self.attrs.rewrite(context, shape));
let mut result = if attrs_str.is_empty() {
"let ".to_owned()
} else {
try_opt!(combine_strs_with_missing_comments(
context,
&attrs_str,
"let ",
mk_sp(self.attrs.last().map(|a| a.span.hi).unwrap(), self.span.lo),
shape,
false,
))
};

// 4 = "let ".len()
let pat_shape = try_opt!(shape.offset_left(4));
Expand Down Expand Up @@ -187,8 +203,7 @@ impl<'a> FmtVisitor<'a> {


fn format_foreign_item(&mut self, item: &ast::ForeignItem) {
let shape = Shape::indented(self.block_indent, self.config);
let rewrite = item.rewrite(&self.get_context(), shape);
let rewrite = item.rewrite(&self.get_context(), self.shape());
self.push_rewrite(item.span(), rewrite);
self.last_pos = item.span.hi;
}
Expand Down Expand Up @@ -312,18 +327,11 @@ impl<'a> FmtVisitor<'a> {
""
};

format_expr(
&e,
ExprType::Statement,
&self.get_context(),
Shape::indented(self.block_indent, self.config),
).map(|s| s + suffix)
format_expr(&e, ExprType::Statement, &self.get_context(), self.shape())
.map(|s| s + suffix)
.or_else(|| Some(self.snippet(e.span)))
}
None => stmt.rewrite(
&self.get_context(),
Shape::indented(self.block_indent, self.config),
),
None => stmt.rewrite(&self.get_context(), self.shape()),
}
} else {
None
Expand Down Expand Up @@ -421,9 +429,7 @@ impl<'a> FmtVisitor<'a> {
false,
);

let shape = Shape::indented(self.block_indent, self.config)
.sub_width(2)
.unwrap();
let shape = self.shape().sub_width(2).unwrap();
let fmt = ListFormatting {
tactic: DefinitiveListTactic::Vertical,
separator: ",",
Expand Down Expand Up @@ -451,7 +457,7 @@ impl<'a> FmtVisitor<'a> {

let context = self.get_context();
let indent = self.block_indent;
let shape = Shape::indented(indent, self.config);
let shape = self.shape();
let attrs_str = try_opt!(field.node.attrs.rewrite(&context, shape));
let lo = field
.node
Expand Down
46 changes: 24 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,32 +91,46 @@ macro_rules! span_with_attrs_lo_hi {
}
}
}

macro_rules! span_with_attrs {
($this:ident) => {
span_with_attrs_lo_hi!($this, $this.span.lo, $this.span.hi)
}
}

impl Spanned for ast::Expr {
fn span(&self) -> Span {
span_with_attrs!(self)
macro_rules! implement_spanned {
($this:ty) => {
impl Spanned for $this {
fn span(&self) -> Span {
span_with_attrs!(self)
}
}
}
}

impl Spanned for ast::Item {
fn span(&self) -> Span {
span_with_attrs!(self)
}
}
// Implement `Spanned` for structs with `attrs` field.
implement_spanned!(ast::Expr);
implement_spanned!(ast::Field);
implement_spanned!(ast::ForeignItem);
implement_spanned!(ast::Item);
implement_spanned!(ast::Local);

impl Spanned for ast::Stmt {
fn span(&self) -> Span {
match self.node {
// Cover attributes
ast::StmtKind::Local(ref local) => mk_sp(local.span().lo, self.span.hi),
ast::StmtKind::Item(ref item) => mk_sp(item.span().lo, self.span.hi),
ast::StmtKind::Expr(ref expr) | ast::StmtKind::Semi(ref expr) => {
mk_sp(expr.span().lo, self.span.hi)
}
_ => self.span,
ast::StmtKind::Mac(ref mac) => {
let (_, _, ref attrs) = **mac;
if attrs.is_empty() {
self.span
} else {
mk_sp(attrs[0].span.lo, self.span.hi)
}
}
}
}
}
Expand Down Expand Up @@ -155,12 +169,6 @@ impl Spanned for ast::StructField {
}
}

impl Spanned for ast::Field {
fn span(&self) -> Span {
span_with_attrs!(self)
}
}

impl Spanned for ast::WherePredicate {
fn span(&self) -> Span {
match *self {
Expand Down Expand Up @@ -208,12 +216,6 @@ impl Spanned for ast::TyParamBound {
}
}

impl Spanned for ast::ForeignItem {
fn span(&self) -> Span {
span_with_attrs!(self)
}
}

#[derive(Copy, Clone, Debug)]
pub struct Indent {
// Width of the block indent, in characters. Must be a multiple of
Expand Down
Loading