mirror of
https://github.com/rust-lang/rust.git
synced 2025-01-19 11:12:43 +00:00
Merge branch 'master' of https://github.com/rust-lang-nursery/rustfmt into config
This commit is contained in:
commit
c3c2e823cb
40
Cargo.lock
generated
40
Cargo.lock
generated
@ -11,15 +11,15 @@ dependencies = [
|
||||
"log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"multimap 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"regex 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde_derive 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde_derive 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde_json 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"strings 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"strings 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"syntex_errors 0.58.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"syntex_syntax 0.58.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"term 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"toml 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"unicode-segmentation 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"toml 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"unicode-segmentation 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"walkdir 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
@ -154,12 +154,12 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.2"
|
||||
version = "1.0.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.2"
|
||||
version = "1.0.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
@ -184,12 +184,12 @@ dependencies = [
|
||||
"dtoa 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"itoa 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"num-traits 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "strings"
|
||||
version = "0.0.1"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
@ -257,7 +257,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "thread-id"
|
||||
version = "3.0.0"
|
||||
version = "3.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
@ -269,21 +269,21 @@ name = "thread_local"
|
||||
version = "0.3.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"thread-id 3.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"thread-id 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"unreachable 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "toml"
|
||||
version = "0.4.0"
|
||||
version = "0.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"serde 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-segmentation"
|
||||
version = "1.1.0"
|
||||
version = "1.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
@ -350,21 +350,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
"checksum regex-syntax 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2f9191b1f57603095f105d317e375d19b1c9c5c3185ea9633a99a6dcbed04457"
|
||||
"checksum rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)" = "dcf128d1287d2ea9d80910b5f1120d0b8eede3fbf1abe91c40d39ea7d51e6fda"
|
||||
"checksum same-file 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d931a44fdaa43b8637009e7632a02adc4f2b2e0733c08caa4cf00e8da4a117a7"
|
||||
"checksum serde 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "3b46a59dd63931010fdb1d88538513f3279090d88b5c22ef4fe8440cfffcc6e3"
|
||||
"checksum serde_derive 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6c06b68790963518008b8ae0152d48be4bbbe77015d2c717f6282eea1824be9a"
|
||||
"checksum serde 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "38a3db3a5757f68069aba764b793823ea9fb9717c42c016f8903f8add50f508a"
|
||||
"checksum serde_derive 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "e46ef71ee001a4279a4513e79a6ebbb59da3a4987bf77a6df2e5534cd6f21d82"
|
||||
"checksum serde_derive_internals 0.15.0 (registry+https://github.com/rust-lang/crates.io-index)" = "021c338d22c7e30f957a6ab7e388cb6098499dda9fd4ba1661ee074ca7a180d1"
|
||||
"checksum serde_json 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "48b04779552e92037212c3615370f6bd57a40ebba7f20e554ff9f55e41a69a7b"
|
||||
"checksum strings 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "54f86446ab480b4f60782188f4f78886465c5793aee248cbb48b7fdc0d022420"
|
||||
"checksum strings 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "da75d8bf2c4d210d63dd09581a041b036001f9f6e03d9b151dbff810fb7ba26a"
|
||||
"checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad"
|
||||
"checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6"
|
||||
"checksum syntex_errors 0.58.1 (registry+https://github.com/rust-lang/crates.io-index)" = "867cc5c2d7140ae7eaad2ae9e8bf39cb18a67ca651b7834f88d46ca98faadb9c"
|
||||
"checksum syntex_pos 0.58.1 (registry+https://github.com/rust-lang/crates.io-index)" = "13ad4762fe52abc9f4008e85c4fb1b1fe3aa91ccb99ff4826a439c7c598e1047"
|
||||
"checksum syntex_syntax 0.58.1 (registry+https://github.com/rust-lang/crates.io-index)" = "6e0e4dbae163dd98989464c23dd503161b338790640e11537686f2ef0f25c791"
|
||||
"checksum term 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)" = "d168af3930b369cfe245132550579d47dfd873d69470755a19c2c6568dbbd989"
|
||||
"checksum thread-id 3.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4437c97558c70d129e40629a5b385b3fb1ffac301e63941335e4d354081ec14a"
|
||||
"checksum thread-id 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8df7875b676fddfadffd96deea3b1124e5ede707d4884248931077518cf1f773"
|
||||
"checksum thread_local 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c85048c6260d17cf486ceae3282d9fb6b90be220bf5b28c400f5485ffc29f0c7"
|
||||
"checksum toml 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3063405db158de3dce8efad5fc89cf1baffb9501a3647dc9505ba109694ce31f"
|
||||
"checksum unicode-segmentation 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "18127285758f0e2c6cf325bb3f3d138a12fee27de4f23e146cd6a179f26c2cf3"
|
||||
"checksum toml 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4cc5dbfb20a481e64b99eb7ae280859ec76730c7191570ba5edaa962394edb0a"
|
||||
"checksum unicode-segmentation 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a8083c594e02b8ae1654ae26f0ade5158b119bd88ad0e8227a5d8fcd72407946"
|
||||
"checksum unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc"
|
||||
"checksum unreachable 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1f2ae5ddb18e1c92664717616dd9549dde73f539f01bd7b77c2edb2446bdff91"
|
||||
"checksum utf8-ranges 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "662fab6525a98beff2921d7f61a39e7d59e0b425ebc7d0d9e66d316e55124122"
|
||||
|
@ -26,7 +26,7 @@ serde_json = "1.0"
|
||||
unicode-segmentation = "1.0.0"
|
||||
regex = "0.2"
|
||||
term = "0.4"
|
||||
strings = "0.0.1"
|
||||
strings = "0.1"
|
||||
diff = "0.1"
|
||||
syntex_syntax = "0.58"
|
||||
syntex_errors = "0.58"
|
||||
|
@ -5,6 +5,11 @@ A tool for formatting Rust code according to style guidelines.
|
||||
If you'd like to help out (and you should, it's a fun project!), see
|
||||
[Contributing.md](Contributing.md).
|
||||
|
||||
If you want latest and greatest, you should use the [libsyntax](https://github.com/rust-lang-nursery/rustfmt/tree/libsyntax)
|
||||
branch. It supports some newer Rust syntax which is missing from master and
|
||||
fixes some bugs. However, it links against libsyntax from the Rust compiler, so
|
||||
you must be using a nightly version of Rust to use it.
|
||||
|
||||
## Quick start
|
||||
|
||||
To install:
|
||||
|
@ -174,7 +174,8 @@ pub fn rewrite_chain(expr: &ast::Expr, context: &RewriteContext, shape: Shape) -
|
||||
let almost_total = rewrites[..rewrites.len() - 1]
|
||||
.iter()
|
||||
.fold(0, |a, b| a + first_line_width(b)) + parent_rewrite.len();
|
||||
let one_line_len = rewrites.iter().fold(0, |a, r| a + r.len()) + parent_rewrite.len();
|
||||
let one_line_len = rewrites.iter().fold(0, |a, r| a + first_line_width(r)) +
|
||||
parent_rewrite.len();
|
||||
|
||||
let veto_single_line = if one_line_len > context.config.chain_one_line_max() {
|
||||
if rewrites.len() > 1 {
|
||||
@ -248,13 +249,11 @@ pub fn rewrite_chain(expr: &ast::Expr, context: &RewriteContext, shape: Shape) -
|
||||
|
||||
// True if the chain is only `?`s.
|
||||
fn chain_only_try(exprs: &[ast::Expr]) -> bool {
|
||||
exprs
|
||||
.iter()
|
||||
.all(|e| if let ast::ExprKind::Try(_) = e.node {
|
||||
true
|
||||
} else {
|
||||
false
|
||||
})
|
||||
exprs.iter().all(|e| if let ast::ExprKind::Try(_) = e.node {
|
||||
true
|
||||
} else {
|
||||
false
|
||||
})
|
||||
}
|
||||
|
||||
pub fn rewrite_try(expr: &ast::Expr,
|
||||
|
@ -1044,11 +1044,10 @@ impl<'a> Rewrite for ControlFlow<'a> {
|
||||
let between_kwd_else_block_comment =
|
||||
extract_comment(between_kwd_else_block, context, shape);
|
||||
|
||||
let after_else =
|
||||
mk_sp(context
|
||||
.codemap
|
||||
.span_after(mk_sp(self.block.span.hi, else_block.span.lo), "else"),
|
||||
else_block.span.lo);
|
||||
let after_else = mk_sp(context.codemap.span_after(mk_sp(self.block.span.hi,
|
||||
else_block.span.lo),
|
||||
"else"),
|
||||
else_block.span.lo);
|
||||
let after_else_comment = extract_comment(after_else, context, shape);
|
||||
|
||||
let between_sep = match context.config.control_brace_style() {
|
||||
|
63
src/items.rs
63
src/items.rs
@ -457,11 +457,8 @@ impl<'a> FmtVisitor<'a> {
|
||||
}
|
||||
|
||||
let indent = self.block_indent;
|
||||
let mut result = try_opt!(field
|
||||
.node
|
||||
.attrs
|
||||
.rewrite(&self.get_context(),
|
||||
Shape::indented(indent, self.config)));
|
||||
let mut result = try_opt!(field.node.attrs.rewrite(&self.get_context(),
|
||||
Shape::indented(indent, self.config)));
|
||||
if !result.is_empty() {
|
||||
result.push('\n');
|
||||
result.push_str(&indent.to_string(self.config));
|
||||
@ -1203,9 +1200,9 @@ impl Rewrite for ast::StructField {
|
||||
|
||||
let name = self.ident;
|
||||
let vis = format_visibility(&self.vis);
|
||||
let mut attr_str =
|
||||
try_opt!(self.attrs
|
||||
.rewrite(context, Shape::indented(shape.indent, context.config)));
|
||||
let mut attr_str = try_opt!(self.attrs.rewrite(context,
|
||||
Shape::indented(shape.indent,
|
||||
context.config)));
|
||||
if !attr_str.is_empty() {
|
||||
attr_str.push('\n');
|
||||
attr_str.push_str(&shape.indent.to_string(context.config));
|
||||
@ -1225,9 +1222,9 @@ impl Rewrite for ast::StructField {
|
||||
|
||||
let last_line_width = last_line_width(&result) + type_annotation_spacing.1.len();
|
||||
let budget = try_opt!(shape.width.checked_sub(last_line_width));
|
||||
let ty_rewritten = self.ty
|
||||
.rewrite(context,
|
||||
Shape::legacy(budget, shape.indent + last_line_width));
|
||||
let ty_rewritten =
|
||||
self.ty.rewrite(context,
|
||||
Shape::legacy(budget, shape.indent + last_line_width));
|
||||
match ty_rewritten {
|
||||
Some(ref ty) if ty.contains('\n') => {
|
||||
let new_ty = rewrite_type_in_next_line();
|
||||
@ -1381,9 +1378,8 @@ impl Rewrite for ast::FunctionRetTy {
|
||||
impl Rewrite for ast::Arg {
|
||||
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
|
||||
if is_named_arg(self) {
|
||||
let mut result = try_opt!(self.pat
|
||||
.rewrite(context,
|
||||
Shape::legacy(shape.width, shape.indent)));
|
||||
let mut result = try_opt!(self.pat.rewrite(context,
|
||||
Shape::legacy(shape.width, shape.indent)));
|
||||
|
||||
if self.ty.node != ast::TyKind::Infer {
|
||||
if context.config.space_before_type_annotation() {
|
||||
@ -1394,10 +1390,9 @@ impl Rewrite for ast::Arg {
|
||||
result.push_str(" ");
|
||||
}
|
||||
let max_width = try_opt!(shape.width.checked_sub(result.len()));
|
||||
let ty_str = try_opt!(self.ty
|
||||
.rewrite(context,
|
||||
Shape::legacy(max_width,
|
||||
shape.indent + result.len())));
|
||||
let ty_str = try_opt!(self.ty.rewrite(context,
|
||||
Shape::legacy(max_width,
|
||||
shape.indent + result.len())));
|
||||
result.push_str(&ty_str);
|
||||
}
|
||||
|
||||
@ -1626,7 +1621,8 @@ fn rewrite_fn_base(context: &RewriteContext,
|
||||
fd.variadic,
|
||||
generics_str.contains('\n')));
|
||||
|
||||
let multi_line_arg_str = arg_str.contains('\n');
|
||||
let multi_line_arg_str = arg_str.contains('\n') ||
|
||||
arg_str.chars().last().map_or(false, |c| c == ',');
|
||||
|
||||
let put_args_in_block = match context.config.fn_args_layout() {
|
||||
IndentStyle::Block => multi_line_arg_str || generics_str.contains('\n'),
|
||||
@ -1697,9 +1693,8 @@ fn rewrite_fn_base(context: &RewriteContext,
|
||||
if multi_line_ret_str || ret_should_indent {
|
||||
// Now that we know the proper indent and width, we need to
|
||||
// re-layout the return type.
|
||||
let ret_str = try_opt!(fd.output
|
||||
.rewrite(context,
|
||||
Shape::indented(ret_indent, context.config)));
|
||||
let ret_str = try_opt!(fd.output.rewrite(context,
|
||||
Shape::indented(ret_indent, context.config)));
|
||||
result.push_str(&ret_str);
|
||||
} else {
|
||||
result.push_str(&ret_str);
|
||||
@ -1859,12 +1854,16 @@ fn rewrite_args(context: &RewriteContext,
|
||||
arg_items.extend(more_items);
|
||||
}
|
||||
|
||||
let fits_in_one_line = !generics_str_contains_newline &&
|
||||
(arg_items.len() == 0 ||
|
||||
arg_items.len() == 1 && arg_item_strs[0].len() <= one_line_budget);
|
||||
|
||||
for (item, arg) in arg_items.iter_mut().zip(arg_item_strs) {
|
||||
item.item = Some(arg);
|
||||
}
|
||||
|
||||
let (indent, trailing_comma, end_with_newline) = match context.config.fn_args_layout() {
|
||||
IndentStyle::Block if !generics_str_contains_newline && arg_items.len() <= 1 => {
|
||||
IndentStyle::Block if fits_in_one_line => {
|
||||
(indent.block_indent(context.config), SeparatorTactic::Never, true)
|
||||
}
|
||||
IndentStyle::Block => {
|
||||
@ -1989,16 +1988,14 @@ fn rewrite_generics(context: &RewriteContext,
|
||||
.map(|ty_param| ty_param.rewrite(context, Shape::legacy(h_budget, offset)));
|
||||
|
||||
// Extract comments between generics.
|
||||
let lt_spans = lifetimes
|
||||
.iter()
|
||||
.map(|l| {
|
||||
let hi = if l.bounds.is_empty() {
|
||||
l.lifetime.span.hi
|
||||
} else {
|
||||
l.bounds[l.bounds.len() - 1].span.hi
|
||||
};
|
||||
mk_sp(l.lifetime.span.lo, hi)
|
||||
});
|
||||
let lt_spans = lifetimes.iter().map(|l| {
|
||||
let hi = if l.bounds.is_empty() {
|
||||
l.lifetime.span.hi
|
||||
} else {
|
||||
l.bounds[l.bounds.len() - 1].span.hi
|
||||
};
|
||||
mk_sp(l.lifetime.span.lo, hi)
|
||||
});
|
||||
let ty_spans = tys.iter().map(span_for_ty_param);
|
||||
|
||||
let items = itemize_list(context.codemap,
|
||||
|
200
src/lists.rs
200
src/lists.rs
@ -339,116 +339,114 @@ impl<'a, T, I, F1, F2, F3> Iterator for ListItems<'a, I, F1, F2, F3>
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
let white_space: &[_] = &[' ', '\t'];
|
||||
|
||||
self.inner
|
||||
.next()
|
||||
.map(|item| {
|
||||
let mut new_lines = false;
|
||||
// Pre-comment
|
||||
let pre_snippet = self.codemap
|
||||
.span_to_snippet(codemap::mk_sp(self.prev_span_end, (self.get_lo)(&item)))
|
||||
.unwrap();
|
||||
let trimmed_pre_snippet = pre_snippet.trim();
|
||||
let has_pre_comment = trimmed_pre_snippet.contains("//") ||
|
||||
trimmed_pre_snippet.contains("/*");
|
||||
let pre_comment = if has_pre_comment {
|
||||
Some(trimmed_pre_snippet.to_owned())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
self.inner.next().map(|item| {
|
||||
let mut new_lines = false;
|
||||
// Pre-comment
|
||||
let pre_snippet = self.codemap
|
||||
.span_to_snippet(codemap::mk_sp(self.prev_span_end, (self.get_lo)(&item)))
|
||||
.unwrap();
|
||||
let trimmed_pre_snippet = pre_snippet.trim();
|
||||
let has_pre_comment = trimmed_pre_snippet.contains("//") ||
|
||||
trimmed_pre_snippet.contains("/*");
|
||||
let pre_comment = if has_pre_comment {
|
||||
Some(trimmed_pre_snippet.to_owned())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
// Post-comment
|
||||
let next_start = match self.inner.peek() {
|
||||
Some(next_item) => (self.get_lo)(next_item),
|
||||
None => self.next_span_start,
|
||||
};
|
||||
let post_snippet = self.codemap
|
||||
.span_to_snippet(codemap::mk_sp((self.get_hi)(&item), next_start))
|
||||
.unwrap();
|
||||
// Post-comment
|
||||
let next_start = match self.inner.peek() {
|
||||
Some(next_item) => (self.get_lo)(next_item),
|
||||
None => self.next_span_start,
|
||||
};
|
||||
let post_snippet = self.codemap
|
||||
.span_to_snippet(codemap::mk_sp((self.get_hi)(&item), next_start))
|
||||
.unwrap();
|
||||
|
||||
let comment_end = match self.inner.peek() {
|
||||
Some(..) => {
|
||||
let mut block_open_index = post_snippet.find("/*");
|
||||
// check if it realy is a block comment (and not //*)
|
||||
if let Some(i) = block_open_index {
|
||||
if i > 0 && &post_snippet[i - 1..i] == "/" {
|
||||
block_open_index = None;
|
||||
}
|
||||
}
|
||||
let newline_index = post_snippet.find('\n');
|
||||
let separator_index = post_snippet.find_uncommented(",").unwrap();
|
||||
|
||||
match (block_open_index, newline_index) {
|
||||
// Separator before comment, with the next item on same line.
|
||||
// Comment belongs to next item.
|
||||
(Some(i), None) if i > separator_index => separator_index + 1,
|
||||
// Block-style post-comment before the separator.
|
||||
(Some(i), None) => {
|
||||
cmp::max(find_comment_end(&post_snippet[i..]).unwrap() + i,
|
||||
separator_index + 1)
|
||||
}
|
||||
// Block-style post-comment. Either before or after the separator.
|
||||
(Some(i), Some(j)) if i < j => {
|
||||
cmp::max(find_comment_end(&post_snippet[i..]).unwrap() + i,
|
||||
separator_index + 1)
|
||||
}
|
||||
// Potential *single* line comment.
|
||||
(_, Some(j)) if j > separator_index => j + 1,
|
||||
_ => post_snippet.len(),
|
||||
let comment_end = match self.inner.peek() {
|
||||
Some(..) => {
|
||||
let mut block_open_index = post_snippet.find("/*");
|
||||
// check if it realy is a block comment (and not //*)
|
||||
if let Some(i) = block_open_index {
|
||||
if i > 0 && &post_snippet[i - 1..i] == "/" {
|
||||
block_open_index = None;
|
||||
}
|
||||
}
|
||||
None => {
|
||||
post_snippet
|
||||
.find_uncommented(self.terminator)
|
||||
.unwrap_or(post_snippet.len())
|
||||
}
|
||||
};
|
||||
let newline_index = post_snippet.find('\n');
|
||||
let separator_index = post_snippet.find_uncommented(",").unwrap();
|
||||
|
||||
if !post_snippet.is_empty() && comment_end > 0 {
|
||||
// Account for extra whitespace between items. This is fiddly
|
||||
// because of the way we divide pre- and post- comments.
|
||||
|
||||
// Everything from the separator to the next item.
|
||||
let test_snippet = &post_snippet[comment_end - 1..];
|
||||
let first_newline = test_snippet.find('\n').unwrap_or(test_snippet.len());
|
||||
// From the end of the first line of comments.
|
||||
let test_snippet = &test_snippet[first_newline..];
|
||||
let first = test_snippet
|
||||
.find(|c: char| !c.is_whitespace())
|
||||
.unwrap_or(test_snippet.len());
|
||||
// From the end of the first line of comments to the next non-whitespace char.
|
||||
let test_snippet = &test_snippet[..first];
|
||||
|
||||
if test_snippet.chars().filter(|c| c == &'\n').count() > 1 {
|
||||
// There were multiple line breaks which got trimmed to nothing.
|
||||
new_lines = true;
|
||||
match (block_open_index, newline_index) {
|
||||
// Separator before comment, with the next item on same line.
|
||||
// Comment belongs to next item.
|
||||
(Some(i), None) if i > separator_index => separator_index + 1,
|
||||
// Block-style post-comment before the separator.
|
||||
(Some(i), None) => {
|
||||
cmp::max(find_comment_end(&post_snippet[i..]).unwrap() + i,
|
||||
separator_index + 1)
|
||||
}
|
||||
// Block-style post-comment. Either before or after the separator.
|
||||
(Some(i), Some(j)) if i < j => {
|
||||
cmp::max(find_comment_end(&post_snippet[i..]).unwrap() + i,
|
||||
separator_index + 1)
|
||||
}
|
||||
// Potential *single* line comment.
|
||||
(_, Some(j)) if j > separator_index => j + 1,
|
||||
_ => post_snippet.len(),
|
||||
}
|
||||
}
|
||||
|
||||
// Cleanup post-comment: strip separators and whitespace.
|
||||
self.prev_span_end = (self.get_hi)(&item) + BytePos(comment_end as u32);
|
||||
let post_snippet = post_snippet[..comment_end].trim();
|
||||
|
||||
let post_snippet_trimmed = if post_snippet.starts_with(',') {
|
||||
post_snippet[1..].trim_matches(white_space)
|
||||
} else if post_snippet.ends_with(',') {
|
||||
post_snippet[..(post_snippet.len() - 1)].trim_matches(white_space)
|
||||
} else {
|
||||
None => {
|
||||
post_snippet
|
||||
};
|
||||
|
||||
let post_comment = if !post_snippet_trimmed.is_empty() {
|
||||
Some(post_snippet_trimmed.to_owned())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
ListItem {
|
||||
pre_comment: pre_comment,
|
||||
item: (self.get_item_string)(&item),
|
||||
post_comment: post_comment,
|
||||
new_lines: new_lines,
|
||||
.find_uncommented(self.terminator)
|
||||
.unwrap_or(post_snippet.len())
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
if !post_snippet.is_empty() && comment_end > 0 {
|
||||
// Account for extra whitespace between items. This is fiddly
|
||||
// because of the way we divide pre- and post- comments.
|
||||
|
||||
// Everything from the separator to the next item.
|
||||
let test_snippet = &post_snippet[comment_end - 1..];
|
||||
let first_newline = test_snippet.find('\n').unwrap_or(test_snippet.len());
|
||||
// From the end of the first line of comments.
|
||||
let test_snippet = &test_snippet[first_newline..];
|
||||
let first = test_snippet
|
||||
.find(|c: char| !c.is_whitespace())
|
||||
.unwrap_or(test_snippet.len());
|
||||
// From the end of the first line of comments to the next non-whitespace char.
|
||||
let test_snippet = &test_snippet[..first];
|
||||
|
||||
if test_snippet.chars().filter(|c| c == &'\n').count() > 1 {
|
||||
// There were multiple line breaks which got trimmed to nothing.
|
||||
new_lines = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Cleanup post-comment: strip separators and whitespace.
|
||||
self.prev_span_end = (self.get_hi)(&item) + BytePos(comment_end as u32);
|
||||
let post_snippet = post_snippet[..comment_end].trim();
|
||||
|
||||
let post_snippet_trimmed = if post_snippet.starts_with(',') {
|
||||
post_snippet[1..].trim_matches(white_space)
|
||||
} else if post_snippet.ends_with(',') {
|
||||
post_snippet[..(post_snippet.len() - 1)].trim_matches(white_space)
|
||||
} else {
|
||||
post_snippet
|
||||
};
|
||||
|
||||
let post_comment = if !post_snippet_trimmed.is_empty() {
|
||||
Some(post_snippet_trimmed.to_owned())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
ListItem {
|
||||
pre_comment: pre_comment,
|
||||
item: (self.get_item_string)(&item),
|
||||
post_comment: post_comment,
|
||||
new_lines: new_lines,
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -159,15 +159,13 @@ pub fn rewrite_macro(mac: &ast::Mac,
|
||||
// Format macro invocation as array literal.
|
||||
let extra_offset = macro_name.len();
|
||||
let shape = try_opt!(shape.shrink_left(extra_offset));
|
||||
let rewrite = try_opt!(rewrite_array(expr_vec.iter().map(|x| &**x),
|
||||
mk_sp(context
|
||||
.codemap
|
||||
.span_after(mac.span,
|
||||
original_style
|
||||
.opener()),
|
||||
mac.span.hi - BytePos(1)),
|
||||
context,
|
||||
shape));
|
||||
let rewrite =
|
||||
try_opt!(rewrite_array(expr_vec.iter().map(|x| &**x),
|
||||
mk_sp(context.codemap.span_after(mac.span,
|
||||
original_style.opener()),
|
||||
mac.span.hi - BytePos(1)),
|
||||
context,
|
||||
shape));
|
||||
|
||||
Some(format!("{}{}", macro_name, rewrite))
|
||||
}
|
||||
|
@ -158,13 +158,12 @@ impl<'a> FmtVisitor<'a> {
|
||||
self.config.max_width() -
|
||||
self.block_indent.width());
|
||||
|
||||
self.buffer
|
||||
.push_str(&rewrite_comment(subslice,
|
||||
false,
|
||||
Shape::legacy(comment_width,
|
||||
self.block_indent),
|
||||
self.config)
|
||||
.unwrap());
|
||||
self.buffer.push_str(&rewrite_comment(subslice,
|
||||
false,
|
||||
Shape::legacy(comment_width,
|
||||
self.block_indent),
|
||||
self.config)
|
||||
.unwrap());
|
||||
|
||||
last_wspace = None;
|
||||
line_start = offset + subslice.len();
|
||||
|
@ -39,11 +39,10 @@ impl Rewrite for Pat {
|
||||
let sub_pat = match *sub_pat {
|
||||
Some(ref p) => {
|
||||
// 3 - ` @ `.
|
||||
let width = try_opt!(shape
|
||||
.width
|
||||
.checked_sub(prefix.len() + mut_infix.len() +
|
||||
id_str.len() +
|
||||
3));
|
||||
let width = try_opt!(shape.width.checked_sub(prefix.len() +
|
||||
mut_infix.len() +
|
||||
id_str.len() +
|
||||
3));
|
||||
format!(" @ {}",
|
||||
try_opt!(p.rewrite(context, Shape::legacy(width, shape.indent))))
|
||||
}
|
||||
@ -331,13 +330,11 @@ fn rewrite_tuple_pat(pats: &[ptr::P<ast::Pat>],
|
||||
fn count_wildcard_suffix_len(items: &[ListItem]) -> usize {
|
||||
let mut suffix_len = 0;
|
||||
|
||||
for item in items
|
||||
.iter()
|
||||
.rev()
|
||||
.take_while(|i| match i.item {
|
||||
Some(ref internal_string) if internal_string == "_" => true,
|
||||
_ => false,
|
||||
}) {
|
||||
for item in items.iter().rev().take_while(|i| match i.item {
|
||||
Some(ref internal_string) if internal_string ==
|
||||
"_" => true,
|
||||
_ => false,
|
||||
}) {
|
||||
suffix_len += 1;
|
||||
|
||||
if item.pre_comment.is_some() || item.post_comment.is_some() {
|
||||
|
37
src/types.rs
37
src/types.rs
@ -163,11 +163,10 @@ impl<'a> Rewrite for SegmentParam<'a> {
|
||||
TypeDensity::Compressed => format!("{}=", binding.ident),
|
||||
};
|
||||
let budget = try_opt!(shape.width.checked_sub(result.len()));
|
||||
let rewrite =
|
||||
try_opt!(binding
|
||||
.ty
|
||||
.rewrite(context,
|
||||
Shape::legacy(budget, shape.indent + result.len())));
|
||||
let rewrite = try_opt!(binding.ty.rewrite(context,
|
||||
Shape::legacy(budget,
|
||||
shape.indent +
|
||||
result.len())));
|
||||
result.push_str(&rewrite);
|
||||
Some(result)
|
||||
}
|
||||
@ -563,10 +562,10 @@ impl Rewrite for ast::PolyTraitRef {
|
||||
// 6 is "for<> ".len()
|
||||
let extra_offset = lifetime_str.len() + 6;
|
||||
let max_path_width = try_opt!(shape.width.checked_sub(extra_offset));
|
||||
let path_str = try_opt!(self.trait_ref
|
||||
.rewrite(context,
|
||||
Shape::legacy(max_path_width,
|
||||
shape.indent + extra_offset)));
|
||||
let path_str = try_opt!(self.trait_ref.rewrite(context,
|
||||
Shape::legacy(max_path_width,
|
||||
shape.indent +
|
||||
extra_offset)));
|
||||
|
||||
Some(if context.config.spaces_within_angle_brackets() && lifetime_str.len() > 0 {
|
||||
format!("for< {} > {}", lifetime_str, path_str)
|
||||
@ -613,22 +612,20 @@ impl Rewrite for ast::Ty {
|
||||
format!("&{} {}{}",
|
||||
lt_str,
|
||||
mut_str,
|
||||
try_opt!(mt.ty
|
||||
.rewrite(context,
|
||||
Shape::legacy(budget,
|
||||
shape.indent + 2 +
|
||||
mut_len +
|
||||
lt_len))))
|
||||
try_opt!(mt.ty.rewrite(context,
|
||||
Shape::legacy(budget,
|
||||
shape.indent + 2 +
|
||||
mut_len +
|
||||
lt_len))))
|
||||
}
|
||||
None => {
|
||||
let budget = try_opt!(shape.width.checked_sub(1 + mut_len));
|
||||
format!("&{}{}",
|
||||
mut_str,
|
||||
try_opt!(mt.ty
|
||||
.rewrite(context,
|
||||
Shape::legacy(budget,
|
||||
shape.indent + 1 +
|
||||
mut_len))))
|
||||
try_opt!(mt.ty.rewrite(context,
|
||||
Shape::legacy(budget,
|
||||
shape.indent + 1 +
|
||||
mut_len))))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -8,3 +8,9 @@ fn lorem(ipsum: usize) {}
|
||||
fn lorem(ipsum: usize, dolor: usize, sit: usize, amet: usize, consectetur: usize, adipiscing: usize, elit: usize) {
|
||||
// body
|
||||
}
|
||||
|
||||
// #1441
|
||||
extern "system" {
|
||||
pub fn GetConsoleHistoryInfo(console_history_info: *mut ConsoleHistoryInfo) -> Boooooooooooooool;
|
||||
}
|
||||
|
||||
|
12
tests/source/issue-1366.rs
Normal file
12
tests/source/issue-1366.rs
Normal file
@ -0,0 +1,12 @@
|
||||
fn main() {
|
||||
fn f() -> Option<i32> {
|
||||
Some("fffffffsssssssssddddssssfffffddddff").map(|s| s).map(|s| s.to_string()).map(|res| {
|
||||
match Some(res) {
|
||||
Some(ref s) if s == "" => 41,
|
||||
Some(_) => 42,
|
||||
_ => 43,
|
||||
}
|
||||
})
|
||||
}
|
||||
println!("{:?}", f())
|
||||
}
|
@ -303,21 +303,18 @@ fn read_significant_comments(file_name: &str) -> HashMap<String, String> {
|
||||
.map(|line| line.expect("Failed getting line"))
|
||||
.take_while(|line| line_regex.is_match(&line))
|
||||
.filter_map(|line| {
|
||||
regex
|
||||
.captures_iter(&line)
|
||||
.next()
|
||||
.map(|capture| {
|
||||
(capture
|
||||
.get(1)
|
||||
.expect("Couldn't unwrap capture")
|
||||
.as_str()
|
||||
.to_owned(),
|
||||
capture
|
||||
.get(2)
|
||||
.expect("Couldn't unwrap capture")
|
||||
.as_str()
|
||||
.to_owned())
|
||||
})
|
||||
regex.captures_iter(&line).next().map(|capture| {
|
||||
(capture
|
||||
.get(1)
|
||||
.expect("Couldn't unwrap capture")
|
||||
.as_str()
|
||||
.to_owned(),
|
||||
capture
|
||||
.get(2)
|
||||
.expect("Couldn't unwrap capture")
|
||||
.as_str()
|
||||
.to_owned())
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
@ -16,3 +16,10 @@ fn lorem(
|
||||
) {
|
||||
// body
|
||||
}
|
||||
|
||||
// #1441
|
||||
extern "system" {
|
||||
pub fn GetConsoleHistoryInfo(
|
||||
console_history_info: *mut ConsoleHistoryInfo,
|
||||
) -> Boooooooooooooool;
|
||||
}
|
||||
|
13
tests/target/issue-1366.rs
Normal file
13
tests/target/issue-1366.rs
Normal file
@ -0,0 +1,13 @@
|
||||
fn main() {
|
||||
fn f() -> Option<i32> {
|
||||
Some("fffffffsssssssssddddssssfffffddddff")
|
||||
.map(|s| s)
|
||||
.map(|s| s.to_string())
|
||||
.map(|res| match Some(res) {
|
||||
Some(ref s) if s == "" => 41,
|
||||
Some(_) => 42,
|
||||
_ => 43,
|
||||
})
|
||||
}
|
||||
println!("{:?}", f())
|
||||
}
|
@ -29,14 +29,13 @@ fn b() {
|
||||
}
|
||||
|
||||
fn issue550() {
|
||||
self.visitor
|
||||
.visit_volume(self.level.sector_id(sector),
|
||||
(floor_y,
|
||||
if is_sky_flat(ceil_tex) {
|
||||
from_wad_height(self.height_range.1)
|
||||
} else {
|
||||
ceil_y
|
||||
}));
|
||||
self.visitor.visit_volume(self.level.sector_id(sector),
|
||||
(floor_y,
|
||||
if is_sky_flat(ceil_tex) {
|
||||
from_wad_height(self.height_range.1)
|
||||
} else {
|
||||
ceil_y
|
||||
}));
|
||||
}
|
||||
|
||||
fn issue775() {
|
||||
|
Loading…
Reference in New Issue
Block a user