]> git.lizzy.rs Git - rust.git/blobdiff - src/utils.rs
Remove BlockIndentStyle::Inherit
[rust.git] / src / utils.rs
index ab5de426571ae18a45aa661ccd5f3d5694bb8e0b..999e1192b5d27e39eff66ba64cb2544800aabbce 100644 (file)
 use syntax::codemap::BytePos;
 use syntax::abi;
 
-use Indent;
+use Shape;
 use rewrite::{Rewrite, RewriteContext};
 
 use SKIP_ANNOTATION;
 
 // Computes the length of a string's last line, minus offset.
-pub fn extra_offset(text: &str, offset: Indent) -> usize {
+pub fn extra_offset(text: &str, shape: Shape) -> usize {
     match text.rfind('\n') {
         // 1 for newline character
-        Some(idx) => text.len().checked_sub(idx + 1 + offset.width()).unwrap_or(0),
+        Some(idx) => text.len().checked_sub(idx + 1 + shape.used_width()).unwrap_or(0),
         None => text.len(),
     }
 }
@@ -39,11 +39,13 @@ pub fn format_visibility(vis: &Visibility) -> Cow<'static, str> {
         Visibility::Inherited => Cow::from(""),
         Visibility::Crate(_) => Cow::from("pub(crate) "),
         Visibility::Restricted { ref path, .. } => {
-            let Path { global, ref segments, .. } = **path;
-            let prefix = if global { "::" } else { "" };
+            let Path { ref segments, .. } = **path;
             let mut segments_iter = segments.iter().map(|seg| seg.identifier.name.as_str());
+            if path.is_global() {
+                segments_iter.next().expect("Non-global path in pub(restricted)?");
+            }
 
-            Cow::from(format!("pub({}{}) ", prefix, segments_iter.join("::")))
+            Cow::from(format!("pub({}) ", segments_iter.join("::")))
         }
     }
 }
@@ -251,18 +253,18 @@ macro_rules! source {
 
 // Wraps string-like values in an Option. Returns Some when the string adheres
 // to the Rewrite constraints defined for the Rewrite trait and else otherwise.
-pub fn wrap_str<S: AsRef<str>>(s: S, max_width: usize, width: usize, offset: Indent) -> Option<S> {
+pub fn wrap_str<S: AsRef<str>>(s: S, max_width: usize, shape: Shape) -> Option<S> {
     {
         let snippet = s.as_ref();
 
-        if !snippet.contains('\n') && snippet.len() > width {
+        if !snippet.contains('\n') && snippet.len() > shape.width {
             return None;
         } else {
             let mut lines = snippet.lines();
 
-            // The caller of this function has already placed `offset`
+            // The caller of this function has already placed `shape.offset`
             // characters on the first line.
-            let first_line_max_len = try_opt!(max_width.checked_sub(offset.width()));
+            let first_line_max_len = try_opt!(max_width.checked_sub(shape.indent.width()));
             if lines.next().unwrap().len() > first_line_max_len {
                 return None;
             }
@@ -276,7 +278,11 @@ pub fn wrap_str<S: AsRef<str>>(s: S, max_width: usize, width: usize, offset: Ind
             // indentation.
             // A special check for the last line, since the caller may
             // place trailing characters on this line.
-            if snippet.lines().rev().next().unwrap().len() > offset.width() + width {
+            if snippet.lines()
+                   .rev()
+                   .next()
+                   .unwrap()
+                   .len() > shape.indent.width() + shape.width {
                 return None;
             }
         }
@@ -286,8 +292,8 @@ pub fn wrap_str<S: AsRef<str>>(s: S, max_width: usize, width: usize, offset: Ind
 }
 
 impl Rewrite for String {
-    fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> {
-        wrap_str(self, context.config.max_width, width, offset).map(ToOwned::to_owned)
+    fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
+        wrap_str(self, context.config.max_width, shape).map(ToOwned::to_owned)
     }
 }