]> git.lizzy.rs Git - rust.git/blobdiff - src/chains.rs
Merge pull request #3011 from topecongiro/rustc-ap-syntax
[rust.git] / src / chains.rs
index d93995c94845aa6821f2d4d1c2bc2009253367a1..3acb8e2eac31b72bda951997a2e90af227e0508b 100644 (file)
 use comment::{rewrite_comment, CharClasses, FullCodeCharKind, RichChar};
 use config::IndentStyle;
 use expr::rewrite_call;
-use lists::{extract_post_comment, extract_pre_comment, get_comment_end};
+use lists::extract_pre_comment;
 use macros::convert_try_mac;
 use rewrite::{Rewrite, RewriteContext};
 use shape::Shape;
 use source_map::SpanUtils;
 use utils::{
-    first_line_width, last_line_extendable, last_line_width, mk_sp, trimmed_last_line_width,
-    wrap_str,
+    first_line_width, last_line_extendable, last_line_width, mk_sp, rewrite_ident,
+    trimmed_last_line_width, wrap_str,
 };
 
 use std::borrow::Cow;
@@ -190,10 +190,12 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
             ChainItemKind::MethodCall(ref segment, ref types, ref exprs) => {
                 Self::rewrite_method_call(segment.ident, types, exprs, self.span, context, shape)?
             }
-            ChainItemKind::StructField(ident) => format!(".{}", ident.name),
-            ChainItemKind::TupleField(ident, nested) => {
-                format!("{}.{}", if nested { " " } else { "" }, ident.name)
-            }
+            ChainItemKind::StructField(ident) => format!(".{}", rewrite_ident(context, ident)),
+            ChainItemKind::TupleField(ident, nested) => format!(
+                "{}.{}",
+                if nested { " " } else { "" },
+                rewrite_ident(context, ident)
+            ),
             ChainItemKind::Comment(ref comment, _) => {
                 rewrite_comment(comment, false, shape, context.config)?
             }
@@ -241,7 +243,7 @@ fn rewrite_method_call(
 
             format!("::<{}>", type_list.join(", "))
         };
-        let callee_str = format!(".{}{}", method_name, type_str);
+        let callee_str = format!(".{}{}", rewrite_ident(context, method_name), type_str);
         rewrite_call(context, &callee_str, &args[1..], span, shape)
     }
 }
@@ -273,6 +275,20 @@ fn is_tries(s: &str) -> bool {
             s.chars().all(|c| c == '?')
         }
 
+        fn is_post_comment(s: &str) -> bool {
+            let comment_start_index = s.chars().position(|c| c == '/');
+            if comment_start_index.is_none() {
+                return false;
+            }
+
+            let newline_index = s.chars().position(|c| c == '\n');
+            if newline_index.is_none() {
+                return true;
+            }
+
+            comment_start_index.unwrap() < newline_index.unwrap()
+        }
+
         fn handle_post_comment(
             post_comment_span: Span,
             post_comment_snippet: &str,
@@ -287,25 +303,14 @@ fn handle_post_comment(
                 // No post comment.
                 return;
             }
-            // HACK: Treat `?`s as separators.
-            let trimmed_snippet = post_comment_snippet.trim_matches('?');
-            let comment_end = get_comment_end(trimmed_snippet, "?", "", false);
-            let maybe_post_comment = extract_post_comment(trimmed_snippet, comment_end, "?")
-                .and_then(|comment| {
-                    if comment.is_empty() {
-                        None
-                    } else {
-                        Some((comment, comment_end))
-                    }
-                });
-
-            if let Some((post_comment, comment_end)) = maybe_post_comment {
+            let trimmed_snippet = trim_tries(post_comment_snippet);
+            if is_post_comment(&trimmed_snippet) {
                 children.push(ChainItem::comment(
                     post_comment_span,
-                    post_comment,
+                    trimmed_snippet.trim().to_owned(),
                     CommentPosition::Back,
                 ));
-                *prev_span_end = *prev_span_end + BytePos(comment_end as u32);
+                *prev_span_end = post_comment_span.hi();
             }
         }