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