Skip to content

Format closed ranges #892

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 2 commits into from
Mar 31, 2016
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
16 changes: 11 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 26 additions & 26 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,48 +176,48 @@ impl Rewrite for ast::Expr {
ast::ExprKind::Cast(ref expr, ref ty) => {
rewrite_pair(&**expr, &**ty, "", " as ", "", context, width, offset)
}
// TODO(#848): Handle type ascription; rust tracking issue
// https://github.com/rust-lang/rust/issues/23416
ast::ExprKind::Type(_, _) => unimplemented!(),
ast::ExprKind::Index(ref expr, ref index) => {
rewrite_pair(&**expr, &**index, "", "[", "]", context, width, offset)
}
ast::ExprKind::Repeat(ref expr, ref repeats) => {
rewrite_pair(&**expr, &**repeats, "[", "; ", "]", context, width, offset)
}
// TODO(#890): Handle closed ranges; rust tracking issue
// https://github.com/rust-lang/rust/issues/28237
ast::ExprKind::Range(Some(ref lhs), Some(ref rhs), _range_limits) => {
rewrite_pair(&**lhs, &**rhs, "", "..", "", context, width, offset)
}
ast::ExprKind::Range(None, Some(ref rhs), _range_limits) => {
rewrite_unary_prefix(context, "..", &**rhs, width, offset)
}
ast::ExprKind::Range(Some(ref lhs), None, _range_limits) => {
Some(format!("{}..",
try_opt!(lhs.rewrite(context,
try_opt!(width.checked_sub(2)),
offset))))
}
ast::ExprKind::Range(None, None, _range_limits) => {
if width >= 2 {
Some("..".into())
} else {
None
ast::ExprKind::Range(ref lhs, ref rhs, limits) => {
let delim = match limits {
ast::RangeLimits::HalfOpen => "..",
ast::RangeLimits::Closed => "...",
};

match (lhs.as_ref().map(|x| &**x), rhs.as_ref().map(|x| &**x)) {
(Some(ref lhs), Some(ref rhs)) => {
rewrite_pair(&**lhs, &**rhs, "", delim, "", context, width, offset)
}
(None, Some(ref rhs)) => {
rewrite_unary_prefix(context, delim, &**rhs, width, offset)
}
(Some(ref lhs), None) => {
Some(format!("{}{}",
try_opt!(lhs.rewrite(context,
try_opt!(width.checked_sub(delim.len())),
offset)),
delim))
}
(None, None) => wrap_str(delim.into(), context.config.max_width, width, offset),
}
}
// We do not format these expressions yet, but they should still
// satisfy our width restrictions.
ast::ExprKind::InPlace(..) |
ast::ExprKind::InlineAsm(..) => {
ast::ExprKind::InlineAsm(..) |
// TODO(#848): Handle type ascription
ast::ExprKind::Type(_, _) |
// TODO(#867): Handle try shorthand
ast::ExprKind::Try(_) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this the "default"?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it just uses the source code without formatting

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good to know for the future!

wrap_str(context.snippet(self.span),
context.config.max_width,
width,
offset)
}
// TODO(#867): Handle type ascription; rust tracking issue
// https://github.com/rust-lang/rust/issues/31436
ast::ExprKind::Try(_) => unimplemented!(),
};
result.and_then(|res| recover_comment_removed(res, self.span, context, width, offset))
}
Expand Down
12 changes: 12 additions & 0 deletions tests/source/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,15 @@ fn issue767() {
} else if let false = false {
}
}

fn ranges() {
let x = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa .. bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
let y = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ... bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
let z = ... x ;
let infi_range_2 = ... ;

a ... b

// the expr below won't compile for some reason...
// let a = 0 ... ;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I commented on #893, but this isn't meant to compile, so the comment can be removed.

}
13 changes: 13 additions & 0 deletions tests/target/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,16 @@ fn issue767() {
} else if let false = false {
}
}

fn ranges() {
let x = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa..bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
let y =
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
let z = ...x;
let infi_range_2 = ...;

a...b

// the expr below won't compile for some reason...
// let a = 0 ... ;
}