Skip to content

Commit 1488d5e

Browse files
committed
Merge pull request #90 from marcusklaas/empty-imports
Remove empty list imports
2 parents 58df466 + 27d6558 commit 1488d5e

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
@@ -16,8 +16,6 @@ use syntax::ast;
1616
use syntax::parse::token;
1717
use syntax::print::pprust;
1818

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

2321
fn rewrite_single_use_list(path_str: String, vpi: ast::PathListItem, vis: &str) -> String {
@@ -41,18 +39,21 @@ fn rewrite_single_use_list(path_str: String, vpi: ast::PathListItem, vis: &str)
4139

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

54-
if path_list.len() == 1 {
55-
return rewrite_single_use_list(path_str, path_list[0], vis);
53+
match path_list.len() {
54+
0 => return None,
55+
1 => return Some(rewrite_single_use_list(path_str, path_list[0], vis)),
56+
_ => ()
5657
}
5758

5859
// 2 = ::
@@ -107,10 +108,10 @@ impl<'a> FmtVisitor<'a> {
107108
ast::PathListItem_::PathListMod{ .. } => None,
108109
}
109110
})).collect();
110-
if path_str.len() == 0 {
111+
Some(if path_str.len() == 0 {
111112
format!("{}use {{{}}};", vis, write_list(&items, &fmt))
112113
} else {
113114
format!("{}use {}::{{{}}};", vis, path_str, write_list(&items, &fmt))
114-
}
115+
})
115116
}
116117
}

src/visitor.rs

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

160160
match item.node {
161161
ast::Item_::ItemUse(ref vp) => {
162-
self.format_missing_with_indent(item.span.lo);
163162
match vp.node {
164163
ast::ViewPath_::ViewPathList(ref path, ref path_list) => {
165164
let block_indent = self.block_indent;
166165
let one_line_budget = config!(max_width) - block_indent;
167166
let multi_line_budget = config!(ideal_width) - block_indent;
168-
let new_str = self.rewrite_use_list(block_indent,
169-
one_line_budget,
170-
multi_line_budget,
171-
path,
172-
path_list,
173-
item.vis);
174-
self.changes.push_str_span(item.span, &new_str);
167+
let formatted = self.rewrite_use_list(block_indent,
168+
one_line_budget,
169+
multi_line_budget,
170+
path,
171+
path_list,
172+
item.vis);
173+
174+
if let Some(new_str) = formatted {
175+
self.format_missing_with_indent(item.span.lo);
176+
self.changes.push_str_span(item.span, &new_str);
177+
} else {
178+
// Format up to last newline
179+
let span = codemap::mk_sp(self.last_pos, item.span.lo);
180+
let span_end = match self.snippet(span).rfind('\n') {
181+
Some(offset) => self.last_pos + BytePos(offset as u32),
182+
None => item.span.lo
183+
};
184+
self.format_missing(span_end);
185+
}
175186
self.last_pos = item.span.hi;
176187
}
177188
ast::ViewPath_::ViewPathGlob(_) => {

0 commit comments

Comments
 (0)