]> git.lizzy.rs Git - rust.git/blob - src/imports.rs
Fix compile error from breaking changes in libsyntax
[rust.git] / src / imports.rs
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.
4 //
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.
10
11 use std::cmp::Ordering;
12
13 use config::lists::*;
14 use syntax::ast::{self, UseTreeKind};
15 use syntax::codemap::{self, BytePos, Span, DUMMY_SP};
16
17 use codemap::SpanUtils;
18 use comment::combine_strs_with_missing_comments;
19 use config::IndentStyle;
20 use lists::{definitive_tactic, itemize_list, write_list, ListFormatting, ListItem, Separator};
21 use rewrite::{Rewrite, RewriteContext};
22 use shape::Shape;
23 use spanned::Spanned;
24 use utils::{mk_sp, rewrite_ident};
25 use visitor::FmtVisitor;
26
27 use std::borrow::Cow;
28 use std::fmt;
29
30 /// Returns a name imported by a `use` declaration. e.g. returns `Ordering`
31 /// for `std::cmp::Ordering` and `self` for `std::cmp::self`.
32 pub fn path_to_imported_ident(path: &ast::Path) -> ast::Ident {
33     path.segments.last().unwrap().ident
34 }
35
36 impl<'a> FmtVisitor<'a> {
37     pub fn format_import(&mut self, item: &ast::Item, tree: &ast::UseTree) {
38         let span = item.span();
39         let shape = self.shape();
40         let rw = UseTree::from_ast(
41             &self.get_context(),
42             tree,
43             None,
44             Some(item.vis.clone()),
45             Some(item.span.lo()),
46             Some(item.attrs.clone()),
47         ).rewrite_top_level(&self.get_context(), shape);
48         match rw {
49             Some(ref s) if s.is_empty() => {
50                 // Format up to last newline
51                 let prev_span = mk_sp(self.last_pos, source!(self, span).lo());
52                 let trimmed_snippet = self.snippet(prev_span).trim_right();
53                 let span_end = self.last_pos + BytePos(trimmed_snippet.len() as u32);
54                 self.format_missing(span_end);
55                 // We have an excessive newline from the removed import.
56                 if self.buffer.ends_with('\n') {
57                     self.buffer.pop();
58                     self.line_number -= 1;
59                 }
60                 self.last_pos = source!(self, span).hi();
61             }
62             Some(ref s) => {
63                 self.format_missing_with_indent(source!(self, span).lo());
64                 self.push_str(s);
65                 self.last_pos = source!(self, span).hi();
66             }
67             None => {
68                 self.format_missing_with_indent(source!(self, span).lo());
69                 self.format_missing(source!(self, span).hi());
70             }
71         }
72     }
73 }
74
75 // Ordering of imports
76
77 // We order imports by translating to our own representation and then sorting.
78 // The Rust AST data structures are really bad for this. Rustfmt applies a bunch
79 // of normalisations to imports and since we want to sort based on the result
80 // of these (and to maintain idempotence) we must apply the same normalisations
81 // to the data structures for sorting.
82 //
83 // We sort `self` and `super` before other imports, then identifier imports,
84 // then glob imports, then lists of imports. We do not take aliases into account
85 // when ordering unless the imports are identical except for the alias (rare in
86 // practice).
87
88 // FIXME(#2531) - we should unify the comparison code here with the formatting
89 // code elsewhere since we are essentially string-ifying twice. Furthermore, by
90 // parsing to our own format on comparison, we repeat a lot of work when
91 // sorting.
92
93 // FIXME we do a lot of allocation to make our own representation.
94 #[derive(Clone, Eq, PartialEq)]
95 pub enum UseSegment {
96     Ident(String, Option<String>),
97     Slf(Option<String>),
98     Super(Option<String>),
99     Glob,
100     List(Vec<UseTree>),
101 }
102
103 #[derive(Clone)]
104 pub struct UseTree {
105     pub path: Vec<UseSegment>,
106     pub span: Span,
107     // Comment information within nested use tree.
108     pub list_item: Option<ListItem>,
109     // Additional fields for top level use items.
110     // Should we have another struct for top-level use items rather than reusing this?
111     visibility: Option<ast::Visibility>,
112     attrs: Option<Vec<ast::Attribute>>,
113 }
114
115 impl PartialEq for UseTree {
116     fn eq(&self, other: &UseTree) -> bool {
117         self.path == other.path
118     }
119 }
120 impl Eq for UseTree {}
121
122 impl Spanned for UseTree {
123     fn span(&self) -> Span {
124         let lo = if let Some(ref attrs) = self.attrs {
125             attrs.iter().next().map_or(self.span.lo(), |a| a.span.lo())
126         } else {
127             self.span.lo()
128         };
129         mk_sp(lo, self.span.hi())
130     }
131 }
132
133 impl UseSegment {
134     // Clone a version of self with any top-level alias removed.
135     fn remove_alias(&self) -> UseSegment {
136         match *self {
137             UseSegment::Ident(ref s, _) => UseSegment::Ident(s.clone(), None),
138             UseSegment::Slf(_) => UseSegment::Slf(None),
139             UseSegment::Super(_) => UseSegment::Super(None),
140             _ => self.clone(),
141         }
142     }
143
144     fn from_path_segment(
145         context: &RewriteContext,
146         path_seg: &ast::PathSegment,
147     ) -> Option<UseSegment> {
148         let name = rewrite_ident(context, path_seg.ident);
149         if name.is_empty() || name == "{{root}}" {
150             return None;
151         }
152         Some(if name == "self" {
153             UseSegment::Slf(None)
154         } else if name == "super" {
155             UseSegment::Super(None)
156         } else {
157             UseSegment::Ident((*name).to_owned(), None)
158         })
159     }
160 }
161
162 pub fn merge_use_trees(use_trees: Vec<UseTree>) -> Vec<UseTree> {
163     let mut result = Vec::with_capacity(use_trees.len());
164     for use_tree in use_trees {
165         if use_tree.has_comment() || use_tree.attrs.is_some() {
166             result.push(use_tree);
167             continue;
168         }
169
170         for flattened in use_tree.flatten() {
171             merge_use_trees_inner(&mut result, flattened);
172         }
173     }
174     result
175 }
176
177 fn merge_use_trees_inner(trees: &mut Vec<UseTree>, use_tree: UseTree) {
178     for tree in trees.iter_mut() {
179         if tree.share_prefix(&use_tree) {
180             tree.merge(use_tree);
181             return;
182         }
183     }
184
185     trees.push(use_tree);
186 }
187
188 impl fmt::Debug for UseTree {
189     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
190         fmt::Display::fmt(self, f)
191     }
192 }
193
194 impl fmt::Debug for UseSegment {
195     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
196         fmt::Display::fmt(self, f)
197     }
198 }
199
200 impl fmt::Display for UseSegment {
201     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
202         match *self {
203             UseSegment::Glob => write!(f, "*"),
204             UseSegment::Ident(ref s, _) => write!(f, "{}", s),
205             UseSegment::Slf(..) => write!(f, "self"),
206             UseSegment::Super(..) => write!(f, "super"),
207             UseSegment::List(ref list) => {
208                 write!(f, "{{")?;
209                 for (i, item) in list.iter().enumerate() {
210                     let is_last = i == list.len() - 1;
211                     write!(f, "{}", item)?;
212                     if !is_last {
213                         write!(f, ", ")?;
214                     }
215                 }
216                 write!(f, "}}")
217             }
218         }
219     }
220 }
221 impl fmt::Display for UseTree {
222     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
223         for (i, segment) in self.path.iter().enumerate() {
224             let is_last = i == self.path.len() - 1;
225             write!(f, "{}", segment)?;
226             if !is_last {
227                 write!(f, "::")?;
228             }
229         }
230         write!(f, "")
231     }
232 }
233
234 impl UseTree {
235     // Rewrite use tree with `use ` and a trailing `;`.
236     pub fn rewrite_top_level(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
237         let vis = self.visibility.as_ref().map_or(Cow::from(""), |vis| {
238             ::utils::format_visibility(context, &vis)
239         });
240         let use_str = self
241             .rewrite(context, shape.offset_left(vis.len())?)
242             .map(|s| {
243                 if s.is_empty() {
244                     s.to_owned()
245                 } else {
246                     format!("{}use {};", vis, s)
247                 }
248             })?;
249         if let Some(ref attrs) = self.attrs {
250             let attr_str = attrs.rewrite(context, shape)?;
251             let lo = attrs.last().as_ref()?.span().hi();
252             let hi = self.span.lo();
253             let span = mk_sp(lo, hi);
254             combine_strs_with_missing_comments(context, &attr_str, &use_str, span, shape, false)
255         } else {
256             Some(use_str)
257         }
258     }
259
260     // FIXME: Use correct span?
261     // The given span is essentially incorrect, since we are reconstructing
262     // use statements. This should not be a problem, though, since we have
263     // already tried to extract comment and observed that there are no comment
264     // around the given use item, and the span will not be used afterward.
265     fn from_path(path: Vec<UseSegment>, span: Span) -> UseTree {
266         UseTree {
267             path,
268             span,
269             list_item: None,
270             visibility: None,
271             attrs: None,
272         }
273     }
274
275     pub fn from_ast_with_normalization(
276         context: &RewriteContext,
277         item: &ast::Item,
278     ) -> Option<UseTree> {
279         match item.node {
280             ast::ItemKind::Use(ref use_tree) => Some(
281                 UseTree::from_ast(
282                     context,
283                     use_tree,
284                     None,
285                     Some(item.vis.clone()),
286                     Some(item.span.lo()),
287                     if item.attrs.is_empty() {
288                         None
289                     } else {
290                         Some(item.attrs.clone())
291                     },
292                 ).normalize(),
293             ),
294             _ => None,
295         }
296     }
297
298     fn from_ast(
299         context: &RewriteContext,
300         a: &ast::UseTree,
301         list_item: Option<ListItem>,
302         visibility: Option<ast::Visibility>,
303         opt_lo: Option<BytePos>,
304         attrs: Option<Vec<ast::Attribute>>,
305     ) -> UseTree {
306         let span = if let Some(lo) = opt_lo {
307             mk_sp(lo, a.span.hi())
308         } else {
309             a.span
310         };
311         let mut result = UseTree {
312             path: vec![],
313             span,
314             list_item,
315             visibility,
316             attrs,
317         };
318         for p in &a.prefix.segments {
319             if let Some(use_segment) = UseSegment::from_path_segment(context, p) {
320                 result.path.push(use_segment);
321             }
322         }
323         match a.kind {
324             UseTreeKind::Glob => {
325                 result.path.push(UseSegment::Glob);
326             }
327             UseTreeKind::Nested(ref list) => {
328                 // Extract comments between nested use items.
329                 // This needs to be done before sorting use items.
330                 let items: Vec<_> = itemize_list(
331                     context.snippet_provider,
332                     list.iter().map(|(tree, _)| tree),
333                     "}",
334                     ",",
335                     |tree| tree.span.lo(),
336                     |tree| tree.span.hi(),
337                     |_| Some("".to_owned()), // We only need comments for now.
338                     context.snippet_provider.span_after(a.span, "{"),
339                     a.span.hi(),
340                     false,
341                 ).collect();
342                 result.path.push(UseSegment::List(
343                     list.iter()
344                         .zip(items.into_iter())
345                         .map(|(t, list_item)| {
346                             Self::from_ast(context, &t.0, Some(list_item), None, None, None)
347                         })
348                         .collect(),
349                 ));
350             }
351             UseTreeKind::Simple(ref rename, ..) => {
352                 let mut name = rewrite_ident(context, path_to_imported_ident(&a.prefix)).to_owned();
353                 let alias = rename.and_then(|ident| {
354                     if ident == path_to_imported_ident(&a.prefix) {
355                         None
356                     } else {
357                         Some(rewrite_ident(context, ident).to_owned())
358                     }
359                 });
360
361                 let segment = if &name == "self" {
362                     UseSegment::Slf(alias)
363                 } else if &name == "super" {
364                     UseSegment::Super(alias)
365                 } else {
366                     UseSegment::Ident(name, alias)
367                 };
368
369                 // `name` is already in result.
370                 result.path.pop();
371                 result.path.push(segment);
372             }
373         }
374         result
375     }
376
377     // Do the adjustments that rustfmt does elsewhere to use paths.
378     pub fn normalize(mut self) -> UseTree {
379         let mut last = self.path.pop().expect("Empty use tree?");
380         // Hack around borrow checker.
381         let mut normalize_sole_list = false;
382         let mut aliased_self = false;
383
384         // Remove foo::{} or self without attributes.
385         match last {
386             _ if self.attrs.is_some() => (),
387             UseSegment::List(ref list) if list.is_empty() => {
388                 self.path = vec![];
389                 return self;
390             }
391             UseSegment::Slf(None) if self.path.is_empty() && self.visibility.is_some() => {
392                 self.path = vec![];
393                 return self;
394             }
395             _ => (),
396         }
397
398         // Normalise foo::self -> foo.
399         if let UseSegment::Slf(None) = last {
400             if !self.path.is_empty() {
401                 return self;
402             }
403         }
404
405         // Normalise foo::self as bar -> foo as bar.
406         if let UseSegment::Slf(_) = last {
407             match self.path.last() {
408                 None => {}
409                 Some(UseSegment::Ident(_, None)) => {
410                     aliased_self = true;
411                 }
412                 _ => unreachable!(),
413             }
414         }
415
416         let mut done = false;
417         if aliased_self {
418             match self.path.last_mut() {
419                 Some(UseSegment::Ident(_, ref mut old_rename)) => {
420                     assert!(old_rename.is_none());
421                     if let UseSegment::Slf(Some(rename)) = last.clone() {
422                         *old_rename = Some(rename);
423                         done = true;
424                     }
425                 }
426                 _ => unreachable!(),
427             }
428         }
429
430         if done {
431             return self;
432         }
433
434         // Normalise foo::{bar} -> foo::bar
435         if let UseSegment::List(ref list) = last {
436             if list.len() == 1 {
437                 normalize_sole_list = true;
438             }
439         }
440
441         if normalize_sole_list {
442             match last {
443                 UseSegment::List(list) => {
444                     for seg in &list[0].path {
445                         self.path.push(seg.clone());
446                     }
447                     return self.normalize();
448                 }
449                 _ => unreachable!(),
450             }
451         }
452
453         // Recursively normalize elements of a list use (including sorting the list).
454         if let UseSegment::List(list) = last {
455             let mut list = list
456                 .into_iter()
457                 .map(|ut| ut.normalize())
458                 .collect::<Vec<_>>();
459             list.sort();
460             last = UseSegment::List(list);
461         }
462
463         self.path.push(last);
464         self
465     }
466
467     fn has_comment(&self) -> bool {
468         self.list_item.as_ref().map_or(false, ListItem::has_comment)
469     }
470
471     fn same_visibility(&self, other: &UseTree) -> bool {
472         match (&self.visibility, &other.visibility) {
473             (
474                 Some(codemap::Spanned {
475                     node: ast::VisibilityKind::Inherited,
476                     ..
477                 }),
478                 None,
479             )
480             | (
481                 None,
482                 Some(codemap::Spanned {
483                     node: ast::VisibilityKind::Inherited,
484                     ..
485                 }),
486             )
487             | (None, None) => true,
488             (
489                 Some(codemap::Spanned { node: lnode, .. }),
490                 Some(codemap::Spanned { node: rnode, .. }),
491             ) => lnode == rnode,
492             _ => false,
493         }
494     }
495
496     fn share_prefix(&self, other: &UseTree) -> bool {
497         if self.path.is_empty()
498             || other.path.is_empty()
499             || self.attrs.is_some()
500             || !self.same_visibility(other)
501         {
502             false
503         } else {
504             self.path[0] == other.path[0]
505         }
506     }
507
508     fn flatten(self) -> Vec<UseTree> {
509         if self.path.is_empty() {
510             return vec![self];
511         }
512         match self.path.clone().last().unwrap() {
513             UseSegment::List(list) => {
514                 let prefix = &self.path[..self.path.len() - 1];
515                 let mut result = vec![];
516                 for nested_use_tree in list {
517                     for mut flattend in &mut nested_use_tree.clone().flatten() {
518                         let mut new_path = prefix.to_vec();
519                         new_path.append(&mut flattend.path);
520                         result.push(UseTree {
521                             path: new_path,
522                             span: self.span,
523                             list_item: None,
524                             visibility: self.visibility.clone(),
525                             attrs: None,
526                         });
527                     }
528                 }
529
530                 result
531             }
532             _ => vec![self],
533         }
534     }
535
536     fn merge(&mut self, other: UseTree) {
537         let mut new_path = vec![];
538         for (mut a, b) in self
539             .path
540             .clone()
541             .iter_mut()
542             .zip(other.path.clone().into_iter())
543         {
544             if *a == b {
545                 new_path.push(b);
546             } else {
547                 break;
548             }
549         }
550         if let Some(merged) = merge_rest(&self.path, &other.path, new_path.len()) {
551             new_path.push(merged);
552             self.span = self.span.to(other.span);
553         }
554         self.path = new_path;
555     }
556 }
557
558 fn merge_rest(a: &[UseSegment], b: &[UseSegment], len: usize) -> Option<UseSegment> {
559     let a_rest = &a[len..];
560     let b_rest = &b[len..];
561     if a_rest.is_empty() && b_rest.is_empty() {
562         return None;
563     }
564     if a_rest.is_empty() {
565         return Some(UseSegment::List(vec![
566             UseTree::from_path(vec![UseSegment::Slf(None)], DUMMY_SP),
567             UseTree::from_path(b_rest.to_vec(), DUMMY_SP),
568         ]));
569     }
570     if b_rest.is_empty() {
571         return Some(UseSegment::List(vec![
572             UseTree::from_path(vec![UseSegment::Slf(None)], DUMMY_SP),
573             UseTree::from_path(a_rest.to_vec(), DUMMY_SP),
574         ]));
575     }
576     if let UseSegment::List(mut list) = a_rest[0].clone() {
577         merge_use_trees_inner(&mut list, UseTree::from_path(b_rest.to_vec(), DUMMY_SP));
578         list.sort();
579         return Some(UseSegment::List(list.clone()));
580     }
581     let mut list = vec![
582         UseTree::from_path(a_rest.to_vec(), DUMMY_SP),
583         UseTree::from_path(b_rest.to_vec(), DUMMY_SP),
584     ];
585     list.sort();
586     Some(UseSegment::List(list))
587 }
588
589 impl PartialOrd for UseSegment {
590     fn partial_cmp(&self, other: &UseSegment) -> Option<Ordering> {
591         Some(self.cmp(other))
592     }
593 }
594 impl PartialOrd for UseTree {
595     fn partial_cmp(&self, other: &UseTree) -> Option<Ordering> {
596         Some(self.cmp(other))
597     }
598 }
599 impl Ord for UseSegment {
600     fn cmp(&self, other: &UseSegment) -> Ordering {
601         use self::UseSegment::*;
602
603         fn is_upper_snake_case(s: &str) -> bool {
604             s.chars().all(|c| c.is_uppercase() || c == '_')
605         }
606
607         match (self, other) {
608             (&Slf(ref a), &Slf(ref b)) | (&Super(ref a), &Super(ref b)) => a.cmp(b),
609             (&Glob, &Glob) => Ordering::Equal,
610             (&Ident(ref ia, ref aa), &Ident(ref ib, ref ab)) => {
611                 // snake_case < CamelCase < UPPER_SNAKE_CASE
612                 if ia.starts_with(char::is_uppercase) && ib.starts_with(char::is_lowercase) {
613                     return Ordering::Greater;
614                 }
615                 if ia.starts_with(char::is_lowercase) && ib.starts_with(char::is_uppercase) {
616                     return Ordering::Less;
617                 }
618                 if is_upper_snake_case(ia) && !is_upper_snake_case(ib) {
619                     return Ordering::Greater;
620                 }
621                 if !is_upper_snake_case(ia) && is_upper_snake_case(ib) {
622                     return Ordering::Less;
623                 }
624                 let ident_ord = ia.cmp(ib);
625                 if ident_ord != Ordering::Equal {
626                     return ident_ord;
627                 }
628                 if aa.is_none() && ab.is_some() {
629                     return Ordering::Less;
630                 }
631                 if aa.is_some() && ab.is_none() {
632                     return Ordering::Greater;
633                 }
634                 aa.cmp(ab)
635             }
636             (&List(ref a), &List(ref b)) => {
637                 for (a, b) in a.iter().zip(b.iter()) {
638                     let ord = a.cmp(b);
639                     if ord != Ordering::Equal {
640                         return ord;
641                     }
642                 }
643
644                 a.len().cmp(&b.len())
645             }
646             (&Slf(_), _) => Ordering::Less,
647             (_, &Slf(_)) => Ordering::Greater,
648             (&Super(_), _) => Ordering::Less,
649             (_, &Super(_)) => Ordering::Greater,
650             (&Ident(..), _) => Ordering::Less,
651             (_, &Ident(..)) => Ordering::Greater,
652             (&Glob, _) => Ordering::Less,
653             (_, &Glob) => Ordering::Greater,
654         }
655     }
656 }
657 impl Ord for UseTree {
658     fn cmp(&self, other: &UseTree) -> Ordering {
659         for (a, b) in self.path.iter().zip(other.path.iter()) {
660             let ord = a.cmp(b);
661             // The comparison without aliases is a hack to avoid situations like
662             // comparing `a::b` to `a as c` - where the latter should be ordered
663             // first since it is shorter.
664             if ord != Ordering::Equal && a.remove_alias().cmp(&b.remove_alias()) != Ordering::Equal
665             {
666                 return ord;
667             }
668         }
669
670         self.path.len().cmp(&other.path.len())
671     }
672 }
673
674 fn rewrite_nested_use_tree(
675     context: &RewriteContext,
676     use_tree_list: &[UseTree],
677     shape: Shape,
678 ) -> Option<String> {
679     let mut list_items = Vec::with_capacity(use_tree_list.len());
680     let nested_shape = match context.config.imports_indent() {
681         IndentStyle::Block => shape
682             .block_indent(context.config.tab_spaces())
683             .with_max_width(context.config)
684             .sub_width(1)?,
685         IndentStyle::Visual => shape.visual_indent(0),
686     };
687     for use_tree in use_tree_list {
688         if let Some(mut list_item) = use_tree.list_item.clone() {
689             list_item.item = use_tree.rewrite(context, nested_shape);
690             list_items.push(list_item);
691         } else {
692             list_items.push(ListItem::from_str(use_tree.rewrite(context, nested_shape)?));
693         }
694     }
695     let has_nested_list = use_tree_list.iter().any(|use_segment| {
696         use_segment
697             .path
698             .last()
699             .map_or(false, |last_segment| match last_segment {
700                 UseSegment::List(..) => true,
701                 _ => false,
702             })
703     });
704
705     let remaining_width = if has_nested_list {
706         0
707     } else {
708         shape.width.saturating_sub(2)
709     };
710
711     let tactic = definitive_tactic(
712         &list_items,
713         context.config.imports_layout(),
714         Separator::Comma,
715         remaining_width,
716     );
717
718     let ends_with_newline = context.config.imports_indent() == IndentStyle::Block
719         && tactic != DefinitiveListTactic::Horizontal;
720     let fmt = ListFormatting {
721         tactic,
722         separator: ",",
723         trailing_separator: if ends_with_newline {
724             context.config.trailing_comma()
725         } else {
726             SeparatorTactic::Never
727         },
728         separator_place: SeparatorPlace::Back,
729         shape: nested_shape,
730         ends_with_newline,
731         preserve_newline: true,
732         nested: has_nested_list,
733         config: context.config,
734     };
735
736     let list_str = write_list(&list_items, &fmt)?;
737
738     let result = if (list_str.contains('\n') || list_str.len() > remaining_width)
739         && context.config.imports_indent() == IndentStyle::Block
740     {
741         format!(
742             "{{\n{}{}\n{}}}",
743             nested_shape.indent.to_string(context.config),
744             list_str,
745             shape.indent.to_string(context.config)
746         )
747     } else {
748         format!("{{{}}}", list_str)
749     };
750
751     Some(result)
752 }
753
754 impl Rewrite for UseSegment {
755     fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
756         Some(match *self {
757             UseSegment::Ident(ref ident, Some(ref rename)) => format!("{} as {}", ident, rename),
758             UseSegment::Ident(ref ident, None) => ident.clone(),
759             UseSegment::Slf(Some(ref rename)) => format!("self as {}", rename),
760             UseSegment::Slf(None) => "self".to_owned(),
761             UseSegment::Super(Some(ref rename)) => format!("super as {}", rename),
762             UseSegment::Super(None) => "super".to_owned(),
763             UseSegment::Glob => "*".to_owned(),
764             UseSegment::List(ref use_tree_list) => rewrite_nested_use_tree(
765                 context,
766                 use_tree_list,
767                 // 1 = "{" and "}"
768                 shape.offset_left(1)?.sub_width(1)?,
769             )?,
770         })
771     }
772 }
773
774 impl Rewrite for UseTree {
775     // This does NOT format attributes and visibility or add a trailing `;`.
776     fn rewrite(&self, context: &RewriteContext, mut shape: Shape) -> Option<String> {
777         let mut result = String::with_capacity(256);
778         let mut iter = self.path.iter().peekable();
779         while let Some(ref segment) = iter.next() {
780             let segment_str = segment.rewrite(context, shape)?;
781             result.push_str(&segment_str);
782             if iter.peek().is_some() {
783                 result.push_str("::");
784                 // 2 = "::"
785                 shape = shape.offset_left(2 + segment_str.len())?;
786             }
787         }
788         Some(result)
789     }
790 }
791
792 #[cfg(test)]
793 mod test {
794     use super::*;
795     use syntax::codemap::DUMMY_SP;
796
797     // Parse the path part of an import. This parser is not robust and is only
798     // suitable for use in a test harness.
799     fn parse_use_tree(s: &str) -> UseTree {
800         use std::iter::Peekable;
801         use std::mem::swap;
802         use std::str::Chars;
803
804         struct Parser<'a> {
805             input: Peekable<Chars<'a>>,
806         }
807
808         impl<'a> Parser<'a> {
809             fn bump(&mut self) {
810                 self.input.next().unwrap();
811             }
812
813             fn eat(&mut self, c: char) {
814                 assert!(self.input.next().unwrap() == c);
815             }
816
817             fn push_segment(
818                 result: &mut Vec<UseSegment>,
819                 buf: &mut String,
820                 alias_buf: &mut Option<String>,
821             ) {
822                 if !buf.is_empty() {
823                     let mut alias = None;
824                     swap(alias_buf, &mut alias);
825                     if buf == "self" {
826                         result.push(UseSegment::Slf(alias));
827                         *buf = String::new();
828                         *alias_buf = None;
829                     } else if buf == "super" {
830                         result.push(UseSegment::Super(alias));
831                         *buf = String::new();
832                         *alias_buf = None;
833                     } else {
834                         let mut name = String::new();
835                         swap(buf, &mut name);
836                         result.push(UseSegment::Ident(name, alias));
837                     }
838                 }
839             }
840
841             fn parse_in_list(&mut self) -> UseTree {
842                 let mut result = vec![];
843                 let mut buf = String::new();
844                 let mut alias_buf = None;
845                 while let Some(&c) = self.input.peek() {
846                     match c {
847                         '{' => {
848                             assert!(buf.is_empty());
849                             self.bump();
850                             result.push(UseSegment::List(self.parse_list()));
851                             self.eat('}');
852                         }
853                         '*' => {
854                             assert!(buf.is_empty());
855                             self.bump();
856                             result.push(UseSegment::Glob);
857                         }
858                         ':' => {
859                             self.bump();
860                             self.eat(':');
861                             Self::push_segment(&mut result, &mut buf, &mut alias_buf);
862                         }
863                         '}' | ',' => {
864                             Self::push_segment(&mut result, &mut buf, &mut alias_buf);
865                             return UseTree {
866                                 path: result,
867                                 span: DUMMY_SP,
868                                 list_item: None,
869                                 visibility: None,
870                                 attrs: None,
871                             };
872                         }
873                         ' ' => {
874                             self.bump();
875                             self.eat('a');
876                             self.eat('s');
877                             self.eat(' ');
878                             alias_buf = Some(String::new());
879                         }
880                         c => {
881                             self.bump();
882                             if let Some(ref mut buf) = alias_buf {
883                                 buf.push(c);
884                             } else {
885                                 buf.push(c);
886                             }
887                         }
888                     }
889                 }
890                 Self::push_segment(&mut result, &mut buf, &mut alias_buf);
891                 UseTree {
892                     path: result,
893                     span: DUMMY_SP,
894                     list_item: None,
895                     visibility: None,
896                     attrs: None,
897                 }
898             }
899
900             fn parse_list(&mut self) -> Vec<UseTree> {
901                 let mut result = vec![];
902                 loop {
903                     match self.input.peek().unwrap() {
904                         ',' | ' ' => self.bump(),
905                         '}' => {
906                             return result;
907                         }
908                         _ => result.push(self.parse_in_list()),
909                     }
910                 }
911             }
912         }
913
914         let mut parser = Parser {
915             input: s.chars().peekable(),
916         };
917         parser.parse_in_list()
918     }
919
920     macro parse_use_trees($($s:expr),* $(,)*) {
921         vec![
922             $(parse_use_tree($s),)*
923         ]
924     }
925
926     #[test]
927     fn test_use_tree_merge() {
928         macro test_merge([$($input:expr),* $(,)*], [$($output:expr),* $(,)*]) {
929             assert_eq!(
930                 merge_use_trees(parse_use_trees!($($input,)*)),
931                 parse_use_trees!($($output,)*),
932             );
933         }
934
935         test_merge!(["a::b::{c, d}", "a::b::{e, f}"], ["a::b::{c, d, e, f}"]);
936         test_merge!(["a::b::c", "a::b"], ["a::b::{self, c}"]);
937         test_merge!(["a::b", "a::b"], ["a::b"]);
938         test_merge!(["a", "a::b", "a::b::c"], ["a::{self, b::{self, c}}"]);
939         test_merge!(
940             ["a::{b::{self, c}, d::e}", "a::d::f"],
941             ["a::{b::{self, c}, d::{e, f}}"]
942         );
943         test_merge!(
944             ["a::d::f", "a::{b::{self, c}, d::e}"],
945             ["a::{b::{self, c}, d::{e, f}}"]
946         );
947         test_merge!(
948             ["a::{c, d, b}", "a::{d, e, b, a, f}", "a::{f, g, c}"],
949             ["a::{a, b, c, d, e, f, g}"]
950         );
951     }
952
953     #[test]
954     fn test_use_tree_flatten() {
955         assert_eq!(
956             parse_use_tree("a::b::{c, d, e, f}").flatten(),
957             parse_use_trees!("a::b::c", "a::b::d", "a::b::e", "a::b::f",)
958         );
959
960         assert_eq!(
961             parse_use_tree("a::b::{c::{d, e, f}, g, h::{i, j, k}}").flatten(),
962             parse_use_trees![
963                 "a::b::c::d",
964                 "a::b::c::e",
965                 "a::b::c::f",
966                 "a::b::g",
967                 "a::b::h::i",
968                 "a::b::h::j",
969                 "a::b::h::k",
970             ]
971         );
972     }
973
974     #[test]
975     fn test_use_tree_normalize() {
976         assert_eq!(parse_use_tree("a::self").normalize(), parse_use_tree("a"));
977         assert_eq!(
978             parse_use_tree("a::self as foo").normalize(),
979             parse_use_tree("a as foo")
980         );
981         assert_eq!(parse_use_tree("a::{self}").normalize(), parse_use_tree("a"));
982         assert_eq!(parse_use_tree("a::{b}").normalize(), parse_use_tree("a::b"));
983         assert_eq!(
984             parse_use_tree("a::{b, c::self}").normalize(),
985             parse_use_tree("a::{b, c}")
986         );
987         assert_eq!(
988             parse_use_tree("a::{b as bar, c::self}").normalize(),
989             parse_use_tree("a::{b as bar, c}")
990         );
991     }
992
993     #[test]
994     fn test_use_tree_ord() {
995         assert!(parse_use_tree("a").normalize() < parse_use_tree("aa").normalize());
996         assert!(parse_use_tree("a").normalize() < parse_use_tree("a::a").normalize());
997         assert!(parse_use_tree("a").normalize() < parse_use_tree("*").normalize());
998         assert!(parse_use_tree("a").normalize() < parse_use_tree("{a, b}").normalize());
999         assert!(parse_use_tree("*").normalize() < parse_use_tree("{a, b}").normalize());
1000
1001         assert!(
1002             parse_use_tree("aaaaaaaaaaaaaaa::{bb, cc, dddddddd}").normalize()
1003                 < parse_use_tree("aaaaaaaaaaaaaaa::{bb, cc, ddddddddd}").normalize()
1004         );
1005         assert!(
1006             parse_use_tree("serde::de::{Deserialize}").normalize()
1007                 < parse_use_tree("serde_json").normalize()
1008         );
1009         assert!(parse_use_tree("a::b::c").normalize() < parse_use_tree("a::b::*").normalize());
1010         assert!(
1011             parse_use_tree("foo::{Bar, Baz}").normalize()
1012                 < parse_use_tree("{Bar, Baz}").normalize()
1013         );
1014
1015         assert!(
1016             parse_use_tree("foo::{self as bar}").normalize()
1017                 < parse_use_tree("foo::{qux as bar}").normalize()
1018         );
1019         assert!(
1020             parse_use_tree("foo::{qux as bar}").normalize()
1021                 < parse_use_tree("foo::{baz, qux as bar}").normalize()
1022         );
1023         assert!(
1024             parse_use_tree("foo::{self as bar, baz}").normalize()
1025                 < parse_use_tree("foo::{baz, qux as bar}").normalize()
1026         );
1027
1028         assert!(parse_use_tree("foo").normalize() < parse_use_tree("Foo").normalize());
1029         assert!(parse_use_tree("foo").normalize() < parse_use_tree("foo::Bar").normalize());
1030
1031         assert!(
1032             parse_use_tree("std::cmp::{d, c, b, a}").normalize()
1033                 < parse_use_tree("std::cmp::{b, e, g, f}").normalize()
1034         );
1035     }
1036 }