]> git.lizzy.rs Git - rust.git/commitdiff
Refactor - remove duplicates
authortopecongiro <seuchida@gmail.com>
Sun, 27 Aug 2017 15:13:42 +0000 (00:13 +0900)
committertopecongiro <seuchida@gmail.com>
Sun, 27 Aug 2017 15:13:42 +0000 (00:13 +0900)
replacing some functions with recover_missing_comments() and
rewrite_missing_comments().

src/comment.rs
src/expr.rs
src/items.rs

index bb7ce14dcddd998cb8c04b8f7e6cf9733dd5fe6a..aa078f4dbf7cf93280385e3631d7300dea2b6c6a 100644 (file)
@@ -153,11 +153,10 @@ pub fn combine_strs_with_missing_comments(
     let mut one_line_width =
         last_line_width(prev_str) + first_line_width(next_str) + first_sep.len();
 
-    let original_snippet = context.snippet(span);
-    let trimmed_snippet = original_snippet.trim();
     let indent_str = shape.indent.to_string(context.config);
+    let missing_comment = try_opt!(rewrite_missing_comment(span, shape, context));
 
-    if trimmed_snippet.is_empty() {
+    if missing_comment.is_empty() {
         if allow_extend && prev_str.len() + first_sep.len() + next_str.len() <= shape.width {
             return Some(format!("{}{}{}", prev_str, first_sep, next_str));
         } else {
@@ -175,18 +174,13 @@ pub fn combine_strs_with_missing_comments(
     // Peek the the original source code and find out whether there is a newline between the first
     // expression and the second expression or the missing comment. We will preserve the orginal
     // layout whenever possible.
+    let original_snippet = context.snippet(span);
     let prefer_same_line = if let Some(pos) = original_snippet.chars().position(|c| c == '/') {
         !original_snippet[..pos].contains('\n')
     } else {
         !original_snippet.contains('\n')
     };
 
-    let missing_comment = try_opt!(rewrite_comment(
-        trimmed_snippet,
-        false,
-        shape,
-        context.config
-    ));
     one_line_width -= first_sep.len();
     let first_sep = if prev_str.is_empty() || missing_comment.is_empty() {
         String::new()
index a42ab973e0d25ac3a891cb5dae35d35a6bb1b9ff..1bb910b61b3ab1b793596e58ffa10febaee5c9bc 100644 (file)
@@ -20,7 +20,7 @@
 use chains::rewrite_chain;
 use codemap::{LineRangeUtils, SpanUtils};
 use comment::{combine_strs_with_missing_comments, contains_comment, recover_comment_removed,
-              rewrite_comment, FindUncommented};
+              rewrite_comment, rewrite_missing_comment, FindUncommented};
 use config::{Config, ControlBraceStyle, IndentStyle, MultilineStyle, Style};
 use items::{span_hi_for_arg, span_lo_for_arg};
 use lists::{definitive_tactic, itemize_list, shape_for_tactic, struct_lit_formatting,
@@ -1195,9 +1195,13 @@ fn rewrite_cond(
             mk_sp(self.block.span.lo, self.block.span.lo)
         };
 
-        // for event in event
+        // `for event in event`
+        // Do not include label in the span.
+        let lo = self.label.map_or(self.span.lo, |label| label.span.hi);
         let between_kwd_cond = mk_sp(
-            context.codemap.span_after(self.span, self.keyword.trim()),
+            context
+                .codemap
+                .span_after(mk_sp(lo, self.span.hi), self.keyword.trim()),
             self.pat
                 .map_or(cond_span.lo, |p| if self.matcher.is_empty() {
                     p.span.lo
@@ -1378,21 +1382,13 @@ fn rewrite_label(label: Option<ast::SpannedIdent>) -> String {
 }
 
 fn extract_comment(span: Span, context: &RewriteContext, shape: Shape) -> Option<String> {
-    let comment_str = context.snippet(span);
-    if contains_comment(&comment_str) {
-        let comment = try_opt!(rewrite_comment(
-            comment_str.trim(),
-            false,
-            shape,
-            context.config,
-        ));
-        Some(format!(
+    match rewrite_missing_comment(span, shape, context) {
+        Some(ref comment) if !comment.is_empty() => Some(format!(
             "\n{indent}{}\n{indent}",
             comment,
             indent = shape.indent.to_string(context.config)
-        ))
-    } else {
-        None
+        )),
+        _ => None,
     }
 }
 
index 9dd1972670a561b43af60af2c92ff3e06b4e5b1f..c3092553b91bcc4d89b348bb4eee5a837ffae155 100644 (file)
@@ -19,7 +19,7 @@
 use {Indent, Shape, Spanned};
 use codemap::{LineRangeUtils, SpanUtils};
 use comment::{combine_strs_with_missing_comments, contains_comment, recover_comment_removed,
-              rewrite_comment, FindUncommented};
+              recover_missing_comment_in_span, rewrite_missing_comment, FindUncommented};
 use config::{BraceStyle, Config, Density, IndentStyle, ReturnIndent, Style};
 use expr::{format_expr, is_empty_block, is_simple_block_stmt, rewrite_assign_rhs,
            rewrite_call_inner, ExprType};
@@ -2006,33 +2006,17 @@ fn rewrite_fn_base(
     // args and `{`.
     if where_clause_str.is_empty() {
         if let ast::FunctionRetTy::Default(ret_span) = fd.output {
-            let sp = mk_sp(args_span.hi, ret_span.hi);
-            let missing_snippet = context.snippet(sp);
-            let trimmed_snippet = missing_snippet.trim();
-            let missing_comment = if trimmed_snippet.is_empty() {
-                String::new()
-            } else {
-                try_opt!(rewrite_comment(
-                    trimmed_snippet,
-                    false,
-                    Shape::indented(indent, context.config),
-                    context.config,
-                ))
-            };
-            if !missing_comment.is_empty() {
-                let pos = missing_snippet.chars().position(|c| c == '/').unwrap_or(0);
-                // 1 = ` `
-                let total_width = missing_comment.len() + last_line_width(&result) + 1;
-                let force_new_line_before_comment = missing_snippet[..pos].contains('\n') ||
-                    total_width > context.config.max_width();
-                let sep = if force_new_line_before_comment {
-                    format!("\n{}", indent.to_string(context.config))
-                } else {
-                    String::from(" ")
-                };
-                result.push_str(&sep);
-                result.push_str(&missing_comment);
-                force_new_line_for_brace = true;
+            match recover_missing_comment_in_span(
+                mk_sp(args_span.hi, ret_span.hi),
+                shape,
+                context,
+                last_line_width(&result),
+            ) {
+                Some(ref missing_comment) if !missing_comment.is_empty() => {
+                    result.push_str(missing_comment);
+                    force_new_line_for_brace = true;
+                }
+                _ => (),
             }
         }
     }
@@ -2684,34 +2668,17 @@ fn missing_span_before_after_where(
     (missing_span_before, missing_span_after)
 }
 
-fn rewrite_missing_comment_in_where(
-    context: &RewriteContext,
-    comment: &str,
-    shape: Shape,
-) -> Option<String> {
-    let comment = comment.trim();
-    if comment.is_empty() {
-        Some(String::new())
-    } else {
-        rewrite_comment(comment, false, shape, context.config)
-    }
-}
-
 fn rewrite_comments_before_after_where(
     context: &RewriteContext,
     span_before_where: Span,
     span_after_where: Span,
     shape: Shape,
 ) -> Option<(String, String)> {
-    let before_comment = try_opt!(rewrite_missing_comment_in_where(
-        context,
-        &context.snippet(span_before_where),
-        shape,
-    ));
-    let after_comment = try_opt!(rewrite_missing_comment_in_where(
-        context,
-        &context.snippet(span_after_where),
+    let before_comment = try_opt!(rewrite_missing_comment(span_before_where, shape, context));
+    let after_comment = try_opt!(rewrite_missing_comment(
+        span_after_where,
         shape.block_indent(context.config.tab_spaces()),
+        context,
     ));
     Some((before_comment, after_comment))
 }