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)",
|
"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)",
|
"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)",
|
"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)",
|
"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)",
|
"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)",
|
"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"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"log 0.3.5 (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)",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -77,20 +77,26 @@ dependencies = [
|
||||||
"libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
"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]]
|
[[package]]
|
||||||
name = "regex"
|
name = "regex"
|
||||||
version = "0.1.58"
|
version = "0.1.60"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"aho-corasick 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
"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)",
|
"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)",
|
"utf8-ranges 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "regex-syntax"
|
name = "regex-syntax"
|
||||||
version = "0.3.0"
|
version = "0.3.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
|
||||||
[[package]]
|
[[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) => {
|
ast::ExprKind::Repeat(ref expr, ref repeats) => {
|
||||||
rewrite_pair(&**expr, &**repeats, "[", "; ", "]", context, width, offset)
|
rewrite_pair(&**expr, &**repeats, "[", "; ", "]", context, width, offset)
|
||||||
}
|
}
|
||||||
// TODO(#890): Handle closed ranges; rust tracking issue
|
ast::ExprKind::Range(ref lhs, ref rhs, limits) => {
|
||||||
// https://github.com/rust-lang/rust/issues/28237
|
let delim = match limits {
|
||||||
ast::ExprKind::Range(Some(ref lhs), Some(ref rhs), _range_limits) => {
|
ast::RangeLimits::HalfOpen => "..",
|
||||||
rewrite_pair(&**lhs, &**rhs, "", "..", "", context, width, offset)
|
ast::RangeLimits::Closed => "...",
|
||||||
}
|
};
|
||||||
ast::ExprKind::Range(None, Some(ref rhs), _range_limits) => {
|
|
||||||
rewrite_unary_prefix(context, "..", &**rhs, width, offset)
|
match (lhs.as_ref().map(|x| &**x), rhs.as_ref().map(|x| &**x)) {
|
||||||
}
|
(Some(ref lhs), Some(ref rhs)) => {
|
||||||
ast::ExprKind::Range(Some(ref lhs), None, _range_limits) => {
|
rewrite_pair(&**lhs, &**rhs, "", delim, "", context, width, offset)
|
||||||
Some(format!("{}..",
|
}
|
||||||
try_opt!(lhs.rewrite(context,
|
(None, Some(ref rhs)) => {
|
||||||
try_opt!(width.checked_sub(2)),
|
rewrite_unary_prefix(context, delim, &**rhs, width, offset)
|
||||||
offset))))
|
}
|
||||||
}
|
(Some(ref lhs), None) => {
|
||||||
ast::ExprKind::Range(None, None, _range_limits) => {
|
Some(format!("{}{}",
|
||||||
if width >= 2 {
|
try_opt!(lhs.rewrite(context,
|
||||||
Some("..".into())
|
try_opt!(width.checked_sub(delim.len())),
|
||||||
} else {
|
offset)),
|
||||||
None
|
delim))
|
||||||
|
}
|
||||||
|
(None, None) => wrap_str(delim.into(), context.config.max_width, width, offset),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// We do not format these expressions yet, but they should still
|
// We do not format these expressions yet, but they should still
|
||||||
|
|
|
@ -244,3 +244,15 @@ fn issue767() {
|
||||||
} else if let false = false {
|
} 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 {
|
} 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
Add a link
Reference in a new issue