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};
17 use syntax::parse::token;
18 use syntax::print::pprust;
19 use syntax::codemap::{CodeMap, Span};
21 // TODO (some day) remove unused imports, expand globs, compress many single 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: usize) -> Option<String> {
27 ast::ViewPath_::ViewPathList(ref path, ref path_list) => {
28 Some(rewrite_use_list(width,
34 context.config).unwrap_or("".to_owned()))
36 ast::ViewPath_::ViewPathGlob(_) => {
37 // FIXME convert to list?
40 ast::ViewPath_::ViewPathSimple(_,_) => {
47 fn rewrite_single_use_list(path_str: String, vpi: ast::PathListItem) -> String {
48 if let ast::PathListItem_::PathListIdent{ name, .. } = vpi.node {
49 let name_str = token::get_ident(name).to_string();
50 if path_str.len() == 0 {
53 format!("{}::{}", path_str, name_str)
56 if path_str.len() != 0 {
59 // This catches the import: use {self}, which is a compiler error, so we just
66 // Basically just pretty prints a multi-item import.
67 // Returns None when the import can be removed.
68 pub fn rewrite_use_list(width: usize,
71 path_list: &[ast::PathListItem],
76 let path_str = pprust::path_to_string(path);
78 match path_list.len() {
80 1 => return Some(rewrite_single_use_list(path_str, path_list[0])),
85 let path_separation_w = if path_str.len() > 0 {
91 let supp_indent = path_str.len() + path_separation_w + 1;
93 let remaining_width = width.checked_sub(supp_indent + 1).unwrap_or(0);
95 let fmt = ListFormatting {
96 tactic: ListTactic::Mixed,
98 trailing_separator: SeparatorTactic::Never,
99 indent: offset + supp_indent,
100 h_width: remaining_width,
101 // FIXME This is too conservative, and will not use all width
103 // (loose 1 column (";"))
104 v_width: remaining_width,
105 ends_with_newline: true,
108 let mut items = itemize_list(codemap,
109 vec![ListItem::from_str("")], /* Dummy value, explanation
116 |vpi| match vpi.node {
117 ast::PathListItem_::PathListIdent{ name, .. } => {
118 token::get_ident(name).to_string()
120 ast::PathListItem_::PathListMod{ .. } => {
124 span_after(span, "{", codemap),
127 // We prefixed the item list with a dummy value so that we can
128 // potentially move "self" to the front of the vector without touching
129 // the rest of the items.
130 // FIXME: Make more efficient by using a linked list? That would
131 // require changes to the signatures of itemize_list and write_list.
132 let has_self = move_self_to_front(&mut items);
133 let first_index = if has_self {
139 if config.reorder_imports {
140 items[1..].sort_by(|a, b| a.item.cmp(&b.item));
143 let list = write_list(&items[first_index..], &fmt);
145 Some(if path_str.len() == 0 {
146 format!("{{{}}}", list)
148 format!("{}::{{{}}}", path_str, list)
152 // Returns true when self item was found.
153 fn move_self_to_front(items: &mut Vec<ListItem>) -> bool {
154 match items.iter().position(|item| item.item == "self") {
156 items[0] = items.remove(pos);