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