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 types::rewrite_path;
14 use utils::span_after;
15 use rewrite::{Rewrite, RewriteContext};
18 use syntax::codemap::Span;
20 // TODO (some day) remove unused imports, expand globs, compress many single
21 // imports into a list import.
23 impl Rewrite for ast::ViewPath {
24 // Returns an empty string when the ViewPath is empty (like foo::bar::{})
25 fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> {
27 ast::ViewPath_::ViewPathList(_, ref path_list) if path_list.is_empty() => {
30 ast::ViewPath_::ViewPathList(ref path, ref path_list) => {
31 rewrite_use_list(width, offset, path, path_list, self.span, context)
33 ast::ViewPath_::ViewPathGlob(_) => {
34 // FIXME convert to list?
37 ast::ViewPath_::ViewPathSimple(ident, ref path) => {
38 let ident_str = ident.to_string();
40 let budget = try_opt!(width.checked_sub(ident_str.len() + 4));
41 let path_str = try_opt!(rewrite_path(context, false, None, path, budget, offset));
43 Some(if path.segments.last().unwrap().identifier == ident {
46 format!("{} as {}", path_str, ident_str)
53 fn rewrite_single_use_list(path_str: String, vpi: &ast::PathListItem) -> String {
54 let path_item_str = if let ast::PathListItem_::PathListIdent { name, .. } = vpi.node {
56 if path_str.is_empty() {
59 format!("{}::{}", path_str, name)
63 if !path_str.is_empty() {
66 // This catches the import: use {self}, which is a compiler error, so we just
72 append_alias(path_item_str, vpi)
75 fn rewrite_path_item(vpi: &&ast::PathListItem) -> Option<String> {
76 let path_item_str = match vpi.node {
77 ast::PathListItem_::PathListIdent { name, .. } => name.to_string(),
78 ast::PathListItem_::PathListMod { .. } => "self".to_owned(),
81 Some(append_alias(path_item_str, vpi))
84 fn append_alias(path_item_str: String, vpi: &ast::PathListItem) -> String {
86 ast::PathListItem_::PathListIdent { rename: Some(rename), .. } |
87 ast::PathListItem_::PathListMod { rename: Some(rename), .. } => {
88 format!("{} as {}", path_item_str, rename)
94 // Pretty prints a multi-item import.
95 // Assumes that path_list.len() > 0.
96 pub fn rewrite_use_list(width: usize,
99 path_list: &[ast::PathListItem],
101 context: &RewriteContext)
104 let budget = try_opt!(width.checked_sub(1));
105 let path_str = try_opt!(rewrite_path(context, false, None, path, budget, offset));
107 match path_list.len() {
109 1 => return Some(rewrite_single_use_list(path_str, &path_list[0])),
114 let path_separation_w = if !path_str.is_empty() {
120 let supp_indent = path_str.len() + path_separation_w + 1;
122 let remaining_width = width.checked_sub(supp_indent + 1).unwrap_or(0);
125 // Dummy value, see explanation below.
126 let mut items = vec![ListItem::from_str("")];
127 let iter = itemize_list(context.codemap,
133 span_after(span, "{", context.codemap),
139 // We prefixed the item list with a dummy value so that we can
140 // potentially move "self" to the front of the vector without touching
141 // the rest of the items.
142 let has_self = move_self_to_front(&mut items);
143 let first_index = if has_self {
149 if context.config.reorder_imports {
150 items[1..].sort_by(|a, b| a.item.cmp(&b.item));
153 let tactic = definitive_tactic(&items[first_index..],
154 ::lists::ListTactic::Mixed,
156 let fmt = ListFormatting {
159 trailing_separator: SeparatorTactic::Never,
160 indent: offset + supp_indent,
161 // FIXME This is too conservative, and will not use all width
163 // (loose 1 column (";"))
164 width: remaining_width,
165 ends_with_newline: false,
166 config: context.config,
168 let list_str = try_opt!(write_list(&items[first_index..], &fmt));
170 Some(if path_str.is_empty() {
171 format!("{{{}}}", list_str)
173 format!("{}::{{{}}}", path_str, list_str)
177 // Returns true when self item was found.
178 fn move_self_to_front(items: &mut Vec<ListItem>) -> bool {
179 match items.iter().position(|item| item.item.as_ref().map(|x| &x[..]) == Some("self")) {
181 items[0] = items.remove(pos);