Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Group imports by their visibilities #5767

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/config/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,15 @@ pub enum GroupImportsTactic {
StdExternalCrate,
/// Discard existing groups, and create a single group for everything
One,
/// Discard existing groups, and create new groups for each visibility modifiers
/// from the least visible to the most visible, i.e.:
/// 1. unexported imports
/// 2. `pub (self)`
/// 3. `pub (super)` imports
/// 4. `pub (crate)` imports
/// 5. `pub (in path)` imports
/// 6. `pub` imports
Visibility,
}

#[config_type]
Expand Down
2 changes: 1 addition & 1 deletion src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ pub(crate) struct UseTree {
pub(crate) list_item: Option<ListItem>,
// Additional fields for top level use items.
// Should we have another struct for top-level use items rather than reusing this?
visibility: Option<ast::Visibility>,
pub(crate) visibility: Option<ast::Visibility>,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

crate export for reorder module

attrs: Option<ast::AttrVec>,
}

Expand Down
48 changes: 47 additions & 1 deletion src/reorder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use std::cmp::{Ord, Ordering};

use rustc_ast::ast;
use rustc_span::{symbol::sym, Span};
use rustc_span::{symbol::sym, Span, Symbol};

use crate::config::{Config, GroupImportsTactic};
use crate::imports::{normalize_use_trees_with_granularity, UseSegmentKind, UseTree};
Expand Down Expand Up @@ -117,6 +117,7 @@ fn rewrite_reorderable_or_regroupable_items(
vec![normalized_items]
}
GroupImportsTactic::StdExternalCrate => group_imports(normalized_items),
GroupImportsTactic::Visibility => group_imports_by_visibility(normalized_items),
};

if context.config.reorder_imports() {
Expand Down Expand Up @@ -198,6 +199,51 @@ fn group_imports(uts: Vec<UseTree>) -> Vec<Vec<UseTree>> {
vec![std_imports, external_imports, local_imports]
}

fn group_imports_by_visibility(uts: Vec<UseTree>) -> Vec<Vec<UseTree>> {
let mut inherited = Vec::new();
let mut pub_self = Vec::new();
let mut pub_super = Vec::new();
let mut pub_crate = Vec::new();
let mut pub_in_path = Vec::new();
let mut pub_other = Vec::new();
let mut public = Vec::new();

for ut in uts.into_iter() {
match ut
.visibility
.as_ref()
.map(|x| &x.kind)
.unwrap_or(&ast::VisibilityKind::Inherited)
{
ast::VisibilityKind::Inherited => inherited.push(ut),
ast::VisibilityKind::Restricted { path, .. } => {
let segments = &path.segments;
if segments.len() > 1 {
pub_in_path.push(ut)
} else {
match &segments[0].ident.name {
scope if scope == &Symbol::intern("self") => pub_self.push(ut),
scope if scope == &Symbol::intern("super") => pub_super.push(ut),
scope if scope == &Symbol::intern("crate") => pub_crate.push(ut),
_ => pub_other.push(ut),
}
}
}
ast::VisibilityKind::Public => public.push(ut),
}
}

vec![
inherited,
pub_self,
pub_super,
pub_crate,
pub_in_path,
pub_other,
public,
]
}

/// A simplified version of `ast::ItemKind`.
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
enum ReorderableItemKind {
Expand Down
2 changes: 1 addition & 1 deletion tests/source/configs/group_imports/One.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use chrono::Utc;
use super::update::convert_publish_payload;

use juniper::{FieldError, FieldResult};
pub use juniper::{FieldError, FieldResult};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just taking a quick glance at the tests, and was wondering why an existing test changed?

use uuid::Uuid;
use alloc::alloc::Layout;

Expand Down
15 changes: 15 additions & 0 deletions tests/source/configs/group_imports/Visibility.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// rustfmt-group_imports: Visibility
pub(super) use chrono::Utc;
pub use super::update::convert_publish_payload;

pub(crate) use juniper::{FieldError, FieldResult};
pub(in crate::models) use uuid::Uuid;
use alloc::alloc::Layout;

use std::sync::Arc;

pub(self) use broker::database::PooledConnection;

use super::schema::{Context, Payload};
use core::f32;
pub use crate::models::Event;
2 changes: 1 addition & 1 deletion tests/target/configs/group_imports/One.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ use alloc::alloc::Layout;
use broker::database::PooledConnection;
use chrono::Utc;
use core::f32;
use juniper::{FieldError, FieldResult};
pub use juniper::{FieldError, FieldResult};
use std::sync::Arc;
use uuid::Uuid;
16 changes: 16 additions & 0 deletions tests/target/configs/group_imports/Visibility.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// rustfmt-group_imports: Visibility
use super::schema::{Context, Payload};
use alloc::alloc::Layout;
use core::f32;
use std::sync::Arc;

pub(self) use broker::database::PooledConnection;

pub(super) use chrono::Utc;

pub(crate) use juniper::{FieldError, FieldResult};

pub(in crate::models) use uuid::Uuid;

pub use super::update::convert_publish_payload;
pub use crate::models::Event;