]> git.lizzy.rs Git - rust.git/commitdiff
Refactoring: remove duplicates
authortopecongiro <seuchida@gmail.com>
Wed, 30 Aug 2017 10:26:45 +0000 (19:26 +0900)
committertopecongiro <seuchida@gmail.com>
Wed, 30 Aug 2017 10:26:45 +0000 (19:26 +0900)
src/imports.rs
src/items.rs
src/visitor.rs

index e5dfb12f4c491a778a2872aaddf5bf94fd7486c5..afa81693548fa9b53d0e5d7d4f288a66d9daa6c9 100644 (file)
@@ -260,7 +260,7 @@ pub fn format_import(
     ) {
         let vis = utils::format_visibility(vis);
         // 4 = `use `, 1 = `;`
-        let rw = Shape::indented(self.block_indent, self.config)
+        let rw = self.shape()
             .offset_left(vis.len() + 4)
             .and_then(|shape| shape.sub_width(1))
             .and_then(|shape| match vp.node {
index 94a13a502eb64c315fb42ed1a38e1dc355e13aed..5908464c416f65998f35950d81c7b1078dcaf8df 100644 (file)
@@ -187,8 +187,7 @@ pub fn format_foreign_mod(&mut self, fm: &ast::ForeignMod, span: Span) {
 
 
     fn format_foreign_item(&mut self, item: &ast::ForeignItem) {
-        let shape = Shape::indented(self.block_indent, self.config);
-        let rewrite = item.rewrite(&self.get_context(), shape);
+        let rewrite = item.rewrite(&self.get_context(), self.shape());
         self.push_rewrite(item.span(), rewrite);
         self.last_pos = item.span.hi;
     }
@@ -312,18 +311,11 @@ fn single_line_fn(&self, fn_str: &str, block: &ast::Block) -> Option<String> {
                                 ""
                             };
 
-                            format_expr(
-                                &e,
-                                ExprType::Statement,
-                                &self.get_context(),
-                                Shape::indented(self.block_indent, self.config),
-                            ).map(|s| s + suffix)
+                            format_expr(&e, ExprType::Statement, &self.get_context(), self.shape())
+                                .map(|s| s + suffix)
                                 .or_else(|| Some(self.snippet(e.span)))
                         }
-                        None => stmt.rewrite(
-                            &self.get_context(),
-                            Shape::indented(self.block_indent, self.config),
-                        ),
+                        None => stmt.rewrite(&self.get_context(), self.shape()),
                     }
                 } else {
                     None
@@ -421,9 +413,7 @@ fn format_variant_list(
             false,
         );
 
-        let shape = Shape::indented(self.block_indent, self.config)
-            .sub_width(2)
-            .unwrap();
+        let shape = self.shape().sub_width(2).unwrap();
         let fmt = ListFormatting {
             tactic: DefinitiveListTactic::Vertical,
             separator: ",",
@@ -451,7 +441,7 @@ fn format_variant(&self, field: &ast::Variant) -> Option<String> {
 
         let context = self.get_context();
         let indent = self.block_indent;
-        let shape = Shape::indented(indent, self.config);
+        let shape = self.shape();
         let attrs_str = try_opt!(field.node.attrs.rewrite(&context, shape));
         let lo = field
             .node
index f07dad4a6ba5e204825cf0dca5d55982b8539da1..fad14f19ce824cf068629c141e331739b014c231 100644 (file)
@@ -58,6 +58,10 @@ pub struct FmtVisitor<'a> {
 }
 
 impl<'a> FmtVisitor<'a> {
+    pub fn shape(&self) -> Shape {
+        Shape::indented(self.block_indent, self.config)
+    }
+
     fn visit_stmt(&mut self, stmt: &ast::Stmt) {
         debug!(
             "visit_stmt: {:?} {:?}",
@@ -138,9 +142,7 @@ pub fn visit_block(&mut self, b: &ast::Block, inner_attrs: Option<&[ast::Attribu
             if let Some(first_stmt) = b.stmts.first() {
                 let attr_lo = inner_attrs
                     .and_then(|attrs| {
-                        utils::inner_attributes(attrs)
-                            .first()
-                            .map(|attr| attr.span.lo)
+                        inner_attributes(attrs).first().map(|attr| attr.span.lo)
                     })
                     .or_else(|| {
                         // Attributes for an item in a statement position
@@ -218,7 +220,7 @@ pub fn visit_block(&mut self, b: &ast::Block, inner_attrs: Option<&[ast::Attribu
         let mut unindent_comment = self.is_if_else_block && !b.stmts.is_empty();
         if unindent_comment {
             let end_pos = source!(self, b.span).hi - brace_compensation - remove_len;
-            let snippet = self.get_context().snippet(mk_sp(self.last_pos, end_pos));
+            let snippet = self.snippet(mk_sp(self.last_pos, end_pos));
             unindent_comment = snippet.contains("//") || snippet.contains("/*");
         }
         // FIXME: we should compress any newlines here to just one
@@ -336,7 +338,7 @@ pub fn visit_item(&mut self, item: &ast::Item) {
                         self.push_rewrite(item.span, None);
                         return;
                     }
-                } else if utils::contains_skip(&item.attrs) {
+                } else if contains_skip(&item.attrs) {
                     // Module is not inline, but should be skipped.
                     return;
                 } else {
@@ -371,7 +373,7 @@ pub fn visit_item(&mut self, item: &ast::Item) {
             }
             ast::ItemKind::Impl(..) => {
                 self.format_missing_with_indent(source!(self, item.span).lo);
-                let snippet = self.get_context().snippet(item.span);
+                let snippet = self.snippet(item.span);
                 let where_span_end = snippet
                     .find_uncommented("{")
                     .map(|x| (BytePos(x as u32)) + source!(self, item.span).lo);
@@ -635,9 +637,7 @@ fn visit_mac(&mut self, mac: &ast::Mac, ident: Option<ast::Ident>, pos: MacroPos
         skip_out_of_file_lines_range_visitor!(self, mac.span);
 
         // 1 = ;
-        let shape = Shape::indented(self.block_indent, self.config)
-            .sub_width(1)
-            .unwrap();
+        let shape = self.shape().sub_width(1).unwrap();
         let rewrite = rewrite_macro(mac, ident, &self.get_context(), shape, pos);
         self.push_rewrite(mac.span, rewrite);
     }
@@ -677,7 +677,7 @@ pub fn snippet(&self, span: Span) -> String {
 
     // Returns true if we should skip the following item.
     pub fn visit_attrs(&mut self, attrs: &[ast::Attribute], style: ast::AttrStyle) -> bool {
-        if utils::contains_skip(attrs) {
+        if contains_skip(attrs) {
             return true;
         }
 
@@ -686,10 +686,7 @@ pub fn visit_attrs(&mut self, attrs: &[ast::Attribute], style: ast::AttrStyle) -
             return false;
         }
 
-        let rewrite = attrs.rewrite(
-            &self.get_context(),
-            Shape::indented(self.block_indent, self.config),
-        );
+        let rewrite = attrs.rewrite(&self.get_context(), self.shape());
         let span = mk_sp(attrs[0].span.lo, attrs[attrs.len() - 1].span.hi);
         self.push_rewrite(span, rewrite);