]> git.lizzy.rs Git - rust.git/blob - src/imports.rs
Keep import lists on a single line when possible
[rust.git] / src / imports.rs
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.
4 //
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.
10
11 use visitor::FmtVisitor;
12 use lists::{write_list, ListFormatting, SeparatorTactic, ListTactic};
13
14 use syntax::ast;
15 use syntax::parse::token;
16 use syntax::print::pprust;
17
18 // TODO change import lists with one item to a single import
19 //      remove empty lists (if they're even possible)
20 // TODO (some day) remove unused imports, expand globs, compress many single imports into a list import
21
22 impl<'a> FmtVisitor<'a> {
23     // Basically just pretty prints a multi-item import.
24     pub fn rewrite_use_list(&mut self,
25                             block_indent: usize,
26                             one_line_budget: usize, // excluding indentation
27                             multi_line_budget: usize,
28                             path: &ast::Path,
29                             path_list: &[ast::PathListItem],
30                             visibility: ast::Visibility) -> String {
31         let path_str = pprust::path_to_string(&path);
32
33         let vis = match visibility {
34             ast::Public => "pub ",
35             _ => ""
36         };
37
38         // 2 = ::
39         let path_separation_w = if path_str.len() > 0 { 2 } else { 0 };
40         // 5 = "use " + {
41         let indent = path_str.len() + 5 + path_separation_w + vis.len();
42         // 2 = } + ;
43         let used_width = indent + 2;
44
45         // Break as early as possible when we've blown our budget.
46         let remaining_line_budget = if used_width > one_line_budget {
47             0
48         } else {
49             one_line_budget - used_width
50         };
51         let remaining_multi_budget = if used_width > multi_line_budget {
52             0
53         } else {
54             multi_line_budget - used_width
55         };
56
57         let fmt = ListFormatting {
58             tactic: ListTactic::Mixed,
59             separator: ",",
60             trailing_separator: SeparatorTactic::Never,
61             indent: block_indent + indent,
62             h_width: remaining_line_budget,
63             v_width: remaining_multi_budget,
64         };
65
66         // TODO handle any comments inbetween items.
67         // If `self` is in the list, put it first.
68         let head = if path_list.iter().any(|vpi|
69             if let ast::PathListItem_::PathListMod{ .. } = vpi.node {
70                 true
71             } else {
72                 false
73             }
74         ) {
75             Some(("self".to_owned(), String::new()))
76         } else {
77             None
78         };
79
80         let items: Vec<_> = head.into_iter().chain(path_list.iter().filter_map(|vpi| {
81             match vpi.node {
82                 ast::PathListItem_::PathListIdent{ name, .. } => {
83                     Some((token::get_ident(name).to_string(), String::new()))
84                 }
85                 // Skip `self`, because we added it above.
86                 ast::PathListItem_::PathListMod{ .. } => None,
87             }
88         })).collect();
89         if path_str.len() == 0 {
90             format!("{}use {{{}}};", vis, write_list(&items, &fmt))
91         } else {
92             format!("{}use {}::{{{}}};", vis, path_str, write_list(&items, &fmt))
93         }
94     }
95 }