From 58a14fbc793f6a3522ebe4db736a664f3271772a Mon Sep 17 00:00:00 2001 From: Marcus Klaas Date: Sat, 2 May 2015 19:12:16 +0200 Subject: [PATCH] Correctly indent use items and functions --- src/imports.rs | 46 +++++++++++++++++-------------------------- src/visitor.rs | 24 ++++++++++++---------- tests/idem/imports.rs | 13 ++++++++++++ 3 files changed, 45 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 { 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); } diff --git a/tests/idem/imports.rs b/tests/idem/imports.rs index 13910c8a628..72c4a1fb769 100644 --- a/tests/idem/imports.rs +++ b/tests/idem/imports.rs @@ -8,3 +8,16 @@ use {Foo, Bar}; use Foo::{Bar, Baz}; pub use syntax::ast::{Expr_, Expr, ExprAssign, ExprCall, ExprMethodCall, ExprPath}; + +mod Foo { + pub use syntax::ast::{Expr_, ExprEval, ToExpr, ExprMethodCall, ToExprPath}; + + mod Foo2 { + pub use syntax::ast::{Expr_, ExprEval, ToExpr, ExprMethodCall, + ToExprPath}; + } +} + +fn test() { + use Baz::*; +}