2015-10-17 15:56:53 +02:00
|
|
|
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2018-02-07 22:48:05 +09:00
|
|
|
use config::lists::*;
|
2017-09-27 22:19:10 +02:00
|
|
|
use syntax::ast::{self, BindingMode, FieldPat, Pat, PatKind, RangeEnd, RangeSyntax};
|
2017-07-13 18:42:14 +09:00
|
|
|
use syntax::codemap::{self, BytePos, Span};
|
|
|
|
use syntax::ptr;
|
|
|
|
|
2016-05-25 20:41:26 +02:00
|
|
|
use codemap::SpanUtils;
|
2017-07-13 18:42:14 +09:00
|
|
|
use comment::FindUncommented;
|
2018-04-29 20:22:48 +08:00
|
|
|
use expr::{
|
|
|
|
can_be_overflowed_expr, rewrite_pair, rewrite_unary_prefix, wrap_struct_field, PairParts,
|
|
|
|
};
|
|
|
|
use lists::{
|
|
|
|
itemize_list, shape_for_tactic, struct_lit_formatting, struct_lit_shape, struct_lit_tactic,
|
|
|
|
write_list,
|
|
|
|
};
|
2017-12-10 00:22:00 +09:00
|
|
|
use macros::{rewrite_macro, MacroPosition};
|
2018-03-07 15:40:52 +09:00
|
|
|
use overflow;
|
2017-07-13 18:42:14 +09:00
|
|
|
use rewrite::{Rewrite, RewriteContext};
|
2017-09-17 15:23:25 +09:00
|
|
|
use shape::Shape;
|
2017-12-24 00:28:58 +09:00
|
|
|
use spanned::Spanned;
|
2017-01-19 10:47:07 +13:00
|
|
|
use types::{rewrite_path, PathContext};
|
2017-09-19 11:40:20 +09:00
|
|
|
use utils::{format_mutability, mk_sp};
|
2015-10-17 15:56:53 +02:00
|
|
|
|
2018-03-21 22:02:18 +09:00
|
|
|
/// Returns true if the given pattern is short. A short pattern is defined by the following grammer:
|
|
|
|
///
|
|
|
|
/// [small, ntp]:
|
|
|
|
/// - single token
|
|
|
|
/// - `&[single-line, ntp]`
|
|
|
|
///
|
|
|
|
/// [small]:
|
|
|
|
/// - `[small, ntp]`
|
|
|
|
/// - unary tuple constructor `([small, ntp])`
|
|
|
|
/// - `&[small]`
|
|
|
|
pub fn is_short_pattern(pat: &ast::Pat, pat_str: &str) -> bool {
|
|
|
|
// We also require that the pattern is reasonably 'small' with its literal width.
|
|
|
|
pat_str.len() <= 20 && !pat_str.contains('\n') && is_short_pattern_inner(pat)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_short_pattern_inner(pat: &ast::Pat) -> bool {
|
|
|
|
match pat.node {
|
|
|
|
ast::PatKind::Wild | ast::PatKind::Lit(_) => true,
|
|
|
|
ast::PatKind::Ident(_, _, ref pat) => pat.is_none(),
|
|
|
|
ast::PatKind::Struct(..)
|
|
|
|
| ast::PatKind::Mac(..)
|
|
|
|
| ast::PatKind::Slice(..)
|
|
|
|
| ast::PatKind::Path(..)
|
|
|
|
| ast::PatKind::Range(..) => false,
|
|
|
|
ast::PatKind::Tuple(ref subpats, _) => subpats.len() <= 1,
|
|
|
|
ast::PatKind::TupleStruct(ref path, ref subpats, _) => {
|
|
|
|
path.segments.len() <= 1 && subpats.len() <= 1
|
|
|
|
}
|
|
|
|
ast::PatKind::Box(ref p) | ast::PatKind::Ref(ref p, _) | ast::PatKind::Paren(ref p) => {
|
|
|
|
is_short_pattern_inner(&*p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-17 15:56:53 +02:00
|
|
|
impl Rewrite for Pat {
|
2017-01-31 08:28:48 +13:00
|
|
|
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
|
2015-10-17 15:56:53 +02:00
|
|
|
match self.node {
|
2017-05-15 22:55:01 +09:00
|
|
|
PatKind::Box(ref pat) => rewrite_unary_prefix(context, "box ", &**pat, shape),
|
2016-03-01 17:27:19 -05:00
|
|
|
PatKind::Ident(binding_mode, ident, ref sub_pat) => {
|
2015-10-17 15:56:53 +02:00
|
|
|
let (prefix, mutability) = match binding_mode {
|
2016-03-01 17:27:19 -05:00
|
|
|
BindingMode::ByRef(mutability) => ("ref ", mutability),
|
|
|
|
BindingMode::ByValue(mutability) => ("", mutability),
|
2015-10-17 15:56:53 +02:00
|
|
|
};
|
|
|
|
let mut_infix = format_mutability(mutability);
|
2018-04-08 22:18:18 +08:00
|
|
|
let id_str = ident.name.to_string();
|
2016-02-12 14:59:13 +13:00
|
|
|
let sub_pat = match *sub_pat {
|
|
|
|
Some(ref p) => {
|
2016-02-15 10:07:19 +13:00
|
|
|
// 3 - ` @ `.
|
2017-10-05 20:50:19 +09:00
|
|
|
let width = shape
|
|
|
|
.width
|
|
|
|
.checked_sub(prefix.len() + mut_infix.len() + id_str.len() + 3)?;
|
2017-06-12 15:58:58 +12:00
|
|
|
format!(
|
|
|
|
" @ {}",
|
2017-10-05 20:50:19 +09:00
|
|
|
p.rewrite(context, Shape::legacy(width, shape.indent))?
|
2017-06-12 15:58:58 +12:00
|
|
|
)
|
2016-02-12 14:59:13 +13:00
|
|
|
}
|
|
|
|
None => "".to_owned(),
|
|
|
|
};
|
|
|
|
|
2017-09-19 11:40:20 +09:00
|
|
|
Some(format!("{}{}{}{}", prefix, mut_infix, id_str, sub_pat))
|
2015-10-17 15:56:53 +02:00
|
|
|
}
|
2017-08-16 00:48:12 +09:00
|
|
|
PatKind::Wild => {
|
|
|
|
if 1 <= shape.width {
|
|
|
|
Some("_".to_owned())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2017-09-27 22:19:10 +02:00
|
|
|
PatKind::Range(ref lhs, ref rhs, ref end_kind) => {
|
|
|
|
let infix = match *end_kind {
|
|
|
|
RangeEnd::Included(RangeSyntax::DotDotDot) => "...",
|
|
|
|
RangeEnd::Included(RangeSyntax::DotDotEq) => "..=",
|
|
|
|
RangeEnd::Excluded => "..",
|
|
|
|
};
|
2018-01-21 16:25:24 -08:00
|
|
|
let infix = if context.config.spaces_around_ranges() {
|
|
|
|
format!(" {} ", infix)
|
|
|
|
} else {
|
|
|
|
infix.to_owned()
|
|
|
|
};
|
2017-09-27 22:19:10 +02:00
|
|
|
rewrite_pair(
|
2017-09-15 12:10:30 +09:00
|
|
|
&**lhs,
|
|
|
|
&**rhs,
|
2018-01-21 16:25:24 -08:00
|
|
|
PairParts::new("", &infix, ""),
|
2017-09-15 12:10:30 +09:00
|
|
|
context,
|
|
|
|
shape,
|
|
|
|
SeparatorPlace::Front,
|
2017-09-27 22:19:10 +02:00
|
|
|
)
|
|
|
|
}
|
2016-03-01 17:27:19 -05:00
|
|
|
PatKind::Ref(ref pat, mutability) => {
|
2015-10-17 15:56:53 +02:00
|
|
|
let prefix = format!("&{}", format_mutability(mutability));
|
2017-05-15 22:55:01 +09:00
|
|
|
rewrite_unary_prefix(context, &prefix, &**pat, shape)
|
2015-10-17 15:56:53 +02:00
|
|
|
}
|
2016-05-30 14:53:04 +02:00
|
|
|
PatKind::Tuple(ref items, dotdot_pos) => {
|
2017-01-31 08:28:48 +13:00
|
|
|
rewrite_tuple_pat(items, dotdot_pos, None, self.span, context, shape)
|
2015-12-06 01:11:26 +01:00
|
|
|
}
|
2016-09-16 15:19:18 +12:00
|
|
|
PatKind::Path(ref q_self, ref path) => {
|
2017-01-31 08:28:48 +13:00
|
|
|
rewrite_path(context, PathContext::Expr, q_self.as_ref(), path, shape)
|
2016-09-16 15:19:18 +12:00
|
|
|
}
|
2016-05-30 14:53:04 +02:00
|
|
|
PatKind::TupleStruct(ref path, ref pat_vec, dotdot_pos) => {
|
2017-10-05 20:50:19 +09:00
|
|
|
let path_str = rewrite_path(context, PathContext::Expr, None, path, shape)?;
|
2017-06-12 15:58:58 +12:00
|
|
|
rewrite_tuple_pat(
|
|
|
|
pat_vec,
|
|
|
|
dotdot_pos,
|
|
|
|
Some(path_str),
|
|
|
|
self.span,
|
|
|
|
context,
|
|
|
|
shape,
|
|
|
|
)
|
2015-10-17 15:56:53 +02:00
|
|
|
}
|
2017-01-31 08:28:48 +13:00
|
|
|
PatKind::Lit(ref expr) => expr.rewrite(context, shape),
|
2016-11-21 08:37:35 +13:00
|
|
|
PatKind::Slice(ref prefix, ref slice_pat, ref suffix) => {
|
2016-02-12 14:59:13 +13:00
|
|
|
// Rewrite all the sub-patterns.
|
2017-03-28 11:25:59 +13:00
|
|
|
let prefix = prefix.iter().map(|p| p.rewrite(context, shape));
|
2017-10-05 20:50:19 +09:00
|
|
|
let slice_pat = slice_pat
|
|
|
|
.as_ref()
|
|
|
|
.map(|p| Some(format!("{}..", p.rewrite(context, shape)?)));
|
2017-03-28 11:25:59 +13:00
|
|
|
let suffix = suffix.iter().map(|p| p.rewrite(context, shape));
|
2016-02-12 14:59:13 +13:00
|
|
|
|
|
|
|
// Munge them together.
|
2017-04-24 16:50:11 +09:00
|
|
|
let pats: Option<Vec<String>> =
|
|
|
|
prefix.chain(slice_pat.into_iter()).chain(suffix).collect();
|
2016-02-12 14:59:13 +13:00
|
|
|
|
|
|
|
// Check that all the rewrites succeeded, and if not return None.
|
2017-10-05 20:50:19 +09:00
|
|
|
let pats = pats?;
|
2016-02-12 14:59:13 +13:00
|
|
|
|
|
|
|
// Unwrap all the sub-strings and join them with commas.
|
2018-05-18 16:35:09 +12:00
|
|
|
Some(format!("[{}]", pats.join(", ")))
|
2016-02-12 14:59:13 +13:00
|
|
|
}
|
2017-11-01 07:33:55 +01:00
|
|
|
PatKind::Struct(ref path, ref fields, ellipsis) => {
|
|
|
|
rewrite_struct_pat(path, fields, ellipsis, self.span, context, shape)
|
2016-02-12 14:59:13 +13:00
|
|
|
}
|
2017-12-10 00:22:00 +09:00
|
|
|
PatKind::Mac(ref mac) => rewrite_macro(mac, None, context, shape, MacroPosition::Pat),
|
2018-05-08 06:25:48 +09:00
|
|
|
PatKind::Paren(ref pat) => pat
|
|
|
|
.rewrite(context, shape.offset_left(1)?.sub_width(1)?)
|
2018-03-06 20:05:47 +09:00
|
|
|
.map(|inner_pat| format!("({})", inner_pat)),
|
2015-10-17 15:56:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-12 14:59:13 +13:00
|
|
|
|
2017-06-12 15:58:58 +12:00
|
|
|
fn rewrite_struct_pat(
|
|
|
|
path: &ast::Path,
|
|
|
|
fields: &[codemap::Spanned<ast::FieldPat>],
|
2017-11-01 07:33:55 +01:00
|
|
|
ellipsis: bool,
|
2017-06-12 15:58:58 +12:00
|
|
|
span: Span,
|
|
|
|
context: &RewriteContext,
|
|
|
|
shape: Shape,
|
|
|
|
) -> Option<String> {
|
2017-05-13 06:44:57 +09:00
|
|
|
// 2 = ` {`
|
2017-10-05 20:50:19 +09:00
|
|
|
let path_shape = shape.sub_width(2)?;
|
|
|
|
let path_str = rewrite_path(context, PathContext::Expr, None, path, path_shape)?;
|
2017-03-21 11:23:59 +13:00
|
|
|
|
2017-11-01 07:33:55 +01:00
|
|
|
if fields.is_empty() && !ellipsis {
|
2017-03-21 11:23:59 +13:00
|
|
|
return Some(format!("{} {{}}", path_str));
|
|
|
|
}
|
|
|
|
|
2017-11-01 07:33:55 +01:00
|
|
|
let (ellipsis_str, terminator) = if ellipsis { (", ..", "..") } else { ("", "}") };
|
2017-03-21 11:23:59 +13:00
|
|
|
|
|
|
|
// 3 = ` { `, 2 = ` }`.
|
2017-10-05 20:50:19 +09:00
|
|
|
let (h_shape, v_shape) =
|
2017-11-01 07:33:55 +01:00
|
|
|
struct_lit_shape(shape, context, path_str.len() + 3, ellipsis_str.len() + 2)?;
|
2017-06-12 15:58:58 +12:00
|
|
|
|
|
|
|
let items = itemize_list(
|
2018-02-19 12:41:43 +09:00
|
|
|
context.snippet_provider,
|
2017-06-12 15:58:58 +12:00
|
|
|
fields.iter(),
|
|
|
|
terminator,
|
2017-11-16 17:38:12 +09:00
|
|
|
",",
|
2017-08-19 21:47:40 +03:00
|
|
|
|f| f.span.lo(),
|
|
|
|
|f| f.span.hi(),
|
2017-06-12 15:58:58 +12:00
|
|
|
|f| f.node.rewrite(context, v_shape),
|
2018-02-19 12:41:43 +09:00
|
|
|
context.snippet_provider.span_after(span, "{"),
|
2017-08-19 21:47:40 +03:00
|
|
|
span.hi(),
|
2017-08-07 17:29:55 +09:00
|
|
|
false,
|
2017-06-12 15:58:58 +12:00
|
|
|
);
|
2017-03-21 11:23:59 +13:00
|
|
|
let item_vec = items.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
let tactic = struct_lit_tactic(h_shape, context, &item_vec);
|
|
|
|
let nested_shape = shape_for_tactic(tactic, h_shape, v_shape);
|
|
|
|
let fmt = struct_lit_formatting(nested_shape, tactic, context, false);
|
|
|
|
|
2017-10-05 20:50:19 +09:00
|
|
|
let mut fields_str = write_list(&item_vec, &fmt)?;
|
2017-07-03 18:53:47 +09:00
|
|
|
let one_line_width = h_shape.map_or(0, |shape| shape.width);
|
2017-03-21 11:23:59 +13:00
|
|
|
|
2017-11-01 07:33:55 +01:00
|
|
|
if ellipsis {
|
2017-07-03 18:53:47 +09:00
|
|
|
if fields_str.contains('\n') || fields_str.len() > one_line_width {
|
2017-05-13 06:44:57 +09:00
|
|
|
// Add a missing trailing comma.
|
|
|
|
if fmt.trailing_separator == SeparatorTactic::Never {
|
|
|
|
fields_str.push_str(",");
|
|
|
|
}
|
2017-03-21 11:23:59 +13:00
|
|
|
fields_str.push_str("\n");
|
|
|
|
fields_str.push_str(&nested_shape.indent.to_string(context.config));
|
|
|
|
fields_str.push_str("..");
|
|
|
|
} else {
|
|
|
|
if !fields_str.is_empty() {
|
2017-11-01 07:33:55 +01:00
|
|
|
// there are preceding struct fields being matched on
|
2017-03-26 01:16:45 -04:00
|
|
|
if fmt.tactic == DefinitiveListTactic::Vertical {
|
|
|
|
// if the tactic is Vertical, write_list already added a trailing ,
|
|
|
|
fields_str.push_str(" ");
|
|
|
|
} else {
|
|
|
|
fields_str.push_str(", ");
|
|
|
|
}
|
2017-03-21 11:23:59 +13:00
|
|
|
}
|
|
|
|
fields_str.push_str("..");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-03 18:53:47 +09:00
|
|
|
let fields_str = wrap_struct_field(context, &fields_str, shape, v_shape, one_line_width);
|
2017-03-21 11:23:59 +13:00
|
|
|
Some(format!("{} {{{}}}", path_str, fields_str))
|
|
|
|
}
|
|
|
|
|
2016-02-12 14:59:13 +13:00
|
|
|
impl Rewrite for FieldPat {
|
2017-01-31 08:28:48 +13:00
|
|
|
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
|
|
|
|
let pat = self.pat.rewrite(context, shape);
|
2016-02-12 14:59:13 +13:00
|
|
|
if self.is_shorthand {
|
|
|
|
pat
|
|
|
|
} else {
|
2017-10-05 20:50:19 +09:00
|
|
|
let pat_str = pat?;
|
2017-09-19 11:40:20 +09:00
|
|
|
let id_str = self.ident.to_string();
|
|
|
|
let one_line_width = id_str.len() + 2 + pat_str.len();
|
|
|
|
if one_line_width <= shape.width {
|
|
|
|
Some(format!("{}: {}", id_str, pat_str))
|
|
|
|
} else {
|
|
|
|
let nested_shape = shape.block_indent(context.config.tab_spaces());
|
2017-10-05 20:50:19 +09:00
|
|
|
let pat_str = self.pat.rewrite(context, nested_shape)?;
|
2017-09-19 11:40:20 +09:00
|
|
|
Some(format!(
|
|
|
|
"{}:\n{}{}",
|
|
|
|
id_str,
|
|
|
|
nested_shape.indent.to_string(context.config),
|
|
|
|
pat_str,
|
|
|
|
))
|
|
|
|
}
|
2016-02-12 14:59:13 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-06 18:03:47 +09:00
|
|
|
|
2017-06-15 16:25:40 +09:00
|
|
|
pub enum TuplePatField<'a> {
|
2016-09-06 18:03:47 +09:00
|
|
|
Pat(&'a ptr::P<ast::Pat>),
|
|
|
|
Dotdot(Span),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Rewrite for TuplePatField<'a> {
|
2017-01-31 08:28:48 +13:00
|
|
|
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
|
2016-09-06 18:03:47 +09:00
|
|
|
match *self {
|
2017-08-29 22:16:04 +09:00
|
|
|
TuplePatField::Pat(p) => p.rewrite(context, shape),
|
2016-09-06 18:03:47 +09:00
|
|
|
TuplePatField::Dotdot(_) => Some("..".to_string()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Spanned for TuplePatField<'a> {
|
|
|
|
fn span(&self) -> Span {
|
|
|
|
match *self {
|
2017-08-29 22:16:04 +09:00
|
|
|
TuplePatField::Pat(p) => p.span(),
|
2016-09-06 18:03:47 +09:00
|
|
|
TuplePatField::Dotdot(span) => span,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-15 16:25:40 +09:00
|
|
|
pub fn can_be_overflowed_pat(context: &RewriteContext, pat: &TuplePatField, len: usize) -> bool {
|
2017-08-29 22:16:04 +09:00
|
|
|
match *pat {
|
|
|
|
TuplePatField::Pat(pat) => match pat.node {
|
2017-11-16 16:42:07 +09:00
|
|
|
ast::PatKind::Path(..)
|
|
|
|
| ast::PatKind::Tuple(..)
|
|
|
|
| ast::PatKind::Struct(..)
|
|
|
|
| ast::PatKind::TupleStruct(..) => context.use_block_indent() && len == 1,
|
2017-07-11 21:53:10 +09:00
|
|
|
ast::PatKind::Ref(ref p, _) | ast::PatKind::Box(ref p) => {
|
|
|
|
can_be_overflowed_pat(context, &TuplePatField::Pat(p), len)
|
|
|
|
}
|
|
|
|
ast::PatKind::Lit(ref expr) => can_be_overflowed_expr(context, expr, len),
|
|
|
|
_ => false,
|
|
|
|
},
|
2017-08-29 22:16:04 +09:00
|
|
|
TuplePatField::Dotdot(..) => false,
|
2017-06-15 16:25:40 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-12 15:58:58 +12:00
|
|
|
fn rewrite_tuple_pat(
|
|
|
|
pats: &[ptr::P<ast::Pat>],
|
|
|
|
dotdot_pos: Option<usize>,
|
|
|
|
path_str: Option<String>,
|
|
|
|
span: Span,
|
|
|
|
context: &RewriteContext,
|
|
|
|
shape: Shape,
|
|
|
|
) -> Option<String> {
|
2017-04-24 16:50:11 +09:00
|
|
|
let mut pat_vec: Vec<_> = pats.into_iter().map(|x| TuplePatField::Pat(x)).collect();
|
2016-09-06 18:03:47 +09:00
|
|
|
|
|
|
|
if let Some(pos) = dotdot_pos {
|
2017-03-09 15:49:58 +13:00
|
|
|
let prev = if pos == 0 {
|
2017-08-19 21:47:40 +03:00
|
|
|
span.lo()
|
2017-03-09 15:49:58 +13:00
|
|
|
} else {
|
2017-08-19 21:47:40 +03:00
|
|
|
pats[pos - 1].span().hi()
|
2017-03-09 15:49:58 +13:00
|
|
|
};
|
|
|
|
let next = if pos + 1 >= pats.len() {
|
2017-08-19 21:47:40 +03:00
|
|
|
span.hi()
|
2017-03-09 15:49:58 +13:00
|
|
|
} else {
|
2017-08-19 21:47:40 +03:00
|
|
|
pats[pos + 1].span().lo()
|
2017-03-09 15:49:58 +13:00
|
|
|
};
|
2017-06-06 06:54:22 +02:00
|
|
|
let dot_span = mk_sp(prev, next);
|
2017-03-09 15:49:58 +13:00
|
|
|
let snippet = context.snippet(dot_span);
|
2017-08-19 21:47:40 +03:00
|
|
|
let lo = dot_span.lo() + BytePos(snippet.find_uncommented("..").unwrap() as u32);
|
|
|
|
let dotdot = TuplePatField::Dotdot(Span::new(
|
|
|
|
lo,
|
2016-09-06 18:03:47 +09:00
|
|
|
// 2 == "..".len()
|
2017-08-19 21:47:40 +03:00
|
|
|
lo + BytePos(2),
|
|
|
|
codemap::NO_EXPANSION,
|
|
|
|
));
|
2016-09-06 18:03:47 +09:00
|
|
|
pat_vec.insert(pos, dotdot);
|
|
|
|
}
|
|
|
|
|
|
|
|
if pat_vec.is_empty() {
|
2017-08-29 22:16:04 +09:00
|
|
|
return Some(format!("{}()", path_str.unwrap_or_default()));
|
2017-03-24 23:58:34 -07:00
|
|
|
}
|
2017-06-15 16:25:40 +09:00
|
|
|
|
|
|
|
let wildcard_suffix_len = count_wildcard_suffix_len(context, &pat_vec, span, shape);
|
2017-07-26 17:43:17 +09:00
|
|
|
let (pat_vec, span) = if context.config.condense_wildcard_suffixes() && wildcard_suffix_len >= 2
|
|
|
|
{
|
|
|
|
let new_item_count = 1 + pat_vec.len() - wildcard_suffix_len;
|
|
|
|
let sp = pat_vec[new_item_count - 1].span();
|
|
|
|
let snippet = context.snippet(sp);
|
2017-08-19 21:47:40 +03:00
|
|
|
let lo = sp.lo() + BytePos(snippet.find_uncommented("_").unwrap() as u32);
|
2017-07-26 17:43:17 +09:00
|
|
|
pat_vec[new_item_count - 1] = TuplePatField::Dotdot(mk_sp(lo, lo + BytePos(1)));
|
2017-08-19 21:47:40 +03:00
|
|
|
(
|
|
|
|
&pat_vec[..new_item_count],
|
|
|
|
mk_sp(span.lo(), lo + BytePos(1)),
|
|
|
|
)
|
2017-07-26 17:43:17 +09:00
|
|
|
} else {
|
|
|
|
(&pat_vec[..], span)
|
|
|
|
};
|
2017-06-15 16:25:40 +09:00
|
|
|
|
2017-03-24 23:58:34 -07:00
|
|
|
// add comma if `(x,)`
|
|
|
|
let add_comma = path_str.is_none() && pat_vec.len() == 1 && dotdot_pos.is_none();
|
2017-08-29 22:16:04 +09:00
|
|
|
let path_str = path_str.unwrap_or_default();
|
2018-04-01 19:35:00 +09:00
|
|
|
let pat_ref_vec = pat_vec.iter().collect::<Vec<_>>();
|
2017-08-29 22:16:04 +09:00
|
|
|
|
2018-03-07 15:40:52 +09:00
|
|
|
overflow::rewrite_with_parens(
|
2017-06-15 16:25:40 +09:00
|
|
|
&context,
|
|
|
|
&path_str,
|
2018-04-01 18:12:50 +09:00
|
|
|
&pat_ref_vec,
|
2017-06-15 16:25:40 +09:00
|
|
|
shape,
|
2018-03-07 15:40:52 +09:00
|
|
|
span,
|
|
|
|
context.config.max_width(),
|
2018-03-07 19:29:42 +09:00
|
|
|
if add_comma {
|
|
|
|
Some(SeparatorTactic::Always)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
},
|
2017-09-28 16:33:30 +09:00
|
|
|
)
|
2017-06-15 16:25:40 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
fn count_wildcard_suffix_len(
|
|
|
|
context: &RewriteContext,
|
|
|
|
patterns: &[TuplePatField],
|
|
|
|
span: Span,
|
|
|
|
shape: Shape,
|
|
|
|
) -> usize {
|
|
|
|
let mut suffix_len = 0;
|
2017-03-24 23:58:34 -07:00
|
|
|
|
2017-06-15 16:25:40 +09:00
|
|
|
let items: Vec<_> = itemize_list(
|
2018-02-19 12:41:43 +09:00
|
|
|
context.snippet_provider,
|
2017-06-15 16:25:40 +09:00
|
|
|
patterns.iter(),
|
|
|
|
")",
|
2017-11-16 17:38:12 +09:00
|
|
|
",",
|
2017-08-19 21:47:40 +03:00
|
|
|
|item| item.span().lo(),
|
|
|
|
|item| item.span().hi(),
|
2017-06-15 16:25:40 +09:00
|
|
|
|item| item.rewrite(context, shape),
|
2018-02-19 12:41:43 +09:00
|
|
|
context.snippet_provider.span_after(span, "("),
|
2017-08-19 21:47:40 +03:00
|
|
|
span.hi() - BytePos(1),
|
2017-08-07 17:29:55 +09:00
|
|
|
false,
|
2017-06-12 15:58:58 +12:00
|
|
|
).collect();
|
2017-03-24 23:58:34 -07:00
|
|
|
|
2017-05-16 23:24:38 +09:00
|
|
|
for item in items.iter().rev().take_while(|i| match i.item {
|
2017-06-12 15:58:58 +12:00
|
|
|
Some(ref internal_string) if internal_string == "_" => true,
|
|
|
|
_ => false,
|
2017-06-17 21:17:20 +09:00
|
|
|
}) {
|
2016-11-07 21:38:20 +01:00
|
|
|
suffix_len += 1;
|
|
|
|
|
2018-04-01 21:43:01 +09:00
|
|
|
if item.has_comment() {
|
2016-11-07 21:38:20 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
suffix_len
|
|
|
|
}
|