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.
|
|
|
|
|
|
|
|
use Indent;
|
2016-05-25 13:41:26 -05:00
|
|
|
use codemap::SpanUtils;
|
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};
|
2015-10-17 08:56:53 -05:00
|
|
|
use lists::{format_item_list, itemize_list};
|
2016-09-06 04:03:47 -05:00
|
|
|
use expr::{rewrite_unary_prefix, rewrite_pair};
|
2015-10-17 08:56:53 -05:00
|
|
|
use types::rewrite_path;
|
2016-09-06 04:03:47 -05:00
|
|
|
use super::Spanned;
|
|
|
|
use comment::FindUncommented;
|
2015-10-17 08:56:53 -05:00
|
|
|
|
2016-09-06 04:03:47 -05:00
|
|
|
use syntax::ast::{self, BindingMode, Pat, PatKind, FieldPat};
|
|
|
|
use syntax::ptr;
|
|
|
|
use syntax::codemap::{self, BytePos, Span};
|
2015-10-17 08:56:53 -05:00
|
|
|
|
|
|
|
impl Rewrite for Pat {
|
|
|
|
fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> {
|
|
|
|
match self.node {
|
2016-03-01 16:27:19 -06:00
|
|
|
PatKind::Box(ref pat) => rewrite_unary_prefix(context, "box ", &**pat, width, offset),
|
|
|
|
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 - ` @ `.
|
2016-02-11 19:59:13 -06:00
|
|
|
let width = try_opt!(width.checked_sub(prefix.len() + mut_infix.len() +
|
2016-02-14 15:07:19 -06:00
|
|
|
id_str.len() +
|
|
|
|
3));
|
2016-02-11 19:59:13 -06:00
|
|
|
format!(" @ {}", try_opt!(p.rewrite(context, width, offset)))
|
|
|
|
}
|
|
|
|
None => "".to_owned(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let result = format!("{}{}{}{}", prefix, mut_infix, id_str, sub_pat);
|
2015-10-17 08:56:53 -05:00
|
|
|
wrap_str(result, context.config.max_width, width, offset)
|
|
|
|
}
|
2016-03-01 16:27:19 -06:00
|
|
|
PatKind::Wild => {
|
2015-11-12 14:38:41 -06:00
|
|
|
if 1 <= width {
|
|
|
|
Some("_".to_owned())
|
2015-10-17 08:56:53 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2016-03-01 16:27:19 -06:00
|
|
|
PatKind::Range(ref lhs, ref rhs) => {
|
2015-10-17 08:56:53 -05:00
|
|
|
rewrite_pair(&**lhs, &**rhs, "", "...", "", context, width, offset)
|
|
|
|
}
|
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));
|
|
|
|
rewrite_unary_prefix(context, &prefix, &**pat, width, offset)
|
|
|
|
}
|
2016-05-30 07:53:04 -05:00
|
|
|
PatKind::Tuple(ref items, dotdot_pos) => {
|
2016-09-06 04:03:47 -05:00
|
|
|
rewrite_tuple_pat(items, dotdot_pos, None, self.span, context, width, offset)
|
2015-12-05 18:11:26 -06:00
|
|
|
}
|
2016-09-15 22:19:18 -05:00
|
|
|
PatKind::Path(ref q_self, ref path) => {
|
|
|
|
rewrite_path(context, true, q_self.as_ref(), path, width, offset)
|
|
|
|
}
|
2016-05-30 07:53:04 -05:00
|
|
|
PatKind::TupleStruct(ref path, ref pat_vec, dotdot_pos) => {
|
2016-02-11 19:59:13 -06:00
|
|
|
let path_str = try_opt!(rewrite_path(context, true, None, path, width, offset));
|
2016-09-06 04:03:47 -05:00
|
|
|
rewrite_tuple_pat(pat_vec,
|
|
|
|
dotdot_pos,
|
|
|
|
Some(path_str),
|
|
|
|
self.span,
|
|
|
|
context,
|
|
|
|
width,
|
|
|
|
offset)
|
2015-10-17 08:56:53 -05:00
|
|
|
}
|
2016-03-01 16:27:19 -06:00
|
|
|
PatKind::Lit(ref expr) => expr.rewrite(context, width, offset),
|
|
|
|
PatKind::Vec(ref prefix, ref slice_pat, ref suffix) => {
|
2016-02-11 19:59:13 -06:00
|
|
|
// Rewrite all the sub-patterns.
|
|
|
|
let prefix = prefix.iter().map(|p| p.rewrite(context, width, offset));
|
2016-04-22 02:03:36 -05:00
|
|
|
let slice_pat = slice_pat.as_ref()
|
|
|
|
.map(|p| Some(format!("{}..", try_opt!(p.rewrite(context, width, offset)))));
|
2016-02-11 19:59:13 -06:00
|
|
|
let suffix = suffix.iter().map(|p| p.rewrite(context, width, offset));
|
|
|
|
|
|
|
|
// Munge them together.
|
2016-02-14 15:07:19 -06:00
|
|
|
let pats: Option<Vec<String>> = prefix.chain(slice_pat.into_iter())
|
2016-04-22 02:03:36 -05:00
|
|
|
.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.
|
2016-10-17 15:09:49 -05:00
|
|
|
let result = if context.config.spaces_within_square_brackets {
|
|
|
|
format!("[ {} ]", pats.join(", "))
|
|
|
|
} else {
|
|
|
|
format!("[{}]", pats.join(", "))
|
|
|
|
};
|
2016-02-14 15:07:19 -06:00
|
|
|
wrap_str(result, context.config.max_width, width, offset)
|
2016-02-11 19:59:13 -06:00
|
|
|
}
|
2016-03-01 16:27:19 -06:00
|
|
|
PatKind::Struct(ref path, ref fields, elipses) => {
|
2016-02-11 19:59:13 -06:00
|
|
|
let path = try_opt!(rewrite_path(context, true, None, path, width, offset));
|
|
|
|
|
2016-05-29 10:58:38 -05:00
|
|
|
let (elipses_str, terminator) = if elipses { (", ..", "..") } else { ("", "}") };
|
2016-02-11 19:59:13 -06:00
|
|
|
|
2016-02-14 15:07:19 -06:00
|
|
|
// 5 = `{` plus space before and after plus `}` plus space before.
|
2016-02-11 19:59:13 -06:00
|
|
|
let budget = try_opt!(width.checked_sub(path.len() + 5 + elipses_str.len()));
|
|
|
|
// FIXME Using visual indenting, should use block or visual to match
|
|
|
|
// struct lit preference (however, in practice I think it is rare
|
|
|
|
// for struct patterns to be multi-line).
|
2016-02-14 15:07:19 -06:00
|
|
|
// 3 = `{` plus space before and after.
|
2016-02-11 19:59:13 -06:00
|
|
|
let offset = offset + path.len() + 3;
|
|
|
|
|
|
|
|
let items = itemize_list(context.codemap,
|
|
|
|
fields.iter(),
|
|
|
|
terminator,
|
|
|
|
|f| f.span.lo,
|
|
|
|
|f| f.span.hi,
|
|
|
|
|f| f.node.rewrite(context, budget, offset),
|
2016-03-07 12:41:32 -06:00
|
|
|
context.codemap.span_after(self.span, "{"),
|
2016-02-11 19:59:13 -06:00
|
|
|
self.span.hi);
|
2016-05-12 14:50:43 -05:00
|
|
|
let mut field_string =
|
|
|
|
try_opt!(format_item_list(items, budget, offset, context.config));
|
2016-02-11 19:59:13 -06:00
|
|
|
if elipses {
|
|
|
|
if field_string.contains('\n') {
|
|
|
|
field_string.push_str(",\n");
|
|
|
|
field_string.push_str(&offset.to_string(context.config));
|
|
|
|
field_string.push_str("..");
|
|
|
|
} else {
|
2016-08-23 09:14:45 -05:00
|
|
|
if !field_string.is_empty() {
|
2016-02-11 19:59:13 -06:00
|
|
|
field_string.push_str(", ");
|
|
|
|
}
|
|
|
|
field_string.push_str("..");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-14 15:07:19 -06:00
|
|
|
if field_string.is_empty() {
|
2016-02-11 19:59:13 -06:00
|
|
|
Some(format!("{} {{}}", path))
|
|
|
|
} else {
|
|
|
|
Some(format!("{} {{ {} }}", path, field_string))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// FIXME(#819) format pattern macros.
|
2016-03-01 16:27:19 -06:00
|
|
|
PatKind::Mac(..) => {
|
2015-10-17 08:56:53 -05:00
|
|
|
wrap_str(context.snippet(self.span),
|
|
|
|
context.config.max_width,
|
|
|
|
width,
|
|
|
|
offset)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-11 19:59:13 -06:00
|
|
|
|
|
|
|
impl Rewrite for FieldPat {
|
|
|
|
fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> {
|
|
|
|
let pat = self.pat.rewrite(context, width, offset);
|
|
|
|
if self.is_shorthand {
|
|
|
|
pat
|
|
|
|
} else {
|
2016-02-14 15:07:19 -06:00
|
|
|
wrap_str(format!("{}: {}", self.ident.to_string(), try_opt!(pat)),
|
|
|
|
context.config.max_width,
|
|
|
|
width,
|
|
|
|
offset)
|
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> {
|
|
|
|
fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> {
|
|
|
|
match *self {
|
|
|
|
TuplePatField::Pat(ref p) => p.rewrite(context, width, offset),
|
|
|
|
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,
|
|
|
|
width: usize,
|
|
|
|
offset: Indent)
|
|
|
|
-> Option<String> {
|
|
|
|
let mut pat_vec: Vec<_> = pats.into_iter().map(|x| TuplePatField::Pat(x)).collect();
|
|
|
|
|
|
|
|
if let Some(pos) = dotdot_pos {
|
|
|
|
let snippet = context.snippet(span);
|
|
|
|
let lo = span.lo + BytePos(snippet.find_uncommented("..").unwrap() as u32);
|
|
|
|
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() {
|
|
|
|
path_str
|
|
|
|
} else {
|
|
|
|
// add comma if `(x,)`
|
|
|
|
let add_comma = path_str.is_none() && pat_vec.len() == 1 && dotdot_pos.is_none();
|
|
|
|
|
|
|
|
let path_len = path_str.as_ref().map(|p| p.len()).unwrap_or(0);
|
|
|
|
// 2 = "()".len(), 3 = "(,)".len()
|
|
|
|
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 list = try_opt!(format_item_list(items, width, offset, context.config));
|
|
|
|
|
|
|
|
match path_str {
|
2016-10-12 20:34:08 -05:00
|
|
|
Some(path_str) => {
|
|
|
|
Some(if context.config.spaces_within_parens {
|
|
|
|
format!("{}( {} )", path_str, list)
|
|
|
|
} else {
|
|
|
|
format!("{}({})", path_str, list)
|
|
|
|
})
|
|
|
|
}
|
2016-09-06 04:03:47 -05:00
|
|
|
None => {
|
|
|
|
let comma = if add_comma { "," } else { "" };
|
2016-10-12 20:34:08 -05:00
|
|
|
Some(if context.config.spaces_within_parens {
|
|
|
|
format!("( {}{} )", list, comma)
|
|
|
|
} else {
|
|
|
|
format!("({}{})", list, comma)
|
|
|
|
})
|
2016-09-06 04:03:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|