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.
11 use lists::{write_list, itemize_list, ListItem, ListFormatting, SeparatorTactic, ListTactic};
12 use utils::span_after;
13 use rewrite::{Rewrite, RewriteContext};
16 use syntax::codemap::Span;
18 // TODO (some day) remove unused imports, expand globs, compress many single
19 // imports into a list import.
21 impl Rewrite for ast::ViewPath {
22 // Returns an empty string when the ViewPath is empty (like foo::bar::{})
23 fn rewrite(&self, context: &RewriteContext, width: usize, offset: usize) -> Option<String> {
25 ast::ViewPath_::ViewPathList(_, ref path_list) if path_list.is_empty() => {
28 ast::ViewPath_::ViewPathList(ref path, ref path_list) => {
29 rewrite_use_list(width, offset, path, path_list, self.span, context)
31 ast::ViewPath_::ViewPathGlob(_) => {
32 // FIXME convert to list?
35 ast::ViewPath_::ViewPathSimple(ident, ref path) => {
36 let ident_str = ident.to_string();
38 let path_str = try_opt!(path.rewrite(context, width - ident_str.len() - 4, offset));
40 Some(if path.segments.last().unwrap().identifier == ident {
43 format!("{} as {}", path_str, ident_str)
50 fn rewrite_single_use_list(path_str: String, vpi: ast::PathListItem) -> String {
51 if let ast::PathListItem_::PathListIdent{ name, .. } = vpi.node {
52 if path_str.is_empty() {
55 format!("{}::{}", path_str, name)
58 if !path_str.is_empty() {
61 // This catches the import: use {self}, which is a compiler error, so we just
68 // Pretty prints a multi-item import.
69 // Assumes that path_list.len() > 0.
70 pub fn rewrite_use_list(width: usize,
73 path_list: &[ast::PathListItem],
75 context: &RewriteContext)
78 let path_str = try_opt!(path.rewrite(context, width - 1, offset));
80 match path_list.len() {
82 1 => return Some(rewrite_single_use_list(path_str, path_list[0])),
87 let path_separation_w = if !path_str.is_empty() {
93 let supp_indent = path_str.len() + path_separation_w + 1;
95 let remaining_width = width.checked_sub(supp_indent + 1).unwrap_or(0);
97 let fmt = ListFormatting {
98 tactic: ListTactic::Mixed,
100 trailing_separator: SeparatorTactic::Never,
101 indent: offset + supp_indent,
102 h_width: remaining_width,
103 // FIXME This is too conservative, and will not use all width
105 // (loose 1 column (";"))
106 v_width: remaining_width,
107 ends_with_newline: false,
111 // Dummy value, see explanation below.
112 let mut items = vec![ListItem::from_str("")];
113 let iter = itemize_list(context.codemap,
120 ast::PathListItem_::PathListIdent{ name, .. } => {
123 ast::PathListItem_::PathListMod{ .. } => {
128 span_after(span, "{", context.codemap),
134 // We prefixed the item list with a dummy value so that we can
135 // potentially move "self" to the front of the vector without touching
136 // the rest of the items.
137 // FIXME: Make more efficient by using a linked list? That would require
138 // changes to the signatures of write_list.
139 let has_self = move_self_to_front(&mut items);
140 let first_index = if has_self {
146 if context.config.reorder_imports {
147 items[1..].sort_by(|a, b| a.item.cmp(&b.item));
150 let list_str = try_opt!(write_list(&items[first_index..], &fmt));
152 Some(if path_str.is_empty() {
153 format!("{{{}}}", list_str)
155 format!("{}::{{{}}}", path_str, list_str)
159 // Returns true when self item was found.
160 fn move_self_to_front(items: &mut Vec<ListItem>) -> bool {
161 match items.iter().position(|item| item.item == "self") {
163 items[0] = items.remove(pos);