]> git.lizzy.rs Git - rust.git/blobdiff - src/imports.rs
Change `print_diff` to output the correct line number.
[rust.git] / src / imports.rs
index 8963f62748b99c011c5a0828163b8ab16e557c51..3ca063a58e72655118401627bac52e3aff59f8ff 100644 (file)
 
 use config::lists::*;
 use syntax::ast::{self, UseTreeKind};
-use syntax::codemap::{self, BytePos, Span, DUMMY_SP};
+use syntax::source_map::{self, BytePos, Span, DUMMY_SP};
 
-use codemap::SpanUtils;
 use comment::combine_strs_with_missing_comments;
-use config::IndentStyle;
+use config::{Edition, IndentStyle};
 use lists::{definitive_tactic, itemize_list, write_list, ListFormatting, ListItem, Separator};
 use rewrite::{Rewrite, RewriteContext};
 use shape::Shape;
+use source_map::SpanUtils;
 use spanned::Spanned;
-use utils::mk_sp;
+use utils::{is_same_visibility, mk_sp, rewrite_ident};
 use visitor::FmtVisitor;
 
 use std::borrow::Cow;
@@ -35,7 +35,7 @@ pub fn path_to_imported_ident(path: &ast::Path) -> ast::Ident {
 
 impl<'a> FmtVisitor<'a> {
     pub fn format_import(&mut self, item: &ast::Item, tree: &ast::UseTree) {
-        let span = item.span;
+        let span = item.span();
         let shape = self.shape();
         let rw = UseTree::from_ast(
             &self.get_context(),
@@ -141,17 +141,22 @@ fn remove_alias(&self) -> UseSegment {
         }
     }
 
-    fn from_path_segment(path_seg: &ast::PathSegment) -> Option<UseSegment> {
-        let name = path_seg.ident.name.as_str();
-        if name == "{{root}}" {
+    fn from_path_segment(
+        context: &RewriteContext,
+        path_seg: &ast::PathSegment,
+        modsep: bool,
+    ) -> Option<UseSegment> {
+        let name = rewrite_ident(context, path_seg.ident);
+        if name.is_empty() || name == "{{root}}" {
             return None;
         }
-        Some(if name == "self" {
-            UseSegment::Slf(None)
-        } else if name == "super" {
-            UseSegment::Super(None)
-        } else {
-            UseSegment::Ident((*name).to_owned(), None)
+        Some(match name {
+            "self" => UseSegment::Slf(None),
+            "super" => UseSegment::Super(None),
+            _ => {
+                let mod_sep = if modsep { "::" } else { "" };
+                UseSegment::Ident(format!("{}{}", mod_sep, name), None)
+            }
         })
     }
 }
@@ -174,7 +179,7 @@ pub fn merge_use_trees(use_trees: Vec<UseTree>) -> Vec<UseTree> {
 fn merge_use_trees_inner(trees: &mut Vec<UseTree>, use_tree: UseTree) {
     for tree in trees.iter_mut() {
         if tree.share_prefix(&use_tree) {
-            tree.merge(use_tree);
+            tree.merge(&use_tree);
             return;
         }
     }
@@ -231,10 +236,11 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
 impl UseTree {
     // Rewrite use tree with `use ` and a trailing `;`.
     pub fn rewrite_top_level(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
-        let vis = self.visibility
-            .as_ref()
-            .map_or(Cow::from(""), |vis| ::utils::format_visibility(&vis));
-        let use_str = self.rewrite(context, shape.offset_left(vis.len())?)
+        let vis = self.visibility.as_ref().map_or(Cow::from(""), |vis| {
+            ::utils::format_visibility(context, &vis)
+        });
+        let use_str = self
+            .rewrite(context, shape.offset_left(vis.len())?)
             .map(|s| {
                 if s.is_empty() {
                     s.to_owned()
@@ -311,8 +317,13 @@ fn from_ast(
             visibility,
             attrs,
         };
+
+        let leading_modsep = context.config.edition() == Edition::Edition2018
+            && a.prefix.to_string().len() > 2
+            && a.prefix.to_string().starts_with("::");
+
         for p in &a.prefix.segments {
-            if let Some(use_segment) = UseSegment::from_path_segment(p) {
+            if let Some(use_segment) = UseSegment::from_path_segment(context, p, leading_modsep) {
                 result.path.push(use_segment);
             }
         }
@@ -340,26 +351,25 @@ fn from_ast(
                         .zip(items.into_iter())
                         .map(|(t, list_item)| {
                             Self::from_ast(context, &t.0, Some(list_item), None, None, None)
-                        })
-                        .collect(),
+                        }).collect(),
                 ));
             }
-            UseTreeKind::Simple(ref rename) => {
-                let mut name = (*path_to_imported_ident(&a.prefix).name.as_str()).to_owned();
+            UseTreeKind::Simple(ref rename, ..) => {
+                let name = rewrite_ident(context, path_to_imported_ident(&a.prefix)).to_owned();
                 let alias = rename.and_then(|ident| {
-                    if ident == path_to_imported_ident(&a.prefix) {
+                    if ident.name == "_" {
+                        // for impl-only-use
+                        Some("_".to_owned())
+                    } else if ident == path_to_imported_ident(&a.prefix) {
                         None
                     } else {
-                        Some(ident.to_string())
+                        Some(rewrite_ident(context, ident).to_owned())
                     }
                 });
-
-                let segment = if &name == "self" {
-                    UseSegment::Slf(alias)
-                } else if &name == "super" {
-                    UseSegment::Super(alias)
-                } else {
-                    UseSegment::Ident(name, alias)
+                let segment = match name.as_ref() {
+                    "self" => UseSegment::Slf(alias),
+                    "super" => UseSegment::Super(alias),
+                    _ => UseSegment::Ident(name, alias),
                 };
 
                 // `name` is already in result.
@@ -448,7 +458,8 @@ pub fn normalize(mut self) -> UseTree {
 
         // Recursively normalize elements of a list use (including sorting the list).
         if let UseSegment::List(list) = last {
-            let mut list = list.into_iter()
+            let mut list = list
+                .into_iter()
                 .map(|ut| ut.normalize())
                 .collect::<Vec<_>>();
             list.sort();
@@ -466,7 +477,7 @@ fn has_comment(&self) -> bool {
     fn same_visibility(&self, other: &UseTree) -> bool {
         match (&self.visibility, &other.visibility) {
             (
-                Some(codemap::Spanned {
+                Some(source_map::Spanned {
                     node: ast::VisibilityKind::Inherited,
                     ..
                 }),
@@ -474,22 +485,21 @@ fn same_visibility(&self, other: &UseTree) -> bool {
             )
             | (
                 None,
-                Some(codemap::Spanned {
+                Some(source_map::Spanned {
                     node: ast::VisibilityKind::Inherited,
                     ..
                 }),
             )
             | (None, None) => true,
-            (
-                Some(codemap::Spanned { node: lnode, .. }),
-                Some(codemap::Spanned { node: rnode, .. }),
-            ) => lnode == rnode,
+            (Some(ref a), Some(ref b)) => is_same_visibility(a, b),
             _ => false,
         }
     }
 
     fn share_prefix(&self, other: &UseTree) -> bool {
-        if self.path.is_empty() || other.path.is_empty() || self.attrs.is_some()
+        if self.path.is_empty()
+            || other.path.is_empty()
+            || self.attrs.is_some()
             || !self.same_visibility(other)
         {
             false
@@ -507,7 +517,7 @@ fn flatten(self) -> Vec<UseTree> {
                 let prefix = &self.path[..self.path.len() - 1];
                 let mut result = vec![];
                 for nested_use_tree in list {
-                    for mut flattend in &mut nested_use_tree.clone().flatten() {
+                    for flattend in &mut nested_use_tree.clone().flatten() {
                         let mut new_path = prefix.to_vec();
                         new_path.append(&mut flattend.path);
                         result.push(UseTree {
@@ -526,9 +536,10 @@ fn flatten(self) -> Vec<UseTree> {
         }
     }
 
-    fn merge(&mut self, other: UseTree) {
+    fn merge(&mut self, other: &UseTree) {
         let mut new_path = vec![];
-        for (mut a, b) in self.path
+        for (a, b) in self
+            .path
             .clone()
             .iter_mut()
             .zip(other.path.clone().into_iter())
@@ -593,7 +604,8 @@ fn cmp(&self, other: &UseSegment) -> Ordering {
         use self::UseSegment::*;
 
         fn is_upper_snake_case(s: &str) -> bool {
-            s.chars().all(|c| c.is_uppercase() || c == '_')
+            s.chars()
+                .all(|c| c.is_uppercase() || c == '_' || c.is_numeric())
         }
 
         match (self, other) {
@@ -693,35 +705,33 @@ fn rewrite_nested_use_tree(
                 _ => false,
             })
     });
-    let (tactic, remaining_width) = if has_nested_list {
-        (DefinitiveListTactic::Vertical, 0)
+
+    let remaining_width = if has_nested_list {
+        0
     } else {
-        let remaining_width = shape.width.checked_sub(2).unwrap_or(0);
-        let tactic = definitive_tactic(
-            &list_items,
-            context.config.imports_layout(),
-            Separator::Comma,
-            remaining_width,
-        );
-        (tactic, remaining_width)
+        shape.width.saturating_sub(2)
     };
 
+    let tactic = definitive_tactic(
+        &list_items,
+        context.config.imports_layout(),
+        Separator::Comma,
+        remaining_width,
+    );
+
     let ends_with_newline = context.config.imports_indent() == IndentStyle::Block
         && tactic != DefinitiveListTactic::Horizontal;
-    let fmt = ListFormatting {
-        tactic,
-        separator: ",",
-        trailing_separator: if ends_with_newline {
-            context.config.trailing_comma()
-        } else {
-            SeparatorTactic::Never
-        },
-        separator_place: SeparatorPlace::Back,
-        shape: nested_shape,
-        ends_with_newline,
-        preserve_newline: true,
-        config: context.config,
+    let trailing_separator = if ends_with_newline {
+        context.config.trailing_comma()
+    } else {
+        SeparatorTactic::Never
     };
+    let fmt = ListFormatting::new(nested_shape, context.config)
+        .tactic(tactic)
+        .trailing_separator(trailing_separator)
+        .ends_with_newline(ends_with_newline)
+        .preserve_newline(true)
+        .nested(has_nested_list);
 
     let list_str = write_list(&list_items, &fmt)?;
 
@@ -743,7 +753,7 @@ fn rewrite_nested_use_tree(
 
 impl Rewrite for UseSegment {
     fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
-        Some(match *self {
+        Some(match self {
             UseSegment::Ident(ref ident, Some(ref rename)) => format!("{} as {}", ident, rename),
             UseSegment::Ident(ref ident, None) => ident.clone(),
             UseSegment::Slf(Some(ref rename)) => format!("self as {}", rename),
@@ -782,7 +792,7 @@ fn rewrite(&self, context: &RewriteContext, mut shape: Shape) -> Option<String>
 #[cfg(test)]
 mod test {
     use super::*;
-    use syntax::codemap::DUMMY_SP;
+    use syntax::source_map::DUMMY_SP;
 
     // Parse the path part of an import. This parser is not robust and is only
     // suitable for use in a test harness.
@@ -799,9 +809,11 @@ impl<'a> Parser<'a> {
             fn bump(&mut self) {
                 self.input.next().unwrap();
             }
+
             fn eat(&mut self, c: char) {
                 assert!(self.input.next().unwrap() == c);
             }
+
             fn push_segment(
                 result: &mut Vec<UseSegment>,
                 buf: &mut String,
@@ -825,6 +837,7 @@ fn push_segment(
                     }
                 }
             }
+
             fn parse_in_list(&mut self) -> UseTree {
                 let mut result = vec![];
                 let mut buf = String::new();