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 std::cmp::Ordering;
14 use syntax::ast::{self, UseTreeKind};
15 use syntax::codemap::{BytePos, Span};
17 use codemap::SpanUtils;
18 use config::IndentStyle;
19 use lists::{definitive_tactic, itemize_list, write_list, ListFormatting, ListItem, Separator};
20 use rewrite::{Rewrite, RewriteContext};
24 use visitor::FmtVisitor;
28 /// Returns a name imported by a `use` declaration. e.g. returns `Ordering`
29 /// for `std::cmp::Ordering` and `self` for `std::cmp::self`.
30 pub fn path_to_imported_ident(path: &ast::Path) -> ast::Ident {
31 path.segments.last().unwrap().ident
34 impl<'a> FmtVisitor<'a> {
35 pub fn format_import(&mut self, item: &ast::Item, tree: &ast::UseTree) {
37 let shape = self.shape();
38 let rw = UseTree::from_ast(
42 Some(item.vis.clone()),
44 Some(item.attrs.clone()),
45 ).rewrite_top_level(&self.get_context(), shape);
47 Some(ref s) if s.is_empty() => {
48 // Format up to last newline
49 let prev_span = mk_sp(self.last_pos, source!(self, span).lo());
50 let trimmed_snippet = self.snippet(prev_span).trim_right();
51 let span_end = self.last_pos + BytePos(trimmed_snippet.len() as u32);
52 self.format_missing(span_end);
53 // We have an excessive newline from the removed import.
54 if self.buffer.ends_with('\n') {
56 self.line_number -= 1;
58 self.last_pos = source!(self, span).hi();
61 self.format_missing_with_indent(source!(self, span).lo());
63 self.last_pos = source!(self, span).hi();
66 self.format_missing_with_indent(source!(self, span).lo());
67 self.format_missing(source!(self, span).hi());
73 // Ordering of imports
75 // We order imports by translating to our own representation and then sorting.
76 // The Rust AST data structures are really bad for this. Rustfmt applies a bunch
77 // of normalisations to imports and since we want to sort based on the result
78 // of these (and to maintain idempotence) we must apply the same normalisations
79 // to the data structures for sorting.
81 // We sort `self` and `super` before other imports, then identifier imports,
82 // then glob imports, then lists of imports. We do not take aliases into account
83 // when ordering unless the imports are identical except for the alias (rare in
86 // FIXME(#2531) - we should unify the comparison code here with the formatting
87 // code elsewhere since we are essentially string-ifying twice. Furthermore, by
88 // parsing to our own format on comparison, we repeat a lot of work when
91 // FIXME we do a lot of allocation to make our own representation.
92 #[derive(Debug, Clone, Eq, PartialEq)]
94 Ident(String, Option<String>),
96 Super(Option<String>),
101 #[derive(Debug, Clone)]
103 pub path: Vec<UseSegment>,
105 // Comment information within nested use tree.
106 list_item: Option<ListItem>,
107 // Additional fields for top level use items.
108 // Should we have another struct for top-level use items rather than reusing this?
109 visibility: Option<ast::Visibility>,
110 attrs: Option<Vec<ast::Attribute>>,
113 impl PartialEq for UseTree {
114 fn eq(&self, other: &UseTree) -> bool {
115 self.path == other.path
118 impl Eq for UseTree {}
121 // Clone a version of self with any top-level alias removed.
122 fn remove_alias(&self) -> UseSegment {
124 UseSegment::Ident(ref s, _) => UseSegment::Ident(s.clone(), None),
125 UseSegment::Slf(_) => UseSegment::Slf(None),
126 UseSegment::Super(_) => UseSegment::Super(None),
131 fn from_path_segment(path_seg: &ast::PathSegment) -> Option<UseSegment> {
132 let name = path_seg.ident.name.as_str();
133 if name == "{{root}}" {
136 Some(if name == "self" {
137 UseSegment::Slf(None)
138 } else if name == "super" {
139 UseSegment::Super(None)
141 UseSegment::Ident((*name).to_owned(), None)
147 // Rewrite use tree with `use ` and a trailing `;`.
148 pub fn rewrite_top_level(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
149 let mut result = String::with_capacity(256);
150 if let Some(ref attrs) = self.attrs {
151 result.push_str(&attrs.rewrite(context, shape)?);
152 if !result.is_empty() {
153 result.push_str(&shape.indent.to_string_with_newline(context.config));
157 let vis = self.visibility
159 .map_or(Cow::from(""), |vis| ::utils::format_visibility(&vis));
160 result.push_str(&self.rewrite(context, shape.offset_left(vis.len())?)
165 format!("{}use {};", vis, s)
171 pub fn from_ast_with_normalization(
172 context: &RewriteContext,
174 ) -> Option<UseTree> {
176 ast::ItemKind::Use(ref use_tree) => Some(
181 Some(item.vis.clone()),
182 Some(item.span().lo()),
183 if item.attrs.is_empty() {
186 Some(item.attrs.clone())
195 context: &RewriteContext,
197 list_item: Option<ListItem>,
198 visibility: Option<ast::Visibility>,
199 opt_lo: Option<BytePos>,
200 attrs: Option<Vec<ast::Attribute>>,
202 let span = if let Some(lo) = opt_lo {
203 mk_sp(lo, a.span.hi())
207 let mut result = UseTree {
214 for p in &a.prefix.segments {
215 if let Some(use_segment) = UseSegment::from_path_segment(p) {
216 result.path.push(use_segment);
220 UseTreeKind::Glob => {
221 result.path.push(UseSegment::Glob);
223 UseTreeKind::Nested(ref list) => {
224 // Extract comments between nested use items.
225 // This needs to be done before sorting use items.
226 let items: Vec<_> = itemize_list(
227 context.snippet_provider,
228 list.iter().map(|(tree, _)| tree),
231 |tree| tree.span.lo(),
232 |tree| tree.span.hi(),
233 |_| Some("".to_owned()), // We only need comments for now.
234 context.snippet_provider.span_after(a.span, "{"),
238 result.path.push(UseSegment::List(
240 .zip(items.into_iter())
241 .map(|(t, list_item)| {
242 Self::from_ast(context, &t.0, Some(list_item), None, None, None)
247 UseTreeKind::Simple(ref rename) => {
248 let mut name = (*path_to_imported_ident(&a.prefix).name.as_str()).to_owned();
249 let alias = rename.and_then(|ident| {
250 if ident == path_to_imported_ident(&a.prefix) {
253 Some(ident.to_string())
257 let segment = if &name == "self" {
258 UseSegment::Slf(alias)
259 } else if &name == "super" {
260 UseSegment::Super(alias)
262 UseSegment::Ident(name, alias)
265 // `name` is already in result.
267 result.path.push(segment);
273 // Do the adjustments that rustfmt does elsewhere to use paths.
274 pub fn normalize(mut self) -> UseTree {
275 let mut last = self.path.pop().expect("Empty use tree?");
276 // Hack around borrow checker.
277 let mut normalize_sole_list = false;
278 let mut aliased_self = false;
280 // Remove foo::{} or self without attributes.
282 _ if self.attrs.is_some() => (),
283 UseSegment::List(ref list) if list.is_empty() => {
287 UseSegment::Slf(None) if self.path.is_empty() && self.visibility.is_some() => {
294 // Normalise foo::self -> foo.
295 if let UseSegment::Slf(None) = last {
296 if self.path.len() > 0 {
301 // Normalise foo::self as bar -> foo as bar.
302 if let UseSegment::Slf(_) = last {
303 match self.path.last() {
305 Some(UseSegment::Ident(_, None)) => {
312 let mut done = false;
314 match self.path.last_mut() {
315 Some(UseSegment::Ident(_, ref mut old_rename)) => {
316 assert!(old_rename.is_none());
317 if let UseSegment::Slf(Some(rename)) = last.clone() {
318 *old_rename = Some(rename);
330 // Normalise foo::{bar} -> foo::bar
331 if let UseSegment::List(ref list) = last {
333 normalize_sole_list = true;
337 if normalize_sole_list {
339 UseSegment::List(list) => {
340 for seg in &list[0].path {
341 self.path.push(seg.clone());
343 return self.normalize();
349 // Recursively normalize elements of a list use (including sorting the list).
350 if let UseSegment::List(list) = last {
351 let mut list = list.into_iter()
352 .map(|ut| ut.normalize())
353 .collect::<Vec<_>>();
355 last = UseSegment::List(list);
358 self.path.push(last);
363 impl PartialOrd for UseSegment {
364 fn partial_cmp(&self, other: &UseSegment) -> Option<Ordering> {
365 Some(self.cmp(other))
368 impl PartialOrd for UseTree {
369 fn partial_cmp(&self, other: &UseTree) -> Option<Ordering> {
370 Some(self.cmp(other))
373 impl Ord for UseSegment {
374 fn cmp(&self, other: &UseSegment) -> Ordering {
375 use self::UseSegment::*;
377 fn is_upper_snake_case(s: &str) -> bool {
378 s.chars().all(|c| c.is_uppercase() || c == '_')
381 match (self, other) {
382 (&Slf(ref a), &Slf(ref b)) | (&Super(ref a), &Super(ref b)) => a.cmp(b),
383 (&Glob, &Glob) => Ordering::Equal,
384 (&Ident(ref ia, ref aa), &Ident(ref ib, ref ab)) => {
385 // snake_case < CamelCase < UPPER_SNAKE_CASE
386 if ia.starts_with(char::is_uppercase) && ib.starts_with(char::is_lowercase) {
387 return Ordering::Greater;
389 if ia.starts_with(char::is_lowercase) && ib.starts_with(char::is_uppercase) {
390 return Ordering::Less;
392 if is_upper_snake_case(ia) && !is_upper_snake_case(ib) {
393 return Ordering::Greater;
395 if !is_upper_snake_case(ia) && is_upper_snake_case(ib) {
396 return Ordering::Less;
398 let ident_ord = ia.cmp(ib);
399 if ident_ord != Ordering::Equal {
402 if aa.is_none() && ab.is_some() {
403 return Ordering::Less;
405 if aa.is_some() && ab.is_none() {
406 return Ordering::Greater;
410 (&List(ref a), &List(ref b)) => {
411 for (a, b) in a.iter().zip(b.iter()) {
413 if ord != Ordering::Equal {
418 a.len().cmp(&b.len())
420 (&Slf(_), _) => Ordering::Less,
421 (_, &Slf(_)) => Ordering::Greater,
422 (&Super(_), _) => Ordering::Less,
423 (_, &Super(_)) => Ordering::Greater,
424 (&Ident(..), _) => Ordering::Less,
425 (_, &Ident(..)) => Ordering::Greater,
426 (&Glob, _) => Ordering::Less,
427 (_, &Glob) => Ordering::Greater,
431 impl Ord for UseTree {
432 fn cmp(&self, other: &UseTree) -> Ordering {
433 for (a, b) in self.path.iter().zip(other.path.iter()) {
435 // The comparison without aliases is a hack to avoid situations like
436 // comparing `a::b` to `a as c` - where the latter should be ordered
437 // first since it is shorter.
438 if ord != Ordering::Equal && a.remove_alias().cmp(&b.remove_alias()) != Ordering::Equal
444 self.path.len().cmp(&other.path.len())
448 fn rewrite_nested_use_tree(
449 context: &RewriteContext,
450 use_tree_list: &[UseTree],
452 ) -> Option<String> {
453 let mut list_items = Vec::with_capacity(use_tree_list.len());
454 let nested_shape = match context.config.imports_indent() {
455 IndentStyle::Block => shape
456 .block_indent(context.config.tab_spaces())
457 .with_max_width(context.config)
459 IndentStyle::Visual => shape.visual_indent(0),
461 for use_tree in use_tree_list {
462 let mut list_item = use_tree.list_item.clone()?;
463 list_item.item = use_tree.rewrite(context, nested_shape);
464 list_items.push(list_item);
466 let (tactic, remaining_width) = if use_tree_list.iter().any(|use_segment| {
470 .map_or(false, |last_segment| match last_segment {
471 UseSegment::List(..) => true,
475 (DefinitiveListTactic::Vertical, 0)
477 let remaining_width = shape.width.checked_sub(2).unwrap_or(0);
478 let tactic = definitive_tactic(
480 context.config.imports_layout(),
484 (tactic, remaining_width)
487 let ends_with_newline = context.config.imports_indent() == IndentStyle::Block
488 && tactic != DefinitiveListTactic::Horizontal;
489 let fmt = ListFormatting {
492 trailing_separator: if ends_with_newline {
493 context.config.trailing_comma()
495 SeparatorTactic::Never
497 separator_place: SeparatorPlace::Back,
500 preserve_newline: true,
501 config: context.config,
504 let list_str = write_list(&list_items, &fmt)?;
506 let result = if (list_str.contains('\n') || list_str.len() > remaining_width)
507 && context.config.imports_indent() == IndentStyle::Block
511 nested_shape.indent.to_string(context.config),
513 shape.indent.to_string(context.config)
516 format!("{{{}}}", list_str)
522 impl Rewrite for UseSegment {
523 fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
525 UseSegment::Ident(ref ident, Some(ref rename)) => format!("{} as {}", ident, rename),
526 UseSegment::Ident(ref ident, None) => ident.clone(),
527 UseSegment::Slf(Some(ref rename)) => format!("self as {}", rename),
528 UseSegment::Slf(None) => "self".to_owned(),
529 UseSegment::Super(Some(ref rename)) => format!("super as {}", rename),
530 UseSegment::Super(None) => "super".to_owned(),
531 UseSegment::Glob => "*".to_owned(),
532 UseSegment::List(ref use_tree_list) => rewrite_nested_use_tree(
536 shape.offset_left(1)?.sub_width(1)?,
542 impl Rewrite for UseTree {
543 // This does NOT format attributes and visibility or add a trailing `;`.
544 fn rewrite(&self, context: &RewriteContext, mut shape: Shape) -> Option<String> {
545 let mut result = String::with_capacity(256);
546 let mut iter = self.path.iter().peekable();
547 while let Some(ref segment) = iter.next() {
548 let segment_str = segment.rewrite(context, shape)?;
549 result.push_str(&segment_str);
550 if iter.peek().is_some() {
551 result.push_str("::");
553 shape = shape.offset_left(2 + segment_str.len())?;
563 use syntax::codemap::DUMMY_SP;
565 // Parse the path part of an import. This parser is not robust and is only
566 // suitable for use in a test harness.
567 fn parse_use_tree(s: &str) -> UseTree {
568 use std::iter::Peekable;
573 input: Peekable<Chars<'a>>,
576 impl<'a> Parser<'a> {
578 self.input.next().unwrap();
580 fn eat(&mut self, c: char) {
581 assert!(self.input.next().unwrap() == c);
584 result: &mut Vec<UseSegment>,
586 alias_buf: &mut Option<String>,
589 let mut alias = None;
590 swap(alias_buf, &mut alias);
592 result.push(UseSegment::Slf(alias));
593 *buf = String::new();
595 } else if buf == "super" {
596 result.push(UseSegment::Super(alias));
597 *buf = String::new();
600 let mut name = String::new();
601 swap(buf, &mut name);
602 result.push(UseSegment::Ident(name, alias));
606 fn parse_in_list(&mut self) -> UseTree {
607 let mut result = vec![];
608 let mut buf = String::new();
609 let mut alias_buf = None;
610 while let Some(&c) = self.input.peek() {
613 assert!(buf.is_empty());
615 result.push(UseSegment::List(self.parse_list()));
619 assert!(buf.is_empty());
621 result.push(UseSegment::Glob);
626 Self::push_segment(&mut result, &mut buf, &mut alias_buf);
629 Self::push_segment(&mut result, &mut buf, &mut alias_buf);
643 alias_buf = Some(String::new());
647 if let Some(ref mut buf) = alias_buf {
655 Self::push_segment(&mut result, &mut buf, &mut alias_buf);
665 fn parse_list(&mut self) -> Vec<UseTree> {
666 let mut result = vec![];
668 match self.input.peek().unwrap() {
669 ',' | ' ' => self.bump(),
673 _ => result.push(self.parse_in_list()),
679 let mut parser = Parser {
680 input: s.chars().peekable(),
682 parser.parse_in_list()
686 fn test_use_tree_normalize() {
687 assert_eq!(parse_use_tree("a::self").normalize(), parse_use_tree("a"));
689 parse_use_tree("a::self as foo").normalize(),
690 parse_use_tree("a as foo")
692 assert_eq!(parse_use_tree("a::{self}").normalize(), parse_use_tree("a"));
693 assert_eq!(parse_use_tree("a::{b}").normalize(), parse_use_tree("a::b"));
695 parse_use_tree("a::{b, c::self}").normalize(),
696 parse_use_tree("a::{b, c}")
699 parse_use_tree("a::{b as bar, c::self}").normalize(),
700 parse_use_tree("a::{b as bar, c}")
705 fn test_use_tree_ord() {
706 assert!(parse_use_tree("a").normalize() < parse_use_tree("aa").normalize());
707 assert!(parse_use_tree("a").normalize() < parse_use_tree("a::a").normalize());
708 assert!(parse_use_tree("a").normalize() < parse_use_tree("*").normalize());
709 assert!(parse_use_tree("a").normalize() < parse_use_tree("{a, b}").normalize());
710 assert!(parse_use_tree("*").normalize() < parse_use_tree("{a, b}").normalize());
713 parse_use_tree("aaaaaaaaaaaaaaa::{bb, cc, dddddddd}").normalize()
714 < parse_use_tree("aaaaaaaaaaaaaaa::{bb, cc, ddddddddd}").normalize()
717 parse_use_tree("serde::de::{Deserialize}").normalize()
718 < parse_use_tree("serde_json").normalize()
720 assert!(parse_use_tree("a::b::c").normalize() < parse_use_tree("a::b::*").normalize());
722 parse_use_tree("foo::{Bar, Baz}").normalize()
723 < parse_use_tree("{Bar, Baz}").normalize()
727 parse_use_tree("foo::{self as bar}").normalize()
728 < parse_use_tree("foo::{qux as bar}").normalize()
731 parse_use_tree("foo::{qux as bar}").normalize()
732 < parse_use_tree("foo::{baz, qux as bar}").normalize()
735 parse_use_tree("foo::{self as bar, baz}").normalize()
736 < parse_use_tree("foo::{baz, qux as bar}").normalize()
739 assert!(parse_use_tree("foo").normalize() < parse_use_tree("Foo").normalize());
740 assert!(parse_use_tree("foo").normalize() < parse_use_tree("foo::Bar").normalize());
743 parse_use_tree("std::cmp::{d, c, b, a}").normalize()
744 < parse_use_tree("std::cmp::{b, e, g, f}").normalize()