2015-10-17 08:56:53 -05: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.
|
|
|
|
|
2017-01-30 13:28:48 -06:00
|
|
|
use Shape;
|
2016-05-25 13:41:26 -05:00
|
|
|
use codemap::SpanUtils;
|
2017-03-20 17:23:59 -05:00
|
|
|
use config::{IndentStyle, MultilineStyle};
|
2015-10-17 08:56:53 -05:00
|
|
|
use rewrite::{Rewrite, RewriteContext};
|
2016-05-25 13:41:26 -05:00
|
|
|
use utils::{wrap_str, format_mutability};
|
2017-05-12 16:44:57 -05:00
|
|
|
use lists::{DefinitiveListTactic, SeparatorTactic, format_item_list, itemize_list, ListItem,
|
|
|
|
struct_lit_shape, struct_lit_tactic, shape_for_tactic, struct_lit_formatting,
|
|
|
|
write_list};
|
2016-09-06 04:03:47 -05:00
|
|
|
use expr::{rewrite_unary_prefix, rewrite_pair};
|
2017-01-18 15:47:07 -06:00
|
|
|
use types::{rewrite_path, PathContext};
|
2016-09-06 04:03:47 -05:00
|
|
|
use super::Spanned;
|
|
|
|
use comment::FindUncommented;
|
2015-10-17 08:56:53 -05:00
|
|
|
|
2017-02-26 08:41:32 -06:00
|
|
|
use syntax::ast::{self, BindingMode, Pat, PatKind, FieldPat, RangeEnd};
|
2016-09-06 04:03:47 -05:00
|
|
|
use syntax::ptr;
|
|
|
|
use syntax::codemap::{self, BytePos, Span};
|
2015-10-17 08:56:53 -05:00
|
|
|
|
|
|
|
impl Rewrite for Pat {
|
2017-01-30 13:28:48 -06:00
|
|
|
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
|
2015-10-17 08:56:53 -05:00
|
|
|
match self.node {
|
2017-05-15 08:55:01 -05:00
|
|
|
PatKind::Box(ref pat) => rewrite_unary_prefix(context, "box ", &**pat, shape),
|
2016-03-01 16:27:19 -06:00
|
|
|
PatKind::Ident(binding_mode, ident, ref sub_pat) => {
|
2015-10-17 08:56:53 -05:00
|
|
|
let (prefix, mutability) = match binding_mode {
|
2016-03-01 16:27:19 -06:00
|
|
|
BindingMode::ByRef(mutability) => ("ref ", mutability),
|
|
|
|
BindingMode::ByValue(mutability) => ("", mutability),
|
2015-10-17 08:56:53 -05:00
|
|
|
};
|
|
|
|
let mut_infix = format_mutability(mutability);
|
2016-02-11 19:59:13 -06:00
|
|
|
let id_str = ident.node.to_string();
|
|
|
|
let sub_pat = match *sub_pat {
|
|
|
|
Some(ref p) => {
|
2016-02-14 15:07:19 -06:00
|
|
|
// 3 - ` @ `.
|
2017-05-24 19:34:10 -05:00
|
|
|
let width =
|
|
|
|
try_opt!(shape.width.checked_sub(prefix.len() + mut_infix.len() +
|
|
|
|
id_str.len() +
|
|
|
|
3));
|
2017-01-30 13:28:48 -06:00
|
|
|
format!(" @ {}",
|
|
|
|
try_opt!(p.rewrite(context, Shape::legacy(width, shape.indent))))
|
2016-02-11 19:59:13 -06:00
|
|
|
}
|
|
|
|
None => "".to_owned(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let result = format!("{}{}{}{}", prefix, mut_infix, id_str, sub_pat);
|
2017-05-16 03:47:09 -05:00
|
|
|
wrap_str(result, context.config.max_width(), shape)
|
2015-10-17 08:56:53 -05:00
|
|
|
}
|
2016-03-01 16:27:19 -06:00
|
|
|
PatKind::Wild => {
|
2017-01-30 13:28:48 -06:00
|
|
|
if 1 <= shape.width {
|
2015-11-12 14:38:41 -06:00
|
|
|
Some("_".to_owned())
|
2015-10-17 08:56:53 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2017-02-26 08:41:32 -06:00
|
|
|
PatKind::Range(ref lhs, ref rhs, ref end_kind) => {
|
|
|
|
match *end_kind {
|
|
|
|
RangeEnd::Included => {
|
|
|
|
rewrite_pair(&**lhs, &**rhs, "", "...", "", context, shape)
|
|
|
|
}
|
|
|
|
RangeEnd::Excluded => {
|
|
|
|
rewrite_pair(&**lhs, &**rhs, "", "..", "", context, shape)
|
|
|
|
}
|
|
|
|
}
|
2015-10-17 08:56:53 -05:00
|
|
|
}
|
2016-03-01 16:27:19 -06:00
|
|
|
PatKind::Ref(ref pat, mutability) => {
|
2015-10-17 08:56:53 -05:00
|
|
|
let prefix = format!("&{}", format_mutability(mutability));
|
2017-05-15 08:55:01 -05:00
|
|
|
rewrite_unary_prefix(context, &prefix, &**pat, shape)
|
2015-10-17 08:56:53 -05:00
|
|
|
}
|
2016-05-30 07:53:04 -05:00
|
|
|
PatKind::Tuple(ref items, dotdot_pos) => {
|
2017-01-30 13:28:48 -06:00
|
|
|
rewrite_tuple_pat(items, dotdot_pos, None, self.span, context, shape)
|
2015-12-05 18:11:26 -06:00
|
|
|
}
|
2016-09-15 22:19:18 -05:00
|
|
|
PatKind::Path(ref q_self, ref path) => {
|
2017-01-30 13:28:48 -06:00
|
|
|
rewrite_path(context, PathContext::Expr, q_self.as_ref(), path, shape)
|
2016-09-15 22:19:18 -05:00
|
|
|
}
|
2016-05-30 07:53:04 -05:00
|
|
|
PatKind::TupleStruct(ref path, ref pat_vec, dotdot_pos) => {
|
2017-01-18 15:47:07 -06:00
|
|
|
let path_str =
|
2017-01-30 13:28:48 -06:00
|
|
|
try_opt!(rewrite_path(context, PathContext::Expr, None, path, shape));
|
2016-09-06 04:03:47 -05:00
|
|
|
rewrite_tuple_pat(pat_vec,
|
|
|
|
dotdot_pos,
|
|
|
|
Some(path_str),
|
|
|
|
self.span,
|
|
|
|
context,
|
2017-01-30 13:28:48 -06:00
|
|
|
shape)
|
2015-10-17 08:56:53 -05:00
|
|
|
}
|
2017-01-30 13:28:48 -06:00
|
|
|
PatKind::Lit(ref expr) => expr.rewrite(context, shape),
|
2016-11-20 13:37:35 -06:00
|
|
|
PatKind::Slice(ref prefix, ref slice_pat, ref suffix) => {
|
2016-02-11 19:59:13 -06:00
|
|
|
// Rewrite all the sub-patterns.
|
2017-03-27 17:25:59 -05:00
|
|
|
let prefix = prefix.iter().map(|p| p.rewrite(context, shape));
|
2017-03-27 17:14:47 -05:00
|
|
|
let slice_pat =
|
|
|
|
slice_pat
|
|
|
|
.as_ref()
|
|
|
|
.map(|p| Some(format!("{}..", try_opt!(p.rewrite(context, shape)))));
|
2017-03-27 17:25:59 -05:00
|
|
|
let suffix = suffix.iter().map(|p| p.rewrite(context, shape));
|
2016-02-11 19:59:13 -06:00
|
|
|
|
|
|
|
// Munge them together.
|
2017-04-24 02:50:11 -05:00
|
|
|
let pats: Option<Vec<String>> =
|
|
|
|
prefix.chain(slice_pat.into_iter()).chain(suffix).collect();
|
2016-02-11 19:59:13 -06:00
|
|
|
|
|
|
|
// Check that all the rewrites succeeded, and if not return None.
|
2016-02-14 15:07:19 -06:00
|
|
|
let pats = try_opt!(pats);
|
2016-02-11 19:59:13 -06:00
|
|
|
|
|
|
|
// Unwrap all the sub-strings and join them with commas.
|
2017-05-16 03:47:09 -05:00
|
|
|
let result = if context.config.spaces_within_square_brackets() {
|
2016-10-17 15:09:49 -05:00
|
|
|
format!("[ {} ]", pats.join(", "))
|
|
|
|
} else {
|
|
|
|
format!("[{}]", pats.join(", "))
|
|
|
|
};
|
2017-05-16 03:47:09 -05:00
|
|
|
wrap_str(result, context.config.max_width(), shape)
|
2016-02-11 19:59:13 -06:00
|
|
|
}
|
2016-03-01 16:27:19 -06:00
|
|
|
PatKind::Struct(ref path, ref fields, elipses) => {
|
2017-03-20 17:23:59 -05:00
|
|
|
rewrite_struct_pat(path, fields, elipses, self.span, context, shape)
|
2016-02-11 19:59:13 -06:00
|
|
|
}
|
|
|
|
// FIXME(#819) format pattern macros.
|
2016-03-01 16:27:19 -06:00
|
|
|
PatKind::Mac(..) => {
|
2017-05-16 03:47:09 -05:00
|
|
|
wrap_str(context.snippet(self.span),
|
|
|
|
context.config.max_width(),
|
|
|
|
shape)
|
2015-10-17 08:56:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-11 19:59:13 -06:00
|
|
|
|
2017-03-20 17:23:59 -05:00
|
|
|
fn rewrite_struct_pat(path: &ast::Path,
|
|
|
|
fields: &[codemap::Spanned<ast::FieldPat>],
|
|
|
|
elipses: bool,
|
|
|
|
span: Span,
|
|
|
|
context: &RewriteContext,
|
|
|
|
shape: Shape)
|
|
|
|
-> Option<String> {
|
2017-05-12 16:44:57 -05:00
|
|
|
// 2 = ` {`
|
2017-03-20 17:23:59 -05:00
|
|
|
let path_shape = try_opt!(shape.sub_width(2));
|
|
|
|
let path_str = try_opt!(rewrite_path(context, PathContext::Expr, None, path, path_shape));
|
|
|
|
|
|
|
|
if fields.len() == 0 && !elipses {
|
|
|
|
return Some(format!("{} {{}}", path_str));
|
|
|
|
}
|
|
|
|
|
|
|
|
let (elipses_str, terminator) = if elipses { (", ..", "..") } else { ("", "}") };
|
|
|
|
|
|
|
|
// 3 = ` { `, 2 = ` }`.
|
|
|
|
let (h_shape, v_shape) =
|
|
|
|
try_opt!(struct_lit_shape(shape, context, path_str.len() + 3, elipses_str.len() + 2));
|
|
|
|
|
|
|
|
let items = itemize_list(context.codemap,
|
|
|
|
fields.iter(),
|
|
|
|
terminator,
|
|
|
|
|f| f.span.lo,
|
|
|
|
|f| f.span.hi,
|
|
|
|
|f| f.node.rewrite(context, v_shape),
|
|
|
|
context.codemap.span_after(span, "{"),
|
|
|
|
span.hi);
|
|
|
|
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);
|
|
|
|
|
|
|
|
let mut fields_str = try_opt!(write_list(&item_vec, &fmt));
|
|
|
|
|
|
|
|
if elipses {
|
|
|
|
if fields_str.contains('\n') {
|
2017-05-12 16:44:57 -05:00
|
|
|
// Add a missing trailing comma.
|
|
|
|
if fmt.trailing_separator == SeparatorTactic::Never {
|
|
|
|
fields_str.push_str(",");
|
|
|
|
}
|
2017-03-20 17:23:59 -05: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-03-26 00:16:45 -05:00
|
|
|
// there are preceeding struct fields being matched on
|
|
|
|
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-20 17:23:59 -05:00
|
|
|
}
|
|
|
|
fields_str.push_str("..");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-16 03:47:09 -05:00
|
|
|
let fields_str = if context.config.struct_lit_style() == IndentStyle::Block &&
|
2017-03-20 17:23:59 -05:00
|
|
|
(fields_str.contains('\n') ||
|
2017-05-16 03:47:09 -05:00
|
|
|
context.config.struct_lit_multiline_style() ==
|
|
|
|
MultilineStyle::ForceMulti ||
|
2017-03-20 17:23:59 -05:00
|
|
|
fields_str.len() > h_shape.map(|s| s.width).unwrap_or(0)) {
|
|
|
|
format!("\n{}{}\n{}",
|
|
|
|
v_shape.indent.to_string(context.config),
|
|
|
|
fields_str,
|
|
|
|
shape.indent.to_string(context.config))
|
|
|
|
} else {
|
|
|
|
// One liner or visual indent.
|
|
|
|
format!(" {} ", fields_str)
|
|
|
|
};
|
|
|
|
|
|
|
|
Some(format!("{} {{{}}}", path_str, fields_str))
|
|
|
|
}
|
|
|
|
|
2016-02-11 19:59:13 -06:00
|
|
|
impl Rewrite for FieldPat {
|
2017-01-30 13:28:48 -06:00
|
|
|
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
|
|
|
|
let pat = self.pat.rewrite(context, shape);
|
2016-02-11 19:59:13 -06:00
|
|
|
if self.is_shorthand {
|
|
|
|
pat
|
|
|
|
} else {
|
2016-02-14 15:07:19 -06:00
|
|
|
wrap_str(format!("{}: {}", self.ident.to_string(), try_opt!(pat)),
|
2017-05-16 03:47:09 -05:00
|
|
|
context.config.max_width(),
|
2017-01-30 13:28:48 -06:00
|
|
|
shape)
|
2016-02-11 19:59:13 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-06 04:03:47 -05:00
|
|
|
|
|
|
|
enum TuplePatField<'a> {
|
|
|
|
Pat(&'a ptr::P<ast::Pat>),
|
|
|
|
Dotdot(Span),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Rewrite for TuplePatField<'a> {
|
2017-01-30 13:28:48 -06:00
|
|
|
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
|
2016-09-06 04:03:47 -05:00
|
|
|
match *self {
|
2017-01-30 13:28:48 -06:00
|
|
|
TuplePatField::Pat(ref p) => p.rewrite(context, shape),
|
2016-09-06 04:03:47 -05:00
|
|
|
TuplePatField::Dotdot(_) => Some("..".to_string()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Spanned for TuplePatField<'a> {
|
|
|
|
fn span(&self) -> Span {
|
|
|
|
match *self {
|
|
|
|
TuplePatField::Pat(ref p) => p.span(),
|
|
|
|
TuplePatField::Dotdot(span) => span,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn rewrite_tuple_pat(pats: &[ptr::P<ast::Pat>],
|
|
|
|
dotdot_pos: Option<usize>,
|
|
|
|
path_str: Option<String>,
|
|
|
|
span: Span,
|
|
|
|
context: &RewriteContext,
|
2017-01-30 13:28:48 -06:00
|
|
|
shape: Shape)
|
2016-09-06 04:03:47 -05:00
|
|
|
-> Option<String> {
|
2017-04-24 02:50:11 -05:00
|
|
|
let mut pat_vec: Vec<_> = pats.into_iter().map(|x| TuplePatField::Pat(x)).collect();
|
2016-09-06 04:03:47 -05:00
|
|
|
|
|
|
|
if let Some(pos) = dotdot_pos {
|
2017-03-08 20:49:58 -06:00
|
|
|
let prev = if pos == 0 {
|
|
|
|
span.lo
|
|
|
|
} else {
|
|
|
|
pats[pos - 1].span().hi
|
|
|
|
};
|
|
|
|
let next = if pos + 1 >= pats.len() {
|
|
|
|
span.hi
|
|
|
|
} else {
|
|
|
|
pats[pos + 1].span().lo
|
|
|
|
};
|
|
|
|
let dot_span = codemap::mk_sp(prev, next);
|
|
|
|
let snippet = context.snippet(dot_span);
|
|
|
|
let lo = dot_span.lo + BytePos(snippet.find_uncommented("..").unwrap() as u32);
|
2016-09-06 04:03:47 -05:00
|
|
|
let span = Span {
|
|
|
|
lo: lo,
|
|
|
|
// 2 == "..".len()
|
|
|
|
hi: lo + BytePos(2),
|
|
|
|
expn_id: codemap::NO_EXPANSION,
|
|
|
|
};
|
|
|
|
let dotdot = TuplePatField::Dotdot(span);
|
|
|
|
pat_vec.insert(pos, dotdot);
|
|
|
|
}
|
|
|
|
|
|
|
|
if pat_vec.is_empty() {
|
2017-03-25 01:58:34 -05:00
|
|
|
return Some(format!("{}()", try_opt!(path_str)));
|
|
|
|
}
|
|
|
|
// add comma if `(x,)`
|
|
|
|
let add_comma = path_str.is_none() && pat_vec.len() == 1 && dotdot_pos.is_none();
|
|
|
|
|
2017-03-27 17:25:59 -05:00
|
|
|
let path_len = path_str.as_ref().map(|p| p.len()).unwrap_or(0);
|
2017-03-25 01:58:34 -05:00
|
|
|
// 2 = "()".len(), 3 = "(,)".len()
|
|
|
|
let nested_shape = try_opt!(shape.sub_width(path_len + if add_comma { 3 } else { 2 }));
|
|
|
|
// 1 = "(".len()
|
|
|
|
let nested_shape = nested_shape.visual_indent(path_len + 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, nested_shape),
|
|
|
|
context.codemap.span_after(span, "("),
|
|
|
|
span.hi - BytePos(1))
|
2017-05-19 05:31:29 -05:00
|
|
|
.collect();
|
2017-03-25 01:58:34 -05:00
|
|
|
|
|
|
|
// Condense wildcard string suffix into a single ..
|
|
|
|
let wildcard_suffix_len = count_wildcard_suffix_len(&items);
|
|
|
|
|
2017-05-19 04:10:27 -05:00
|
|
|
let list = if context.config.condense_wildcard_suffixes() && wildcard_suffix_len >= 2 {
|
2017-03-25 01:58:34 -05:00
|
|
|
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, nested_shape, context.config))
|
2016-09-06 04:03:47 -05:00
|
|
|
} else {
|
2017-03-25 01:58:34 -05:00
|
|
|
try_opt!(format_item_list(items.into_iter(), nested_shape, context.config))
|
|
|
|
};
|
2016-09-06 04:03:47 -05:00
|
|
|
|
2017-03-25 01:58:34 -05:00
|
|
|
match path_str {
|
|
|
|
Some(path_str) => {
|
2017-05-16 03:47:09 -05:00
|
|
|
Some(if context.config.spaces_within_parens() {
|
2017-03-25 01:58:34 -05:00
|
|
|
format!("{}( {} )", path_str, list)
|
|
|
|
} else {
|
|
|
|
format!("{}({})", path_str, list)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
let comma = if add_comma { "," } else { "" };
|
2017-05-16 03:47:09 -05:00
|
|
|
Some(if context.config.spaces_within_parens() {
|
2017-03-25 01:58:34 -05:00
|
|
|
format!("( {}{} )", list, comma)
|
|
|
|
} else {
|
|
|
|
format!("({}{})", list, comma)
|
|
|
|
})
|
2016-09-06 04:03:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-07 14:38:20 -06:00
|
|
|
|
|
|
|
fn count_wildcard_suffix_len(items: &[ListItem]) -> usize {
|
|
|
|
let mut suffix_len = 0;
|
|
|
|
|
2017-05-16 09:24:38 -05:00
|
|
|
for item in items.iter().rev().take_while(|i| match i.item {
|
|
|
|
Some(ref internal_string) if internal_string ==
|
|
|
|
"_" => true,
|
|
|
|
_ => false,
|
|
|
|
}) {
|
2016-11-07 14:38:20 -06:00
|
|
|
suffix_len += 1;
|
|
|
|
|
|
|
|
if item.pre_comment.is_some() || item.post_comment.is_some() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
suffix_len
|
|
|
|
}
|