]> git.lizzy.rs Git - rust.git/blob - src/imports.rs
Merge pull request #62 from marcusklaas/use-indent
[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                             budget: usize, // excluding indentation
27                             path: &ast::Path,
28                             path_list: &[ast::PathListItem],
29                             visibility: ast::Visibility) -> Option<String> {
30         let path_str = pprust::path_to_string(&path);
31
32         let vis = match visibility {
33             ast::Public => "pub ",
34             _ => ""
35         };
36
37         // 2 = ::
38         let path_separation_w = if path_str.len() > 0 { 2 } else { 0 };
39         // 5 = "use " + {
40         let indent = path_str.len() + 5 + path_separation_w + vis.len();
41         // 2 = } + ;
42         let used_width = indent + 2;
43
44         let remaining_budget = if used_width >= budget {
45             return None;
46         } else {
47             budget - used_width
48         };
49
50         let fmt = ListFormatting {
51             tactic: ListTactic::Mixed,
52             separator: ",",
53             trailing_separator: SeparatorTactic::Never,
54             indent: block_indent + indent,
55             h_width: remaining_budget,
56             v_width: remaining_budget,
57         };
58
59         // TODO handle any comments inbetween items.
60         // If `self` is in the list, put it first.
61         let head = if path_list.iter().any(|vpi|
62             if let ast::PathListItem_::PathListMod{ .. } = vpi.node {
63                 true
64             } else {
65                 false
66             }
67         ) {
68             Some(("self".to_owned(), String::new()))
69         } else {
70             None
71         };
72
73         let items: Vec<_> = head.into_iter().chain(path_list.iter().filter_map(|vpi| {
74             match vpi.node {
75                 ast::PathListItem_::PathListIdent{ name, .. } => {
76                     Some((token::get_ident(name).to_string(), String::new()))
77                 }
78                 // Skip `self`, because we added it above.
79                 ast::PathListItem_::PathListMod{ .. } => None,
80             }
81         })).collect();
82         Some(if path_str.len() == 0 {
83             format!("{}use {{{}}};", vis, write_list(&items, &fmt))
84         } else {
85             format!("{}use {}::{{{}}};", vis, path_str, write_list(&items, &fmt))
86         })
87     }
88 }