]> git.lizzy.rs Git - rust.git/blobdiff - src/patterns.rs
Use trim_tries to extract post comment over simple trim_matches
[rust.git] / src / patterns.rs
index 943434b68eca9271b9e07f8b5415a3a2970fb76d..d5c4802904b65ed9747d90099e413d556b7b4400 100644 (file)
 
 use config::lists::*;
 use syntax::ast::{self, BindingMode, FieldPat, Pat, PatKind, RangeEnd, RangeSyntax};
-use syntax::codemap::{self, BytePos, Span};
 use syntax::ptr;
+use syntax::source_map::{self, BytePos, Span};
 
-use codemap::SpanUtils;
 use comment::FindUncommented;
-use expr::{
-    can_be_overflowed_expr, rewrite_pair, rewrite_unary_prefix, wrap_struct_field, PairParts,
-};
+use expr::{can_be_overflowed_expr, rewrite_unary_prefix, wrap_struct_field};
 use lists::{
     itemize_list, shape_for_tactic, struct_lit_formatting, struct_lit_shape, struct_lit_tactic,
     write_list,
 };
 use macros::{rewrite_macro, MacroPosition};
 use overflow;
+use pairs::{rewrite_pair, PairParts};
 use rewrite::{Rewrite, RewriteContext};
 use shape::Shape;
+use source_map::SpanUtils;
 use spanned::Spanned;
 use types::{rewrite_path, PathContext};
 use utils::{format_mutability, mk_sp, rewrite_ident};
@@ -99,7 +98,7 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
                 }
             }
             PatKind::Range(ref lhs, ref rhs, ref end_kind) => {
-                let infix = match *end_kind {
+                let infix = match end_kind.node {
                     RangeEnd::Included(RangeSyntax::DotDotDot) => "...",
                     RangeEnd::Included(RangeSyntax::DotDotEq) => "..=",
                     RangeEnd::Excluded => "..",
@@ -112,7 +111,7 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
                 rewrite_pair(
                     &**lhs,
                     &**rhs,
-                    PairParts::new("", &infix, ""),
+                    PairParts::infix(&infix),
                     context,
                     shape,
                     SeparatorPlace::Front,
@@ -145,7 +144,8 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
                 let prefix = prefix.iter().map(|p| p.rewrite(context, shape));
                 let slice_pat = slice_pat
                     .as_ref()
-                    .map(|p| Some(format!("{}..", p.rewrite(context, shape)?)));
+                    .and_then(|p| p.rewrite(context, shape))
+                    .map(|rw| Some(format!("{}..", if rw == "_" { "" } else { &rw })));
                 let suffix = suffix.iter().map(|p| p.rewrite(context, shape));
 
                 // Munge them together.
@@ -171,7 +171,7 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
 
 fn rewrite_struct_pat(
     path: &ast::Path,
-    fields: &[codemap::Spanned<ast::FieldPat>],
+    fields: &[source_map::Spanned<ast::FieldPat>],
     ellipsis: bool,
     span: Span,
     context: &RewriteContext,
@@ -215,7 +215,7 @@ fn rewrite_struct_pat(
     if ellipsis {
         if fields_str.contains('\n') || fields_str.len() > one_line_width {
             // Add a missing trailing comma.
-            if fmt.trailing_separator == SeparatorTactic::Never {
+            if context.config.trailing_comma() == SeparatorTactic::Never {
                 fields_str.push_str(",");
             }
             fields_str.push_str("\n");
@@ -224,7 +224,7 @@ fn rewrite_struct_pat(
         } else {
             if !fields_str.is_empty() {
                 // there are preceding struct fields being matched on
-                if fmt.tactic == DefinitiveListTactic::Vertical {
+                if tactic == DefinitiveListTactic::Vertical {
                     // if the tactic is Vertical, write_list already added a trailing ,
                     fields_str.push_str(" ");
                 } else {
@@ -332,33 +332,32 @@ fn rewrite_tuple_pat(
             lo,
             // 2 == "..".len()
             lo + BytePos(2),
-            codemap::NO_EXPANSION,
+            source_map::NO_EXPANSION,
         ));
         pat_vec.insert(pos, dotdot);
     }
-
     if pat_vec.is_empty() {
         return Some(format!("{}()", path_str.unwrap_or_default()));
     }
-
     let wildcard_suffix_len = count_wildcard_suffix_len(context, &pat_vec, span, shape);
-    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);
-        let lo = sp.lo() + BytePos(snippet.find_uncommented("_").unwrap() as u32);
-        pat_vec[new_item_count - 1] = TuplePatField::Dotdot(mk_sp(lo, lo + BytePos(1)));
-        (
-            &pat_vec[..new_item_count],
-            mk_sp(span.lo(), lo + BytePos(1)),
-        )
-    } else {
-        (&pat_vec[..], span)
-    };
+    let (pat_vec, span, condensed) =
+        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);
+            let lo = sp.lo() + BytePos(snippet.find_uncommented("_").unwrap() as u32);
+            pat_vec[new_item_count - 1] = TuplePatField::Dotdot(mk_sp(lo, lo + BytePos(1)));
+            (
+                &pat_vec[..new_item_count],
+                mk_sp(span.lo(), lo + BytePos(1)),
+                true,
+            )
+        } else {
+            (&pat_vec[..], span, false)
+        };
 
     // add comma if `(x,)`
-    let add_comma = path_str.is_none() && pat_vec.len() == 1 && dotdot_pos.is_none();
+    let add_comma = path_str.is_none() && pat_vec.len() == 1 && dotdot_pos.is_none() && !condensed;
     let path_str = path_str.unwrap_or_default();
     let pat_ref_vec = pat_vec.iter().collect::<Vec<_>>();
 
@@ -369,7 +368,9 @@ fn rewrite_tuple_pat(
         shape,
         span,
         context.config.max_width(),
-        if add_comma {
+        if dotdot_pos.is_some() {
+            Some(SeparatorTactic::Never)
+        } else if add_comma {
             Some(SeparatorTactic::Always)
         } else {
             None