Simplify iterators

This commit is contained in:
Shotaro Yamada 2019-03-29 17:54:29 +09:00
parent 8e4c20da80
commit ff0683d666
5 changed files with 22 additions and 20 deletions

View File

@ -550,7 +550,7 @@ impl<'a> ChainFormatterShared<'a> {
let almost_total = if extendable {
prev_last_line_width
} else {
self.rewrites.iter().fold(0, |a, b| a + b.len())
self.rewrites.iter().map(|a| a.len()).sum()
} + last.tries;
let one_line_budget = if self.child_count == 1 {
shape.width

View File

@ -1,6 +1,7 @@
use std::borrow::Cow;
use std::cmp::min;
use itertools::Itertools;
use syntax::parse::token::DelimToken;
use syntax::source_map::{BytePos, SourceMap, Span};
use syntax::{ast, ptr};
@ -1246,8 +1247,7 @@ fn rewrite_string_lit(context: &RewriteContext<'_>, span: Span, shape: Shape) ->
if !context.config.format_strings() {
if string_lit
.lines()
.rev()
.skip(1)
.dropping_back(1)
.all(|line| line.ends_with('\\'))
{
let new_indent = shape.visual_indent(1).indent;

View File

@ -577,13 +577,8 @@ pub fn extract_pre_comment(pre_snippet: &str) -> (Option<String>, ListItemCommen
let has_block_comment = trimmed_pre_snippet.ends_with("*/");
let has_single_line_comment = trimmed_pre_snippet.starts_with("//");
if has_block_comment {
let comment_end = pre_snippet.chars().rev().position(|c| c == '/').unwrap();
if pre_snippet
.chars()
.rev()
.take(comment_end + 1)
.any(|c| c == '\n')
{
let comment_end = pre_snippet.rfind(|c| c == '/').unwrap();
if pre_snippet[comment_end..].contains('\n') {
(
Some(trimmed_pre_snippet.to_owned()),
ListItemCommentStyle::DifferentLine,

View File

@ -2,6 +2,7 @@
use std::cmp::min;
use itertools::Itertools;
use syntax::parse::token::DelimToken;
use syntax::source_map::Span;
use syntax::{ast, ptr};
@ -711,10 +712,14 @@ fn last_item_shape(
if items.len() == 1 && !lists.get(0)?.is_nested_call() {
return Some(shape);
}
let offset = items.iter().rev().skip(1).fold(0, |acc, i| {
// 2 = ", "
acc + 2 + i.inner_as_ref().len()
});
let offset = items
.iter()
.dropping_back(1)
.map(|i| {
// 2 = ", "
2 + i.inner_as_ref().len()
})
.sum();
Shape {
width: min(args_max_width, shape.width),
..shape

View File

@ -2,6 +2,7 @@
use std::cmp;
use itertools::Itertools;
use syntax::ast;
use syntax::source_map::{BytePos, Span};
@ -190,11 +191,8 @@ fn struct_field_prefix_max_min_width<T: AlignedItem>(
}
})
})
.fold(Some((0, ::std::usize::MAX)), |acc, len| match (acc, len) {
(Some((max_len, min_len)), Some(len)) => {
Some((cmp::max(max_len, len), cmp::min(min_len, len)))
}
_ => None,
.fold_options((0, ::std::usize::MAX), |(max_len, min_len), len| {
(cmp::max(max_len, len), cmp::min(min_len, len))
})
.unwrap_or((0, 0))
}
@ -274,7 +272,11 @@ fn group_aligned_items<T: AlignedItem>(
.skip(1)
.collect::<Vec<_>>()
.join("\n");
let spacings = if snippet.lines().rev().skip(1).any(|l| l.trim().is_empty()) {
let spacings = if snippet
.lines()
.dropping_back(1)
.any(|l| l.trim().is_empty())
{
"\n"
} else {
""