]> git.lizzy.rs Git - rust.git/blobdiff - src/reorder.rs
Use `AttrVec` for `Arm`, `FieldDef`, and `Variant`
[rust.git] / src / reorder.rs
index 49a4638f33fc99c572dfbdceaf9d9f9529c33ac9..ac65ff2c1086e853edfd5e10ac355fe93631657c 100644 (file)
@@ -8,15 +8,14 @@
 
 use std::cmp::{Ord, Ordering};
 
-use syntax::{ast, attr, source_map::Span};
+use rustc_ast::ast;
+use rustc_span::{symbol::sym, Span};
 
-use crate::attr::filter_inline_attrs;
-use crate::comment::combine_strs_with_missing_comments;
-use crate::config::Config;
-use crate::imports::{merge_use_trees, UseTree};
+use crate::config::{Config, GroupImportsTactic, ImportGranularity};
+use crate::imports::{flatten_use_trees, merge_use_trees, SharedPrefix, UseSegment, UseTree};
 use crate::items::{is_mod_decl, rewrite_extern_crate, rewrite_mod};
 use crate::lists::{itemize_list, write_list, ListFormatting, ListItem};
-use crate::rewrite::{Rewrite, RewriteContext};
+use crate::rewrite::RewriteContext;
 use crate::shape::Shape;
 use crate::source_map::LineRangeUtils;
 use crate::spanned::Spanned;
 
 /// Choose the ordering between the given two items.
 fn compare_items(a: &ast::Item, b: &ast::Item) -> Ordering {
-    match (&a.node, &b.node) {
+    match (&a.kind, &b.kind) {
         (&ast::ItemKind::Mod(..), &ast::ItemKind::Mod(..)) => {
             a.ident.as_str().cmp(&b.ident.as_str())
         }
         (&ast::ItemKind::ExternCrate(ref a_name), &ast::ItemKind::ExternCrate(ref b_name)) => {
             // `extern crate foo as bar;`
             //               ^^^ Comparing this.
-            let a_orig_name =
-                a_name.map_or_else(|| a.ident.as_str(), syntax_pos::symbol::Symbol::as_str);
-            let b_orig_name =
-                b_name.map_or_else(|| b.ident.as_str(), syntax_pos::symbol::Symbol::as_str);
+            let a_orig_name = a_name.map_or_else(|| a.ident.as_str(), rustc_span::Symbol::as_str);
+            let b_orig_name = b_name.map_or_else(|| b.ident.as_str(), rustc_span::Symbol::as_str);
             let result = a_orig_name.cmp(&b_orig_name);
             if result != Ordering::Equal {
                 return result;
@@ -70,48 +67,23 @@ fn rewrite_reorderable_item(
     item: &ast::Item,
     shape: Shape,
 ) -> Option<String> {
-    let attrs = filter_inline_attrs(&item.attrs, item.span());
-    let attrs_str = attrs.rewrite(context, shape)?;
-
-    let missed_span = if attrs.is_empty() {
-        mk_sp(item.span.lo(), item.span.lo())
-    } else {
-        mk_sp(attrs.last().unwrap().span.hi(), item.span.lo())
-    };
-
-    let item_str = match item.node {
-        ast::ItemKind::ExternCrate(..) => rewrite_extern_crate(context, item)?,
-        ast::ItemKind::Mod(..) => rewrite_mod(context, item),
-        _ => return None,
-    };
-
-    let allow_extend = if attrs.len() == 1 {
-        let line_len = attrs_str.len() + 1 + item_str.len();
-        !attrs.first().unwrap().is_sugared_doc
-            && context.config.inline_attribute_width() >= line_len
-    } else {
-        false
-    };
-
-    combine_strs_with_missing_comments(
-        context,
-        &attrs_str,
-        &item_str,
-        missed_span,
-        shape,
-        allow_extend,
-    )
+    match item.kind {
+        ast::ItemKind::ExternCrate(..) => rewrite_extern_crate(context, item, shape),
+        ast::ItemKind::Mod(..) => rewrite_mod(context, item, shape),
+        _ => None,
+    }
 }
 
-/// Rewrite a list of items with reordering. Every item in `items` must have
-/// the same `ast::ItemKind`.
-fn rewrite_reorderable_items(
+/// Rewrite a list of items with reordering and/or regrouping. Every item
+/// in `items` must have the same `ast::ItemKind`. Whether reordering, regrouping,
+/// or both are done is determined from the `context`.
+fn rewrite_reorderable_or_regroupable_items(
     context: &RewriteContext<'_>,
     reorderable_items: &[&ast::Item],
     shape: Shape,
     span: Span,
 ) -> Option<String> {
-    match reorderable_items[0].node {
+    match reorderable_items[0].kind {
         // FIXME: Remove duplicated code.
         ast::ItemKind::Use(..) => {
             let mut normalized_items: Vec<_> = reorderable_items
@@ -135,22 +107,43 @@ fn rewrite_reorderable_items(
             for (item, list_item) in normalized_items.iter_mut().zip(list_items) {
                 item.list_item = Some(list_item.clone());
             }
-            if context.config.merge_imports() {
-                normalized_items = merge_use_trees(normalized_items);
+            normalized_items = match context.config.imports_granularity() {
+                ImportGranularity::Crate => merge_use_trees(normalized_items, SharedPrefix::Crate),
+                ImportGranularity::Module => {
+                    merge_use_trees(normalized_items, SharedPrefix::Module)
+                }
+                ImportGranularity::Item => flatten_use_trees(normalized_items),
+                ImportGranularity::Preserve => normalized_items,
+            };
+
+            let mut regrouped_items = match context.config.group_imports() {
+                GroupImportsTactic::Preserve => vec![normalized_items],
+                GroupImportsTactic::StdExternalCrate => group_imports(normalized_items),
+            };
+
+            if context.config.reorder_imports() {
+                regrouped_items.iter_mut().for_each(|items| items.sort())
             }
-            normalized_items.sort();
 
             // 4 = "use ", 1 = ";"
             let nested_shape = shape.offset_left(4)?.sub_width(1)?;
-            let item_vec: Vec<_> = normalized_items
+            let item_vec: Vec<_> = regrouped_items
                 .into_iter()
-                .map(|use_tree| ListItem {
-                    item: use_tree.rewrite_top_level(context, nested_shape),
-                    ..use_tree.list_item.unwrap_or_else(ListItem::empty)
+                .filter(|use_group| !use_group.is_empty())
+                .map(|use_group| {
+                    let item_vec: Vec<_> = use_group
+                        .into_iter()
+                        .map(|use_tree| ListItem {
+                            item: use_tree.rewrite_top_level(context, nested_shape),
+                            ..use_tree.list_item.unwrap_or_else(ListItem::empty)
+                        })
+                        .collect();
+                    wrap_reorderable_items(context, &item_vec, nested_shape)
                 })
-                .collect();
+                .collect::<Option<Vec<_>>>()?;
 
-            wrap_reorderable_items(context, &item_vec, nested_shape)
+            let join_string = format!("\n\n{}", shape.indent.to_string(context.config));
+            Some(item_vec.join(&join_string))
         }
         _ => {
             let list_items = itemize_list(
@@ -176,7 +169,35 @@ fn rewrite_reorderable_items(
 }
 
 fn contains_macro_use_attr(item: &ast::Item) -> bool {
-    attr::contains_name(&filter_inline_attrs(&item.attrs, item.span()), "macro_use")
+    crate::attr::contains_name(&item.attrs, sym::macro_use)
+}
+
+/// Divides imports into three groups, corresponding to standard, external
+/// and local imports. Sorts each subgroup.
+fn group_imports(uts: Vec<UseTree>) -> Vec<Vec<UseTree>> {
+    let mut std_imports = Vec::new();
+    let mut external_imports = Vec::new();
+    let mut local_imports = Vec::new();
+
+    for ut in uts.into_iter() {
+        if ut.path.is_empty() {
+            external_imports.push(ut);
+            continue;
+        }
+        match &ut.path[0] {
+            UseSegment::Ident(id, _) => match id.as_ref() {
+                "std" | "alloc" | "core" => std_imports.push(ut),
+                _ => external_imports.push(ut),
+            },
+            UseSegment::Slf(_) | UseSegment::Super(_) | UseSegment::Crate(_) => {
+                local_imports.push(ut)
+            }
+            // These are probably illegal here
+            UseSegment::Glob | UseSegment::List(_) => external_imports.push(ut),
+        }
+    }
+
+    vec![std_imports, external_imports, local_imports]
 }
 
 /// A simplified version of `ast::ItemKind`.
@@ -192,7 +213,7 @@ enum ReorderableItemKind {
 
 impl ReorderableItemKind {
     fn from(item: &ast::Item) -> Self {
-        match item.node {
+        match item.kind {
             _ if contains_macro_use_attr(item) | contains_skip(&item.attrs) => {
                 ReorderableItemKind::Other
             }
@@ -216,33 +237,41 @@ fn is_reorderable(self, config: &Config) -> bool {
         }
     }
 
-    fn in_group(self) -> bool {
+    fn is_regroupable(self, config: &Config) -> bool {
         match self {
             ReorderableItemKind::ExternCrate
             | ReorderableItemKind::Mod
-            | ReorderableItemKind::Use => true,
+            | ReorderableItemKind::Other => false,
+            ReorderableItemKind::Use => config.group_imports() != GroupImportsTactic::Preserve,
+        }
+    }
+
+    fn in_group(self, config: &Config) -> bool {
+        match self {
+            ReorderableItemKind::ExternCrate | ReorderableItemKind::Mod => true,
+            ReorderableItemKind::Use => config.group_imports() == GroupImportsTactic::Preserve,
             ReorderableItemKind::Other => false,
         }
     }
 }
 
 impl<'b, 'a: 'b> FmtVisitor<'a> {
-    /// Format items with the same item kind and reorder them. If `in_group` is
-    /// `true`, then the items separated by an empty line will not be reordered
-    /// together.
-    fn walk_reorderable_items(
+    /// Format items with the same item kind and reorder them, regroup them, or
+    /// both. If `in_group` is `true`, then the items separated by an empty line
+    /// will not be reordered together.
+    fn walk_reorderable_or_regroupable_items(
         &mut self,
         items: &[&ast::Item],
         item_kind: ReorderableItemKind,
         in_group: bool,
     ) -> usize {
-        let mut last = self.source_map.lookup_line_range(items[0].span());
+        let mut last = self.parse_sess.lookup_line_range(items[0].span());
         let item_length = items
             .iter()
             .take_while(|ppi| {
                 item_kind.is_same_item_kind(&***ppi)
                     && (!in_group || {
-                        let current = self.source_map.lookup_line_range(ppi.span());
+                        let current = self.parse_sess.lookup_line_range(ppi.span());
                         let in_same_group = current.lo < last.hi + 2;
                         last = current;
                         in_same_group
@@ -259,7 +288,12 @@ fn walk_reorderable_items(
             let lo = items.first().unwrap().span().lo();
             let hi = items.last().unwrap().span().hi();
             let span = mk_sp(lo, hi);
-            let rw = rewrite_reorderable_items(&self.get_context(), items, self.shape(), span);
+            let rw = rewrite_reorderable_or_regroupable_items(
+                &self.get_context(),
+                items,
+                self.shape(),
+                span,
+            );
             self.push_rewrite(span, rw);
         } else {
             for item in items {
@@ -272,15 +306,18 @@ fn walk_reorderable_items(
 
     /// Visits and format the given items. Items are reordered If they are
     /// consecutive and reorderable.
-    pub fn visit_items_with_reordering(&mut self, mut items: &[&ast::Item]) {
+    pub(crate) fn visit_items_with_reordering(&mut self, mut items: &[&ast::Item]) {
         while !items.is_empty() {
             // If the next item is a `use`, `extern crate` or `mod`, then extract it and any
             // subsequent items that have the same item kind to be reordered within
             // `walk_reorderable_items`. Otherwise, just format the next item for output.
             let item_kind = ReorderableItemKind::from(items[0]);
-            if item_kind.is_reorderable(self.config) {
-                let visited_items_num =
-                    self.walk_reorderable_items(items, item_kind, item_kind.in_group());
+            if item_kind.is_reorderable(self.config) || item_kind.is_regroupable(self.config) {
+                let visited_items_num = self.walk_reorderable_or_regroupable_items(
+                    items,
+                    item_kind,
+                    item_kind.in_group(self.config),
+                );
                 let (_, rest) = items.split_at(visited_items_num);
                 items = rest;
             } else {