Skip to content

Commit

Permalink
Move imports_granularity=Item logic into sep fn.
Browse files Browse the repository at this point in the history
  • Loading branch information
msmorgan committed Jan 18, 2021
1 parent ff07d6a commit 9c9ad03
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 31 deletions.
57 changes: 30 additions & 27 deletions src/formatting/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,19 +179,26 @@ pub(crate) fn merge_use_trees(use_trees: Vec<UseTree>, merge_by: SharedPrefix) -
}
}
}
for merged in &mut result {
// If a path ends with `::self`, rewrite it to `::{self}`.
if let Some(UseSegment::Slf(..)) = merged.path.last() {
let slf_segment = merged.path.pop().unwrap();
merged.path.push(UseSegment::List(vec![UseTree::from_path(
vec![slf_segment],
DUMMY_SP,
)]));
}
}
result
}

pub(crate) fn flatten_use_trees(use_trees: Vec<UseTree>) -> Vec<UseTree> {
use_trees
.into_iter()
.flat_map(UseTree::flatten)
.map(|mut tree| {
if let Some(UseSegment::Slf(..)) = tree.path.last() {
let self_segment = tree.path.pop().unwrap();
tree.path.push(UseSegment::List(vec![UseTree::from_path(
vec![self_segment],
DUMMY_SP,
)]));
}
tree
})
.collect()
}

impl fmt::Debug for UseTree {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self, f)
Expand Down Expand Up @@ -552,25 +559,24 @@ impl UseTree {
SharedPrefix::Module => {
self.path[..self.path.len() - 1] == other.path[..other.path.len() - 1]
}
SharedPrefix::NoPrefix => false,
}
}
}

fn flatten(self) -> Vec<UseTree> {
match self.path.last() {
Some(UseSegment::List(list)) => {
if list.len() == 1 && list[0].path.len() == 1 {
if let UseSegment::Slf(..) = list[0].path[0] {
return vec![self];
};
}
let prefix = &self.path[..self.path.len() - 1];
let mut result = vec![];
for nested_use_tree in list {
for flattened in &mut nested_use_tree.clone().flatten() {
let mut new_path = prefix.to_vec();
new_path.append(&mut flattened.path);
if flattened.path.len() == 1 {
if let UseSegment::Slf(..) = flattened.path[0] {
new_path.pop();
}
}
result.push(UseTree {
path: new_path,
span: self.span,
Expand Down Expand Up @@ -865,7 +871,6 @@ impl Rewrite for UseTree {
pub(crate) enum SharedPrefix {
Crate,
Module,
NoPrefix,
}

#[cfg(test)]
Expand Down Expand Up @@ -1081,24 +1086,22 @@ mod test {
}

#[test]
fn test_use_tree_merge_no_prefix() {
test_merge!(
NoPrefix,
["foo::{a::{b, c}, d::e}"],
["foo::a::b", "foo::a::c", "foo::d::e"]
fn test_flatten_use_trees() {
assert_eq!(
flatten_use_trees(parse_use_trees!["foo::{a::{b, c}, d::e}"]),
parse_use_trees!["foo::a::b", "foo::a::c", "foo::d::e"]
);

test_merge!(
NoPrefix,
["foo::{self, a, b::{c, d}, e::*}"],
[
assert_eq!(
flatten_use_trees(parse_use_trees!["foo::{self, a, b::{c, d}, e::*}"]),
parse_use_trees![
"foo::{self}",
"foo::a",
"foo::b::c",
"foo::b::d",
"foo::e::*"
]
)
);
}

#[test]
Expand Down
6 changes: 2 additions & 4 deletions src/formatting/reorder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use rustc_ast::ast;
use rustc_span::{symbol::sym, Span};

use crate::config::{Config, GroupImportsTactic, ImportGranularity};
use crate::formatting::imports::UseSegment;
use crate::formatting::imports::{flatten_use_trees, UseSegment};
use crate::formatting::modules::{get_mod_inner_attrs, FileModMap};
use crate::formatting::{
imports::{merge_use_trees, UseTree},
Expand Down Expand Up @@ -233,9 +233,7 @@ fn rewrite_reorderable_or_regroupable_items(
ImportGranularity::Module => {
merge_use_trees(normalized_items, SharedPrefix::Module)
}
ImportGranularity::Item => {
merge_use_trees(normalized_items, SharedPrefix::NoPrefix)
}
ImportGranularity::Item => flatten_use_trees(normalized_items),
ImportGranularity::Preserve => normalized_items,
};

Expand Down

0 comments on commit 9c9ad03

Please sign in to comment.