From 27d6558964dd9e5704d23a722d3ae8598bd3c13c Mon Sep 17 00:00:00 2001 From: Marcus Klaas Date: Tue, 2 Jun 2015 18:44:33 +0200 Subject: [PATCH] Remove empty list imports --- src/imports.rs | 15 ++++++++------- src/visitor.rs | 27 +++++++++++++++++++-------- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/imports.rs b/src/imports.rs index cab3fa452e4..586caa76b64 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -15,8 +15,6 @@ use syntax::ast; use syntax::parse::token; use syntax::print::pprust; - -// 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 { @@ -40,13 +38,14 @@ fn rewrite_single_use_list(path_str: String, vpi: ast::PathListItem, vis: &str) impl<'a> FmtVisitor<'a> { // Basically just pretty prints a multi-item import. + // Returns None when the import can be removed. pub fn rewrite_use_list(&mut self, block_indent: usize, one_line_budget: usize, // excluding indentation multi_line_budget: usize, path: &ast::Path, path_list: &[ast::PathListItem], - visibility: ast::Visibility) -> String { + visibility: ast::Visibility) -> Option { let path_str = pprust::path_to_string(path); let vis = match visibility { @@ -54,8 +53,10 @@ impl<'a> FmtVisitor<'a> { _ => "" }; - if path_list.len() == 1 { - return rewrite_single_use_list(path_str, path_list[0], vis); + match path_list.len() { + 0 => return None, + 1 => return Some(rewrite_single_use_list(path_str, path_list[0], vis)), + _ => () } // 2 = :: @@ -110,10 +111,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 c15b3c34af0..630638e7bda 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -152,19 +152,30 @@ 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) => { let block_indent = self.block_indent; let one_line_budget = config!(max_width) - block_indent; let multi_line_budget = config!(ideal_width) - block_indent; - let new_str = self.rewrite_use_list(block_indent, - one_line_budget, - multi_line_budget, - path, - path_list, - item.vis); - self.changes.push_str_span(item.span, &new_str); + let formatted = self.rewrite_use_list(block_indent, + one_line_budget, + multi_line_budget, + path, + path_list, + item.vis); + + if let Some(new_str) = formatted { + self.format_missing_with_indent(item.span.lo); + self.changes.push_str_span(item.span, &new_str); + } else { + // Format up to last newline + let span = codemap::mk_sp(self.last_pos, item.span.lo); + let span_end = match self.snippet(span).rfind('\n') { + Some(offset) => self.last_pos + BytePos(offset as u32), + None => item.span.lo + }; + self.format_missing(span_end); + } self.last_pos = item.span.hi; } ast::ViewPath_::ViewPathGlob(_) => {