]> git.lizzy.rs Git - rust.git/commitdiff
Simplify post-comment extraction
authorSeiichi Uchida <seuchida@gmail.com>
Fri, 31 Aug 2018 09:19:13 +0000 (18:19 +0900)
committerSeiichi Uchida <seuchida@gmail.com>
Fri, 31 Aug 2018 09:20:28 +0000 (18:20 +0900)
src/chains.rs

index f73f57a19a6ab6bb833e4de7dc430132621467e8..a0174c622d50846f2d901c61cacedf10a2e32dee 100644 (file)
@@ -68,7 +68,7 @@
 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;
@@ -273,6 +273,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 +301,14 @@ fn handle_post_comment(
                 // No post comment.
                 return;
             }
-            // HACK: Treat `?`s as separators.
             let trimmed_snippet = trim_tries(post_comment_snippet);
-            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 {
+            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();
             }
         }