about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNick Cameron <ncameron@mozilla.com>2015-09-24 08:36:21 +1200
committerNick Cameron <ncameron@mozilla.com>2015-09-26 13:48:02 +1200
commit92fbb80097bf172339b11a6387edacbc87c8dc07 (patch)
treec2404141534065e47a50b32fc926cdfc1945a28f
parentc14cfca0e3de3dfa5fd91d39a85c5b452e7756e3 (diff)
Formatting of some types
Closes #341
-rw-r--r--src/types.rs36
-rw-r--r--tests/source/multiple.rs2
-rw-r--r--tests/target/multiple.rs3
3 files changed, 38 insertions, 3 deletions
diff --git a/src/types.rs b/src/types.rs
index 9ba50a59cd9..28465404988 100644
--- a/src/types.rs
+++ b/src/types.rs
@@ -15,7 +15,7 @@ use syntax::codemap::{self, Span, BytePos, CodeMap};
 use Indent;
 use lists::{itemize_list, write_list, ListFormatting};
 use rewrite::{Rewrite, RewriteContext};
-use utils::{extra_offset, span_after};
+use utils::{extra_offset, span_after, format_mutability};
 
 impl Rewrite for ast::Path {
     fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> {
@@ -437,9 +437,39 @@ impl Rewrite for ast::Ty {
                 p.rewrite(context, width, offset)
             }
             ast::TyObjectSum(ref ty, ref bounds) => {
+                let ty_str = try_opt!(ty.rewrite(context, width, offset));
+                let overhead = ty_str.len() + 3;
                 Some(format!("{} + {}",
-                             try_opt!(ty.rewrite(context, width, offset)),
-                             try_opt!(bounds.rewrite(context, width, offset))))
+                             ty_str,
+                             try_opt!(bounds.rewrite(context,
+                                                     try_opt!(width.checked_sub(overhead)),
+                                                     offset + overhead))))
+            }
+            ast::TyRptr(ref lifetime, ref mt) => {
+                let mut_str = format_mutability(mt.mutbl);
+                let mut_len = mut_str.len();
+                Some(match lifetime {
+                    &Some(ref lifetime) => {
+                        let lt_str = pprust::lifetime_to_string(lifetime);
+                        let lt_len = lt_str.len();
+                        format!("&{} {}{}",
+                                lt_str,
+                                mut_str,
+                                try_opt!(mt.ty.rewrite(context,
+                                                       width - (2 + mut_len + lt_len),
+                                                       offset + 2 + mut_len + lt_len)))
+                    }
+                    &None => {
+                        format!("&{}{}",
+                                mut_str,
+                                try_opt!(mt.ty.rewrite(context,
+                                                       width - (1 + mut_len),
+                                                       offset + 1 + mut_len)))
+                    }
+                })
+            }
+            ast::TyParen(ref ty) => {
+                ty.rewrite(context, width - 2, offset + 1).map(|ty_str| format!("({})", ty_str))
             }
             _ => Some(pprust::ty_to_string(self)),
         }
diff --git a/tests/source/multiple.rs b/tests/source/multiple.rs
index abc16035be2..a0a3f2599e8 100644
--- a/tests/source/multiple.rs
+++ b/tests/source/multiple.rs
@@ -92,6 +92,8 @@ pub struct Foo<'a, Y: Baz>
     f: SomeType, // Comment beside a field
 }
 
+fn foo(ann: &'a (PpAnn+'a)) {}
+
 fn main() {
     for i in 0i32..4 {
         println!("{}", i);
diff --git a/tests/target/multiple.rs b/tests/target/multiple.rs
index 9ff3f2cf7a8..c2e2a471afc 100644
--- a/tests/target/multiple.rs
+++ b/tests/target/multiple.rs
@@ -117,6 +117,9 @@ pub struct Foo<'a, Y: Baz>
     f: SomeType, // Comment beside a field
 }
 
+fn foo(ann: &'a (PpAnn + 'a)) {
+}
+
 fn main() {
     for i in 0i32..4 {
         println!("{}", i);