1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 use rewrite::{Rewrite, RewriteContext};
13 use utils::{CodeMapSpanUtils, wrap_str, format_mutability};
14 use lists::{format_item_list, itemize_list};
15 use expr::{rewrite_unary_prefix, rewrite_pair, rewrite_tuple};
16 use types::rewrite_path;
18 use syntax::ast::{BindingMode, Pat, PatKind, FieldPat};
20 impl Rewrite for Pat {
21 fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> {
23 PatKind::Box(ref pat) => rewrite_unary_prefix(context, "box ", &**pat, width, offset),
24 PatKind::Ident(binding_mode, ident, ref sub_pat) => {
25 let (prefix, mutability) = match binding_mode {
26 BindingMode::ByRef(mutability) => ("ref ", mutability),
27 BindingMode::ByValue(mutability) => ("", mutability),
29 let mut_infix = format_mutability(mutability);
30 let id_str = ident.node.to_string();
32 let sub_pat = match *sub_pat {
35 let width = try_opt!(width.checked_sub(prefix.len() + mut_infix.len() +
38 format!(" @ {}", try_opt!(p.rewrite(context, width, offset)))
40 None => "".to_owned(),
43 let result = format!("{}{}{}{}", prefix, mut_infix, id_str, sub_pat);
44 wrap_str(result, context.config.max_width, width, offset)
53 PatKind::QPath(ref q_self, ref path) => {
54 rewrite_path(context, true, Some(q_self), path, width, offset)
56 PatKind::Range(ref lhs, ref rhs) => {
57 rewrite_pair(&**lhs, &**rhs, "", "...", "", context, width, offset)
59 PatKind::Ref(ref pat, mutability) => {
60 let prefix = format!("&{}", format_mutability(mutability));
61 rewrite_unary_prefix(context, &prefix, &**pat, width, offset)
63 PatKind::Tup(ref items) => {
64 rewrite_tuple(context,
65 items.iter().map(|x| &**x),
70 PatKind::Path(ref path) => rewrite_path(context, true, None, path, width, offset),
71 PatKind::TupleStruct(ref path, ref pat_vec) => {
72 let path_str = try_opt!(rewrite_path(context, true, None, path, width, offset));
75 Some(ref pat_vec) => {
76 if pat_vec.is_empty() {
80 let width = try_opt!(width.checked_sub(path_str.len() + 2));
82 let offset = offset + path_str.len() + 1;
83 let items = itemize_list(context.codemap,
88 |item| item.rewrite(context, width, offset),
89 context.codemap.span_after(self.span, "("),
91 Some(format!("{}({})",
93 try_opt!(format_item_list(items,
99 None => Some(format!("{}(..)", path_str)),
102 PatKind::Lit(ref expr) => expr.rewrite(context, width, offset),
103 PatKind::Vec(ref prefix, ref slice_pat, ref suffix) => {
104 // Rewrite all the sub-patterns.
105 let prefix = prefix.iter().map(|p| p.rewrite(context, width, offset));
106 let slice_pat = slice_pat.as_ref().map(|p| {
107 Some(format!("{}..", try_opt!(p.rewrite(context, width, offset))))
109 let suffix = suffix.iter().map(|p| p.rewrite(context, width, offset));
111 // Munge them together.
112 let pats: Option<Vec<String>> = prefix.chain(slice_pat.into_iter())
116 // Check that all the rewrites succeeded, and if not return None.
117 let pats = try_opt!(pats);
119 // Unwrap all the sub-strings and join them with commas.
120 let result = format!("[{}]", pats.join(", "));
121 wrap_str(result, context.config.max_width, width, offset)
123 PatKind::Struct(ref path, ref fields, elipses) => {
124 let path = try_opt!(rewrite_path(context, true, None, path, width, offset));
126 let (elipses_str, terminator) = if elipses {
132 // 5 = `{` plus space before and after plus `}` plus space before.
133 let budget = try_opt!(width.checked_sub(path.len() + 5 + elipses_str.len()));
134 // FIXME Using visual indenting, should use block or visual to match
135 // struct lit preference (however, in practice I think it is rare
136 // for struct patterns to be multi-line).
137 // 3 = `{` plus space before and after.
138 let offset = offset + path.len() + 3;
140 let items = itemize_list(context.codemap,
145 |f| f.node.rewrite(context, budget, offset),
146 context.codemap.span_after(self.span, "{"),
148 let mut field_string = try_opt!(format_item_list(items,
153 if field_string.contains('\n') {
154 field_string.push_str(",\n");
155 field_string.push_str(&offset.to_string(context.config));
156 field_string.push_str("..");
158 if field_string.len() > 0 {
159 field_string.push_str(", ");
161 field_string.push_str("..");
165 if field_string.is_empty() {
166 Some(format!("{} {{}}", path))
168 Some(format!("{} {{ {} }}", path, field_string))
171 // FIXME(#819) format pattern macros.
172 PatKind::Mac(..) => {
173 wrap_str(context.snippet(self.span),
174 context.config.max_width,
182 impl Rewrite for FieldPat {
183 fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> {
184 let pat = self.pat.rewrite(context, width, offset);
185 if self.is_shorthand {
188 wrap_str(format!("{}: {}", self.ident.to_string(), try_opt!(pat)),
189 context.config.max_width,