]> git.lizzy.rs Git - rust.git/blobdiff - src/types.rs
Make children list in-order
[rust.git] / src / types.rs
index d6e4001eacfcb5aaef4c010874f07b53890702ec..5d57b99d32d8d10855acf635668f8c3d80832582 100644 (file)
@@ -27,8 +27,8 @@
 use shape::Shape;
 use spanned::Spanned;
 use utils::{
-    colon_spaces, extra_offset, first_line_width, format_abi, format_mutability, last_line_width,
-    mk_sp, rewrite_ident,
+    colon_spaces, extra_offset, first_line_width, format_abi, format_mutability,
+    last_line_extendable, last_line_width, mk_sp, rewrite_ident,
 };
 
 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
@@ -521,7 +521,24 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
 
 impl Rewrite for ast::GenericBounds {
     fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
-        join_bounds(context, shape, self, true)
+        if self.is_empty() {
+            return Some(String::new());
+        }
+
+        let span = mk_sp(self.get(0)?.span().lo(), self.last()?.span().hi());
+        let has_paren = context.snippet(span).starts_with("(");
+        let bounds_shape = if has_paren {
+            shape.offset_left(1)?.sub_width(1)?
+        } else {
+            shape
+        };
+        join_bounds(context, bounds_shape, self, true).map(|s| {
+            if has_paren {
+                format!("({})", s)
+            } else {
+                s
+            }
+        })
     }
 }
 
@@ -733,15 +750,28 @@ fn rewrite_bare_fn(
     Some(result)
 }
 
-fn join_bounds<T>(
+fn is_generic_bounds_in_order(generic_bounds: &[ast::GenericBound]) -> bool {
+    let is_trait = |b: &ast::GenericBound| match b {
+        ast::GenericBound::Outlives(..) => false,
+        ast::GenericBound::Trait(..) => true,
+    };
+    let is_lifetime = |b: &ast::GenericBound| !is_trait(b);
+    let last_trait_index = generic_bounds.iter().rposition(is_trait);
+    let first_lifetime_index = generic_bounds.iter().position(is_lifetime);
+    match (last_trait_index, first_lifetime_index) {
+        (Some(last_trait_index), Some(first_lifetime_index)) => {
+            last_trait_index < first_lifetime_index
+        }
+        _ => true,
+    }
+}
+
+fn join_bounds(
     context: &RewriteContext,
     shape: Shape,
-    items: &[T],
+    items: &[ast::GenericBound],
     need_indent: bool,
-) -> Option<String>
-where
-    T: Rewrite,
-{
+) -> Option<String> {
     // Try to join types in a single line
     let joiner = match context.config.type_punctuation_density() {
         TypeDensity::Compressed => "+",
@@ -752,7 +782,7 @@ fn join_bounds<T>(
         .map(|item| item.rewrite(context, shape))
         .collect::<Option<Vec<_>>>()?;
     let result = type_strs.join(joiner);
-    if items.len() == 1 || (!result.contains('\n') && result.len() <= shape.width) {
+    if items.len() <= 1 || (!result.contains('\n') && result.len() <= shape.width) {
         return Some(result);
     }
 
@@ -769,8 +799,26 @@ fn join_bounds<T>(
         (type_strs, shape.indent)
     };
 
-    let joiner = format!("{}+ ", offset.to_string_with_newline(context.config));
-    Some(type_strs.join(&joiner))
+    let is_bound_extendable = |s: &str, b: &ast::GenericBound| match b {
+        ast::GenericBound::Outlives(..) => true,
+        ast::GenericBound::Trait(..) => last_line_extendable(s),
+    };
+    let mut result = String::with_capacity(128);
+    result.push_str(&type_strs[0]);
+    let mut can_be_put_on_the_same_line = is_bound_extendable(&result, &items[0]);
+    let generic_bounds_in_order = is_generic_bounds_in_order(items);
+    for (bound, bound_str) in items[1..].iter().zip(type_strs[1..].iter()) {
+        if generic_bounds_in_order && can_be_put_on_the_same_line {
+            result.push_str(joiner);
+        } else {
+            result.push_str(&offset.to_string_with_newline(context.config));
+            result.push_str("+ ");
+        }
+        result.push_str(bound_str);
+        can_be_put_on_the_same_line = is_bound_extendable(bound_str, bound);
+    }
+
+    Some(result)
 }
 
 pub fn can_be_overflowed_type(context: &RewriteContext, ty: &ast::Ty, len: usize) -> bool {