]> git.lizzy.rs Git - rust.git/commitdiff
Added support for associated type rewriting
authorConnor Brewster <brewsterc@my.caspercollege.edu>
Sat, 12 Mar 2016 05:32:08 +0000 (22:32 -0700)
committerConnor Brewster <brewsterc@my.caspercollege.edu>
Sat, 12 Mar 2016 05:32:08 +0000 (22:32 -0700)
src/items.rs
src/visitor.rs

index 85e25c0b95affc6d1429900e30a60bcacd2d1e7a..38ae285197e5f9c475611847666f12f25b8f3b8b 100644 (file)
@@ -22,7 +22,7 @@
 use rewrite::{Rewrite, RewriteContext};
 use config::{Config, BlockIndentStyle, Density, ReturnIndent, BraceStyle, StructLitStyle};
 
-use syntax::{ast, abi, ptr};
+use syntax::{ast, abi};
 use syntax::codemap::{Span, BytePos, mk_sp};
 use syntax::parse::token;
 
@@ -600,8 +600,14 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
         let trait_bound_str = try_opt!(rewrite_trait_bounds(context,
                                                             type_param_bounds,
                                                             offset,
-                                                            0));
+                                                            context.config.max_width));
 
+        if offset.width() + result.len() + trait_bound_str.len() > context.config.max_width {
+            result.push('\n');
+            let width = context.block_indent.width() + context.config.tab_spaces - 1;
+            let trait_indent = Indent::new(0, width);
+            result.push_str(&trait_indent.to_string(context.config));
+        }
         result.push_str(&trait_bound_str);
 
         let where_budget = try_opt!(context.config.max_width.checked_sub(last_line_width(&result)));
@@ -623,12 +629,48 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
         }
         result.push_str(&where_clause_str);
 
-        if trait_items.len() > 0 {
-            result.push_str(" {");
-        } else {
-            result.push_str(" {}");
+        match context.config.item_brace_style {
+            BraceStyle::AlwaysNextLine => {
+                result.push('\n');
+                result.push_str(&offset.to_string(context.config));
+            },
+            BraceStyle::PreferSameLine => result.push(' '),
+            BraceStyle::SameLineWhere => {
+                if !where_clause_str.is_empty() {
+                    result.push('\n');
+                    result.push_str(&offset.to_string(context.config));
+                } else {
+                    result.push(' ');
+                }
+            }
+        }
+        result.push('{');
+
+        let snippet = context.snippet(item.span);
+        let open_pos = try_opt!(snippet.find_uncommented("{")) + 1;
+
+        if !trait_items.is_empty() || contains_comment(&snippet[open_pos..]) {
+            let mut visitor = FmtVisitor::from_codemap(context.parse_session, context.config, None);
+            visitor.block_indent = context.block_indent.block_indent(context.config);
+            visitor.last_pos = item.span.lo + BytePos(open_pos as u32);
+
+            for item in trait_items {
+                visitor.visit_trait_item(&item);
+            }
+
+            visitor.format_missing(item.span.hi - BytePos(1));
+
+            let inner_indent_str = visitor.block_indent.to_string(context.config);
+            let outer_indent_str = context.block_indent.to_string(context.config);
+
+            result.push('\n');
+            result.push_str(&inner_indent_str);
+            result.push_str(&trim_newlines(&visitor.buffer.to_string().trim()));
+            result.push('\n');
+            result.push_str(&outer_indent_str);
         }
 
+        result.push('}');
         Some(result)
     } else {
         unreachable!();
@@ -1501,18 +1543,18 @@ fn rewrite_generics(context: &RewriteContext,
 }
 
 fn rewrite_trait_bounds(context: &RewriteContext,
-                      param_bounds: &ast::TyParamBounds,
+                      type_param_bounds: &ast::TyParamBounds,
                       indent: Indent,
                       width: usize)
                       -> Option<String> {
-    let bounds: &[_] = &param_bounds.as_slice();
+    let bounds: &[_] = &type_param_bounds.as_slice();
 
     if bounds.is_empty() {
         return Some(String::new());
     }
 
     let bound_str = bounds.iter()
-                            .filter_map(|ty_bound| ty_bound.rewrite(&context, 100, indent))
+                            .filter_map(|ty_bound| ty_bound.rewrite(&context, width, indent))
                             .collect::<Vec<String>>()
                             .join(" + ");
 
index 3a8bb0bae8f3ddc5d55dbf132d312283169ba7b5..22f9807b8c55fe694a3724f109f84ff07117792c 100644 (file)
@@ -217,11 +217,11 @@ fn visit_item(&mut self, item: &ast::Item) {
                     self.buffer.push_str(&trait_str);
                     self.last_pos = item.span.hi;
                 }
-                self.block_indent = self.block_indent.block_indent(self.config);
-                for item in trait_items {
-                    self.visit_trait_item(&item);
-                }
-                self.block_indent = self.block_indent.block_unindent(self.config);
+                // self.block_indent = self.block_indent.block_indent(self.config);
+                // for item in trait_items {
+                //     self.visit_trait_item(&item);
+                // }
+                // self.block_indent = self.block_indent.block_unindent(self.config);
             }
             ast::Item_::ItemExternCrate(_) => {
                 self.format_missing_with_indent(item.span.lo);
@@ -319,7 +319,7 @@ fn visit_item(&mut self, item: &ast::Item) {
         }
     }
 
-    fn visit_trait_item(&mut self, ti: &ast::TraitItem) {
+    pub fn visit_trait_item(&mut self, ti: &ast::TraitItem) {
         if self.visit_attrs(&ti.attrs) {
             return;
         }
@@ -340,8 +340,22 @@ fn visit_trait_item(&mut self, ti: &ast::TraitItem) {
                               ti.span,
                               ti.id);
             }
-            ast::TypeTraitItem(..) => {
-                // FIXME: Implement
+            ast::TypeTraitItem(ref type_param_bounds, _) => {
+                let indent = self.block_indent;
+                let mut result = String::new();
+                result.push_str(&format!("type {}", ti.ident));
+
+                let bounds: &[_] = &type_param_bounds.as_slice();
+                let bound_str = bounds.iter()
+                                        .filter_map(|ty_bound| ty_bound.rewrite(&self.get_context(), self.config.max_width, indent))
+                                        .collect::<Vec<String>>()
+                                        .join(" + ");
+                if bounds.len() > 0 {
+                    result.push_str(&format!(": {}", bound_str));
+                }
+
+                result.push(';');
+                self.push_rewrite(ti.span, Some(result));
             }
         }
     }