Skip to content

Commit 27d6558

Browse files
committed
Remove empty list imports
1 parent b601ea2 commit 27d6558

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

src/imports.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ use syntax::ast;
1515
use syntax::parse::token;
1616
use syntax::print::pprust;
1717

18-
19-
// TODO remove empty lists (if they're even possible)
2018
// TODO (some day) remove unused imports, expand globs, compress many single imports into a list import
2119

2220
fn rewrite_single_use_list(path_str: String, vpi: ast::PathListItem, vis: &str) -> String {
@@ -40,22 +38,25 @@ fn rewrite_single_use_list(path_str: String, vpi: ast::PathListItem, vis: &str)
4038

4139
impl<'a> FmtVisitor<'a> {
4240
// Basically just pretty prints a multi-item import.
41+
// Returns None when the import can be removed.
4342
pub fn rewrite_use_list(&mut self,
4443
block_indent: usize,
4544
one_line_budget: usize, // excluding indentation
4645
multi_line_budget: usize,
4746
path: &ast::Path,
4847
path_list: &[ast::PathListItem],
49-
visibility: ast::Visibility) -> String {
48+
visibility: ast::Visibility) -> Option<String> {
5049
let path_str = pprust::path_to_string(path);
5150

5251
let vis = match visibility {
5352
ast::Public => "pub ",
5453
_ => ""
5554
};
5655

57-
if path_list.len() == 1 {
58-
return rewrite_single_use_list(path_str, path_list[0], vis);
56+
match path_list.len() {
57+
0 => return None,
58+
1 => return Some(rewrite_single_use_list(path_str, path_list[0], vis)),
59+
_ => ()
5960
}
6061

6162
// 2 = ::
@@ -110,10 +111,10 @@ impl<'a> FmtVisitor<'a> {
110111
ast::PathListItem_::PathListMod{ .. } => None,
111112
}
112113
})).collect();
113-
if path_str.len() == 0 {
114+
Some(if path_str.len() == 0 {
114115
format!("{}use {{{}}};", vis, write_list(&items, &fmt))
115116
} else {
116117
format!("{}use {}::{{{}}};", vis, path_str, write_list(&items, &fmt))
117-
}
118+
})
118119
}
119120
}

src/visitor.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,19 +152,30 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
152152

153153
match item.node {
154154
ast::Item_::ItemUse(ref vp) => {
155-
self.format_missing_with_indent(item.span.lo);
156155
match vp.node {
157156
ast::ViewPath_::ViewPathList(ref path, ref path_list) => {
158157
let block_indent = self.block_indent;
159158
let one_line_budget = config!(max_width) - block_indent;
160159
let multi_line_budget = config!(ideal_width) - block_indent;
161-
let new_str = self.rewrite_use_list(block_indent,
162-
one_line_budget,
163-
multi_line_budget,
164-
path,
165-
path_list,
166-
item.vis);
167-
self.changes.push_str_span(item.span, &new_str);
160+
let formatted = self.rewrite_use_list(block_indent,
161+
one_line_budget,
162+
multi_line_budget,
163+
path,
164+
path_list,
165+
item.vis);
166+
167+
if let Some(new_str) = formatted {
168+
self.format_missing_with_indent(item.span.lo);
169+
self.changes.push_str_span(item.span, &new_str);
170+
} else {
171+
// Format up to last newline
172+
let span = codemap::mk_sp(self.last_pos, item.span.lo);
173+
let span_end = match self.snippet(span).rfind('\n') {
174+
Some(offset) => self.last_pos + BytePos(offset as u32),
175+
None => item.span.lo
176+
};
177+
self.format_missing(span_end);
178+
}
168179
self.last_pos = item.span.hi;
169180
}
170181
ast::ViewPath_::ViewPathGlob(_) => {

0 commit comments

Comments
 (0)