Skip to content

Commit

Permalink
Handle pub use (fixes #23)
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth committed Apr 30, 2015
1 parent 35b0081 commit 9398aed
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
12 changes: 8 additions & 4 deletions src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 27,17 @@ impl<'a> FmtVisitor<'a> {
pub fn rewrite_use_list(&mut self,
path: &ast::Path,
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 {
ast::Public => "pub ",
_ => ""
};

// 1 = {
let mut indent = l_loc.col.0 path_str.len() 1;
Expand All @@ -41,7 46,7 @@ impl<'a> FmtVisitor<'a> {
indent = 2;
}
// 2 = } ;
let used_width = indent 2;
let used_width = indent 2 vis.len();
let budget = if used_width >= IDEAL_WIDTH {
if used_width < MAX_WIDTH {
MAX_WIDTH - used_width
Expand Down Expand Up @@ -84,11 89,10 @@ impl<'a> FmtVisitor<'a> {
ast::PathListItem_::PathListMod{ .. } => None,
}
})).collect();

if path_str.len() == 0 {
format!("use {{{}}};", write_list(&items, &fmt))
format!("{}use {{{}}};", vis, write_list(&items, &fmt))
} else {
format!("use {}::{{{}}};", path_str, write_list(&items, &fmt))
format!("{}use {}::{{{}}};", vis, path_str, write_list(&items, &fmt))
}
}
}
5 changes: 4 additions & 1 deletion src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 148,10 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
match vp.node {
ast::ViewPath_::ViewPathList(ref path, ref path_list) => {
self.format_missing(item.span.lo);
let new_str = self.rewrite_use_list(path, path_list, vp.span);
let new_str = self.rewrite_use_list(path,
path_list,
item.vis,
vp.span);
self.changes.push_str_span(item.span, &new_str);
self.last_pos = item.span.hi;
}
Expand Down
2 changes: 2 additions & 0 deletions tests/idem/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 6,5 @@ use syntax::ast::{ItemForeignMod, ItemImpl, ItemMac, ItemMod, ItemStatic,

use {Foo, Bar};
use Foo::{Bar, Baz};
pub use syntax::ast::{Expr_, Expr, ExprAssign, ExprCall, ExprMethodCall,
ExprPath};

0 comments on commit 9398aed

Please sign in to comment.