Format closed ranges
This commit is contained in:
parent
d3b18d0b45
commit
9eee93306a
4 changed files with 57 additions and 24 deletions
16
Cargo.lock
generated
16
Cargo.lock
generated
|
@ -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]]
|
||||
|
|
40
src/expr.rs
40
src/expr.rs
|
@ -185,25 +185,27 @@ impl Rewrite for ast::Expr {
|
|||
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
|
||||
|
|
|
@ -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 ... ;
|
||||
}
|
||||
|
|
|
@ -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 ... ;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue