2020-09-03 18:44:39 +02:00
|
|
|
//! Handle syntactic aspects of inserting a new `use`.
|
2020-11-02 21:40:52 +01:00
|
|
|
use std::{cmp::Ordering, iter::successors};
|
2020-08-13 11:56:11 +02:00
|
|
|
|
2020-09-18 23:40:11 +03:00
|
|
|
use hir::Semantics;
|
|
|
|
|
use ide_db::RootDatabase;
|
2020-09-30 21:10:42 +02:00
|
|
|
use itertools::{EitherOrBoth, Itertools};
|
2020-08-12 18:26:51 +02:00
|
|
|
use syntax::{
|
2020-11-02 21:40:52 +01:00
|
|
|
algo::SyntaxRewriter,
|
2020-09-30 21:10:42 +02:00
|
|
|
ast::{
|
|
|
|
|
self,
|
|
|
|
|
edit::{AstNodeEdit, IndentLevel},
|
|
|
|
|
make, AstNode, PathSegmentKind, VisibilityOwner,
|
|
|
|
|
},
|
2020-11-11 11:37:41 +02:00
|
|
|
AstToken, InsertPosition, NodeOrToken, SyntaxElement, SyntaxNode, SyntaxToken,
|
2020-02-28 21:53:20 +01:00
|
|
|
};
|
2020-10-23 17:18:41 +02:00
|
|
|
use test_utils::mark;
|
2020-06-13 18:56:14 +02:00
|
|
|
|
2020-11-16 23:16:41 +02:00
|
|
|
#[derive(Debug, Clone)]
|
2020-09-18 23:40:11 +03:00
|
|
|
pub enum ImportScope {
|
2020-09-03 16:23:33 +02:00
|
|
|
File(ast::SourceFile),
|
|
|
|
|
Module(ast::ItemList),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ImportScope {
|
2020-11-02 16:31:38 +01:00
|
|
|
pub(crate) fn from(syntax: SyntaxNode) -> Option<Self> {
|
2020-09-03 16:23:33 +02:00
|
|
|
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)
|
2020-09-03 14:22:22 +02:00
|
|
|
} else {
|
2020-09-03 16:23:33 +02:00
|
|
|
ast::ItemList::cast(syntax).map(ImportScope::Module)
|
2020-06-13 18:56:14 +02:00
|
|
|
}
|
2020-09-03 16:23:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Determines the containing syntax node in which to insert a `use` statement affecting `position`.
|
2020-09-18 23:40:11 +03:00
|
|
|
pub fn find_insert_use_container(
|
2020-09-03 16:23:33 +02:00
|
|
|
position: &SyntaxNode,
|
2020-09-18 23:40:11 +03:00
|
|
|
sema: &Semantics<'_, RootDatabase>,
|
2020-09-03 16:23:33 +02:00
|
|
|
) -> Option<Self> {
|
2020-09-18 23:40:11 +03:00
|
|
|
sema.ancestors_with_macros(position.clone()).find_map(Self::from)
|
2020-09-03 16:23:33 +02:00
|
|
|
}
|
|
|
|
|
|
2020-09-18 23:40:11 +03:00
|
|
|
pub fn as_syntax_node(&self) -> &SyntaxNode {
|
2020-09-03 16:23:33 +02:00
|
|
|
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),
|
2020-09-03 18:44:39 +02:00
|
|
|
// don't insert the imports before the item list's opening curly brace
|
2020-09-03 16:23:33 +02:00
|
|
|
ImportScope::Module(item_list) => item_list
|
|
|
|
|
.l_curly_token()
|
|
|
|
|
.map(|b| (InsertPosition::After(b.into()), AddBlankLine::Around))
|
|
|
|
|
.unwrap_or((InsertPosition::First, AddBlankLine::AfterTwice)),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-12 13:52:24 +02:00
|
|
|
fn insert_pos_after_last_inner_element(&self) -> (InsertPosition<SyntaxElement>, AddBlankLine) {
|
|
|
|
|
self.as_syntax_node()
|
|
|
|
|
.children_with_tokens()
|
|
|
|
|
.filter(|child| match child {
|
|
|
|
|
NodeOrToken::Node(node) => is_inner_attribute(node.clone()),
|
|
|
|
|
NodeOrToken::Token(token) => is_inner_comment(token.clone()),
|
|
|
|
|
})
|
|
|
|
|
.last()
|
|
|
|
|
.map(|last_inner_element| {
|
|
|
|
|
(InsertPosition::After(last_inner_element.into()), AddBlankLine::BeforeTwice)
|
|
|
|
|
})
|
|
|
|
|
.unwrap_or_else(|| self.first_insert_pos())
|
2020-09-03 16:23:33 +02:00
|
|
|
}
|
2020-06-13 18:56:14 +02:00
|
|
|
}
|
2020-05-06 18:45:35 +02:00
|
|
|
|
2020-11-12 13:52:24 +02:00
|
|
|
fn is_inner_attribute(node: SyntaxNode) -> bool {
|
2020-11-11 11:37:41 +02:00
|
|
|
ast::Attr::cast(node).map(|attr| attr.kind()) == Some(ast::AttrKind::Inner)
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-12 13:52:24 +02:00
|
|
|
fn is_inner_comment(token: SyntaxToken) -> bool {
|
2020-11-11 11:37:41 +02:00
|
|
|
ast::Comment::cast(token).and_then(|comment| comment.kind().doc)
|
|
|
|
|
== Some(ast::CommentPlacement::Inner)
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-02 19:22:23 +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.
|
2020-09-18 23:40:11 +03:00
|
|
|
pub fn insert_use<'a>(
|
2020-09-03 16:23:33 +02:00
|
|
|
scope: &ImportScope,
|
2020-09-02 15:21:10 +02:00
|
|
|
path: ast::Path,
|
2020-09-02 19:22:23 +02:00
|
|
|
merge: Option<MergeBehaviour>,
|
2020-11-02 21:40:52 +01:00
|
|
|
) -> SyntaxRewriter<'a> {
|
2020-11-27 18:00:03 +02:00
|
|
|
let _p = profile::span("mod_path_to_ast");
|
2020-11-02 21:40:52 +01:00
|
|
|
let mut rewriter = SyntaxRewriter::default();
|
2020-09-02 15:21:10 +02:00
|
|
|
let use_item = make::use_(make::use_tree(path.clone(), None, None, false));
|
|
|
|
|
// merge into existing imports if possible
|
2020-09-02 19:22:23 +02:00
|
|
|
if let Some(mb) = merge {
|
2020-09-03 16:23:33 +02:00
|
|
|
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) {
|
2020-11-02 21:40:52 +01:00
|
|
|
rewriter.replace(existing_use.syntax(), merged.syntax());
|
|
|
|
|
return rewriter;
|
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
|
2020-09-03 16:23:33 +02:00
|
|
|
let (insert_position, add_blank) = find_insert_position(scope, path);
|
2020-09-02 15:21:10 +02:00
|
|
|
|
2020-10-23 17:18:41 +02:00
|
|
|
let indent = if let ident_level @ 1..=usize::MAX = scope.indent_level().0 as usize {
|
|
|
|
|
Some(make::tokens::whitespace(&" ".repeat(4 * ident_level)).into())
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
};
|
|
|
|
|
|
2020-09-02 15:21:10 +02:00
|
|
|
let to_insert: Vec<SyntaxElement> = {
|
|
|
|
|
let mut buf = Vec::new();
|
|
|
|
|
|
2020-09-02 19:22:23 +02:00
|
|
|
match add_blank {
|
2020-09-03 16:23:33 +02:00
|
|
|
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 19:22:23 +02:00
|
|
|
_ => (),
|
2020-09-02 15:21:10 +02:00
|
|
|
}
|
|
|
|
|
|
2020-10-23 17:18:41 +02:00
|
|
|
if add_blank.has_before() {
|
|
|
|
|
if let Some(indent) = indent.clone() {
|
|
|
|
|
mark::hit!(insert_use_indent_before);
|
|
|
|
|
buf.push(indent);
|
|
|
|
|
}
|
2020-09-03 16:23:33 +02:00
|
|
|
}
|
2020-10-23 17:18:41 +02:00
|
|
|
|
2020-09-02 15:21:10 +02:00
|
|
|
buf.push(use_item.syntax().clone().into());
|
|
|
|
|
|
2020-09-02 19:22:23 +02:00
|
|
|
match add_blank {
|
2020-09-03 16:23:33 +02:00
|
|
|
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 19:22:23 +02:00
|
|
|
_ => (),
|
2020-09-02 15:21:10 +02:00
|
|
|
}
|
2020-02-28 21:53:20 +01:00
|
|
|
|
2020-10-23 17:18:41 +02:00
|
|
|
// only add indentation *after* our stuff if there's another node directly after it
|
|
|
|
|
if add_blank.has_after() && matches!(insert_position, InsertPosition::Before(_)) {
|
|
|
|
|
if let Some(indent) = indent {
|
|
|
|
|
mark::hit!(insert_use_indent_after);
|
|
|
|
|
buf.push(indent);
|
|
|
|
|
}
|
|
|
|
|
} else if add_blank.has_after() && matches!(insert_position, InsertPosition::After(_)) {
|
|
|
|
|
mark::hit!(insert_use_no_indent_after);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-02 15:21:10 +02:00
|
|
|
buf
|
|
|
|
|
};
|
|
|
|
|
|
2020-11-02 21:40:52 +01:00
|
|
|
match insert_position {
|
|
|
|
|
InsertPosition::First => {
|
|
|
|
|
rewriter.insert_many_as_first_children(scope.as_syntax_node(), to_insert)
|
|
|
|
|
}
|
|
|
|
|
InsertPosition::Last => return rewriter, // actually unreachable
|
|
|
|
|
InsertPosition::Before(anchor) => rewriter.insert_many_before(&anchor, to_insert),
|
|
|
|
|
InsertPosition::After(anchor) => rewriter.insert_many_after(&anchor, to_insert),
|
|
|
|
|
}
|
|
|
|
|
rewriter
|
2020-09-02 15:21:10 +02:00
|
|
|
}
|
|
|
|
|
|
2020-09-05 16:15:16 +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,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-05 15:00:06 +02:00
|
|
|
pub(crate) fn try_merge_imports(
|
2020-09-12 23:54:49 +02:00
|
|
|
lhs: &ast::Use,
|
|
|
|
|
rhs: &ast::Use,
|
2020-09-02 15:21:10 +02:00
|
|
|
merge_behaviour: MergeBehaviour,
|
|
|
|
|
) -> Option<ast::Use> {
|
2020-09-05 15:51:26 +02:00
|
|
|
// don't merge imports with different visibilities
|
2020-09-12 23:54:49 +02:00
|
|
|
if !eq_visibility(lhs.visibility(), rhs.visibility()) {
|
2020-09-02 15:21:10 +02:00
|
|
|
return None;
|
|
|
|
|
}
|
2020-09-12 23:54:49 +02:00
|
|
|
let lhs_tree = lhs.use_tree()?;
|
|
|
|
|
let rhs_tree = rhs.use_tree()?;
|
|
|
|
|
let merged = try_merge_trees(&lhs_tree, &rhs_tree, merge_behaviour)?;
|
|
|
|
|
Some(lhs.with_use_tree(merged))
|
2020-09-02 15:21:10 +02:00
|
|
|
}
|
|
|
|
|
|
2020-09-05 15:00:06 +02:00
|
|
|
pub(crate) fn try_merge_trees(
|
2020-09-12 23:54:49 +02:00
|
|
|
lhs: &ast::UseTree,
|
|
|
|
|
rhs: &ast::UseTree,
|
2020-09-12 19:18:14 +02:00
|
|
|
merge: MergeBehaviour,
|
2020-09-02 15:21:10 +02:00
|
|
|
) -> Option<ast::UseTree> {
|
2020-09-12 23:54:49 +02:00
|
|
|
let lhs_path = lhs.path()?;
|
|
|
|
|
let rhs_path = rhs.path()?;
|
2020-09-02 15:21:10 +02:00
|
|
|
|
|
|
|
|
let (lhs_prefix, rhs_prefix) = common_prefix(&lhs_path, &rhs_path)?;
|
2020-10-14 01:39:58 +02:00
|
|
|
let (lhs, rhs) = if is_simple_path(lhs)
|
|
|
|
|
&& is_simple_path(rhs)
|
|
|
|
|
&& lhs_path == lhs_prefix
|
|
|
|
|
&& rhs_path == rhs_prefix
|
|
|
|
|
{
|
|
|
|
|
(lhs.clone(), rhs.clone())
|
|
|
|
|
} else {
|
|
|
|
|
(lhs.split_prefix(&lhs_prefix), rhs.split_prefix(&rhs_prefix))
|
|
|
|
|
};
|
2020-09-30 18:11:46 +02:00
|
|
|
recursive_merge(&lhs, &rhs, merge)
|
2020-09-12 19:18:14 +02:00
|
|
|
}
|
2020-09-02 15:21:10 +02:00
|
|
|
|
2020-09-12 23:27:01 +02:00
|
|
|
/// Recursively "zips" together lhs and rhs.
|
2020-09-12 19:18:14 +02:00
|
|
|
fn recursive_merge(
|
|
|
|
|
lhs: &ast::UseTree,
|
|
|
|
|
rhs: &ast::UseTree,
|
|
|
|
|
merge: MergeBehaviour,
|
2020-09-30 18:11:46 +02:00
|
|
|
) -> Option<ast::UseTree> {
|
2020-09-12 19:18:14 +02:00
|
|
|
let mut use_trees = lhs
|
|
|
|
|
.use_tree_list()
|
|
|
|
|
.into_iter()
|
|
|
|
|
.flat_map(|list| list.use_trees())
|
2020-09-30 18:11:46 +02:00
|
|
|
// we use Option here to early return from this function(this is not the same as a `filter` op)
|
|
|
|
|
.map(|tree| match merge.is_tree_allowed(&tree) {
|
|
|
|
|
true => Some(tree),
|
|
|
|
|
false => None,
|
2020-09-12 19:18:14 +02:00
|
|
|
})
|
|
|
|
|
.collect::<Option<Vec<_>>>()?;
|
2020-09-30 21:10:42 +02:00
|
|
|
use_trees.sort_unstable_by(|a, b| path_cmp_for_sort(a.path(), b.path()));
|
2020-09-12 19:18:14 +02:00
|
|
|
for rhs_t in rhs.use_tree_list().into_iter().flat_map(|list| list.use_trees()) {
|
2020-09-30 18:11:46 +02:00
|
|
|
if !merge.is_tree_allowed(&rhs_t) {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
2020-09-12 19:18:14 +02:00
|
|
|
let rhs_path = rhs_t.path();
|
2020-10-12 23:34:32 +02:00
|
|
|
match use_trees.binary_search_by(|lhs_t| {
|
|
|
|
|
let (lhs_t, rhs_t) = match lhs_t
|
|
|
|
|
.path()
|
|
|
|
|
.zip(rhs_path.clone())
|
|
|
|
|
.and_then(|(lhs, rhs)| common_prefix(&lhs, &rhs))
|
|
|
|
|
{
|
|
|
|
|
Some((lhs_p, rhs_p)) => (lhs_t.split_prefix(&lhs_p), rhs_t.split_prefix(&rhs_p)),
|
|
|
|
|
None => (lhs_t.clone(), rhs_t.clone()),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
path_cmp_bin_search(lhs_t.path(), rhs_t.path())
|
|
|
|
|
}) {
|
2020-09-12 19:18:14 +02:00
|
|
|
Ok(idx) => {
|
2020-09-12 23:27:01 +02:00
|
|
|
let lhs_t = &mut use_trees[idx];
|
|
|
|
|
let lhs_path = lhs_t.path()?;
|
2020-09-12 19:18:14 +02:00
|
|
|
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 {
|
2020-09-12 23:27:01 +02:00
|
|
|
let tree_is_self = |tree: ast::UseTree| {
|
|
|
|
|
tree.path().as_ref().map(path_is_self).unwrap_or(false)
|
|
|
|
|
};
|
2020-09-12 19:18:14 +02:00
|
|
|
// 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`
|
2020-09-12 23:54:49 +02:00
|
|
|
let tree_contains_self = |tree: &ast::UseTree| {
|
|
|
|
|
tree.use_tree_list()
|
|
|
|
|
.map(|tree_list| tree_list.use_trees().any(tree_is_self))
|
|
|
|
|
.unwrap_or(false)
|
|
|
|
|
};
|
|
|
|
|
match (tree_contains_self(&lhs_t), tree_contains_self(&rhs_t)) {
|
|
|
|
|
(true, false) => continue,
|
|
|
|
|
(false, true) => {
|
|
|
|
|
*lhs_t = rhs_t;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
_ => (),
|
2020-09-12 19:18:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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
|
2020-09-12 23:27:01 +02:00
|
|
|
if lhs_t.star_token().is_some() || rhs_t.star_token().is_some() {
|
|
|
|
|
*lhs_t = make::use_tree(
|
2020-09-12 19:18:14 +02:00
|
|
|
make::path_unqualified(make::path_segment_self()),
|
|
|
|
|
None,
|
|
|
|
|
None,
|
|
|
|
|
false,
|
|
|
|
|
);
|
2020-09-16 20:33:08 +02:00
|
|
|
use_trees.insert(idx, make::glob_use_tree());
|
2020-09-12 19:18:14 +02:00
|
|
|
continue;
|
|
|
|
|
}
|
2020-10-14 01:39:58 +02:00
|
|
|
|
|
|
|
|
if lhs_t.use_tree_list().is_none() && rhs_t.use_tree_list().is_none() {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2020-09-12 19:18:14 +02:00
|
|
|
}
|
2020-09-12 23:27:01 +02:00
|
|
|
let lhs = lhs_t.split_prefix(&lhs_prefix);
|
2020-09-12 19:18:14 +02:00
|
|
|
let rhs = rhs_t.split_prefix(&rhs_prefix);
|
|
|
|
|
match recursive_merge(&lhs, &rhs, merge) {
|
2020-09-30 18:11:46 +02:00
|
|
|
Some(use_tree) => use_trees[idx] = use_tree,
|
|
|
|
|
None => return None,
|
2020-09-12 19:18:14 +02:00
|
|
|
}
|
|
|
|
|
}
|
2020-09-12 23:54:49 +02:00
|
|
|
Err(_)
|
|
|
|
|
if merge == MergeBehaviour::Last
|
|
|
|
|
&& use_trees.len() > 0
|
|
|
|
|
&& rhs_t.use_tree_list().is_some() =>
|
|
|
|
|
{
|
|
|
|
|
return None
|
|
|
|
|
}
|
2020-09-12 19:18:14 +02:00
|
|
|
Err(idx) => {
|
|
|
|
|
use_trees.insert(idx, rhs_t);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-09-30 18:11:46 +02:00
|
|
|
Some(lhs.with_use_tree_list(make::use_tree_list(use_trees)))
|
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() => (),
|
2020-09-12 23:27:01 +02:00
|
|
|
_ => break res,
|
2020-09-02 15:21:10 +02:00
|
|
|
}
|
|
|
|
|
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-12 23:27:01 +02:00
|
|
|
_ => break res,
|
2020-02-28 21:53:20 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-09-02 15:21:10 +02:00
|
|
|
|
2020-10-14 01:39:58 +02:00
|
|
|
fn is_simple_path(use_tree: &ast::UseTree) -> bool {
|
|
|
|
|
use_tree.use_tree_list().is_none() && use_tree.star_token().is_none()
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-12 23:27:01 +02:00
|
|
|
fn path_is_self(path: &ast::Path) -> bool {
|
2020-09-12 19:18:14 +02:00
|
|
|
path.segment().and_then(|seg| seg.self_token()).is_some() && path.qualifier().is_none()
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-12 23:27:01 +02:00
|
|
|
#[inline]
|
2020-09-12 19:18:14 +02:00
|
|
|
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-30 18:11:46 +02:00
|
|
|
fn path_len(path: ast::Path) -> usize {
|
|
|
|
|
segment_iter(&path).count()
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-12 23:27:01 +02:00
|
|
|
/// Orders paths in the following way:
|
|
|
|
|
/// the sole self token comes first, after that come uppercase identifiers, then lowercase identifiers
|
2020-09-30 21:10:42 +02:00
|
|
|
// FIXME: rustfmt sorts lowercase idents before uppercase, in general we want to have the same ordering rustfmt has
|
2020-09-12 23:27:01 +02:00
|
|
|
// which is `self` and `super` first, then identifier imports with lowercase ones first, then glob imports and at last list imports.
|
|
|
|
|
// Example foo::{self, foo, baz, Baz, Qux, *, {Bar}}
|
2020-09-30 21:10:42 +02:00
|
|
|
fn path_cmp_for_sort(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(ref a), Some(ref b)) => match (path_is_self(a), path_is_self(b)) {
|
|
|
|
|
(true, true) => Ordering::Equal,
|
|
|
|
|
(true, false) => Ordering::Less,
|
|
|
|
|
(false, true) => Ordering::Greater,
|
|
|
|
|
(false, false) => path_cmp_short(a, b),
|
|
|
|
|
},
|
2020-09-12 23:27:01 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-30 21:10:42 +02:00
|
|
|
/// Path comparison func for binary searching for merging.
|
|
|
|
|
fn path_cmp_bin_search(lhs: Option<ast::Path>, rhs: Option<ast::Path>) -> Ordering {
|
2020-10-12 23:34:32 +02:00
|
|
|
match (lhs.and_then(|path| path.segment()), rhs.and_then(|path| path.segment())) {
|
2020-09-12 23:27:01 +02:00
|
|
|
(None, None) => Ordering::Equal,
|
|
|
|
|
(None, Some(_)) => Ordering::Less,
|
|
|
|
|
(Some(_), None) => Ordering::Greater,
|
2020-10-12 23:34:32 +02:00
|
|
|
(Some(ref a), Some(ref b)) => path_segment_cmp(a, b),
|
2020-09-12 23:27:01 +02:00
|
|
|
}
|
2020-02-28 21:53:20 +01:00
|
|
|
}
|
|
|
|
|
|
2020-09-30 21:10:42 +02:00
|
|
|
/// Short circuiting comparison, if both paths are equal until one of them ends they are considered
|
|
|
|
|
/// equal
|
|
|
|
|
fn path_cmp_short(a: &ast::Path, b: &ast::Path) -> Ordering {
|
|
|
|
|
let a = segment_iter(a);
|
|
|
|
|
let b = segment_iter(b);
|
|
|
|
|
// 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)
|
|
|
|
|
.find_map(|(a, b)| match path_segment_cmp(&a, &b) {
|
|
|
|
|
Ordering::Equal => None,
|
|
|
|
|
ord => Some(ord),
|
|
|
|
|
})
|
|
|
|
|
.unwrap_or(Ordering::Equal)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Compares to paths, if one ends earlier than the other the has_tl parameters decide which is
|
|
|
|
|
/// greater as a a path that has a tree list should be greater, while one that just ends without
|
|
|
|
|
/// a tree list should be considered less.
|
|
|
|
|
fn use_tree_path_cmp(a: &ast::Path, a_has_tl: bool, b: &ast::Path, b_has_tl: bool) -> Ordering {
|
|
|
|
|
let a_segments = segment_iter(a);
|
|
|
|
|
let b_segments = segment_iter(b);
|
|
|
|
|
// 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_segments
|
|
|
|
|
.zip_longest(b_segments)
|
|
|
|
|
.find_map(|zipped| match zipped {
|
|
|
|
|
EitherOrBoth::Both(ref a, ref b) => match path_segment_cmp(a, b) {
|
|
|
|
|
Ordering::Equal => None,
|
|
|
|
|
ord => Some(ord),
|
|
|
|
|
},
|
|
|
|
|
EitherOrBoth::Left(_) if !b_has_tl => Some(Ordering::Greater),
|
|
|
|
|
EitherOrBoth::Left(_) => Some(Ordering::Less),
|
|
|
|
|
EitherOrBoth::Right(_) if !a_has_tl => Some(Ordering::Less),
|
|
|
|
|
EitherOrBoth::Right(_) => Some(Ordering::Greater),
|
|
|
|
|
})
|
|
|
|
|
.unwrap_or(Ordering::Equal)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn path_segment_cmp(a: &ast::PathSegment, b: &ast::PathSegment) -> Ordering {
|
|
|
|
|
let a = a.name_ref();
|
|
|
|
|
let b = b.name_ref();
|
|
|
|
|
a.as_ref().map(ast::NameRef::text).cmp(&b.as_ref().map(ast::NameRef::text))
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-02 15:21:10 +02:00
|
|
|
/// What type of merges are allowed.
|
2020-09-12 11:55:01 +02:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
2020-09-02 15:21:10 +02:00
|
|
|
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-30 18:11:46 +02:00
|
|
|
impl MergeBehaviour {
|
|
|
|
|
#[inline]
|
|
|
|
|
fn is_tree_allowed(&self, tree: &ast::UseTree) -> bool {
|
|
|
|
|
match self {
|
|
|
|
|
MergeBehaviour::Full => true,
|
|
|
|
|
// only simple single segment paths are allowed
|
|
|
|
|
MergeBehaviour::Last => {
|
|
|
|
|
tree.use_tree_list().is_none() && tree.path().map(path_len) <= Some(1)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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,
|
|
|
|
|
_ => 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,
|
2020-09-02 19:22:23 +02:00
|
|
|
BeforeTwice,
|
2020-09-03 16:23:33 +02:00
|
|
|
Around,
|
2020-09-02 15:21:10 +02:00
|
|
|
After,
|
|
|
|
|
AfterTwice,
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-23 17:18:41 +02:00
|
|
|
impl AddBlankLine {
|
|
|
|
|
fn has_before(&self) -> bool {
|
|
|
|
|
matches!(self, AddBlankLine::Before | AddBlankLine::BeforeTwice | AddBlankLine::Around)
|
|
|
|
|
}
|
|
|
|
|
fn has_after(&self) -> bool {
|
|
|
|
|
matches!(self, AddBlankLine::After | AddBlankLine::AfterTwice | AddBlankLine::Around)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-02 15:21:10 +02:00
|
|
|
fn find_insert_position(
|
2020-09-03 16:23:33 +02:00
|
|
|
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
|
2020-09-03 16:23:33 +02:00
|
|
|
.as_syntax_node()
|
2020-09-02 15:21:10 +02:00
|
|
|
.children()
|
|
|
|
|
.filter_map(|node| ast::Use::cast(node.clone()).zip(Some(node)))
|
2020-09-30 21:10:42 +02:00
|
|
|
.flat_map(|(use_, node)| {
|
|
|
|
|
let tree = use_.use_tree()?;
|
|
|
|
|
let path = tree.path()?;
|
|
|
|
|
let has_tl = tree.use_tree_list().is_some();
|
|
|
|
|
Some((path, has_tl, node))
|
|
|
|
|
});
|
2020-09-02 15:21:10 +02:00
|
|
|
// 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()
|
2020-09-30 21:10:42 +02:00
|
|
|
.skip_while(|(path, ..)| ImportGroup::new(path) != group)
|
|
|
|
|
.take_while(|(path, ..)| ImportGroup::new(path) == group);
|
2020-09-02 15:21:10 +02:00
|
|
|
|
|
|
|
|
// 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
|
2020-09-30 21:10:42 +02:00
|
|
|
let post_insert = group_iter.inspect(|(.., node)| last = Some(node.clone())).find(
|
|
|
|
|
|&(ref path, has_tl, _)| {
|
|
|
|
|
use_tree_path_cmp(&insert_path, false, path, has_tl) != Ordering::Greater
|
|
|
|
|
},
|
|
|
|
|
);
|
2020-09-02 15:21:10 +02:00
|
|
|
match post_insert {
|
|
|
|
|
// insert our import before that element
|
2020-09-30 21:10:42 +02:00
|
|
|
Some((.., node)) => (InsertPosition::Before(node.into()), AddBlankLine::After),
|
2020-09-02 15:21:10 +02:00
|
|
|
// 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
|
2020-09-30 21:10:42 +02:00
|
|
|
.inspect(|(.., node)| last = Some(node.clone()))
|
|
|
|
|
.find(|(p, ..)| ImportGroup::new(p) > group);
|
2020-09-02 15:21:10 +02:00
|
|
|
match post_group {
|
2020-09-30 21:10:42 +02:00
|
|
|
Some((.., node)) => {
|
2020-09-02 15:21:10 +02:00
|
|
|
(InsertPosition::Before(node.into()), AddBlankLine::AfterTwice)
|
|
|
|
|
}
|
|
|
|
|
// there is no such group, so append after the last one
|
|
|
|
|
None => match last {
|
2020-09-02 19:22:23 +02:00
|
|
|
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
|
2020-11-12 13:52:24 +02:00
|
|
|
None => scope.insert_pos_after_last_inner_element(),
|
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;
|
|
|
|
|
|
2020-10-14 01:39:58 +02:00
|
|
|
#[test]
|
|
|
|
|
fn insert_existing() {
|
|
|
|
|
check_full("std::fs", "use std::fs;", "use std::fs;")
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-02 15:21:10 +02:00
|
|
|
#[test]
|
|
|
|
|
fn insert_start() {
|
|
|
|
|
check_none(
|
2020-09-02 19:22:23 +02:00
|
|
|
"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;",
|
2020-09-02 19:22:23 +02:00
|
|
|
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-10-23 17:18:41 +02:00
|
|
|
#[test]
|
|
|
|
|
fn insert_start_indent() {
|
|
|
|
|
mark::check!(insert_use_indent_after);
|
|
|
|
|
check_none(
|
|
|
|
|
"std::bar::AA",
|
|
|
|
|
r"
|
|
|
|
|
use std::bar::B;
|
|
|
|
|
use std::bar::D;",
|
|
|
|
|
r"
|
|
|
|
|
use std::bar::AA;
|
|
|
|
|
use std::bar::B;
|
|
|
|
|
use std::bar::D;",
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-02 15:21:10 +02:00
|
|
|
#[test]
|
|
|
|
|
fn insert_middle() {
|
|
|
|
|
check_none(
|
2020-09-02 19:22:23 +02:00
|
|
|
"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;",
|
2020-09-02 19:22:23 +02:00
|
|
|
r"
|
|
|
|
|
use std::bar::A;
|
2020-09-02 15:21:10 +02:00
|
|
|
use std::bar::D;
|
2020-09-02 19:22:23 +02:00
|
|
|
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-10-23 17:18:41 +02:00
|
|
|
#[test]
|
|
|
|
|
fn insert_middle_indent() {
|
|
|
|
|
check_none(
|
|
|
|
|
"std::bar::EE",
|
|
|
|
|
r"
|
|
|
|
|
use std::bar::A;
|
|
|
|
|
use std::bar::D;
|
|
|
|
|
use std::bar::F;
|
|
|
|
|
use std::bar::G;",
|
|
|
|
|
r"
|
|
|
|
|
use std::bar::A;
|
|
|
|
|
use std::bar::D;
|
|
|
|
|
use std::bar::EE;
|
|
|
|
|
use std::bar::F;
|
|
|
|
|
use std::bar::G;",
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-02 15:21:10 +02:00
|
|
|
#[test]
|
|
|
|
|
fn insert_end() {
|
|
|
|
|
check_none(
|
2020-09-02 19:22:23 +02:00
|
|
|
"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;",
|
2020-09-02 19:22:23 +02:00
|
|
|
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;
|
2020-09-02 19:22:23 +02:00
|
|
|
use std::bar::ZZ;",
|
2020-09-02 15:21:10 +02:00
|
|
|
)
|
2020-02-28 21:53:20 +01:00
|
|
|
}
|
|
|
|
|
|
2020-10-23 17:18:41 +02:00
|
|
|
#[test]
|
|
|
|
|
fn insert_end_indent() {
|
|
|
|
|
mark::check!(insert_use_indent_before);
|
|
|
|
|
check_none(
|
|
|
|
|
"std::bar::ZZ",
|
|
|
|
|
r"
|
|
|
|
|
use std::bar::A;
|
|
|
|
|
use std::bar::D;
|
|
|
|
|
use std::bar::F;
|
|
|
|
|
use std::bar::G;",
|
|
|
|
|
r"
|
|
|
|
|
use std::bar::A;
|
|
|
|
|
use std::bar::D;
|
|
|
|
|
use std::bar::F;
|
|
|
|
|
use std::bar::G;
|
|
|
|
|
use std::bar::ZZ;",
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-02 15:21:10 +02:00
|
|
|
#[test]
|
2020-09-02 19:22:23 +02:00
|
|
|
fn insert_middle_nested() {
|
2020-09-02 15:21:10 +02:00
|
|
|
check_none(
|
2020-09-02 19:22:23 +02:00
|
|
|
"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;",
|
2020-09-02 19:22:23 +02:00
|
|
|
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(
|
2020-09-02 19:22:23 +02:00
|
|
|
"foo::bar::GG",
|
|
|
|
|
r"
|
2020-10-23 17:18:41 +02:00
|
|
|
use std::bar::A;
|
|
|
|
|
use std::bar::D;
|
2020-09-02 15:21:10 +02:00
|
|
|
|
2020-10-23 17:18:41 +02:00
|
|
|
use foo::bar::F;
|
|
|
|
|
use foo::bar::H;",
|
2020-09-02 19:22:23 +02:00
|
|
|
r"
|
2020-10-23 17:18:41 +02:00
|
|
|
use std::bar::A;
|
|
|
|
|
use std::bar::D;
|
2020-09-02 15:21:10 +02:00
|
|
|
|
2020-10-23 17:18:41 +02:00
|
|
|
use foo::bar::F;
|
|
|
|
|
use foo::bar::GG;
|
|
|
|
|
use foo::bar::H;",
|
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_first_matching_group() {
|
|
|
|
|
check_none(
|
2020-09-02 19:22:23 +02:00
|
|
|
"foo::bar::GG",
|
|
|
|
|
r"
|
2020-10-23 17:18:41 +02:00
|
|
|
use foo::bar::A;
|
|
|
|
|
use foo::bar::D;
|
2020-09-02 15:21:10 +02:00
|
|
|
|
2020-10-23 17:18:41 +02:00
|
|
|
use std;
|
2020-09-02 15:21:10 +02:00
|
|
|
|
2020-10-23 17:18:41 +02:00
|
|
|
use foo::bar::F;
|
|
|
|
|
use foo::bar::H;",
|
2020-09-02 19:22:23 +02:00
|
|
|
r"
|
2020-10-23 17:18:41 +02:00
|
|
|
use foo::bar::A;
|
|
|
|
|
use foo::bar::D;
|
|
|
|
|
use foo::bar::GG;
|
2020-09-02 15:21:10 +02:00
|
|
|
|
2020-10-23 17:18:41 +02:00
|
|
|
use std;
|
2020-09-02 15:21:10 +02:00
|
|
|
|
2020-10-23 17:18:41 +02:00
|
|
|
use foo::bar::F;
|
|
|
|
|
use foo::bar::H;",
|
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]
|
2020-09-02 19:22:23 +02:00
|
|
|
fn insert_missing_group_std() {
|
2020-09-02 15:21:10 +02:00
|
|
|
check_none(
|
|
|
|
|
"std::fmt",
|
2020-09-02 19:22:23 +02:00
|
|
|
r"
|
2020-10-23 17:18:41 +02:00
|
|
|
use foo::bar::A;
|
|
|
|
|
use foo::bar::D;",
|
2020-09-02 19:22:23 +02:00
|
|
|
r"
|
2020-10-23 17:18:41 +02:00
|
|
|
use std::fmt;
|
2020-09-02 15:21:10 +02:00
|
|
|
|
2020-10-23 17:18:41 +02:00
|
|
|
use foo::bar::A;
|
|
|
|
|
use foo::bar::D;",
|
2020-09-02 15:21:10 +02:00
|
|
|
)
|
|
|
|
|
}
|
2020-02-28 21:53:20 +01:00
|
|
|
|
2020-09-02 19:22:23 +02: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
|
|
|
|
2020-10-23 17:18:41 +02:00
|
|
|
#[test]
|
|
|
|
|
fn insert_empty_module() {
|
|
|
|
|
mark::check!(insert_use_no_indent_after);
|
|
|
|
|
check(
|
|
|
|
|
"foo::bar",
|
|
|
|
|
"mod x {}",
|
|
|
|
|
r"{
|
|
|
|
|
use foo::bar;
|
|
|
|
|
}",
|
|
|
|
|
None,
|
|
|
|
|
true,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-03 14:38:34 +02: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)]
|
|
|
|
|
|
2020-11-11 11:37:41 +02:00
|
|
|
#![no_std]
|
2020-09-03 14:38:34 +02:00
|
|
|
fn main() {}",
|
|
|
|
|
r"#![allow(unused_imports)]
|
|
|
|
|
|
2020-11-11 11:37:41 +02:00
|
|
|
#![no_std]
|
2020-09-03 14:38:34 +02:00
|
|
|
|
2020-11-11 11:37:41 +02:00
|
|
|
use foo::bar;
|
2020-09-03 14:38:34 +02:00
|
|
|
fn main() {}",
|
2020-11-11 11:37:41 +02:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn inserts_after_single_line_inner_comments() {
|
|
|
|
|
check_none(
|
|
|
|
|
"foo::bar::Baz",
|
|
|
|
|
"//! Single line inner comments do not allow any code before them.",
|
|
|
|
|
r#"//! Single line inner comments do not allow any code before them.
|
|
|
|
|
|
|
|
|
|
use foo::bar::Baz;"#,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn inserts_after_multiline_inner_comments() {
|
|
|
|
|
check_none(
|
|
|
|
|
"foo::bar::Baz",
|
|
|
|
|
r#"/*! Multiline inner comments do not allow any code before them. */
|
|
|
|
|
|
2020-11-11 14:04:31 +02:00
|
|
|
/*! Still an inner comment, cannot place any code before. */
|
2020-11-11 11:37:41 +02:00
|
|
|
fn main() {}"#,
|
|
|
|
|
r#"/*! Multiline inner comments do not allow any code before them. */
|
|
|
|
|
|
2020-11-11 14:04:31 +02:00
|
|
|
/*! Still an inner comment, cannot place any code before. */
|
2020-11-11 11:37:41 +02:00
|
|
|
|
|
|
|
|
use foo::bar::Baz;
|
|
|
|
|
fn main() {}"#,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn inserts_after_all_inner_items() {
|
|
|
|
|
check_none(
|
|
|
|
|
"foo::bar::Baz",
|
|
|
|
|
r#"#![allow(unused_imports)]
|
|
|
|
|
/*! Multiline line comment 2 */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//! Single line comment 1
|
|
|
|
|
#![no_std]
|
|
|
|
|
//! Single line comment 2
|
|
|
|
|
fn main() {}"#,
|
|
|
|
|
r#"#![allow(unused_imports)]
|
|
|
|
|
/*! Multiline line comment 2 */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//! Single line comment 1
|
|
|
|
|
#![no_std]
|
|
|
|
|
//! Single line comment 2
|
|
|
|
|
|
|
|
|
|
use foo::bar::Baz;
|
|
|
|
|
fn main() {}"#,
|
2020-09-03 14:38:34 +02:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-02 15:21:10 +02:00
|
|
|
#[test]
|
2020-09-02 19:22:23 +02:00
|
|
|
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]
|
2020-09-02 19:22:23 +02:00
|
|
|
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-30 18:11:46 +02:00
|
|
|
#[test]
|
|
|
|
|
fn merge_last_into_self() {
|
|
|
|
|
check_last("foo::bar::baz", r"use foo::bar;", r"use foo::bar::{self, baz};");
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-02 15:21:10 +02:00
|
|
|
#[test]
|
2020-09-02 19:22:23 +02:00
|
|
|
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]
|
2020-09-02 19:22:23 +02:00
|
|
|
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;",
|
2020-09-12 19:18:14 +02:00
|
|
|
r"use std::foo::bar::{Baz, Qux};",
|
2020-09-02 15:50:29 +02:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2020-09-02 19:22:23 +02:00
|
|
|
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;",
|
2020-09-12 19:18:14 +02:00
|
|
|
r"use std::foo::bar::{Baz, Qux};",
|
2020-09-02 15:50:29 +02:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2020-09-02 19:22:23 +02:00
|
|
|
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};",
|
2020-09-12 19:18:14 +02:00
|
|
|
r"use std::foo::bar::{Baz, Quux, Qux};",
|
2020-09-02 15:50:29 +02:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2020-09-02 19:22:23 +02:00
|
|
|
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};",
|
2020-09-12 19:18:14 +02:00
|
|
|
r"use std::foo::bar::{Baz, Quux, Qux};",
|
2020-09-02 15:50:29 +02:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2020-09-02 19:22:23 +02:00
|
|
|
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}};",
|
2020-09-12 19:18:14 +02:00
|
|
|
r"use std::foo::bar::{Baz, Qux, quux::{Fez, Fizz}};",
|
2020-09-02 15:50:29 +02:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2020-09-02 19:22:23 +02:00
|
|
|
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;
|
2020-09-03 14:22:22 +02:00
|
|
|
use std::foo::bar::{Qux, quux::{Fez, Fizz}};",
|
2020-09-02 15:50:29 +02:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-12 19:18:14 +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-10-12 23:34:32 +02:00
|
|
|
#[test]
|
|
|
|
|
fn merge_groups_full_nested_long() {
|
|
|
|
|
check_full(
|
|
|
|
|
"std::foo::bar::Baz",
|
|
|
|
|
r"use std::{foo::bar::Qux};",
|
|
|
|
|
r"use std::{foo::bar::{Baz, Qux}};",
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn merge_groups_last_nested_long() {
|
|
|
|
|
check_full(
|
|
|
|
|
"std::foo::bar::Baz",
|
|
|
|
|
r"use std::{foo::bar::Qux};",
|
|
|
|
|
r"use std::{foo::bar::{Baz, Qux}};",
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-02 15:21:10 +02:00
|
|
|
#[test]
|
2020-09-02 19:22:23 +02:00
|
|
|
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
|
|
|
|
2020-09-03 18:44:39 +02: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]
|
2020-09-02 19:22:23 +02:00
|
|
|
#[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};",
|
2020-09-12 19:18:14 +02:00
|
|
|
r"use std::fmt::{self, Result};
|
2020-09-02 15:50:29 +02:00
|
|
|
use std::io;",
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-12 19:18:14 +02:00
|
|
|
#[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]
|
2020-09-02 19:22:23 +02:00
|
|
|
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
|
|
|
|
2020-09-02 19:22:23 +02:00
|
|
|
#[test]
|
2020-09-12 19:18:14 +02:00
|
|
|
fn merge_mod_into_glob() {
|
2020-09-02 19:22:23 +02:00
|
|
|
check_full(
|
|
|
|
|
"token::TokenKind",
|
|
|
|
|
r"use token::TokenKind::*;",
|
2020-09-16 20:33:08 +02:00
|
|
|
r"use token::TokenKind::{*, self};",
|
2020-09-02 19:22:23 +02:00
|
|
|
)
|
2020-09-12 19:18:14 +02:00
|
|
|
// FIXME: have it emit `use token::TokenKind::{self, *}`?
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn merge_self_glob() {
|
2020-09-16 20:33:08 +02:00
|
|
|
check_full("self", r"use self::*;", r"use self::{*, self};")
|
2020-09-12 19:18:14 +02:00
|
|
|
// FIXME: have it emit `use {self, *}`?
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn merge_glob_nested() {
|
|
|
|
|
check_full(
|
|
|
|
|
"foo::bar::quux::Fez",
|
2020-09-12 23:54:49 +02:00
|
|
|
r"use foo::bar::{Baz, quux::*};",
|
|
|
|
|
r"use foo::bar::{Baz, quux::{self::*, Fez}};",
|
2020-09-02 19:22:23 +02:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-02 19:34:01 +02:00
|
|
|
#[test]
|
2020-09-30 18:11:46 +02:00
|
|
|
fn skip_merge_last_too_long() {
|
|
|
|
|
check_last(
|
|
|
|
|
"foo::bar",
|
|
|
|
|
r"use foo::bar::baz::Qux;",
|
|
|
|
|
r"use foo::bar;
|
|
|
|
|
use foo::bar::baz::Qux;",
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn skip_merge_last_too_long2() {
|
|
|
|
|
check_last(
|
|
|
|
|
"foo::bar::baz::Qux",
|
|
|
|
|
r"use foo::bar;",
|
|
|
|
|
r"use foo::bar;
|
|
|
|
|
use foo::bar::baz::Qux;",
|
|
|
|
|
);
|
2020-09-05 15:00:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[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;",
|
2020-09-02 19:34:01 +02:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-12 19:18:14 +02:00
|
|
|
#[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>,
|
2020-10-23 17:18:41 +02:00
|
|
|
module: bool,
|
2020-09-02 15:21:10 +02:00
|
|
|
) {
|
2020-10-23 17:18:41 +02:00
|
|
|
let mut syntax = ast::SourceFile::parse(ra_fixture_before).tree().syntax().clone();
|
|
|
|
|
if module {
|
|
|
|
|
syntax = syntax.descendants().find_map(ast::Module::cast).unwrap().syntax().clone();
|
|
|
|
|
}
|
|
|
|
|
let file = super::ImportScope::from(syntax).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-11-02 21:40:52 +01:00
|
|
|
let rewriter = insert_use(&file, path, mb);
|
|
|
|
|
let result = rewriter.rewrite(file.as_syntax_node()).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) {
|
2020-10-23 17:18:41 +02:00
|
|
|
check(path, ra_fixture_before, ra_fixture_after, Some(MergeBehaviour::Full), false)
|
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) {
|
2020-10-23 17:18:41 +02:00
|
|
|
check(path, ra_fixture_before, ra_fixture_after, Some(MergeBehaviour::Last), false)
|
2020-09-02 15:21:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn check_none(path: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
|
2020-10-23 17:18:41 +02:00
|
|
|
check(path, ra_fixture_before, ra_fixture_after, None, false)
|
2020-08-13 11:56:11 +02:00
|
|
|
}
|
2020-09-12 19:18:14 +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);
|
2020-09-12 23:27:01 +02:00
|
|
|
assert_eq!(result.map(|u| u.to_string()), None);
|
2020-09-12 19:18:14 +02:00
|
|
|
}
|
2020-08-13 11:56:11 +02:00
|
|
|
}
|