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 path_str = try_opt!(path.rewrite(context, width - ident_str.len() - 4, offset));
41 Some(if path.segments.last().unwrap().identifier == ident {
44 format!("{} as {}", path_str, ident_str)
51 fn rewrite_single_use_list(path_str: String, vpi: &ast::PathListItem) -> String {
52 let path_item_str = if let ast::PathListItem_::PathListIdent{ name, .. } = vpi.node {
54 if path_str.is_empty() {
57 format!("{}::{}", path_str, name)
61 if !path_str.is_empty() {
64 // This catches the import: use {self}, which is a compiler error, so we just
70 append_alias(path_item_str, vpi)
73 fn rewrite_path_item(vpi: &&ast::PathListItem) -> String {
74 let path_item_str = match vpi.node {
75 ast::PathListItem_::PathListIdent{ name, .. } => {
78 ast::PathListItem_::PathListMod{ .. } => {
83 append_alias(path_item_str, vpi)
86 fn append_alias(path_item_str: String, vpi: &ast::PathListItem) -> String {
88 ast::PathListItem_::PathListIdent{ rename: Some(rename), .. } |
89 ast::PathListItem_::PathListMod{ rename: Some(rename), .. } => {
90 format!("{} as {}", path_item_str, rename)
96 // Pretty prints a multi-item import.
97 // Assumes that path_list.len() > 0.
98 pub fn rewrite_use_list(width: usize,
101 path_list: &[ast::PathListItem],
103 context: &RewriteContext)
106 let path_str = try_opt!(path.rewrite(context, width - 1, offset));
108 match path_list.len() {
110 1 => return Some(rewrite_single_use_list(path_str, &path_list[0])),
115 let path_separation_w = if !path_str.is_empty() {
121 let supp_indent = path_str.len() + path_separation_w + 1;
123 let remaining_width = width.checked_sub(supp_indent + 1).unwrap_or(0);
125 let fmt = ListFormatting {
126 tactic: ListTactic::Mixed,
128 trailing_separator: SeparatorTactic::Never,
129 indent: offset + supp_indent,
130 h_width: remaining_width,
131 // FIXME This is too conservative, and will not use all width
133 // (loose 1 column (";"))
134 v_width: remaining_width,
135 ends_with_newline: false,
136 config: context.config,
140 // Dummy value, see explanation below.
141 let mut items = vec![ListItem::from_str("")];
142 let iter = itemize_list(context.codemap,
148 span_after(span, "{", context.codemap),
154 // We prefixed the item list with a dummy value so that we can
155 // potentially move "self" to the front of the vector without touching
156 // the rest of the items.
157 // FIXME: Make more efficient by using a linked list? That would require
158 // changes to the signatures of write_list.
159 let has_self = move_self_to_front(&mut items);
160 let first_index = if has_self {
166 if context.config.reorder_imports {
167 items[1..].sort_by(|a, b| a.item.cmp(&b.item));
170 let list_str = try_opt!(write_list(&items[first_index..], &fmt));
172 Some(if path_str.is_empty() {
173 format!("{{{}}}", list_str)
175 format!("{}::{{{}}}", path_str, list_str)
179 // Returns true when self item was found.
180 fn move_self_to_front(items: &mut Vec<ListItem>) -> bool {
181 match items.iter().position(|item| item.item == "self") {
183 items[0] = items.remove(pos);