1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use visitor::FmtVisitor;
12 use lists::{write_list, itemize_list, ListItem, ListFormatting, SeparatorTactic, ListTactic};
13 use utils::{span_after, format_visibility};
16 use syntax::parse::token;
17 use syntax::print::pprust;
18 use syntax::codemap::Span;
20 // TODO (some day) remove unused imports, expand globs, compress many single imports into a list import
22 fn rewrite_single_use_list(path_str: String, vpi: ast::PathListItem, vis: &str) -> String {
23 if let ast::PathListItem_::PathListIdent{ name, .. } = vpi.node {
24 let name_str = token::get_ident(name).to_string();
25 if path_str.len() == 0 {
26 format!("{}use {};", vis, name_str)
28 format!("{}use {}::{};", vis, path_str, name_str)
31 if path_str.len() != 0 {
32 format!("{}use {};", vis, path_str)
34 // This catches the import: use {self}, which is a compiler error, so we just
36 format!("{}use {{self}};", vis)
41 impl<'a> FmtVisitor<'a> {
42 // Basically just pretty prints a multi-item import.
43 // Returns None when the import can be removed.
44 pub fn rewrite_use_list(&self,
46 one_line_budget: usize, // excluding indentation
47 multi_line_budget: usize,
49 path_list: &[ast::PathListItem],
50 visibility: ast::Visibility,
53 let path_str = pprust::path_to_string(path);
54 let vis = format_visibility(visibility);
56 match path_list.len() {
58 1 => return Some(rewrite_single_use_list(path_str, path_list[0], vis)),
63 let path_separation_w = if path_str.len() > 0 { 2 } else { 0 };
65 let indent = path_str.len() + 5 + path_separation_w + vis.len();
68 let used_width = indent + 2;
70 // Break as early as possible when we've blown our budget.
71 let remaining_line_budget = one_line_budget.checked_sub(used_width).unwrap_or(0);
72 let remaining_multi_budget = multi_line_budget.checked_sub(used_width).unwrap_or(0);
74 let fmt = ListFormatting { tactic: ListTactic::Mixed,
76 trailing_separator: SeparatorTactic::Never,
77 indent: block_indent + indent,
78 h_width: remaining_line_budget,
79 v_width: remaining_multi_budget,
80 ends_with_newline: true, };
82 let mut items = itemize_list(self.codemap,
83 vec![ListItem::from_str("")], /* Dummy value, explanation
90 |vpi| match vpi.node {
91 ast::PathListItem_::PathListIdent{ name, .. } => {
92 token::get_ident(name).to_string()
94 ast::PathListItem_::PathListMod{ .. } => {
98 span_after(span, "{", self.codemap),
101 // We prefixed the item list with a dummy value so that we can
102 // potentially move "self" to the front of the vector without touching
103 // the rest of the items.
104 // FIXME: Make more efficient by using a linked list? That would
105 // require changes to the signatures of itemize_list and write_list.
106 let has_self = move_self_to_front(&mut items);
107 let first_index = if has_self { 0 } else { 1 };
109 if self.config.reorder_imports {
110 items.tail_mut().sort_by(|a, b| a.item.cmp(&b.item));
113 let list = write_list(&items[first_index..], &fmt);
115 Some(if path_str.len() == 0 {
116 format!("{}use {{{}}};", vis, list)
118 format!("{}use {}::{{{}}};", vis, path_str, list)
123 // Returns true when self item was found.
124 fn move_self_to_front(items: &mut Vec<ListItem>) -> bool {
125 match items.iter().position(|item| item.item == "self") {
127 items[0] = items.remove(pos);