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, ListTactic};
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) -> String {
75 let path_item_str = match vpi.node {
76 ast::PathListItem_::PathListIdent{ name, .. } => {
79 ast::PathListItem_::PathListMod{ .. } => {
84 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);
127 let fmt = ListFormatting {
128 tactic: ListTactic::Mixed,
130 trailing_separator: SeparatorTactic::Never,
131 indent: offset + supp_indent,
132 h_width: remaining_width,
133 // FIXME This is too conservative, and will not use all width
135 // (loose 1 column (";"))
136 v_width: remaining_width,
137 ends_with_newline: false,
138 config: context.config,
142 // Dummy value, see explanation below.
143 let mut items = vec![ListItem::from_str("")];
144 let iter = itemize_list(context.codemap,
150 span_after(span, "{", context.codemap),
156 // We prefixed the item list with a dummy value so that we can
157 // potentially move "self" to the front of the vector without touching
158 // the rest of the items.
159 // FIXME: Make more efficient by using a linked list? That would require
160 // changes to the signatures of write_list.
161 let has_self = move_self_to_front(&mut items);
162 let first_index = if has_self {
168 if context.config.reorder_imports {
169 items[1..].sort_by(|a, b| a.item.cmp(&b.item));
172 let list_str = try_opt!(write_list(&items[first_index..], &fmt));
174 Some(if path_str.is_empty() {
175 format!("{{{}}}", list_str)
177 format!("{}::{{{}}}", path_str, list_str)
181 // Returns true when self item was found.
182 fn move_self_to_front(items: &mut Vec<ListItem>) -> bool {
183 match items.iter().position(|item| item.item == "self") {
185 items[0] = items.remove(pos);