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, ref path_list) => {
26 Some(rewrite_use_list(width,
31 context).unwrap_or("".to_owned()))
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 path_str = try_opt!(path.rewrite(context, width - ident_str.len() - 4, 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 if let ast::PathListItem_::PathListIdent{ name, .. } = vpi.node {
54 if path_str.len() == 0 {
57 format!("{}::{}", path_str, name)
60 if path_str.len() != 0 {
63 // This catches the import: use {self}, which is a compiler error, so we just
70 // Basically just pretty prints a multi-item import.
71 // Returns None when the import can be removed.
72 pub fn rewrite_use_list(width: usize,
75 path_list: &[ast::PathListItem],
77 context: &RewriteContext)
80 let path_str = try_opt!(path.rewrite(context, width - 1, offset));
82 match path_list.len() {
84 1 => return Some(rewrite_single_use_list(path_str, path_list[0])),
89 let path_separation_w = if path_str.len() > 0 {
95 let supp_indent = path_str.len() + path_separation_w + 1;
97 let remaining_width = width.checked_sub(supp_indent + 1).unwrap_or(0);
99 let fmt = ListFormatting {
100 tactic: ListTactic::Mixed,
102 trailing_separator: SeparatorTactic::Never,
103 indent: offset + supp_indent,
104 h_width: remaining_width,
105 // FIXME This is too conservative, and will not use all width
107 // (loose 1 column (";"))
108 v_width: remaining_width,
109 ends_with_newline: false,
113 // Dummy value, see explanation below.
114 let mut items = vec![ListItem::from_str("")];
115 let iter = itemize_list(context.codemap,
122 ast::PathListItem_::PathListIdent{ name, .. } => {
125 ast::PathListItem_::PathListMod{ .. } => {
130 span_after(span, "{", context.codemap),
136 // We prefixed the item list with a dummy value so that we can
137 // potentially move "self" to the front of the vector without touching
138 // the rest of the items.
139 // FIXME: Make more efficient by using a linked list? That would require
140 // changes to the signatures of write_list.
141 let has_self = move_self_to_front(&mut items);
142 let first_index = if has_self {
148 if context.config.reorder_imports {
149 items[1..].sort_by(|a, b| a.item.cmp(&b.item));
152 let list = write_list(&items[first_index..], &fmt);
154 Some(if path_str.len() == 0 {
155 format!("{{{}}}", list)
157 format!("{}::{{{}}}", path_str, list)
161 // Returns true when self item was found.
162 fn move_self_to_front(items: &mut Vec<ListItem>) -> bool {
163 match items.iter().position(|item| item.item == "self") {
165 items[0] = items.remove(pos);