Merge pull request #892 from marcusklaas/closed-ranges

Format closed ranges
This commit is contained in:
Nick Cameron 2016-03-31 13:47:09 +13:00
commit 5b11d2e80f
4 changed files with 62 additions and 31 deletions

16
Cargo.lock generated
View File

@ -6,7 +6,7 @@ dependencies = [
"env_logger 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
"getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 0.1.58 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 0.1.60 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)",
"strings 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"syntex_syntax 0.30.0 (registry+https://github.com/rust-lang/crates.io-index)",
@ -39,7 +39,7 @@ version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"log 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 0.1.58 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 0.1.60 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -77,20 +77,26 @@ dependencies = [
"libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "mempool"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "regex"
version = "0.1.58"
version = "0.1.60"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"aho-corasick 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"memchr 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
"regex-syntax 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"mempool 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"regex-syntax 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
"utf8-ranges 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "regex-syntax"
version = "0.3.0"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]

View File

@ -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(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)
}
ast::ExprKind::Range(None, Some(ref rhs), _range_limits) => {
rewrite_unary_prefix(context, "..", &**rhs, width, offset)
(None, Some(ref rhs)) => {
rewrite_unary_prefix(context, delim, &**rhs, width, offset)
}
ast::ExprKind::Range(Some(ref lhs), None, _range_limits) => {
Some(format!("{}..",
(Some(ref lhs), None) => {
Some(format!("{}{}",
try_opt!(lhs.rewrite(context,
try_opt!(width.checked_sub(2)),
offset))))
try_opt!(width.checked_sub(delim.len())),
offset)),
delim))
}
ast::ExprKind::Range(None, None, _range_limits) => {
if width >= 2 {
Some("..".into())
} else {
None
(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(_) => {
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))
}

View File

@ -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 ... ;
}

View File

@ -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 ... ;
}