Skip to content

Commit

Permalink
Condense suffix strings of simple wildcards in patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusklaas committed Nov 7, 2016
1 parent 664f646 commit 5fc9fa3
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/config.rs
Expand Up @@ -422,4 +422,6 @@ create_config! {
use_try_shorthand: bool, false, "Replace uses of the try! macro by the ? shorthand";
write_mode: WriteMode, WriteMode::Replace,
"What Write Mode to use when none is supplied: Replace, Overwrite, Display, Diff, Coverage";
condense_wildcard_suffices: bool, false, "Replace strings of _ wildcards by a single .. in
tuple patterns"
}
49 changes: 39 additions & 10 deletions src/patterns.rs
Expand Up @@ -12,7 +12,7 @@ use Indent;
use codemap::SpanUtils;
use rewrite::{Rewrite, RewriteContext};
use utils::{wrap_str, format_mutability};
use lists::{format_item_list, itemize_list};
use lists::{format_item_list, itemize_list, ListItem};
use expr::{rewrite_unary_prefix, rewrite_pair};
use types::rewrite_path;
use super::Spanned;
Expand Down Expand Up @@ -226,16 +226,28 @@ fn rewrite_tuple_pat(pats: &[ptr::P<ast::Pat>],
let width = try_opt!(width.checked_sub(path_len + if add_comma { 3 } else { 2 }));
// 1 = "(".len()
let offset = offset + path_len + 1;
let items = itemize_list(context.codemap,
pat_vec.iter(),
if add_comma { ",)" } else { ")" },
|item| item.span().lo,
|item| item.span().hi,
|item| item.rewrite(context, width, offset),
context.codemap.span_after(span, "("),
span.hi - BytePos(1));
let mut items: Vec<_> = itemize_list(context.codemap,
pat_vec.iter(),
if add_comma { ",)" } else { ")" },
|item| item.span().lo,
|item| item.span().hi,
|item| item.rewrite(context, width, offset),
context.codemap.span_after(span, "("),
span.hi - BytePos(1))
.collect();

let list = try_opt!(format_item_list(items, width, offset, context.config));
// Condense wildcard string suffix into a single ..
let wildcard_suffix_len = count_wildcard_suffix_len(&items);

let list = if context.config.condense_wildcard_suffices && wildcard_suffix_len >= 2 {
let new_item_count = 1 + pats.len() - wildcard_suffix_len;
items[new_item_count - 1].item = Some("..".to_owned());

let da_iter = items.into_iter().take(new_item_count);
try_opt!(format_item_list(da_iter, width, offset, context.config))
} else {
try_opt!(format_item_list(items.into_iter(), width, offset, context.config))
};

match path_str {
Some(path_str) => {
Expand All @@ -256,3 +268,20 @@ 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,
}) {
suffix_len += 1;

if item.pre_comment.is_some() || item.post_comment.is_some() {
break;
}
}

suffix_len
}
11 changes: 11 additions & 0 deletions tests/source/pattern-condense-wildcards.rs
@@ -0,0 +1,11 @@
// rustfmt-condense_wildcard_suffices: true

fn main() {
match x {
Butt (_,_) => "hah",
Tup (_) => "nah",
Quad (_,_, x,_) => " also no rewrite",
Quad (x, _, _, _) => "condense me pls",
Weird (x, _, _, /* dont condense before */ _, _, _) => "pls work",
}
}
15 changes: 15 additions & 0 deletions tests/target/pattern-condense-wildcards.rs
@@ -0,0 +1,15 @@
// rustfmt-condense_wildcard_suffices: true

fn main() {
match x {
Butt(..) => "hah",
Tup(_) => "nah",
Quad(_, _, x, _) => " also no rewrite",
Quad(x, ..) => "condense me pls",
Weird(x,
_,
_,
// dont condense before
..) => "pls work",
}
}

0 comments on commit 5fc9fa3

Please sign in to comment.