Files
rust/crates/assists/src/utils/insert_use.rs

930 lines
27 KiB
Rust
Raw Normal View History

//! Handle syntactic aspects of inserting a new `use`.
use std::{
cmp::Ordering,
iter::{self, successors},
};
2020-08-13 11:56:11 +02:00
use ast::{
edit::{AstNodeEdit, IndentLevel},
PathSegmentKind, VisibilityOwner,
};
2020-08-12 18:26:51 +02:00
use syntax::{
2020-09-02 15:21:10 +02:00
algo,
ast::{self, make, AstNode},
InsertPosition, SyntaxElement, SyntaxNode,
2020-02-28 21:53:20 +01:00
};
2020-06-13 18:56:14 +02:00
#[derive(Debug)]
pub enum ImportScope {
File(ast::SourceFile),
Module(ast::ItemList),
}
impl ImportScope {
pub fn from(syntax: SyntaxNode) -> Option<Self> {
if let Some(module) = ast::Module::cast(syntax.clone()) {
module.item_list().map(ImportScope::Module)
} else if let this @ Some(_) = ast::SourceFile::cast(syntax.clone()) {
this.map(ImportScope::File)
} else {
ast::ItemList::cast(syntax).map(ImportScope::Module)
2020-06-13 18:56:14 +02:00
}
}
/// Determines the containing syntax node in which to insert a `use` statement affecting `position`.
pub(crate) fn find_insert_use_container(
position: &SyntaxNode,
ctx: &crate::assist_context::AssistContext,
) -> Option<Self> {
ctx.sema.ancestors_with_macros(position.clone()).find_map(Self::from)
}
pub(crate) fn as_syntax_node(&self) -> &SyntaxNode {
match self {
ImportScope::File(file) => file.syntax(),
ImportScope::Module(item_list) => item_list.syntax(),
}
}
fn indent_level(&self) -> IndentLevel {
match self {
ImportScope::File(file) => file.indent_level(),
ImportScope::Module(item_list) => item_list.indent_level() + 1,
}
}
fn first_insert_pos(&self) -> (InsertPosition<SyntaxElement>, AddBlankLine) {
match self {
ImportScope::File(_) => (InsertPosition::First, AddBlankLine::AfterTwice),
// don't insert the imports before the item list's opening curly brace
ImportScope::Module(item_list) => item_list
.l_curly_token()
.map(|b| (InsertPosition::After(b.into()), AddBlankLine::Around))
.unwrap_or((InsertPosition::First, AddBlankLine::AfterTwice)),
}
}
fn insert_pos_after_inner_attribute(&self) -> (InsertPosition<SyntaxElement>, AddBlankLine) {
// check if the scope has inner attributes, we dont want to insert in front of them
match self
.as_syntax_node()
.children()
// no flat_map here cause we want to short circuit the iterator
.map(ast::Attr::cast)
.take_while(|attr| {
attr.as_ref().map(|attr| attr.kind() == ast::AttrKind::Inner).unwrap_or(false)
})
.last()
.flatten()
{
Some(attr) => {
(InsertPosition::After(attr.syntax().clone().into()), AddBlankLine::BeforeTwice)
}
None => self.first_insert_pos(),
}
}
2020-06-13 18:56:14 +02:00
}
/// Insert an import path into the given file/node. A `merge` value of none indicates that no import merging is allowed to occur.
pub(crate) fn insert_use(
scope: &ImportScope,
2020-09-02 15:21:10 +02:00
path: ast::Path,
merge: Option<MergeBehaviour>,
2020-09-02 15:21:10 +02:00
) -> SyntaxNode {
let use_item = make::use_(make::use_tree(path.clone(), None, None, false));
// merge into existing imports if possible
if let Some(mb) = merge {
for existing_use in scope.as_syntax_node().children().filter_map(ast::Use::cast) {
2020-09-02 15:21:10 +02:00
if let Some(merged) = try_merge_imports(&existing_use, &use_item, mb) {
let to_delete: SyntaxElement = existing_use.syntax().clone().into();
let to_delete = to_delete.clone()..=to_delete;
let to_insert = iter::once(merged.syntax().clone().into());
return algo::replace_children(scope.as_syntax_node(), to_delete, to_insert);
2020-09-02 15:21:10 +02:00
}
}
}
// either we weren't allowed to merge or there is no import that fits the merge conditions
// so look for the place we have to insert to
let (insert_position, add_blank) = find_insert_position(scope, path);
2020-09-02 15:21:10 +02:00
let to_insert: Vec<SyntaxElement> = {
let mut buf = Vec::new();
match add_blank {
AddBlankLine::Before | AddBlankLine::Around => {
buf.push(make::tokens::single_newline().into())
}
2020-09-03 12:36:16 +02:00
AddBlankLine::BeforeTwice => buf.push(make::tokens::blank_line().into()),
_ => (),
2020-09-02 15:21:10 +02:00
}
if let ident_level @ 1..=usize::MAX = scope.indent_level().0 as usize {
// FIXME: this alone doesnt properly re-align all cases
buf.push(make::tokens::whitespace(&" ".repeat(4 * ident_level)).into());
}
2020-09-02 15:21:10 +02:00
buf.push(use_item.syntax().clone().into());
match add_blank {
AddBlankLine::After | AddBlankLine::Around => {
buf.push(make::tokens::single_newline().into())
}
2020-09-03 12:36:16 +02:00
AddBlankLine::AfterTwice => buf.push(make::tokens::blank_line().into()),
_ => (),
2020-09-02 15:21:10 +02:00
}
2020-02-28 21:53:20 +01:00
2020-09-02 15:21:10 +02:00
buf
};
algo::insert_children(scope.as_syntax_node(), insert_position, to_insert)
2020-09-02 15:21:10 +02:00
}
fn eq_visibility(vis0: Option<ast::Visibility>, vis1: Option<ast::Visibility>) -> bool {
match (vis0, vis1) {
(None, None) => true,
// FIXME: Don't use the string representation to check for equality
// spaces inside of the node would break this comparison
(Some(vis0), Some(vis1)) => vis0.to_string() == vis1.to_string(),
_ => false,
}
}
pub(crate) fn try_merge_imports(
2020-09-02 15:21:10 +02:00
old: &ast::Use,
new: &ast::Use,
merge_behaviour: MergeBehaviour,
) -> Option<ast::Use> {
2020-09-05 15:51:26 +02:00
// don't merge imports with different visibilities
if !eq_visibility(old.visibility(), new.visibility()) {
2020-09-02 15:21:10 +02:00
return None;
}
let old_tree = old.use_tree()?;
let new_tree = new.use_tree()?;
let merged = try_merge_trees(&old_tree, &new_tree, merge_behaviour)?;
Some(old.with_use_tree(merged))
}
/// Orders paths in the following way:
/// the sole self token comes first, after that come uppercase identifiers, then lowercase identifiers
/// FIXME: rustfmt sort lowercase idents before uppercase
fn path_cmp(a: &ast::Path, b: &ast::Path) -> Ordering {
let a = segment_iter(a);
let b = segment_iter(b);
let mut a_clone = a.clone();
let mut b_clone = b.clone();
match (
a_clone.next().and_then(|ps| ps.self_token()).is_some() && a_clone.next().is_none(),
b_clone.next().and_then(|ps| ps.self_token()).is_some() && b_clone.next().is_none(),
) {
(true, true) => Ordering::Equal,
(true, false) => Ordering::Less,
(false, true) => Ordering::Greater,
(false, false) => {
// cmp_by would be useful for us here but that is currently unstable
// cmp doesnt work due the lifetimes on text's return type
a.zip(b)
.flat_map(|(seg, seg2)| seg.name_ref().zip(seg2.name_ref()))
.find_map(|(a, b)| match a.text().cmp(b.text()) {
ord @ Ordering::Greater | ord @ Ordering::Less => Some(ord),
Ordering::Equal => None,
})
.unwrap_or(Ordering::Equal)
}
}
}
fn path_cmp_opt(a: Option<ast::Path>, b: Option<ast::Path>) -> Ordering {
match (a, b) {
(None, None) => Ordering::Equal,
(None, Some(_)) => Ordering::Less,
(Some(_), None) => Ordering::Greater,
(Some(a), Some(b)) => path_cmp(&a, &b),
}
2020-09-02 15:21:10 +02:00
}
pub(crate) fn try_merge_trees(
2020-09-02 15:21:10 +02:00
old: &ast::UseTree,
new: &ast::UseTree,
merge: MergeBehaviour,
2020-09-02 15:21:10 +02:00
) -> Option<ast::UseTree> {
let lhs_path = old.path()?;
let rhs_path = new.path()?;
let (lhs_prefix, rhs_prefix) = common_prefix(&lhs_path, &rhs_path)?;
let lhs = old.split_prefix(&lhs_prefix);
let rhs = new.split_prefix(&rhs_prefix);
recursive_merge(&lhs, &rhs, merge).map(|(merged, _)| merged)
}
2020-09-02 15:21:10 +02:00
/// Returns the merged tree and the number of children it has, which is required to check if the tree is allowed to be used for MergeBehaviour::Last
fn recursive_merge(
lhs: &ast::UseTree,
rhs: &ast::UseTree,
merge: MergeBehaviour,
) -> Option<(ast::UseTree, usize)> {
let mut use_trees = lhs
.use_tree_list()
.into_iter()
.flat_map(|list| list.use_trees())
// check if any of the use trees are nested, if they are and the behaviour is last only we are not allowed to merge this
.map(|tree| match merge == MergeBehaviour::Last && tree.use_tree_list().is_some() {
true => None,
false => Some(tree),
})
.collect::<Option<Vec<_>>>()?;
use_trees.sort_unstable_by(|a, b| path_cmp_opt(a.path(), b.path()));
for rhs_t in rhs.use_tree_list().into_iter().flat_map(|list| list.use_trees()) {
let rhs_path = rhs_t.path();
match use_trees.binary_search_by(|p| path_cmp_opt(p.path(), rhs_path.clone())) {
Ok(idx) => {
let lhs_path = use_trees[idx].path()?;
let rhs_path = rhs_path?;
let (lhs_prefix, rhs_prefix) = common_prefix(&lhs_path, &rhs_path)?;
if lhs_prefix == lhs_path && rhs_prefix == rhs_path {
let tree_is_self =
|tree: ast::UseTree| tree.path().map(path_is_self).unwrap_or(false);
// check if only one of the two trees has a tree list, and whether that then contains `self` or not.
// If this is the case we can skip this iteration since the path without the list is already included in the other one via `self`
if use_trees[idx]
.use_tree_list()
.xor(rhs_t.use_tree_list())
.map(|tree_list| tree_list.use_trees().any(tree_is_self))
.unwrap_or(false)
{
continue;
}
// glob imports arent part of the use-tree lists so we need to special handle them here as well
// this special handling is only required for when we merge a module import into a glob import of said module
// see the `merge_self_glob` or `merge_mod_into_glob` tests
if use_trees[idx].star_token().is_some() || rhs_t.star_token().is_some() {
use_trees[idx] = make::use_tree(
make::path_unqualified(make::path_segment_self()),
None,
None,
false,
);
use_trees.insert(
idx,
make::use_tree(
make::path_unqualified(make::path_segment_self()),
None,
None,
true,
),
);
continue;
}
}
let lhs = use_trees[idx].split_prefix(&lhs_prefix);
let rhs = rhs_t.split_prefix(&rhs_prefix);
match recursive_merge(&lhs, &rhs, merge) {
Some((_, count))
if merge == MergeBehaviour::Last && use_trees.len() > 1 && count > 1 =>
{
return None
}
Some((use_tree, _)) => use_trees[idx] = use_tree,
None => use_trees.insert(idx, rhs_t),
}
}
Err(idx) => {
use_trees.insert(idx, rhs_t);
}
}
}
let count = use_trees.len();
let tl = make::use_tree_list(use_trees);
Some((lhs.with_use_tree_list(tl), count))
2020-02-28 21:53:20 +01:00
}
2020-09-02 15:21:10 +02:00
/// Traverses both paths until they differ, returning the common prefix of both.
fn common_prefix(lhs: &ast::Path, rhs: &ast::Path) -> Option<(ast::Path, ast::Path)> {
let mut res = None;
let mut lhs_curr = first_path(&lhs);
let mut rhs_curr = first_path(&rhs);
2020-02-28 21:53:20 +01:00
loop {
2020-09-02 15:21:10 +02:00
match (lhs_curr.segment(), rhs_curr.segment()) {
(Some(lhs), Some(rhs)) if lhs.syntax().text() == rhs.syntax().text() => (),
_ => break,
}
res = Some((lhs_curr.clone(), rhs_curr.clone()));
match lhs_curr.parent_path().zip(rhs_curr.parent_path()) {
Some((lhs, rhs)) => {
lhs_curr = lhs;
rhs_curr = rhs;
2020-02-28 21:53:20 +01:00
}
2020-09-02 15:21:10 +02:00
_ => break,
2020-02-28 21:53:20 +01:00
}
}
2020-09-02 15:21:10 +02:00
res
2020-02-28 21:53:20 +01:00
}
fn path_is_self(path: ast::Path) -> bool {
path.segment().and_then(|seg| seg.self_token()).is_some() && path.qualifier().is_none()
}
fn first_segment(path: &ast::Path) -> Option<ast::PathSegment> {
first_path(path).segment()
}
fn first_path(path: &ast::Path) -> ast::Path {
successors(Some(path.clone()), ast::Path::qualifier).last().unwrap()
}
fn segment_iter(path: &ast::Path) -> impl Iterator<Item = ast::PathSegment> + Clone {
// cant make use of SyntaxNode::siblings, because the returned Iterator is not clone
successors(first_segment(path), |p| p.parent_path().parent_path().and_then(|p| p.segment()))
}
2020-09-02 15:21:10 +02:00
/// What type of merges are allowed.
#[derive(Copy, Clone, PartialEq, Eq)]
pub enum MergeBehaviour {
/// Merge everything together creating deeply nested imports.
Full,
/// Only merge the last import level, doesn't allow import nesting.
Last,
2020-02-28 21:53:20 +01:00
}
2020-09-02 15:21:10 +02:00
#[derive(Eq, PartialEq, PartialOrd, Ord)]
enum ImportGroup {
// the order here defines the order of new group inserts
Std,
ExternCrate,
ThisCrate,
ThisModule,
SuperModule,
2020-02-28 21:53:20 +01:00
}
2020-09-02 15:21:10 +02:00
impl ImportGroup {
fn new(path: &ast::Path) -> ImportGroup {
let default = ImportGroup::ExternCrate;
let first_segment = match first_segment(path) {
Some(it) => it,
None => return default,
};
let kind = first_segment.kind().unwrap_or(PathSegmentKind::SelfKw);
match kind {
PathSegmentKind::SelfKw => ImportGroup::ThisModule,
PathSegmentKind::SuperKw => ImportGroup::SuperModule,
PathSegmentKind::CrateKw => ImportGroup::ThisCrate,
PathSegmentKind::Name(name) => match name.text().as_str() {
"std" => ImportGroup::Std,
"core" => ImportGroup::Std,
// FIXME: can be ThisModule as well
_ => ImportGroup::ExternCrate,
},
PathSegmentKind::Type { .. } => unreachable!(),
2020-02-28 21:53:20 +01:00
}
}
}
2020-09-02 15:21:10 +02:00
#[derive(PartialEq, Eq)]
enum AddBlankLine {
Before,
BeforeTwice,
Around,
2020-09-02 15:21:10 +02:00
After,
AfterTwice,
}
fn find_insert_position(
scope: &ImportScope,
2020-09-02 15:21:10 +02:00
insert_path: ast::Path,
) -> (InsertPosition<SyntaxElement>, AddBlankLine) {
let group = ImportGroup::new(&insert_path);
let path_node_iter = scope
.as_syntax_node()
2020-09-02 15:21:10 +02:00
.children()
.filter_map(|node| ast::Use::cast(node.clone()).zip(Some(node)))
.flat_map(|(use_, node)| use_.use_tree().and_then(|tree| tree.path()).zip(Some(node)));
// Iterator that discards anything thats not in the required grouping
// This implementation allows the user to rearrange their import groups as this only takes the first group that fits
let group_iter = path_node_iter
.clone()
.skip_while(|(path, _)| ImportGroup::new(path) != group)
.take_while(|(path, _)| ImportGroup::new(path) == group);
let segments = segment_iter(&insert_path);
// track the last element we iterated over, if this is still None after the iteration then that means we never iterated in the first place
let mut last = None;
// find the element that would come directly after our new import
let post_insert =
group_iter.inspect(|(_, node)| last = Some(node.clone())).find(|(path, _)| {
let check_segments = segment_iter(&path);
segments
.clone()
.zip(check_segments)
.flat_map(|(seg, seg2)| seg.name_ref().zip(seg2.name_ref()))
.all(|(l, r)| l.text() <= r.text())
});
match post_insert {
// insert our import before that element
Some((_, node)) => (InsertPosition::Before(node.into()), AddBlankLine::After),
// there is no element after our new import, so append it to the end of the group
None => match last {
Some(node) => (InsertPosition::After(node.into()), AddBlankLine::Before),
// the group we were looking for actually doesnt exist, so insert
None => {
// similar concept here to the `last` from above
let mut last = None;
// find the group that comes after where we want to insert
let post_group = path_node_iter
.inspect(|(_, node)| last = Some(node.clone()))
.find(|(p, _)| ImportGroup::new(p) > group);
match post_group {
Some((_, node)) => {
(InsertPosition::Before(node.into()), AddBlankLine::AfterTwice)
}
// there is no such group, so append after the last one
None => match last {
Some(node) => {
(InsertPosition::After(node.into()), AddBlankLine::BeforeTwice)
}
2020-09-02 15:21:10 +02:00
// there are no imports in this file at all
None => scope.insert_pos_after_inner_attribute(),
2020-09-02 15:21:10 +02:00
},
}
}
},
2020-02-28 21:53:20 +01:00
}
2020-09-02 15:21:10 +02:00
}
2020-02-28 21:53:20 +01:00
2020-09-02 15:21:10 +02:00
#[cfg(test)]
mod tests {
use super::*;
use test_utils::assert_eq_text;
#[test]
fn insert_start() {
check_none(
"std::bar::AA",
r"
use std::bar::B;
2020-09-02 15:21:10 +02:00
use std::bar::D;
use std::bar::F;
use std::bar::G;",
r"
use std::bar::AA;
2020-09-02 15:21:10 +02:00
use std::bar::B;
use std::bar::D;
use std::bar::F;
use std::bar::G;",
)
2020-02-28 21:53:20 +01:00
}
2020-09-02 15:21:10 +02:00
#[test]
fn insert_middle() {
check_none(
"std::bar::EE",
r"
use std::bar::A;
2020-09-02 15:21:10 +02:00
use std::bar::D;
use std::bar::F;
use std::bar::G;",
r"
use std::bar::A;
2020-09-02 15:21:10 +02:00
use std::bar::D;
use std::bar::EE;
2020-09-02 15:21:10 +02:00
use std::bar::F;
use std::bar::G;",
)
2020-02-28 21:53:20 +01:00
}
2020-09-02 15:21:10 +02:00
#[test]
fn insert_end() {
check_none(
"std::bar::ZZ",
r"
use std::bar::A;
2020-09-02 15:21:10 +02:00
use std::bar::D;
use std::bar::F;
use std::bar::G;",
r"
use std::bar::A;
2020-09-02 15:21:10 +02:00
use std::bar::D;
use std::bar::F;
use std::bar::G;
use std::bar::ZZ;",
2020-09-02 15:21:10 +02:00
)
2020-02-28 21:53:20 +01:00
}
2020-09-02 15:21:10 +02:00
#[test]
fn insert_middle_nested() {
2020-09-02 15:21:10 +02:00
check_none(
"std::bar::EE",
r"
use std::bar::A;
2020-09-02 15:21:10 +02:00
use std::bar::{D, Z}; // example of weird imports due to user
use std::bar::F;
use std::bar::G;",
r"
use std::bar::A;
use std::bar::EE;
2020-09-02 15:21:10 +02:00
use std::bar::{D, Z}; // example of weird imports due to user
use std::bar::F;
use std::bar::G;",
)
2020-02-28 21:53:20 +01:00
}
2020-09-02 15:21:10 +02:00
#[test]
fn insert_middle_groups() {
check_none(
"foo::bar::GG",
r"
use std::bar::A;
2020-09-02 15:21:10 +02:00
use std::bar::D;
use foo::bar::F;
use foo::bar::H;",
r"
use std::bar::A;
2020-09-02 15:21:10 +02:00
use std::bar::D;
use foo::bar::F;
use foo::bar::GG;
2020-09-02 15:21:10 +02:00
use foo::bar::H;",
)
}
2020-02-28 21:53:20 +01:00
2020-09-02 15:21:10 +02:00
#[test]
fn insert_first_matching_group() {
check_none(
"foo::bar::GG",
r"
use foo::bar::A;
2020-09-02 15:21:10 +02:00
use foo::bar::D;
use std;
use foo::bar::F;
use foo::bar::H;",
r"
use foo::bar::A;
2020-09-02 15:21:10 +02:00
use foo::bar::D;
use foo::bar::GG;
2020-09-02 15:21:10 +02:00
use std;
use foo::bar::F;
use foo::bar::H;",
)
2020-02-28 21:53:20 +01:00
}
2020-09-02 15:21:10 +02:00
#[test]
fn insert_missing_group_std() {
2020-09-02 15:21:10 +02:00
check_none(
"std::fmt",
r"
use foo::bar::A;
2020-09-02 15:21:10 +02:00
use foo::bar::D;",
r"
use std::fmt;
2020-09-02 15:21:10 +02:00
use foo::bar::A;
use foo::bar::D;",
)
}
2020-02-28 21:53:20 +01:00
#[test]
fn insert_missing_group_self() {
check_none(
"self::fmt",
r"
use foo::bar::A;
use foo::bar::D;",
r"
use foo::bar::A;
use foo::bar::D;
use self::fmt;",
)
}
2020-09-02 15:21:10 +02:00
#[test]
fn insert_no_imports() {
check_full(
"foo::bar",
"fn main() {}",
r"use foo::bar;
2020-02-28 21:53:20 +01:00
2020-09-02 15:21:10 +02:00
fn main() {}",
)
}
2020-02-28 21:53:20 +01:00
2020-09-02 15:21:10 +02:00
#[test]
fn insert_empty_file() {
// empty files will get two trailing newlines
// this is due to the test case insert_no_imports above
check_full(
"foo::bar",
"",
r"use foo::bar;
",
)
}
2020-02-28 21:53:20 +01:00
#[test]
fn insert_after_inner_attr() {
check_full(
"foo::bar",
r"#![allow(unused_imports)]",
r"#![allow(unused_imports)]
use foo::bar;",
)
}
#[test]
fn insert_after_inner_attr2() {
check_full(
"foo::bar",
r"#![allow(unused_imports)]
fn main() {}",
r"#![allow(unused_imports)]
use foo::bar;
fn main() {}",
)
}
2020-09-02 15:21:10 +02:00
#[test]
fn merge_groups() {
2020-09-02 15:21:10 +02:00
check_last("std::io", r"use std::fmt;", r"use std::{fmt, io};")
2020-02-28 21:53:20 +01:00
}
2020-09-02 15:21:10 +02:00
#[test]
fn merge_groups_last() {
2020-09-02 15:21:10 +02:00
check_last(
"std::io",
r"use std::fmt::{Result, Display};",
r"use std::fmt::{Result, Display};
use std::io;",
)
2020-02-28 21:53:20 +01:00
}
2020-09-02 15:21:10 +02:00
#[test]
fn merge_groups_full() {
2020-09-02 15:21:10 +02:00
check_full(
"std::io",
r"use std::fmt::{Result, Display};",
r"use std::{fmt::{Result, Display}, io};",
)
2020-02-28 21:53:20 +01:00
}
2020-09-02 15:50:29 +02:00
#[test]
fn merge_groups_long_full() {
2020-09-02 15:50:29 +02:00
check_full(
"std::foo::bar::Baz",
r"use std::foo::bar::Qux;",
r"use std::foo::bar::{Baz, Qux};",
2020-09-02 15:50:29 +02:00
)
}
#[test]
fn merge_groups_long_last() {
2020-09-02 15:50:29 +02:00
check_last(
"std::foo::bar::Baz",
r"use std::foo::bar::Qux;",
r"use std::foo::bar::{Baz, Qux};",
2020-09-02 15:50:29 +02:00
)
}
#[test]
fn merge_groups_long_full_list() {
2020-09-02 15:50:29 +02:00
check_full(
"std::foo::bar::Baz",
r"use std::foo::bar::{Qux, Quux};",
r"use std::foo::bar::{Baz, Quux, Qux};",
2020-09-02 15:50:29 +02:00
)
}
#[test]
fn merge_groups_long_last_list() {
2020-09-02 15:50:29 +02:00
check_last(
"std::foo::bar::Baz",
r"use std::foo::bar::{Qux, Quux};",
r"use std::foo::bar::{Baz, Quux, Qux};",
2020-09-02 15:50:29 +02:00
)
}
#[test]
fn merge_groups_long_full_nested() {
2020-09-02 15:50:29 +02:00
check_full(
"std::foo::bar::Baz",
r"use std::foo::bar::{Qux, quux::{Fez, Fizz}};",
r"use std::foo::bar::{Baz, Qux, quux::{Fez, Fizz}};",
2020-09-02 15:50:29 +02:00
)
}
#[test]
fn merge_groups_long_last_nested() {
2020-09-02 15:50:29 +02:00
check_last(
"std::foo::bar::Baz",
r"use std::foo::bar::{Qux, quux::{Fez, Fizz}};",
r"use std::foo::bar::Baz;
use std::foo::bar::{Qux, quux::{Fez, Fizz}};",
2020-09-02 15:50:29 +02:00
)
}
#[test]
fn merge_groups_full_nested_deep() {
check_full(
"std::foo::bar::quux::Baz",
r"use std::foo::bar::{Qux, quux::{Fez, Fizz}};",
r"use std::foo::bar::{Qux, quux::{Baz, Fez, Fizz}};",
)
}
2020-09-02 15:21:10 +02:00
#[test]
fn merge_groups_skip_pub() {
2020-09-02 15:21:10 +02:00
check_full(
"std::io",
r"pub use std::fmt::{Result, Display};",
r"pub use std::fmt::{Result, Display};
use std::io;",
)
}
2020-02-28 21:53:20 +01:00
#[test]
fn merge_groups_skip_pub_crate() {
check_full(
"std::io",
r"pub(crate) use std::fmt::{Result, Display};",
r"pub(crate) use std::fmt::{Result, Display};
use std::io;",
)
}
2020-09-02 15:50:29 +02:00
#[test]
#[ignore] // FIXME: Support this
fn split_out_merge() {
2020-09-02 15:50:29 +02:00
check_last(
"std::fmt::Result",
r"use std::{fmt, io};",
r"use std::fmt::{self, Result};
2020-09-02 15:50:29 +02:00
use std::io;",
)
}
#[test]
fn merge_into_module_import() {
check_full(
"std::fmt::Result",
r"use std::{fmt, io};",
r"use std::{fmt::{self, Result}, io};",
)
}
2020-09-02 15:21:10 +02:00
#[test]
fn merge_groups_self() {
2020-09-02 15:21:10 +02:00
check_full("std::fmt::Debug", r"use std::fmt;", r"use std::fmt::{self, Debug};")
2020-02-28 21:53:20 +01:00
}
2020-08-13 11:56:11 +02:00
#[test]
fn merge_mod_into_glob() {
check_full(
"token::TokenKind",
r"use token::TokenKind::*;",
r"use token::TokenKind::{self::*, self};",
)
// FIXME: have it emit `use token::TokenKind::{self, *}`?
}
#[test]
fn merge_self_glob() {
check_full("self", r"use self::*;", r"use self::{self::*, self};")
// FIXME: have it emit `use {self, *}`?
}
#[test]
#[ignore] // FIXME: Support this
fn merge_partial_path() {
check_full(
"ast::Foo",
r"use syntax::{ast, algo};",
r"use syntax::{ast::{self, Foo}, algo};",
)
}
#[test]
fn merge_glob_nested() {
check_full(
"foo::bar::quux::Fez",
r"use foo::bar::{Baz, quux::*;",
r"use foo::bar::{Baz, quux::{self::*, Fez}}",
)
}
#[test]
fn merge_last_too_long() {
check_last("foo::bar", r"use foo::bar::baz::Qux;", r"use foo::bar::{self, baz::Qux};");
}
#[test]
fn insert_short_before_long() {
check_none(
"foo::bar",
r"use foo::bar::baz::Qux;",
r"use foo::bar;
use foo::bar::baz::Qux;",
);
}
#[test]
fn merge_last_fail() {
check_merge_only_fail(
r"use foo::bar::{baz::{Qux, Fez}};",
r"use foo::bar::{baaz::{Quux, Feez}};",
MergeBehaviour::Last,
);
}
#[test]
fn merge_last_fail1() {
check_merge_only_fail(
r"use foo::bar::{baz::{Qux, Fez}};",
r"use foo::bar::baaz::{Quux, Feez};",
MergeBehaviour::Last,
);
}
#[test]
fn merge_last_fail2() {
check_merge_only_fail(
r"use foo::bar::baz::{Qux, Fez};",
r"use foo::bar::{baaz::{Quux, Feez}};",
MergeBehaviour::Last,
);
}
#[test]
fn merge_last_fail3() {
check_merge_only_fail(
r"use foo::bar::baz::{Qux, Fez};",
r"use foo::bar::baaz::{Quux, Feez};",
MergeBehaviour::Last,
);
}
2020-09-02 15:21:10 +02:00
fn check(
path: &str,
ra_fixture_before: &str,
ra_fixture_after: &str,
mb: Option<MergeBehaviour>,
) {
let file = super::ImportScope::from(
ast::SourceFile::parse(ra_fixture_before).tree().syntax().clone(),
)
.unwrap();
2020-09-02 15:21:10 +02:00
let path = ast::SourceFile::parse(&format!("use {};", path))
.tree()
.syntax()
.descendants()
.find_map(ast::Path::cast)
.unwrap();
2020-09-03 12:36:16 +02:00
let result = insert_use(&file, path, mb).to_string();
2020-09-02 15:21:10 +02:00
assert_eq_text!(&result, ra_fixture_after);
}
fn check_full(path: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
check(path, ra_fixture_before, ra_fixture_after, Some(MergeBehaviour::Full))
2020-08-13 11:56:11 +02:00
}
2020-09-02 15:21:10 +02:00
fn check_last(path: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
check(path, ra_fixture_before, ra_fixture_after, Some(MergeBehaviour::Last))
}
fn check_none(path: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
check(path, ra_fixture_before, ra_fixture_after, None)
2020-08-13 11:56:11 +02:00
}
fn check_merge_only_fail(ra_fixture0: &str, ra_fixture1: &str, mb: MergeBehaviour) {
let use0 = ast::SourceFile::parse(ra_fixture0)
.tree()
.syntax()
.descendants()
.find_map(ast::Use::cast)
.unwrap();
let use1 = ast::SourceFile::parse(ra_fixture1)
.tree()
.syntax()
.descendants()
.find_map(ast::Use::cast)
.unwrap();
let result = try_merge_imports(&use0, &use1, mb);
assert_eq!(result, None);
}
2020-08-13 11:56:11 +02:00
}