2015-04-21 21:01:19 +12:00
|
|
|
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2017-09-06 18:47:50 +09:00
|
|
|
use std::cmp::Ordering;
|
2017-07-13 18:42:14 +09:00
|
|
|
|
2017-09-15 22:31:52 +09:00
|
|
|
use syntax::ast;
|
2017-06-06 06:54:22 +02:00
|
|
|
use syntax::codemap::{BytePos, Span};
|
2017-07-13 18:42:14 +09:00
|
|
|
|
2017-09-17 15:32:00 +09:00
|
|
|
use spanned::Spanned;
|
2016-05-25 20:41:26 +02:00
|
|
|
use codemap::SpanUtils;
|
2017-09-06 18:47:50 +09:00
|
|
|
use comment::combine_strs_with_missing_comments;
|
2017-07-13 20:40:49 +09:00
|
|
|
use config::IndentStyle;
|
|
|
|
use lists::{definitive_tactic, itemize_list, write_list, DefinitiveListTactic, ListFormatting,
|
2017-08-18 23:19:47 +09:00
|
|
|
ListItem, Separator, SeparatorPlace, SeparatorTactic};
|
2015-07-17 15:58:47 +02:00
|
|
|
use rewrite::{Rewrite, RewriteContext};
|
2017-09-17 15:23:25 +09:00
|
|
|
use shape::Shape;
|
2017-07-13 18:42:14 +09:00
|
|
|
use types::{rewrite_path, PathContext};
|
2017-09-06 18:47:50 +09:00
|
|
|
use utils::{format_visibility, mk_sp};
|
|
|
|
use visitor::{rewrite_extern_crate, FmtVisitor};
|
2016-07-26 06:20:01 +01:00
|
|
|
|
|
|
|
fn compare_path_segments(a: &ast::PathSegment, b: &ast::PathSegment) -> Ordering {
|
2017-04-24 16:50:11 +09:00
|
|
|
a.identifier.name.as_str().cmp(&b.identifier.name.as_str())
|
2016-07-26 06:20:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn compare_paths(a: &ast::Path, b: &ast::Path) -> Ordering {
|
|
|
|
for segment in a.segments.iter().zip(b.segments.iter()) {
|
|
|
|
let ord = compare_path_segments(segment.0, segment.1);
|
|
|
|
if ord != Ordering::Equal {
|
|
|
|
return ord;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
a.segments.len().cmp(&b.segments.len())
|
|
|
|
}
|
|
|
|
|
2017-11-30 23:55:12 +01:00
|
|
|
fn compare_use_trees(a: &ast::UseTree, b: &ast::UseTree, nested: bool) -> Ordering {
|
|
|
|
use ast::UseTreeKind::*;
|
2016-07-26 06:20:01 +01:00
|
|
|
|
2017-11-30 23:55:12 +01:00
|
|
|
// `use_nested_groups` is not yet supported, remove the `if !nested` when support will be
|
|
|
|
// fully added
|
|
|
|
if !nested {
|
|
|
|
let paths_cmp = compare_paths(&a.prefix, &b.prefix);
|
|
|
|
if paths_cmp != Ordering::Equal {
|
|
|
|
return paths_cmp;
|
2016-07-26 06:20:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-30 23:55:12 +01:00
|
|
|
match (&a.kind, &b.kind) {
|
|
|
|
(&Simple(ident_a), &Simple(ident_b)) => {
|
|
|
|
let name_a = &*a.prefix.segments.last().unwrap().identifier.name.as_str();
|
|
|
|
let name_b = &*b.prefix.segments.last().unwrap().identifier.name.as_str();
|
|
|
|
let name_ordering = if name_a == "self" {
|
|
|
|
if name_b == "self" {
|
|
|
|
Ordering::Equal
|
|
|
|
} else {
|
|
|
|
Ordering::Less
|
|
|
|
}
|
|
|
|
} else if name_b == "self" {
|
|
|
|
Ordering::Greater
|
|
|
|
} else {
|
|
|
|
name_a.cmp(name_b)
|
|
|
|
};
|
|
|
|
if name_ordering == Ordering::Equal {
|
|
|
|
if ident_a.name.as_str() != name_a {
|
|
|
|
if ident_b.name.as_str() != name_b {
|
|
|
|
ident_a.name.as_str().cmp(&ident_b.name.as_str())
|
|
|
|
} else {
|
|
|
|
Ordering::Greater
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Ordering::Less
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
name_ordering
|
|
|
|
}
|
2017-10-26 16:53:30 +09:00
|
|
|
}
|
2017-11-30 23:55:12 +01:00
|
|
|
(&Glob, &Glob) => Ordering::Equal,
|
|
|
|
(&Simple(_), _) | (&Glob, &Nested(_)) => Ordering::Less,
|
|
|
|
(&Nested(ref a_items), &Nested(ref b_items)) => {
|
|
|
|
let mut a = a_items
|
|
|
|
.iter()
|
|
|
|
.map(|&(ref tree, _)| tree.clone())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
let mut b = b_items
|
|
|
|
.iter()
|
|
|
|
.map(|&(ref tree, _)| tree.clone())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
a.sort_by(|a, b| compare_use_trees(a, b, true));
|
|
|
|
b.sort_by(|a, b| compare_use_trees(a, b, true));
|
|
|
|
for comparison_pair in a.iter().zip(b.iter()) {
|
|
|
|
let ord = compare_use_trees(comparison_pair.0, comparison_pair.1, true);
|
|
|
|
if ord != Ordering::Equal {
|
|
|
|
return ord;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
a.len().cmp(&b.len())
|
2016-07-26 06:20:01 +01:00
|
|
|
}
|
2017-11-30 23:55:12 +01:00
|
|
|
(&Glob, &Simple(_)) | (&Nested(_), _) => Ordering::Greater,
|
2016-07-26 06:20:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-09 00:16:18 +09:00
|
|
|
fn compare_use_items(context: &RewriteContext, a: &ast::Item, b: &ast::Item) -> Option<Ordering> {
|
2016-07-26 06:20:01 +01:00
|
|
|
match (&a.node, &b.node) {
|
2017-11-30 23:55:12 +01:00
|
|
|
(&ast::ItemKind::Use(ref a_tree), &ast::ItemKind::Use(ref b_tree)) => {
|
|
|
|
Some(compare_use_trees(&a_tree, &b_tree, false))
|
2016-07-26 06:20:01 +01:00
|
|
|
}
|
2017-08-09 00:16:18 +09:00
|
|
|
(&ast::ItemKind::ExternCrate(..), &ast::ItemKind::ExternCrate(..)) => {
|
|
|
|
Some(context.snippet(a.span).cmp(&context.snippet(b.span)))
|
|
|
|
}
|
2016-07-26 06:20:01 +01:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2015-04-21 21:01:19 +12:00
|
|
|
|
2015-08-16 15:58:17 +12:00
|
|
|
// TODO (some day) remove unused imports, expand globs, compress many single
|
|
|
|
// imports into a list import.
|
2015-04-21 22:50:43 +12:00
|
|
|
|
2017-11-30 23:55:12 +01:00
|
|
|
fn rewrite_prefix(path: &ast::Path, context: &RewriteContext, shape: Shape) -> Option<String> {
|
2017-09-15 12:10:58 +09:00
|
|
|
let path_str = if path.segments.last().unwrap().identifier.to_string() == "self"
|
|
|
|
&& path.segments.len() > 1
|
2017-06-12 15:58:58 +12:00
|
|
|
{
|
2017-03-08 10:10:56 +13:00
|
|
|
let path = &ast::Path {
|
2017-08-29 22:16:04 +09:00
|
|
|
span: path.span,
|
2017-04-15 18:42:45 +09:00
|
|
|
segments: path.segments[..path.segments.len() - 1].to_owned(),
|
|
|
|
};
|
2017-10-05 20:50:19 +09:00
|
|
|
rewrite_path(context, PathContext::Import, None, path, shape)?
|
2017-03-08 10:10:56 +13:00
|
|
|
} else {
|
2017-10-05 20:50:19 +09:00
|
|
|
rewrite_path(context, PathContext::Import, None, path, shape)?
|
2017-03-08 10:10:56 +13:00
|
|
|
};
|
|
|
|
Some(path_str)
|
|
|
|
}
|
|
|
|
|
2017-11-30 23:55:12 +01:00
|
|
|
impl Rewrite for ast::UseTree {
|
2017-01-31 08:28:48 +13:00
|
|
|
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
|
2017-11-30 23:55:12 +01:00
|
|
|
match self.kind {
|
|
|
|
ast::UseTreeKind::Nested(ref items) => {
|
|
|
|
rewrite_nested_use_tree(shape, &self.prefix, items, self.span, context)
|
2015-07-17 15:58:47 +02:00
|
|
|
}
|
2017-11-30 23:55:12 +01:00
|
|
|
ast::UseTreeKind::Glob => {
|
2017-10-05 20:50:19 +09:00
|
|
|
let prefix_shape = shape.sub_width(3)?;
|
2017-11-30 23:55:12 +01:00
|
|
|
|
|
|
|
if self.prefix.segments.len() > 0 {
|
|
|
|
let path_str = rewrite_prefix(&self.prefix, context, prefix_shape)?;
|
|
|
|
Some(format!("{}::*", path_str))
|
|
|
|
} else {
|
2017-12-08 13:07:42 +09:00
|
|
|
Some("*".to_owned())
|
2017-11-30 23:55:12 +01:00
|
|
|
}
|
2017-03-08 10:10:56 +13:00
|
|
|
}
|
2017-11-30 23:55:12 +01:00
|
|
|
ast::UseTreeKind::Simple(ident) => {
|
2015-08-14 14:09:19 +02:00
|
|
|
let ident_str = ident.to_string();
|
2017-11-30 23:55:12 +01:00
|
|
|
|
2015-08-14 14:09:19 +02:00
|
|
|
// 4 = " as ".len()
|
2017-10-05 20:50:19 +09:00
|
|
|
let prefix_shape = shape.sub_width(ident_str.len() + 4)?;
|
2017-11-30 23:55:12 +01:00
|
|
|
let path_str = rewrite_prefix(&self.prefix, context, prefix_shape)?;
|
2015-07-25 23:10:48 +02:00
|
|
|
|
2017-11-30 23:55:12 +01:00
|
|
|
if self.prefix.segments.last().unwrap().identifier == ident {
|
|
|
|
Some(path_str)
|
2017-06-12 16:01:41 +12:00
|
|
|
} else {
|
2017-11-30 23:55:12 +01:00
|
|
|
Some(format!("{} as {}", path_str, ident_str))
|
|
|
|
}
|
2015-07-17 15:58:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-06 18:47:50 +09:00
|
|
|
// Rewrite `use foo;` WITHOUT attributes.
|
|
|
|
fn rewrite_import(
|
|
|
|
context: &RewriteContext,
|
|
|
|
vis: &ast::Visibility,
|
2017-11-30 23:55:12 +01:00
|
|
|
tree: &ast::UseTree,
|
2017-09-06 18:47:50 +09:00
|
|
|
attrs: &[ast::Attribute],
|
|
|
|
shape: Shape,
|
|
|
|
) -> Option<String> {
|
|
|
|
let vis = format_visibility(vis);
|
|
|
|
// 4 = `use `, 1 = `;`
|
|
|
|
let rw = shape
|
|
|
|
.offset_left(vis.len() + 4)
|
|
|
|
.and_then(|shape| shape.sub_width(1))
|
2017-11-30 23:55:12 +01:00
|
|
|
.and_then(|shape| match tree.kind {
|
|
|
|
// If we have an empty nested group with no attributes, we erase it
|
|
|
|
ast::UseTreeKind::Nested(ref items) if items.is_empty() && attrs.is_empty() => {
|
2017-12-08 13:07:42 +09:00
|
|
|
Some("".to_owned())
|
2016-08-10 08:13:27 +02:00
|
|
|
}
|
2017-11-30 23:55:12 +01:00
|
|
|
_ => tree.rewrite(context, shape),
|
2017-09-06 18:47:50 +09:00
|
|
|
});
|
|
|
|
match rw {
|
|
|
|
Some(ref s) if !s.is_empty() => Some(format!("{}use {};", vis, s)),
|
|
|
|
_ => rw,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn rewrite_imports(
|
|
|
|
context: &RewriteContext,
|
2017-09-15 22:31:52 +09:00
|
|
|
use_items: &[&ast::Item],
|
2017-09-06 18:47:50 +09:00
|
|
|
shape: Shape,
|
|
|
|
span: Span,
|
|
|
|
) -> Option<String> {
|
|
|
|
let items = itemize_list(
|
|
|
|
context.codemap,
|
|
|
|
use_items.iter(),
|
|
|
|
"",
|
2017-11-16 17:38:12 +09:00
|
|
|
";",
|
2017-09-06 18:47:50 +09:00
|
|
|
|item| item.span().lo(),
|
|
|
|
|item| item.span().hi(),
|
|
|
|
|item| {
|
2017-10-05 20:50:19 +09:00
|
|
|
let attrs_str = item.attrs.rewrite(context, shape)?;
|
2017-09-06 18:47:50 +09:00
|
|
|
|
|
|
|
let missed_span = if item.attrs.is_empty() {
|
|
|
|
mk_sp(item.span.lo(), item.span.lo())
|
|
|
|
} else {
|
|
|
|
mk_sp(item.attrs.last().unwrap().span.hi(), item.span.lo())
|
|
|
|
};
|
|
|
|
|
|
|
|
let item_str = match item.node {
|
2017-11-30 23:55:12 +01:00
|
|
|
ast::ItemKind::Use(ref tree) => {
|
|
|
|
rewrite_import(context, &item.vis, tree, &item.attrs, shape)?
|
2017-09-06 18:47:50 +09:00
|
|
|
}
|
2017-10-05 20:50:19 +09:00
|
|
|
ast::ItemKind::ExternCrate(..) => rewrite_extern_crate(context, item)?,
|
2017-09-06 18:47:50 +09:00
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
|
|
|
|
combine_strs_with_missing_comments(
|
|
|
|
context,
|
|
|
|
&attrs_str,
|
|
|
|
&item_str,
|
|
|
|
missed_span,
|
|
|
|
shape,
|
|
|
|
false,
|
|
|
|
)
|
|
|
|
},
|
|
|
|
span.lo(),
|
|
|
|
span.hi(),
|
|
|
|
false,
|
|
|
|
);
|
|
|
|
let mut item_pair_vec: Vec<_> = items.zip(use_items.iter()).collect();
|
|
|
|
item_pair_vec.sort_by(|a, b| compare_use_items(context, a.1, b.1).unwrap());
|
|
|
|
let item_vec: Vec<_> = item_pair_vec.into_iter().map(|pair| pair.0).collect();
|
|
|
|
|
|
|
|
let fmt = ListFormatting {
|
|
|
|
tactic: DefinitiveListTactic::Vertical,
|
|
|
|
separator: "",
|
|
|
|
trailing_separator: SeparatorTactic::Never,
|
|
|
|
separator_place: SeparatorPlace::Back,
|
|
|
|
shape: shape,
|
|
|
|
ends_with_newline: true,
|
|
|
|
preserve_newline: false,
|
|
|
|
config: context.config,
|
|
|
|
};
|
|
|
|
|
|
|
|
write_list(&item_vec, &fmt)
|
|
|
|
}
|
2016-08-24 21:32:04 +01:00
|
|
|
|
2017-09-06 18:47:50 +09:00
|
|
|
impl<'a> FmtVisitor<'a> {
|
2017-09-15 22:31:52 +09:00
|
|
|
pub fn format_imports(&mut self, use_items: &[&ast::Item]) {
|
2017-09-06 18:47:50 +09:00
|
|
|
if use_items.is_empty() {
|
|
|
|
return;
|
2016-07-26 06:20:01 +01:00
|
|
|
}
|
2017-09-06 18:47:50 +09:00
|
|
|
|
|
|
|
let lo = use_items.first().unwrap().span().lo();
|
|
|
|
let hi = use_items.last().unwrap().span().hi();
|
|
|
|
let span = mk_sp(lo, hi);
|
|
|
|
let rw = rewrite_imports(&self.get_context(), use_items, self.shape(), span);
|
|
|
|
self.push_rewrite(span, rw);
|
2016-07-26 06:20:01 +01:00
|
|
|
}
|
|
|
|
|
2017-11-30 23:55:12 +01:00
|
|
|
pub fn format_import(&mut self, item: &ast::Item, tree: &ast::UseTree) {
|
2017-09-06 18:47:50 +09:00
|
|
|
let span = item.span;
|
|
|
|
let shape = self.shape();
|
2017-11-30 23:55:12 +01:00
|
|
|
let rw = rewrite_import(&self.get_context(), &item.vis, tree, &item.attrs, shape);
|
2017-07-13 20:40:49 +09:00
|
|
|
match rw {
|
2016-07-26 06:20:01 +01:00
|
|
|
Some(ref s) if s.is_empty() => {
|
|
|
|
// Format up to last newline
|
2017-09-06 18:47:50 +09:00
|
|
|
let prev_span = mk_sp(self.last_pos, source!(self, span).lo());
|
2016-07-26 06:20:01 +01:00
|
|
|
let span_end = match self.snippet(prev_span).rfind('\n') {
|
|
|
|
Some(offset) => self.last_pos + BytePos(offset as u32),
|
2017-08-19 21:47:40 +03:00
|
|
|
None => source!(self, span).lo(),
|
2016-07-26 06:20:01 +01:00
|
|
|
};
|
|
|
|
self.format_missing(span_end);
|
2017-08-19 21:47:40 +03:00
|
|
|
self.last_pos = source!(self, span).hi();
|
2016-07-26 06:20:01 +01:00
|
|
|
}
|
|
|
|
Some(ref s) => {
|
2017-08-19 21:47:40 +03:00
|
|
|
self.format_missing_with_indent(source!(self, span).lo());
|
2017-10-26 16:54:41 +09:00
|
|
|
self.buffer.push_str(s);
|
2017-08-19 21:47:40 +03:00
|
|
|
self.last_pos = source!(self, span).hi();
|
2016-07-26 06:20:01 +01:00
|
|
|
}
|
|
|
|
None => {
|
2017-08-19 21:47:40 +03:00
|
|
|
self.format_missing_with_indent(source!(self, span).lo());
|
|
|
|
self.format_missing(source!(self, span).hi());
|
2016-07-26 06:20:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-30 23:55:12 +01:00
|
|
|
fn rewrite_nested_use_tree_single(path_str: String, tree: &ast::UseTree) -> String {
|
|
|
|
if let ast::UseTreeKind::Simple(rename) = tree.kind {
|
|
|
|
let ident = tree.prefix.segments.last().unwrap().identifier;
|
|
|
|
let mut item_str = ident.name.to_string();
|
|
|
|
if item_str == "self" {
|
|
|
|
item_str = "".to_owned();
|
|
|
|
}
|
|
|
|
|
|
|
|
let path_item_str = if path_str.is_empty() {
|
|
|
|
if item_str.is_empty() {
|
|
|
|
"self".to_owned()
|
|
|
|
} else {
|
|
|
|
item_str
|
|
|
|
}
|
|
|
|
} else if item_str.is_empty() {
|
|
|
|
path_str
|
2017-01-19 10:47:07 +13:00
|
|
|
} else {
|
2017-11-30 23:55:12 +01:00
|
|
|
format!("{}::{}", path_str, item_str)
|
|
|
|
};
|
|
|
|
|
|
|
|
if ident == rename {
|
|
|
|
path_item_str
|
|
|
|
} else {
|
|
|
|
format!("{} as {}", path_item_str, rename)
|
2017-01-19 10:47:07 +13:00
|
|
|
}
|
|
|
|
} else {
|
2017-11-30 23:55:12 +01:00
|
|
|
unimplemented!("`use_nested_groups` is not yet fully supported");
|
|
|
|
}
|
2015-09-26 18:12:25 +12:00
|
|
|
}
|
|
|
|
|
2017-11-30 23:55:12 +01:00
|
|
|
fn rewrite_nested_use_tree_item(tree: &&ast::UseTree) -> Option<String> {
|
|
|
|
Some(if let ast::UseTreeKind::Simple(rename) = tree.kind {
|
|
|
|
let ident = tree.prefix.segments.last().unwrap().identifier;
|
2015-09-26 18:12:25 +12:00
|
|
|
|
2017-11-30 23:55:12 +01:00
|
|
|
if ident == rename {
|
|
|
|
ident.name.to_string()
|
|
|
|
} else {
|
|
|
|
format!("{} as {}", ident.name.to_string(), rename)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unimplemented!("`use_nested_groups` is not yet fully supported");
|
|
|
|
})
|
2015-05-01 17:27:54 +01:00
|
|
|
}
|
|
|
|
|
2017-07-13 18:36:31 +09:00
|
|
|
#[derive(Eq, PartialEq)]
|
|
|
|
enum ImportItem<'a> {
|
|
|
|
// `self` or `self as a`
|
|
|
|
SelfImport(&'a str),
|
|
|
|
// name_one, name_two, ...
|
|
|
|
SnakeCase(&'a str),
|
|
|
|
// NameOne, NameTwo, ...
|
|
|
|
CamelCase(&'a str),
|
|
|
|
// NAME_ONE, NAME_TWO, ...
|
|
|
|
AllCaps(&'a str),
|
|
|
|
// Failed to format the import item
|
|
|
|
Invalid,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> ImportItem<'a> {
|
|
|
|
fn from_str(s: &str) -> ImportItem {
|
|
|
|
if s == "self" || s.starts_with("self as") {
|
|
|
|
ImportItem::SelfImport(s)
|
|
|
|
} else if s.chars().all(|c| c.is_lowercase() || c == '_' || c == ' ') {
|
|
|
|
ImportItem::SnakeCase(s)
|
|
|
|
} else if s.chars().all(|c| c.is_uppercase() || c == '_' || c == ' ') {
|
|
|
|
ImportItem::AllCaps(s)
|
|
|
|
} else {
|
|
|
|
ImportItem::CamelCase(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_opt_str(s: Option<&String>) -> ImportItem {
|
|
|
|
s.map_or(ImportItem::Invalid, |s| ImportItem::from_str(s))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_str(&self) -> Option<&str> {
|
|
|
|
match *self {
|
2017-11-16 16:42:07 +09:00
|
|
|
ImportItem::SelfImport(s)
|
|
|
|
| ImportItem::SnakeCase(s)
|
|
|
|
| ImportItem::CamelCase(s)
|
|
|
|
| ImportItem::AllCaps(s) => Some(s),
|
2017-07-13 18:36:31 +09:00
|
|
|
ImportItem::Invalid => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_u32(&self) -> u32 {
|
|
|
|
match *self {
|
|
|
|
ImportItem::SelfImport(..) => 0,
|
|
|
|
ImportItem::SnakeCase(..) => 1,
|
|
|
|
ImportItem::CamelCase(..) => 2,
|
|
|
|
ImportItem::AllCaps(..) => 3,
|
|
|
|
ImportItem::Invalid => 4,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> PartialOrd for ImportItem<'a> {
|
|
|
|
fn partial_cmp(&self, other: &ImportItem<'a>) -> Option<Ordering> {
|
|
|
|
Some(self.cmp(other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Ord for ImportItem<'a> {
|
|
|
|
fn cmp(&self, other: &ImportItem<'a>) -> Ordering {
|
|
|
|
let res = self.to_u32().cmp(&other.to_u32());
|
|
|
|
if res != Ordering::Equal {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
self.to_str().map_or(Ordering::Greater, |self_str| {
|
|
|
|
other
|
|
|
|
.to_str()
|
|
|
|
.map_or(Ordering::Less, |other_str| self_str.cmp(other_str))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-04 18:09:05 +02:00
|
|
|
// Pretty prints a multi-item import.
|
2017-08-06 23:04:33 -04:00
|
|
|
// If the path list is empty, it leaves the braces empty.
|
2017-11-30 23:55:12 +01:00
|
|
|
fn rewrite_nested_use_tree(
|
2017-06-12 15:58:58 +12:00
|
|
|
shape: Shape,
|
|
|
|
path: &ast::Path,
|
2017-11-30 23:55:12 +01:00
|
|
|
trees: &[(ast::UseTree, ast::NodeId)],
|
2017-06-12 15:58:58 +12:00
|
|
|
span: Span,
|
|
|
|
context: &RewriteContext,
|
|
|
|
) -> Option<String> {
|
2016-06-08 13:00:11 +02:00
|
|
|
// Returns a different option to distinguish `::foo` and `foo`
|
2017-10-05 20:50:19 +09:00
|
|
|
let path_str = rewrite_path(context, PathContext::Import, None, path, shape)?;
|
2015-07-17 15:58:47 +02:00
|
|
|
|
2017-11-30 23:55:12 +01:00
|
|
|
match trees.len() {
|
2017-08-06 23:04:33 -04:00
|
|
|
0 => {
|
|
|
|
return rewrite_path(context, PathContext::Import, None, path, shape)
|
|
|
|
.map(|path_str| format!("{}::{{}}", path_str));
|
|
|
|
}
|
2017-11-30 23:55:12 +01:00
|
|
|
// TODO: fix this
|
|
|
|
1 => return Some(rewrite_nested_use_tree_single(path_str, &trees[0].0)),
|
2015-08-16 15:58:17 +12:00
|
|
|
_ => (),
|
2015-07-17 15:58:47 +02:00
|
|
|
}
|
2015-05-02 19:12:16 +02:00
|
|
|
|
2017-07-13 20:40:49 +09:00
|
|
|
let path_str = if path_str.is_empty() {
|
|
|
|
path_str
|
|
|
|
} else {
|
|
|
|
format!("{}::", path_str)
|
|
|
|
};
|
2017-03-25 22:21:43 -07:00
|
|
|
|
|
|
|
// 2 = "{}"
|
2017-07-13 20:40:49 +09:00
|
|
|
let remaining_width = shape.width.checked_sub(path_str.len() + 2).unwrap_or(0);
|
2015-07-17 15:58:47 +02:00
|
|
|
|
2015-08-19 18:12:05 +02:00
|
|
|
let mut items = {
|
|
|
|
// Dummy value, see explanation below.
|
|
|
|
let mut items = vec![ListItem::from_str("")];
|
2017-06-12 15:58:58 +12:00
|
|
|
let iter = itemize_list(
|
|
|
|
context.codemap,
|
2017-11-30 23:55:12 +01:00
|
|
|
trees.iter().map(|ref tree| &tree.0),
|
2017-06-12 15:58:58 +12:00
|
|
|
"}",
|
2017-11-16 17:38:12 +09:00
|
|
|
",",
|
2017-11-30 23:55:12 +01:00
|
|
|
|tree| tree.span.lo(),
|
|
|
|
|tree| tree.span.hi(),
|
|
|
|
rewrite_nested_use_tree_item,
|
2017-06-12 15:58:58 +12:00
|
|
|
context.codemap.span_after(span, "{"),
|
2017-08-19 21:47:40 +03:00
|
|
|
span.hi(),
|
2017-08-07 17:29:55 +09:00
|
|
|
false,
|
2017-06-12 15:58:58 +12:00
|
|
|
);
|
2015-08-19 18:12:05 +02:00
|
|
|
items.extend(iter);
|
|
|
|
items
|
|
|
|
};
|
2015-07-17 15:58:47 +02:00
|
|
|
|
|
|
|
// We prefixed the item list with a dummy value so that we can
|
|
|
|
// potentially move "self" to the front of the vector without touching
|
|
|
|
// the rest of the items.
|
|
|
|
let has_self = move_self_to_front(&mut items);
|
2016-05-29 17:58:38 +02:00
|
|
|
let first_index = if has_self { 0 } else { 1 };
|
2015-05-02 19:12:16 +02:00
|
|
|
|
2017-05-16 15:47:09 +07:00
|
|
|
if context.config.reorder_imported_names() {
|
2017-07-13 18:36:31 +09:00
|
|
|
items[1..].sort_by(|a, b| {
|
|
|
|
let a = ImportItem::from_opt_str(a.item.as_ref());
|
|
|
|
let b = ImportItem::from_opt_str(b.item.as_ref());
|
|
|
|
a.cmp(&b)
|
|
|
|
});
|
2015-07-17 15:58:47 +02:00
|
|
|
}
|
2015-04-21 21:01:19 +12:00
|
|
|
|
2017-06-12 15:58:58 +12:00
|
|
|
let tactic = definitive_tactic(
|
|
|
|
&items[first_index..],
|
2017-07-13 20:40:49 +09:00
|
|
|
context.config.imports_layout(),
|
2017-07-31 16:23:42 +09:00
|
|
|
Separator::Comma,
|
2017-06-12 15:58:58 +12:00
|
|
|
remaining_width,
|
|
|
|
);
|
2017-03-25 22:21:43 -07:00
|
|
|
|
2017-07-13 20:40:49 +09:00
|
|
|
let nested_indent = match context.config.imports_indent() {
|
|
|
|
IndentStyle::Block => shape.indent.block_indent(context.config),
|
|
|
|
// 1 = `{`
|
|
|
|
IndentStyle::Visual => shape.visual_indent(path_str.len() + 1).indent,
|
|
|
|
};
|
|
|
|
|
|
|
|
let nested_shape = match context.config.imports_indent() {
|
|
|
|
IndentStyle::Block => Shape::indented(nested_indent, context.config),
|
|
|
|
IndentStyle::Visual => Shape::legacy(remaining_width, nested_indent),
|
|
|
|
};
|
|
|
|
|
2017-09-15 12:10:58 +09:00
|
|
|
let ends_with_newline = context.config.imports_indent() == IndentStyle::Block
|
|
|
|
&& tactic != DefinitiveListTactic::Horizontal;
|
2017-07-13 20:40:49 +09:00
|
|
|
|
2015-10-02 13:50:24 +02:00
|
|
|
let fmt = ListFormatting {
|
|
|
|
tactic: tactic,
|
|
|
|
separator: ",",
|
2017-07-13 20:40:49 +09:00
|
|
|
trailing_separator: if ends_with_newline {
|
|
|
|
context.config.trailing_comma()
|
|
|
|
} else {
|
|
|
|
SeparatorTactic::Never
|
|
|
|
},
|
2017-08-18 23:19:47 +09:00
|
|
|
separator_place: SeparatorPlace::Back,
|
2017-07-13 20:40:49 +09:00
|
|
|
shape: nested_shape,
|
|
|
|
ends_with_newline: ends_with_newline,
|
2017-07-26 22:43:36 +09:00
|
|
|
preserve_newline: true,
|
2015-10-02 13:50:24 +02:00
|
|
|
config: context.config,
|
|
|
|
};
|
2017-10-05 20:50:19 +09:00
|
|
|
let list_str = write_list(&items[first_index..], &fmt)?;
|
2015-06-26 03:29:54 +02:00
|
|
|
|
2017-07-26 17:43:17 +09:00
|
|
|
let result = if list_str.contains('\n') && context.config.imports_indent() == IndentStyle::Block
|
|
|
|
{
|
|
|
|
format!(
|
|
|
|
"{}{{\n{}{}\n{}}}",
|
|
|
|
path_str,
|
|
|
|
nested_shape.indent.to_string(context.config),
|
|
|
|
list_str,
|
|
|
|
shape.indent.to_string(context.config)
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
format!("{}{{{}}}", path_str, list_str)
|
|
|
|
};
|
2017-07-13 20:40:49 +09:00
|
|
|
Some(result)
|
2015-04-21 21:01:19 +12:00
|
|
|
}
|
2015-06-26 03:29:54 +02:00
|
|
|
|
|
|
|
// Returns true when self item was found.
|
|
|
|
fn move_self_to_front(items: &mut Vec<ListItem>) -> bool {
|
2017-06-16 08:49:49 +09:00
|
|
|
match items
|
|
|
|
.iter()
|
2017-07-20 00:09:43 +09:00
|
|
|
.position(|item| item.item.as_ref().map(|x| &x[..]) == Some("self"))
|
|
|
|
{
|
2015-06-26 03:29:54 +02:00
|
|
|
Some(pos) => {
|
|
|
|
items[0] = items.remove(pos);
|
|
|
|
true
|
2015-08-16 15:58:17 +12:00
|
|
|
}
|
|
|
|
None => false,
|
2015-06-26 03:29:54 +02:00
|
|
|
}
|
|
|
|
}
|