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;
|
2015-06-26 03:29:54 +02:00
|
|
|
use lists::{write_list, itemize_list, ListItem, ListFormatting, SeparatorTactic, ListTactic};
|
|
|
|
use utils::{span_after, format_visibility};
|
2015-04-21 21:01:19 +12:00
|
|
|
|
|
|
|
use syntax::ast;
|
|
|
|
use syntax::parse::token;
|
|
|
|
use syntax::print::pprust;
|
2015-06-26 03:29:54 +02:00
|
|
|
use syntax::codemap::Span;
|
2015-04-21 21:01:19 +12:00
|
|
|
|
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.
|
2015-06-02 18:44:33 +02:00
|
|
|
// Returns None when the import can be removed.
|
2015-06-26 03:29:54 +02:00
|
|
|
pub fn rewrite_use_list(&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-06-26 03:29:54 +02:00
|
|
|
visibility: ast::Visibility,
|
2015-07-03 11:13:28 +02:00
|
|
|
span: Span)
|
|
|
|
-> Option<String> {
|
2015-05-25 19:11:53 +12:00
|
|
|
let path_str = pprust::path_to_string(path);
|
2015-05-29 12:41:26 +02:00
|
|
|
let vis = format_visibility(visibility);
|
2015-04-28 21:36:31 +12:00
|
|
|
|
2015-06-02 18:44:33 +02:00
|
|
|
match path_list.len() {
|
|
|
|
0 => return None,
|
|
|
|
1 => return Some(rewrite_single_use_list(path_str, path_list[0], vis)),
|
|
|
|
_ => ()
|
2015-05-01 17:27:54 +01:00
|
|
|
}
|
|
|
|
|
2015-05-02 19:12:16 +02:00
|
|
|
// 2 = ::
|
2015-07-19 23:42:54 +02:00
|
|
|
let path_separation_w = if path_str.len() > 0 {
|
|
|
|
2
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
};
|
2015-05-02 19:12:16 +02:00
|
|
|
// 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.
|
2015-06-23 15:58:58 +02:00
|
|
|
let remaining_line_budget = one_line_budget.checked_sub(used_width).unwrap_or(0);
|
|
|
|
let remaining_multi_budget = multi_line_budget.checked_sub(used_width).unwrap_or(0);
|
2015-05-02 19:12:16 +02:00
|
|
|
|
2015-07-16 13:31:20 +12:00
|
|
|
let fmt = ListFormatting {
|
|
|
|
tactic: ListTactic::Mixed,
|
|
|
|
separator: ",",
|
|
|
|
trailing_separator: SeparatorTactic::Never,
|
|
|
|
indent: block_indent + indent,
|
|
|
|
h_width: remaining_line_budget,
|
|
|
|
v_width: remaining_multi_budget,
|
|
|
|
ends_with_newline: true,
|
|
|
|
};
|
2015-04-21 21:01:19 +12:00
|
|
|
|
2015-06-26 03:29:54 +02:00
|
|
|
let mut items = itemize_list(self.codemap,
|
2015-07-03 11:13:28 +02:00
|
|
|
vec![ListItem::from_str("")], /* Dummy value, explanation
|
|
|
|
* below */
|
2015-06-26 03:29:54 +02:00
|
|
|
path_list.iter(),
|
|
|
|
",",
|
|
|
|
"}",
|
|
|
|
|vpi| vpi.span.lo,
|
|
|
|
|vpi| vpi.span.hi,
|
|
|
|
|vpi| match vpi.node {
|
|
|
|
ast::PathListItem_::PathListIdent{ name, .. } => {
|
|
|
|
token::get_ident(name).to_string()
|
|
|
|
}
|
|
|
|
ast::PathListItem_::PathListMod{ .. } => {
|
|
|
|
"self".to_owned()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
span_after(span, "{", self.codemap),
|
|
|
|
span.hi);
|
|
|
|
|
|
|
|
// We prefixed the item list with a dummy value so that we can
|
|
|
|
// potentially move "self" to the front of the vector without touching
|
|
|
|
// the rest of the items.
|
|
|
|
// FIXME: Make more efficient by using a linked list? That would
|
|
|
|
// require changes to the signatures of itemize_list and write_list.
|
|
|
|
let has_self = move_self_to_front(&mut items);
|
2015-07-19 23:42:54 +02:00
|
|
|
let first_index = if has_self {
|
|
|
|
0
|
|
|
|
} else {
|
|
|
|
1
|
|
|
|
};
|
2015-06-26 03:29:54 +02:00
|
|
|
|
|
|
|
if self.config.reorder_imports {
|
2015-07-17 18:26:10 +12:00
|
|
|
items[1..].sort_by(|a, b| a.item.cmp(&b.item));
|
2015-06-26 03:29:54 +02:00
|
|
|
}
|
2015-04-21 21:01:19 +12:00
|
|
|
|
2015-06-26 03:29:54 +02:00
|
|
|
let list = write_list(&items[first_index..], &fmt);
|
2015-06-23 15:58:58 +02:00
|
|
|
|
2015-06-02 18:44:33 +02:00
|
|
|
Some(if path_str.len() == 0 {
|
2015-07-20 23:29:25 +02:00
|
|
|
format!("{}use {{{}}};", vis, list)
|
|
|
|
} else {
|
|
|
|
format!("{}use {}::{{{}}};", vis, path_str, list)
|
|
|
|
})
|
2015-04-21 21:01:19 +12:00
|
|
|
}
|
|
|
|
}
|
2015-06-26 03:29:54 +02:00
|
|
|
|
|
|
|
// Returns true when self item was found.
|
|
|
|
fn move_self_to_front(items: &mut Vec<ListItem>) -> bool {
|
|
|
|
match items.iter().position(|item| item.item == "self") {
|
|
|
|
Some(pos) => {
|
|
|
|
items[0] = items.remove(pos);
|
|
|
|
true
|
|
|
|
},
|
|
|
|
None => false
|
|
|
|
}
|
|
|
|
}
|