Correctly indent use items and functions

This commit is contained in:
Marcus Klaas 2015-05-02 19:12:16 +02:00
parent f83e3400cb
commit 58a14fbc79
3 changed files with 45 additions and 38 deletions

View File

@ -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))
}
})
}
}

View File

@ -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);
}

View File

@ -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::*;
}