about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorMarcus Klaas <mail@marcusklaas.nl>2015-05-02 19:12:16 +0200
committerMarcus Klaas <mail@marcusklaas.nl>2015-05-04 09:25:20 +0200
commit58a14fbc793f6a3522ebe4db736a664f3271772a (patch)
tree117001e945680500f28112c467bb9dde0bf20691 /src
parentf83e3400cbbb5e30a11276799af15cc14158eda0 (diff)
Correctly indent use items and functions
Diffstat (limited to 'src')
-rw-r--r--src/imports.rs46
-rw-r--r--src/visitor.rs24
2 files changed, 32 insertions, 38 deletions
diff --git a/src/imports.rs b/src/imports.rs
index d29e9eb6e84..9cdd89df0cb 100644
--- a/src/imports.rs
+++ b/src/imports.rs
@@ -12,12 +12,9 @@ use visitor::FmtVisitor;
 use lists::{write_list, ListFormatting, SeparatorTactic, ListTactic};
 
 use syntax::ast;
-use syntax::codemap::Span;
 use syntax::parse::token;
 use syntax::print::pprust;
 
-use {IDEAL_WIDTH, MAX_WIDTH};
-
 // TODO change import lists with one item to a single import
 //      remove empty lists (if they're even possible)
 // TODO (some day) remove unused imports, expand globs, compress many single imports into a list import
@@ -25,13 +22,11 @@ use {IDEAL_WIDTH, MAX_WIDTH};
 impl<'a> FmtVisitor<'a> {
     // Basically just pretty prints a multi-item import.
     pub fn rewrite_use_list(&mut self,
+                            block_indent: usize,
+                            budget: usize, // excluding indentation
                             path: &ast::Path,
                             path_list: &[ast::PathListItem],
-                            visibility: ast::Visibility,
-                            vp_span: Span) -> String {
-        // FIXME check indentation
-        let l_loc = self.codemap.lookup_char_pos(vp_span.lo);
-
+                            visibility: ast::Visibility) -> Option<String> {
         let path_str = pprust::path_to_string(&path);
 
         let vis = match visibility {
@@ -39,31 +34,26 @@ impl<'a> FmtVisitor<'a> {
             _ => ""
         };
 
-        // 1 = {
-        let mut indent = l_loc.col.0 + path_str.len() + 1;
-        if path_str.len() > 0 {
-            // 2 = ::
-            indent += 2;
-        }
+        // 2 = ::
+        let path_separation_w = if path_str.len() > 0 { 2 } else { 0 };
+        // 5 = "use " + {
+        let indent = path_str.len() + 5 + path_separation_w + vis.len();
         // 2 = } + ;
-        let used_width = indent + 2 + vis.len();
-        let budget = if used_width >= IDEAL_WIDTH {
-            if used_width < MAX_WIDTH {
-                MAX_WIDTH - used_width
-            } else {
-                // Give up
-                return String::new();
-            }
+        let used_width = indent + 2;
+
+        let remaining_budget = if used_width >= budget {
+            return None;
         } else {
-            IDEAL_WIDTH - used_width
+            budget - used_width
         };
+
         let fmt = ListFormatting {
             tactic: ListTactic::Mixed,
             separator: ",",
             trailing_separator: SeparatorTactic::Never,
-            indent: indent,
-            h_width: budget,
-            v_width: budget,
+            indent: block_indent + indent,
+            h_width: remaining_budget,
+            v_width: remaining_budget,
         };
 
         // TODO handle any comments inbetween items.
@@ -89,10 +79,10 @@ impl<'a> FmtVisitor<'a> {
                 ast::PathListItem_::PathListMod{ .. } => None,
             }
         })).collect();
-        if path_str.len() == 0 {
+        Some(if path_str.len() == 0 {
             format!("{}use {{{}}};", vis, write_list(&items, &fmt))
         } else {
             format!("{}use {}::{{{}}};", vis, path_str, write_list(&items, &fmt))
-        }
+        })
     }
 }
diff --git a/src/visitor.rs b/src/visitor.rs
index 138be5f3636..b4fe95c9dd3 100644
--- a/src/visitor.rs
+++ b/src/visitor.rs
@@ -14,7 +14,7 @@ use syntax::visit;
 
 use utils;
 
-use {MAX_WIDTH, TAB_SPACES, SKIP_ANNOTATION};
+use {IDEAL_WIDTH, MAX_WIDTH, TAB_SPACES, SKIP_ANNOTATION};
 use changes::ChangeSet;
 
 pub struct FmtVisitor<'a> {
@@ -93,7 +93,7 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
                 b: &'v ast::Block,
                 s: Span,
                 _: ast::NodeId) {
-        self.format_missing(s.lo);
+        self.format_missing_with_indent(s.lo);
         self.last_pos = s.lo;
 
         let indent = self.block_indent;
@@ -145,20 +145,24 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
 
         match item.node {
             ast::Item_::ItemUse(ref vp) => {
+                self.format_missing_with_indent(item.span.lo);
                 match vp.node {
                     ast::ViewPath_::ViewPathList(ref path, ref path_list) => {
-                        self.format_missing(item.span.lo);
-                        let new_str = self.rewrite_use_list(path,
-                                                            path_list,
-                                                            item.vis,
-                                                            vp.span);
-                        self.changes.push_str_span(item.span, &new_str);
-                        self.last_pos = item.span.hi;
+                        let block_indent = self.block_indent;
+                        let budget = IDEAL_WIDTH - block_indent;
+                        if let Some(new_str) = self.rewrite_use_list(block_indent,
+                                                                     budget,
+                                                                     path,
+                                                                     path_list,
+                                                                     item.vis) {
+                            self.changes.push_str_span(item.span, &new_str);
+                            self.last_pos = item.span.hi;
+                        }
                     }
                     ast::ViewPath_::ViewPathGlob(_) => {
                         // FIXME convert to list?
                     }
-                    _ => {}
+                    ast::ViewPath_::ViewPathSimple(_,_) => {}
                 }
                 visit::walk_item(self, item);
             }