2015-04-21 21:01:19 +12:00
|
|
|
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
use visitor::FmtVisitor;
|
|
|
|
use lists::{write_list, ListFormatting, SeparatorTactic, ListTactic};
|
|
|
|
|
|
|
|
use syntax::ast;
|
|
|
|
use syntax::parse::token;
|
|
|
|
use syntax::print::pprust;
|
|
|
|
|
|
|
|
|
2015-05-01 17:27:54 +01:00
|
|
|
// TODO remove empty lists (if they're even possible)
|
2015-04-21 22:50:43 +12:00
|
|
|
// TODO (some day) remove unused imports, expand globs, compress many single imports into a list import
|
|
|
|
|
2015-05-01 17:27:54 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-21 21:01:19 +12:00
|
|
|
impl<'a> FmtVisitor<'a> {
|
|
|
|
// Basically just pretty prints a multi-item import.
|
|
|
|
pub fn rewrite_use_list(&mut self,
|
2015-05-02 19:12:16 +02:00
|
|
|
block_indent: usize,
|
2015-05-04 17:16:51 +02:00
|
|
|
one_line_budget: usize, // excluding indentation
|
|
|
|
multi_line_budget: usize,
|
2015-04-28 21:24:56 +12:00
|
|
|
path: &ast::Path,
|
|
|
|
path_list: &[ast::PathListItem],
|
2015-05-04 17:16:51 +02:00
|
|
|
visibility: ast::Visibility) -> String {
|
2015-05-25 19:11:53 +12:00
|
|
|
let path_str = pprust::path_to_string(path);
|
2015-04-21 21:01:19 +12:00
|
|
|
|
2015-04-30 13:33:26 +05:30
|
|
|
let vis = match visibility {
|
|
|
|
ast::Public => "pub ",
|
|
|
|
_ => ""
|
|
|
|
};
|
2015-04-28 21:36:31 +12:00
|
|
|
|
2015-05-01 17:27:54 +01:00
|
|
|
if path_list.len() == 1 {
|
|
|
|
return rewrite_single_use_list(path_str, path_list[0], vis);
|
|
|
|
}
|
|
|
|
|
2015-05-02 19:12:16 +02:00
|
|
|
// 2 = ::
|
|
|
|
let path_separation_w = if path_str.len() > 0 { 2 } else { 0 };
|
|
|
|
// 5 = "use " + {
|
|
|
|
let indent = path_str.len() + 5 + path_separation_w + vis.len();
|
2015-05-01 17:27:54 +01:00
|
|
|
|
2015-04-28 21:24:56 +12:00
|
|
|
// 2 = } + ;
|
2015-05-02 19:12:16 +02:00
|
|
|
let used_width = indent + 2;
|
|
|
|
|
2015-05-04 17:16:51 +02:00
|
|
|
// Break as early as possible when we've blown our budget.
|
|
|
|
let remaining_line_budget = if used_width > one_line_budget {
|
|
|
|
0
|
2015-04-28 21:24:56 +12:00
|
|
|
} else {
|
2015-05-04 17:16:51 +02:00
|
|
|
one_line_budget - used_width
|
|
|
|
};
|
|
|
|
let remaining_multi_budget = if used_width > multi_line_budget {
|
|
|
|
0
|
|
|
|
} else {
|
|
|
|
multi_line_budget - used_width
|
2015-04-28 21:24:56 +12:00
|
|
|
};
|
2015-05-02 19:12:16 +02:00
|
|
|
|
2015-04-21 21:01:19 +12:00
|
|
|
let fmt = ListFormatting {
|
|
|
|
tactic: ListTactic::Mixed,
|
|
|
|
separator: ",",
|
|
|
|
trailing_separator: SeparatorTactic::Never,
|
2015-05-02 19:12:16 +02:00
|
|
|
indent: block_indent + indent,
|
2015-05-04 17:16:51 +02:00
|
|
|
h_width: remaining_line_budget,
|
|
|
|
v_width: remaining_multi_budget,
|
2015-04-21 21:01:19 +12:00
|
|
|
};
|
|
|
|
|
|
|
|
// TODO handle any comments inbetween items.
|
|
|
|
// If `self` is in the list, put it first.
|
|
|
|
let head = if path_list.iter().any(|vpi|
|
|
|
|
if let ast::PathListItem_::PathListMod{ .. } = vpi.node {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
) {
|
2015-04-30 10:31:42 +02:00
|
|
|
Some(("self".to_owned(), String::new()))
|
2015-04-21 21:01:19 +12:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
let items: Vec<_> = head.into_iter().chain(path_list.iter().filter_map(|vpi| {
|
|
|
|
match vpi.node {
|
|
|
|
ast::PathListItem_::PathListIdent{ name, .. } => {
|
|
|
|
Some((token::get_ident(name).to_string(), String::new()))
|
|
|
|
}
|
|
|
|
// Skip `self`, because we added it above.
|
|
|
|
ast::PathListItem_::PathListMod{ .. } => None,
|
|
|
|
}
|
|
|
|
})).collect();
|
2015-05-04 17:16:51 +02:00
|
|
|
if path_str.len() == 0 {
|
2015-04-30 13:33:26 +05:30
|
|
|
format!("{}use {{{}}};", vis, write_list(&items, &fmt))
|
2015-04-28 21:36:31 +12:00
|
|
|
} else {
|
2015-04-30 13:33:26 +05:30
|
|
|
format!("{}use {}::{{{}}};", vis, path_str, write_list(&items, &fmt))
|
2015-05-04 17:16:51 +02:00
|
|
|
}
|
2015-04-21 21:01:19 +12:00
|
|
|
}
|
|
|
|
}
|