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(ident, ref path) => {
41 let path_str = pprust::path_to_string(path);
43 Some(if path.segments.last().unwrap().identifier == ident {
46 format!("{} as {}", path_str, ident)
53 fn rewrite_single_use_list(path_str: String, vpi: ast::PathListItem) -> String {
54 if let ast::PathListItem_::PathListIdent{ name, .. } = vpi.node {
55 let name_str = token::get_ident(name).to_string();
56 if path_str.len() == 0 {
59 format!("{}::{}", path_str, name_str)
62 if path_str.len() != 0 {
65 // This catches the import: use {self}, which is a compiler error, so we just
72 // Basically just pretty prints a multi-item import.
73 // Returns None when the import can be removed.
74 pub fn rewrite_use_list(width: usize,
77 path_list: &[ast::PathListItem],
82 let path_str = pprust::path_to_string(path);
84 match path_list.len() {
86 1 => return Some(rewrite_single_use_list(path_str, path_list[0])),
91 let path_separation_w = if path_str.len() > 0 {
97 let supp_indent = path_str.len() + path_separation_w + 1;
99 let remaining_width = width.checked_sub(supp_indent + 1).unwrap_or(0);
101 let fmt = ListFormatting {
102 tactic: ListTactic::Mixed,
104 trailing_separator: SeparatorTactic::Never,
105 indent: offset + supp_indent,
106 h_width: remaining_width,
107 // FIXME This is too conservative, and will not use all width
109 // (loose 1 column (";"))
110 v_width: remaining_width,
111 ends_with_newline: true,
114 let mut items = itemize_list(codemap,
115 vec![ListItem::from_str("")], /* Dummy value, explanation
122 |vpi| match vpi.node {
123 ast::PathListItem_::PathListIdent{ name, .. } => {
124 token::get_ident(name).to_string()
126 ast::PathListItem_::PathListMod{ .. } => {
130 span_after(span, "{", codemap),
133 // We prefixed the item list with a dummy value so that we can
134 // potentially move "self" to the front of the vector without touching
135 // the rest of the items.
136 // FIXME: Make more efficient by using a linked list? That would
137 // require changes to the signatures of itemize_list and write_list.
138 let has_self = move_self_to_front(&mut items);
139 let first_index = if has_self {
145 if config.reorder_imports {
146 items[1..].sort_by(|a, b| a.item.cmp(&b.item));
149 let list = write_list(&items[first_index..], &fmt);
151 Some(if path_str.len() == 0 {
152 format!("{{{}}}", list)
154 format!("{}::{{{}}}", path_str, list)
158 // Returns true when self item was found.
159 fn move_self_to_front(items: &mut Vec<ListItem>) -> bool {
160 match items.iter().position(|item| item.item == "self") {
162 items[0] = items.remove(pos);