Change import lists with one item to single import

Changes lists such as ``use std::{fmt}`` to a form without a list, eg
``use std::fmt``.
This commit is contained in:
Matthew Hall 2015-05-01 17:27:54 +01:00
parent 59554dd5e1
commit 59352172b3

View File

@ -18,10 +18,28 @@ 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 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,
@ -29,9 +47,6 @@ impl<'a> FmtVisitor<'a> {
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);
let path_str = pprust::path_to_string(&path);
let vis = match visibility {
@ -39,6 +54,13 @@ impl<'a> FmtVisitor<'a> {
_ => ""
};
if path_list.len() == 1 {
return rewrite_single_use_list(path_str, path_list[0], vis);
}
// FIXME check indentation
let l_loc = self.codemap.lookup_char_pos(vp_span.lo);
// 1 = {
let mut indent = l_loc.col.0 + path_str.len() + 1;
if path_str.len() > 0 {