Merge branch 'mattyhall-single-import'

This commit is contained in:
Nick Cameron 2015-05-12 10:06:12 +12:00
commit 83285269ee

View File

@ -15,10 +15,29 @@
use syntax::parse::token;
use syntax::print::pprust;
// TODO change import lists with one item to a single import
// remove empty lists (if they're even possible)
// TODO remove empty lists (if they're even possible)
// TODO (some day) remove unused imports, expand globs, compress many single imports into a list import
fn rewrite_single_use_list(path_str: String, vpi: ast::PathListItem, vis: &str) -> String {
if let ast::PathListItem_::PathListIdent{ name, .. } = vpi.node {
let name_str = token::get_ident(name).to_string();
if path_str.len() == 0 {
format!("{}use {};", vis, name_str)
} else {
format!("{}use {}::{};", vis, path_str, name_str)
}
} else {
if path_str.len() != 0 {
format!("{}use {};", vis, path_str)
} else {
// This catches the import: use {self}, which is a compiler error, so we just
// leave it alone.
format!("{}use {{self}};", vis)
}
}
}
impl<'a> FmtVisitor<'a> {
// Basically just pretty prints a multi-item import.
pub fn rewrite_use_list(&mut self,
@ -35,10 +54,15 @@ pub fn rewrite_use_list(&mut self,
_ => ""
};
if path_list.len() == 1 {
return rewrite_single_use_list(path_str, path_list[0], vis);
}
// 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;