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.
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.
12 use lists::{write_list, itemize_list, ListItem, ListFormatting, SeparatorTactic, definitive_tactic};
13 use utils::span_after;
14 use rewrite::{Rewrite, RewriteContext};
17 use syntax::codemap::Span;
19 // TODO (some day) remove unused imports, expand globs, compress many single
20 // imports into a list import.
22 impl Rewrite for ast::ViewPath {
23 // Returns an empty string when the ViewPath is empty (like foo::bar::{})
24 fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> {
26 ast::ViewPath_::ViewPathList(_, ref path_list) if path_list.is_empty() => {
29 ast::ViewPath_::ViewPathList(ref path, ref path_list) => {
30 rewrite_use_list(width, offset, path, path_list, self.span, context)
32 ast::ViewPath_::ViewPathGlob(_) => {
33 // FIXME convert to list?
36 ast::ViewPath_::ViewPathSimple(ident, ref path) => {
37 let ident_str = ident.to_string();
39 let budget = try_opt!(width.checked_sub(ident_str.len() + 4));
40 let path_str = try_opt!(path.rewrite(context, budget, offset));
42 Some(if path.segments.last().unwrap().identifier == ident {
45 format!("{} as {}", path_str, ident_str)
52 fn rewrite_single_use_list(path_str: String, vpi: &ast::PathListItem) -> String {
53 let path_item_str = if let ast::PathListItem_::PathListIdent{ name, .. } = vpi.node {
55 if path_str.is_empty() {
58 format!("{}::{}", path_str, name)
62 if !path_str.is_empty() {
65 // This catches the import: use {self}, which is a compiler error, so we just
71 append_alias(path_item_str, vpi)
74 fn rewrite_path_item(vpi: &&ast::PathListItem) -> Option<String> {
75 let path_item_str = match vpi.node {
76 ast::PathListItem_::PathListIdent{ name, .. } => {
79 ast::PathListItem_::PathListMod{ .. } => {
84 Some(append_alias(path_item_str, vpi))
87 fn append_alias(path_item_str: String, vpi: &ast::PathListItem) -> String {
89 ast::PathListItem_::PathListIdent{ rename: Some(rename), .. } |
90 ast::PathListItem_::PathListMod{ rename: Some(rename), .. } => {
91 format!("{} as {}", path_item_str, rename)
97 // Pretty prints a multi-item import.
98 // Assumes that path_list.len() > 0.
99 pub fn rewrite_use_list(width: usize,
102 path_list: &[ast::PathListItem],
104 context: &RewriteContext)
107 let budget = try_opt!(width.checked_sub(1));
108 let path_str = try_opt!(path.rewrite(context, budget, offset));
110 match path_list.len() {
112 1 => return Some(rewrite_single_use_list(path_str, &path_list[0])),
117 let path_separation_w = if !path_str.is_empty() {
123 let supp_indent = path_str.len() + path_separation_w + 1;
125 let remaining_width = width.checked_sub(supp_indent + 1).unwrap_or(0);
128 // Dummy value, see explanation below.
129 let mut items = vec![ListItem::from_str("")];
130 let iter = itemize_list(context.codemap,
136 span_after(span, "{", context.codemap),
142 // We prefixed the item list with a dummy value so that we can
143 // potentially move "self" to the front of the vector without touching
144 // the rest of the items.
145 let has_self = move_self_to_front(&mut items);
146 let first_index = if has_self {
152 if context.config.reorder_imports {
153 items[1..].sort_by(|a, b| a.item.cmp(&b.item));
156 let tactic = definitive_tactic(&items[first_index..],
157 ::lists::ListTactic::Mixed,
159 let fmt = ListFormatting {
162 trailing_separator: SeparatorTactic::Never,
163 indent: offset + supp_indent,
164 // FIXME This is too conservative, and will not use all width
166 // (loose 1 column (";"))
167 width: remaining_width,
168 ends_with_newline: false,
169 config: context.config,
171 let list_str = try_opt!(write_list(&items[first_index..], &fmt));
173 Some(if path_str.is_empty() {
174 format!("{{{}}}", list_str)
176 format!("{}::{{{}}}", path_str, list_str)
180 // Returns true when self item was found.
181 fn move_self_to_front(items: &mut Vec<ListItem>) -> bool {
182 match items.iter().position(|item| item.item.as_ref().map(|x| &x[..]) == Some("self")) {
184 items[0] = items.remove(pos);