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