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, ListFormatting, SeparatorTactic, ListTactic};
15 use syntax::parse::token;
16 use syntax::print::pprust;
18 // TODO (some day) remove unused imports, expand globs, compress many single imports into a list import
20 fn rewrite_single_use_list(path_str: String, vpi: ast::PathListItem, vis: &str) -> String {
21 if let ast::PathListItem_::PathListIdent{ name, .. } = vpi.node {
22 let name_str = token::get_ident(name).to_string();
23 if path_str.len() == 0 {
24 format!("{}use {};", vis, name_str)
26 format!("{}use {}::{};", vis, path_str, name_str)
29 if path_str.len() != 0 {
30 format!("{}use {};", vis, path_str)
32 // This catches the import: use {self}, which is a compiler error, so we just
34 format!("{}use {{self}};", vis)
39 impl<'a> FmtVisitor<'a> {
40 // Basically just pretty prints a multi-item import.
41 // Returns None when the import can be removed.
42 pub fn rewrite_use_list(&mut self,
44 one_line_budget: usize, // excluding indentation
45 multi_line_budget: usize,
47 path_list: &[ast::PathListItem],
48 visibility: ast::Visibility) -> Option<String> {
49 let path_str = pprust::path_to_string(path);
51 let vis = match visibility {
52 ast::Public => "pub ",
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 = if used_width > one_line_budget {
74 one_line_budget - used_width
76 let remaining_multi_budget = if used_width > multi_line_budget {
79 multi_line_budget - used_width
82 let fmt = ListFormatting {
83 tactic: ListTactic::Mixed,
85 trailing_separator: SeparatorTactic::Never,
86 indent: block_indent + indent,
87 h_width: remaining_line_budget,
88 v_width: remaining_multi_budget,
91 // TODO handle any comments inbetween items.
92 // If `self` is in the list, put it first.
93 let head = if path_list.iter().any(|vpi|
94 if let ast::PathListItem_::PathListMod{ .. } = vpi.node {
100 Some(("self".to_owned(), String::new()))
105 let items: Vec<_> = head.into_iter().chain(path_list.iter().filter_map(|vpi| {
107 ast::PathListItem_::PathListIdent{ name, .. } => {
108 Some((token::get_ident(name).to_string(), String::new()))
110 // Skip `self`, because we added it above.
111 ast::PathListItem_::PathListMod{ .. } => None,
114 Some(if path_str.len() == 0 {
115 format!("{}use {{{}}};", vis, write_list(&items, &fmt))
117 format!("{}use {}::{{{}}};", vis, path_str, write_list(&items, &fmt))