2020-08-15 11:50:41 -05:00
|
|
|
use either::Either;
|
2021-11-10 15:02:50 -06:00
|
|
|
use hir::{AssocItem, HasVisibility, Module, ModuleDef, Name, PathResolution, ScopeDef};
|
2020-08-13 09:39:16 -05:00
|
|
|
use ide_db::{
|
2020-10-15 10:27:50 -05:00
|
|
|
defs::{Definition, NameRefClass},
|
2020-08-16 08:25:10 -05:00
|
|
|
search::SearchScope,
|
2020-08-02 14:56:54 -05:00
|
|
|
};
|
2021-05-08 06:38:56 -05:00
|
|
|
use stdx::never;
|
2020-08-31 08:47:42 -05:00
|
|
|
use syntax::{
|
|
|
|
ast::{self, make},
|
2021-05-08 06:38:56 -05:00
|
|
|
ted, AstNode, Direction, SyntaxNode, SyntaxToken, T,
|
2020-08-31 08:47:42 -05:00
|
|
|
};
|
2020-08-02 14:56:54 -05:00
|
|
|
|
|
|
|
use crate::{
|
2020-11-08 16:22:11 -06:00
|
|
|
assist_context::{AssistContext, Assists},
|
2020-08-02 14:56:54 -05:00
|
|
|
AssistId, AssistKind,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Assist: expand_glob_import
|
|
|
|
//
|
|
|
|
// Expands glob imports.
|
|
|
|
//
|
|
|
|
// ```
|
|
|
|
// mod foo {
|
|
|
|
// pub struct Bar;
|
|
|
|
// pub struct Baz;
|
|
|
|
// }
|
|
|
|
//
|
2021-01-06 14:15:48 -06:00
|
|
|
// use foo::*$0;
|
2020-08-02 14:56:54 -05:00
|
|
|
//
|
|
|
|
// fn qux(bar: Bar, baz: Baz) {}
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
|
|
|
// mod foo {
|
|
|
|
// pub struct Bar;
|
|
|
|
// pub struct Baz;
|
|
|
|
// }
|
|
|
|
//
|
2020-08-02 15:07:56 -05:00
|
|
|
// use foo::{Baz, Bar};
|
2020-08-02 14:56:54 -05:00
|
|
|
//
|
|
|
|
// fn qux(bar: Bar, baz: Baz) {}
|
|
|
|
// ```
|
|
|
|
pub(crate) fn expand_glob_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
2020-11-06 15:21:56 -06:00
|
|
|
let star = ctx.find_token_syntax_at_offset(T![*])?;
|
2021-05-08 06:38:56 -05:00
|
|
|
let use_tree = star.parent().and_then(ast::UseTree::cast)?;
|
2020-08-12 16:51:15 -05:00
|
|
|
let (parent, mod_path) = find_parent_and_path(&star)?;
|
2020-08-16 08:25:10 -05:00
|
|
|
let target_module = match ctx.sema.resolve_path(&mod_path)? {
|
2020-08-15 11:50:41 -05:00
|
|
|
PathResolution::Def(ModuleDef::Module(it)) => it,
|
|
|
|
_ => return None,
|
|
|
|
};
|
2020-08-02 14:56:54 -05:00
|
|
|
|
2021-01-30 09:19:21 -06:00
|
|
|
let current_scope = ctx.sema.scope(&star.parent()?);
|
2020-08-16 08:25:10 -05:00
|
|
|
let current_module = current_scope.module()?;
|
2020-08-02 14:56:54 -05:00
|
|
|
|
2020-08-16 08:25:10 -05:00
|
|
|
let refs_in_target = find_refs_in_mod(ctx, target_module, Some(current_module))?;
|
|
|
|
let imported_defs = find_imported_defs(ctx, star)?;
|
2020-08-02 14:56:54 -05:00
|
|
|
|
2021-05-26 10:34:50 -05:00
|
|
|
let target = parent.either(|n| n.syntax().clone(), |n| n.syntax().clone());
|
2020-08-02 14:56:54 -05:00
|
|
|
acc.add(
|
|
|
|
AssistId("expand_glob_import", AssistKind::RefactorRewrite),
|
|
|
|
"Expand glob import",
|
2020-08-12 16:51:15 -05:00
|
|
|
target.text_range(),
|
2020-08-02 14:56:54 -05:00
|
|
|
|builder| {
|
2021-05-16 06:18:49 -05:00
|
|
|
let use_tree = builder.make_mut(use_tree);
|
2021-05-08 06:38:56 -05:00
|
|
|
|
|
|
|
let names_to_import = find_names_to_import(ctx, refs_in_target, imported_defs);
|
|
|
|
let expanded = make::use_tree_list(names_to_import.iter().map(|n| {
|
2021-05-09 11:51:06 -05:00
|
|
|
let path = make::ext::ident_path(&n.to_string());
|
2021-05-08 06:38:56 -05:00
|
|
|
make::use_tree(path, None, None, false)
|
|
|
|
}))
|
|
|
|
.clone_for_update();
|
|
|
|
|
|
|
|
match use_tree.star_token() {
|
|
|
|
Some(star) => {
|
2021-10-19 09:31:30 -05:00
|
|
|
let needs_braces = use_tree.path().is_some() && names_to_import.len() != 1;
|
2021-05-08 06:38:56 -05:00
|
|
|
if needs_braces {
|
|
|
|
ted::replace(star, expanded.syntax())
|
|
|
|
} else {
|
|
|
|
let without_braces = expanded
|
|
|
|
.syntax()
|
|
|
|
.children_with_tokens()
|
|
|
|
.filter(|child| !matches!(child.kind(), T!['{'] | T!['}']))
|
|
|
|
.collect();
|
|
|
|
ted::replace_with_many(star, without_braces)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => never!(),
|
|
|
|
}
|
2020-08-02 14:56:54 -05:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-08-14 14:10:49 -05:00
|
|
|
fn find_parent_and_path(
|
|
|
|
star: &SyntaxToken,
|
|
|
|
) -> Option<(Either<ast::UseTree, ast::UseTreeList>, ast::Path)> {
|
|
|
|
return star.ancestors().find_map(|n| {
|
|
|
|
find_use_tree_list(n.clone())
|
2021-03-16 20:34:46 -05:00
|
|
|
.map(|(u, p)| (Either::Right(u), p))
|
|
|
|
.or_else(|| find_use_tree(n).map(|(u, p)| (Either::Left(u), p)))
|
2020-08-14 14:10:49 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
fn find_use_tree_list(n: SyntaxNode) -> Option<(ast::UseTreeList, ast::Path)> {
|
|
|
|
let use_tree_list = ast::UseTreeList::cast(n)?;
|
|
|
|
let path = use_tree_list.parent_use_tree().path()?;
|
|
|
|
Some((use_tree_list, path))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn find_use_tree(n: SyntaxNode) -> Option<(ast::UseTree, ast::Path)> {
|
2020-08-12 16:51:15 -05:00
|
|
|
let use_tree = ast::UseTree::cast(n)?;
|
|
|
|
let path = use_tree.path()?;
|
|
|
|
Some((use_tree, path))
|
2020-08-14 14:10:49 -05:00
|
|
|
}
|
2020-08-02 14:56:54 -05:00
|
|
|
}
|
|
|
|
|
2021-11-10 15:02:50 -06:00
|
|
|
fn def_is_referenced_in(def: Definition, ctx: &AssistContext) -> bool {
|
|
|
|
let search_scope = SearchScope::single_file(ctx.file_id());
|
|
|
|
def.usages(&ctx.sema).in_scope(search_scope).at_least_one()
|
2020-08-16 10:37:15 -05:00
|
|
|
}
|
|
|
|
|
2020-08-16 08:25:10 -05:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
struct Ref {
|
|
|
|
// could be alias
|
|
|
|
visible_name: Name,
|
2021-11-10 15:02:50 -06:00
|
|
|
def: Definition,
|
2020-08-16 08:25:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Ref {
|
|
|
|
fn from_scope_def(name: Name, scope_def: ScopeDef) -> Option<Self> {
|
|
|
|
match scope_def {
|
2021-11-10 15:02:50 -06:00
|
|
|
ScopeDef::ModuleDef(def) => {
|
|
|
|
Some(Ref { visible_name: name, def: Definition::from(def) })
|
|
|
|
}
|
|
|
|
ScopeDef::MacroDef(def) => {
|
|
|
|
Some(Ref { visible_name: name, def: Definition::Macro(def) })
|
|
|
|
}
|
2020-08-16 08:25:10 -05:00
|
|
|
_ => None,
|
2020-08-02 14:56:54 -05:00
|
|
|
}
|
|
|
|
}
|
2020-08-16 08:25:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
struct Refs(Vec<Ref>);
|
|
|
|
|
|
|
|
impl Refs {
|
2020-08-16 10:37:15 -05:00
|
|
|
fn used_refs(&self, ctx: &AssistContext) -> Refs {
|
|
|
|
Refs(
|
|
|
|
self.0
|
|
|
|
.clone()
|
|
|
|
.into_iter()
|
|
|
|
.filter(|r| {
|
2021-11-10 15:02:50 -06:00
|
|
|
if let Definition::Trait(tr) = r.def {
|
2021-03-21 07:13:34 -05:00
|
|
|
if tr.items(ctx.db()).into_iter().any(|ai| {
|
|
|
|
if let AssocItem::Function(f) = ai {
|
2021-11-10 15:02:50 -06:00
|
|
|
def_is_referenced_in(Definition::Function(f), ctx)
|
2021-03-21 07:13:34 -05:00
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}) {
|
2020-08-16 10:37:15 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-10 15:02:50 -06:00
|
|
|
def_is_referenced_in(r.def, ctx)
|
2020-08-16 10:37:15 -05:00
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
)
|
2020-08-02 14:56:54 -05:00
|
|
|
}
|
|
|
|
|
2021-11-10 15:02:50 -06:00
|
|
|
fn filter_out_by_defs(&self, defs: Vec<Definition>) -> Refs {
|
2020-08-16 08:25:10 -05:00
|
|
|
Refs(self.0.clone().into_iter().filter(|r| !defs.contains(&r.def)).collect())
|
|
|
|
}
|
2020-08-02 14:56:54 -05:00
|
|
|
}
|
|
|
|
|
2020-08-16 08:25:10 -05:00
|
|
|
fn find_refs_in_mod(
|
2020-08-02 14:56:54 -05:00
|
|
|
ctx: &AssistContext,
|
2020-08-16 08:25:10 -05:00
|
|
|
module: Module,
|
|
|
|
visible_from: Option<Module>,
|
|
|
|
) -> Option<Refs> {
|
2020-08-17 12:22:14 -05:00
|
|
|
if let Some(from) = visible_from {
|
|
|
|
if !is_mod_visible_from(ctx, module, from) {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-16 08:25:10 -05:00
|
|
|
let module_scope = module.scope(ctx.db(), visible_from);
|
|
|
|
let refs = module_scope.into_iter().filter_map(|(n, d)| Ref::from_scope_def(n, d)).collect();
|
|
|
|
Some(Refs(refs))
|
|
|
|
}
|
2020-08-02 14:56:54 -05:00
|
|
|
|
2020-08-17 12:22:14 -05:00
|
|
|
fn is_mod_visible_from(ctx: &AssistContext, module: Module, from: Module) -> bool {
|
|
|
|
match module.parent(ctx.db()) {
|
|
|
|
Some(parent) => {
|
2021-07-20 09:11:09 -05:00
|
|
|
module.visibility(ctx.db()).is_visible_from(ctx.db(), from.into())
|
|
|
|
&& is_mod_visible_from(ctx, parent, from)
|
2020-08-17 12:22:14 -05:00
|
|
|
}
|
|
|
|
None => true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-16 08:25:10 -05:00
|
|
|
// looks for name refs in parent use block's siblings
|
|
|
|
//
|
|
|
|
// mod bar {
|
|
|
|
// mod qux {
|
|
|
|
// struct Qux;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// pub use qux::Qux;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// ↓ ---------------
|
2021-01-06 14:15:48 -06:00
|
|
|
// use foo::*$0;
|
2020-08-16 08:25:10 -05:00
|
|
|
// use baz::Baz;
|
|
|
|
// ↑ ---------------
|
2021-11-10 15:02:50 -06:00
|
|
|
fn find_imported_defs(ctx: &AssistContext, star: SyntaxToken) -> Option<Vec<Definition>> {
|
2020-08-16 08:25:10 -05:00
|
|
|
let parent_use_item_syntax =
|
|
|
|
star.ancestors().find_map(|n| if ast::Use::can_cast(n.kind()) { Some(n) } else { None })?;
|
|
|
|
|
|
|
|
Some(
|
|
|
|
[Direction::Prev, Direction::Next]
|
2021-11-03 07:21:46 -05:00
|
|
|
.into_iter()
|
2021-09-13 11:50:19 -05:00
|
|
|
.flat_map(|dir| {
|
2020-08-16 08:25:10 -05:00
|
|
|
parent_use_item_syntax
|
|
|
|
.siblings(dir.to_owned())
|
|
|
|
.filter(|n| ast::Use::can_cast(n.kind()))
|
|
|
|
})
|
2021-09-13 11:50:19 -05:00
|
|
|
.flat_map(|n| n.descendants().filter_map(ast::NameRef::cast))
|
2020-10-15 10:27:50 -05:00
|
|
|
.filter_map(|r| match NameRefClass::classify(&ctx.sema, &r)? {
|
2021-11-10 15:02:50 -06:00
|
|
|
NameRefClass::Definition(
|
|
|
|
def
|
|
|
|
@
|
|
|
|
(Definition::Macro(_)
|
|
|
|
| Definition::Module(_)
|
|
|
|
| Definition::Function(_)
|
|
|
|
| Definition::Adt(_)
|
|
|
|
| Definition::Variant(_)
|
|
|
|
| Definition::Const(_)
|
|
|
|
| Definition::Static(_)
|
|
|
|
| Definition::Trait(_)
|
|
|
|
| Definition::TypeAlias(_)),
|
|
|
|
) => Some(def),
|
2020-08-16 08:25:10 -05:00
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
)
|
|
|
|
}
|
2020-08-05 03:29:00 -05:00
|
|
|
|
2020-08-16 08:25:10 -05:00
|
|
|
fn find_names_to_import(
|
|
|
|
ctx: &AssistContext,
|
|
|
|
refs_in_target: Refs,
|
2021-11-10 15:02:50 -06:00
|
|
|
imported_defs: Vec<Definition>,
|
2020-08-16 08:25:10 -05:00
|
|
|
) -> Vec<Name> {
|
2020-08-16 10:37:15 -05:00
|
|
|
let used_refs = refs_in_target.used_refs(ctx).filter_out_by_defs(imported_defs);
|
2020-08-16 08:25:10 -05:00
|
|
|
used_refs.0.iter().map(|r| r.visible_name.clone()).collect()
|
2020-08-02 14:56:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::tests::{check_assist, check_assist_not_applicable};
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn expanding_glob_import() {
|
|
|
|
check_assist(
|
|
|
|
expand_glob_import,
|
|
|
|
r"
|
|
|
|
mod foo {
|
|
|
|
pub struct Bar;
|
|
|
|
pub struct Baz;
|
|
|
|
pub struct Qux;
|
|
|
|
|
|
|
|
pub fn f() {}
|
|
|
|
}
|
|
|
|
|
2021-01-06 14:15:48 -06:00
|
|
|
use foo::*$0;
|
2020-08-02 14:56:54 -05:00
|
|
|
|
|
|
|
fn qux(bar: Bar, baz: Baz) {
|
|
|
|
f();
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
mod foo {
|
|
|
|
pub struct Bar;
|
|
|
|
pub struct Baz;
|
|
|
|
pub struct Qux;
|
|
|
|
|
|
|
|
pub fn f() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
use foo::{Baz, Bar, f};
|
|
|
|
|
|
|
|
fn qux(bar: Bar, baz: Baz) {
|
|
|
|
f();
|
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-10-19 09:31:30 -05:00
|
|
|
#[test]
|
|
|
|
fn expanding_glob_import_unused() {
|
|
|
|
check_assist(
|
|
|
|
expand_glob_import,
|
|
|
|
r"
|
|
|
|
mod foo {
|
|
|
|
pub struct Bar;
|
|
|
|
pub struct Baz;
|
|
|
|
pub struct Qux;
|
|
|
|
|
|
|
|
pub fn f() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
use foo::*$0;
|
|
|
|
|
|
|
|
fn qux() {}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
mod foo {
|
|
|
|
pub struct Bar;
|
|
|
|
pub struct Baz;
|
|
|
|
pub struct Qux;
|
|
|
|
|
|
|
|
pub fn f() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
use foo::{};
|
|
|
|
|
|
|
|
fn qux() {}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-08-02 14:56:54 -05:00
|
|
|
#[test]
|
|
|
|
fn expanding_glob_import_with_existing_explicit_names() {
|
|
|
|
check_assist(
|
|
|
|
expand_glob_import,
|
|
|
|
r"
|
|
|
|
mod foo {
|
|
|
|
pub struct Bar;
|
|
|
|
pub struct Baz;
|
|
|
|
pub struct Qux;
|
|
|
|
|
|
|
|
pub fn f() {}
|
|
|
|
}
|
|
|
|
|
2021-01-06 14:15:48 -06:00
|
|
|
use foo::{*$0, f};
|
2020-08-02 14:56:54 -05:00
|
|
|
|
|
|
|
fn qux(bar: Bar, baz: Baz) {
|
|
|
|
f();
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
mod foo {
|
|
|
|
pub struct Bar;
|
|
|
|
pub struct Baz;
|
|
|
|
pub struct Qux;
|
|
|
|
|
|
|
|
pub fn f() {}
|
|
|
|
}
|
|
|
|
|
2021-05-08 06:38:56 -05:00
|
|
|
use foo::{Baz, Bar, f};
|
2020-08-14 14:10:49 -05:00
|
|
|
|
|
|
|
fn qux(bar: Bar, baz: Baz) {
|
|
|
|
f();
|
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn expanding_glob_import_with_existing_uses_in_same_module() {
|
|
|
|
check_assist(
|
|
|
|
expand_glob_import,
|
|
|
|
r"
|
|
|
|
mod foo {
|
|
|
|
pub struct Bar;
|
|
|
|
pub struct Baz;
|
|
|
|
pub struct Qux;
|
|
|
|
|
|
|
|
pub fn f() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
use foo::Bar;
|
2021-01-06 14:15:48 -06:00
|
|
|
use foo::{*$0, f};
|
2020-08-14 14:10:49 -05:00
|
|
|
|
|
|
|
fn qux(bar: Bar, baz: Baz) {
|
|
|
|
f();
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
mod foo {
|
|
|
|
pub struct Bar;
|
|
|
|
pub struct Baz;
|
|
|
|
pub struct Qux;
|
|
|
|
|
|
|
|
pub fn f() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
use foo::Bar;
|
2021-05-08 06:38:56 -05:00
|
|
|
use foo::{Baz, f};
|
2020-08-02 14:56:54 -05:00
|
|
|
|
|
|
|
fn qux(bar: Bar, baz: Baz) {
|
|
|
|
f();
|
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn expanding_nested_glob_import() {
|
|
|
|
check_assist(
|
|
|
|
expand_glob_import,
|
|
|
|
r"
|
|
|
|
mod foo {
|
2020-08-12 16:51:15 -05:00
|
|
|
pub mod bar {
|
2020-08-02 14:56:54 -05:00
|
|
|
pub struct Bar;
|
|
|
|
pub struct Baz;
|
|
|
|
pub struct Qux;
|
|
|
|
|
|
|
|
pub fn f() {}
|
|
|
|
}
|
|
|
|
|
2020-08-12 16:51:15 -05:00
|
|
|
pub mod baz {
|
2020-08-02 14:56:54 -05:00
|
|
|
pub fn g() {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-06 14:15:48 -06:00
|
|
|
use foo::{bar::{*$0, f}, baz::*};
|
2020-08-02 14:56:54 -05:00
|
|
|
|
|
|
|
fn qux(bar: Bar, baz: Baz) {
|
|
|
|
f();
|
|
|
|
g();
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
mod foo {
|
2020-08-12 16:51:15 -05:00
|
|
|
pub mod bar {
|
2020-08-02 14:56:54 -05:00
|
|
|
pub struct Bar;
|
|
|
|
pub struct Baz;
|
|
|
|
pub struct Qux;
|
|
|
|
|
|
|
|
pub fn f() {}
|
|
|
|
}
|
|
|
|
|
2020-08-12 16:51:15 -05:00
|
|
|
pub mod baz {
|
2020-08-02 14:56:54 -05:00
|
|
|
pub fn g() {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-08 06:38:56 -05:00
|
|
|
use foo::{bar::{Baz, Bar, f}, baz::*};
|
2020-08-02 14:56:54 -05:00
|
|
|
|
|
|
|
fn qux(bar: Bar, baz: Baz) {
|
|
|
|
f();
|
|
|
|
g();
|
|
|
|
}
|
|
|
|
",
|
2020-08-12 16:51:15 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
check_assist(
|
|
|
|
expand_glob_import,
|
|
|
|
r"
|
|
|
|
mod foo {
|
|
|
|
pub mod bar {
|
|
|
|
pub struct Bar;
|
|
|
|
pub struct Baz;
|
|
|
|
pub struct Qux;
|
|
|
|
|
|
|
|
pub fn f() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub mod baz {
|
|
|
|
pub fn g() {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-06 14:15:48 -06:00
|
|
|
use foo::{bar::{Bar, Baz, f}, baz::*$0};
|
2020-08-12 16:51:15 -05:00
|
|
|
|
|
|
|
fn qux(bar: Bar, baz: Baz) {
|
|
|
|
f();
|
|
|
|
g();
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
mod foo {
|
|
|
|
pub mod bar {
|
|
|
|
pub struct Bar;
|
|
|
|
pub struct Baz;
|
|
|
|
pub struct Qux;
|
|
|
|
|
|
|
|
pub fn f() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub mod baz {
|
|
|
|
pub fn g() {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
use foo::{bar::{Bar, Baz, f}, baz::g};
|
|
|
|
|
|
|
|
fn qux(bar: Bar, baz: Baz) {
|
|
|
|
f();
|
|
|
|
g();
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
|
|
|
|
check_assist(
|
|
|
|
expand_glob_import,
|
|
|
|
r"
|
|
|
|
mod foo {
|
|
|
|
pub mod bar {
|
|
|
|
pub struct Bar;
|
|
|
|
pub struct Baz;
|
|
|
|
pub struct Qux;
|
|
|
|
|
|
|
|
pub fn f() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub mod baz {
|
|
|
|
pub fn g() {}
|
|
|
|
|
|
|
|
pub mod qux {
|
|
|
|
pub fn h() {}
|
|
|
|
pub fn m() {}
|
|
|
|
|
|
|
|
pub mod q {
|
|
|
|
pub fn j() {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
use foo::{
|
|
|
|
bar::{*, f},
|
2021-01-06 14:15:48 -06:00
|
|
|
baz::{g, qux::*$0}
|
2020-08-12 16:51:15 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
fn qux(bar: Bar, baz: Baz) {
|
|
|
|
f();
|
|
|
|
g();
|
|
|
|
h();
|
|
|
|
q::j();
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
mod foo {
|
|
|
|
pub mod bar {
|
|
|
|
pub struct Bar;
|
|
|
|
pub struct Baz;
|
|
|
|
pub struct Qux;
|
|
|
|
|
|
|
|
pub fn f() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub mod baz {
|
|
|
|
pub fn g() {}
|
|
|
|
|
|
|
|
pub mod qux {
|
|
|
|
pub fn h() {}
|
|
|
|
pub fn m() {}
|
|
|
|
|
|
|
|
pub mod q {
|
|
|
|
pub fn j() {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
use foo::{
|
|
|
|
bar::{*, f},
|
|
|
|
baz::{g, qux::{q, h}}
|
|
|
|
};
|
|
|
|
|
|
|
|
fn qux(bar: Bar, baz: Baz) {
|
|
|
|
f();
|
|
|
|
g();
|
|
|
|
h();
|
|
|
|
q::j();
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
|
|
|
|
check_assist(
|
|
|
|
expand_glob_import,
|
|
|
|
r"
|
|
|
|
mod foo {
|
|
|
|
pub mod bar {
|
|
|
|
pub struct Bar;
|
|
|
|
pub struct Baz;
|
|
|
|
pub struct Qux;
|
|
|
|
|
|
|
|
pub fn f() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub mod baz {
|
|
|
|
pub fn g() {}
|
|
|
|
|
|
|
|
pub mod qux {
|
|
|
|
pub fn h() {}
|
|
|
|
pub fn m() {}
|
|
|
|
|
|
|
|
pub mod q {
|
|
|
|
pub fn j() {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
use foo::{
|
|
|
|
bar::{*, f},
|
2021-01-06 14:15:48 -06:00
|
|
|
baz::{g, qux::{h, q::*$0}}
|
2020-08-12 16:51:15 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
fn qux(bar: Bar, baz: Baz) {
|
|
|
|
f();
|
|
|
|
g();
|
|
|
|
h();
|
|
|
|
j();
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
mod foo {
|
|
|
|
pub mod bar {
|
|
|
|
pub struct Bar;
|
|
|
|
pub struct Baz;
|
|
|
|
pub struct Qux;
|
|
|
|
|
|
|
|
pub fn f() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub mod baz {
|
|
|
|
pub fn g() {}
|
|
|
|
|
|
|
|
pub mod qux {
|
|
|
|
pub fn h() {}
|
|
|
|
pub fn m() {}
|
|
|
|
|
|
|
|
pub mod q {
|
|
|
|
pub fn j() {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
use foo::{
|
|
|
|
bar::{*, f},
|
|
|
|
baz::{g, qux::{h, q::j}}
|
|
|
|
};
|
|
|
|
|
2020-08-14 14:10:49 -05:00
|
|
|
fn qux(bar: Bar, baz: Baz) {
|
|
|
|
f();
|
|
|
|
g();
|
|
|
|
h();
|
|
|
|
j();
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
|
|
|
|
check_assist(
|
|
|
|
expand_glob_import,
|
|
|
|
r"
|
|
|
|
mod foo {
|
|
|
|
pub mod bar {
|
|
|
|
pub struct Bar;
|
|
|
|
pub struct Baz;
|
|
|
|
pub struct Qux;
|
|
|
|
|
|
|
|
pub fn f() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub mod baz {
|
|
|
|
pub fn g() {}
|
|
|
|
|
|
|
|
pub mod qux {
|
|
|
|
pub fn h() {}
|
|
|
|
pub fn m() {}
|
|
|
|
|
|
|
|
pub mod q {
|
|
|
|
pub fn j() {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
use foo::{
|
|
|
|
bar::{*, f},
|
2021-01-06 14:15:48 -06:00
|
|
|
baz::{g, qux::{q::j, *$0}}
|
2020-08-14 14:10:49 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
fn qux(bar: Bar, baz: Baz) {
|
|
|
|
f();
|
|
|
|
g();
|
|
|
|
h();
|
|
|
|
j();
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
mod foo {
|
|
|
|
pub mod bar {
|
|
|
|
pub struct Bar;
|
|
|
|
pub struct Baz;
|
|
|
|
pub struct Qux;
|
|
|
|
|
|
|
|
pub fn f() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub mod baz {
|
|
|
|
pub fn g() {}
|
|
|
|
|
|
|
|
pub mod qux {
|
|
|
|
pub fn h() {}
|
|
|
|
pub fn m() {}
|
|
|
|
|
|
|
|
pub mod q {
|
|
|
|
pub fn j() {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
use foo::{
|
|
|
|
bar::{*, f},
|
|
|
|
baz::{g, qux::{q::j, h}}
|
|
|
|
};
|
|
|
|
|
2020-08-12 16:51:15 -05:00
|
|
|
fn qux(bar: Bar, baz: Baz) {
|
|
|
|
f();
|
|
|
|
g();
|
|
|
|
h();
|
|
|
|
j();
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
2020-08-02 14:56:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn expanding_glob_import_with_macro_defs() {
|
2020-08-16 08:42:44 -05:00
|
|
|
// FIXME: this is currently fails because `Definition::find_usages` ignores macros
|
2020-08-16 08:25:10 -05:00
|
|
|
// https://github.com/rust-analyzer/rust-analyzer/issues/3484
|
|
|
|
//
|
|
|
|
// check_assist(
|
|
|
|
// expand_glob_import,
|
|
|
|
// r"
|
|
|
|
// //- /lib.rs crate:foo
|
|
|
|
// #[macro_export]
|
|
|
|
// macro_rules! bar {
|
|
|
|
// () => ()
|
|
|
|
// }
|
|
|
|
|
|
|
|
// pub fn baz() {}
|
|
|
|
|
|
|
|
// //- /main.rs crate:main deps:foo
|
2021-01-06 14:15:48 -06:00
|
|
|
// use foo::*$0;
|
2020-08-16 08:25:10 -05:00
|
|
|
|
|
|
|
// fn main() {
|
|
|
|
// bar!();
|
|
|
|
// baz();
|
|
|
|
// }
|
|
|
|
// ",
|
|
|
|
// r"
|
|
|
|
// use foo::{bar, baz};
|
|
|
|
|
|
|
|
// fn main() {
|
|
|
|
// bar!();
|
|
|
|
// baz();
|
|
|
|
// }
|
|
|
|
// ",
|
|
|
|
// )
|
2020-08-02 14:56:54 -05:00
|
|
|
}
|
|
|
|
|
2020-08-05 03:29:00 -05:00
|
|
|
#[test]
|
|
|
|
fn expanding_glob_import_with_trait_method_uses() {
|
|
|
|
check_assist(
|
|
|
|
expand_glob_import,
|
|
|
|
r"
|
|
|
|
//- /lib.rs crate:foo
|
|
|
|
pub trait Tr {
|
|
|
|
fn method(&self) {}
|
|
|
|
}
|
|
|
|
impl Tr for () {}
|
|
|
|
|
|
|
|
//- /main.rs crate:main deps:foo
|
2021-01-06 14:15:48 -06:00
|
|
|
use foo::*$0;
|
2020-08-05 03:29:00 -05:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
().method();
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
use foo::Tr;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
().method();
|
|
|
|
}
|
|
|
|
",
|
2020-08-16 10:37:15 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
check_assist(
|
|
|
|
expand_glob_import,
|
|
|
|
r"
|
|
|
|
//- /lib.rs crate:foo
|
|
|
|
pub trait Tr {
|
|
|
|
fn method(&self) {}
|
|
|
|
}
|
|
|
|
impl Tr for () {}
|
|
|
|
|
|
|
|
pub trait Tr2 {
|
|
|
|
fn method2(&self) {}
|
|
|
|
}
|
|
|
|
impl Tr2 for () {}
|
|
|
|
|
|
|
|
//- /main.rs crate:main deps:foo
|
2021-01-06 14:15:48 -06:00
|
|
|
use foo::*$0;
|
2020-08-16 10:37:15 -05:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
().method();
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
use foo::Tr;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
().method();
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
2020-08-05 03:29:00 -05:00
|
|
|
}
|
|
|
|
|
2020-08-17 12:22:14 -05:00
|
|
|
#[test]
|
|
|
|
fn expanding_is_not_applicable_if_target_module_is_not_accessible_from_current_scope() {
|
|
|
|
check_assist_not_applicable(
|
|
|
|
expand_glob_import,
|
|
|
|
r"
|
|
|
|
mod foo {
|
|
|
|
mod bar {
|
|
|
|
pub struct Bar;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-06 14:15:48 -06:00
|
|
|
use foo::bar::*$0;
|
2020-08-17 12:22:14 -05:00
|
|
|
|
|
|
|
fn baz(bar: Bar) {}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
|
2021-07-20 09:49:02 -05:00
|
|
|
check_assist_not_applicable(
|
|
|
|
expand_glob_import,
|
|
|
|
r"
|
|
|
|
mod foo {
|
|
|
|
mod bar {
|
|
|
|
pub mod baz {
|
|
|
|
pub struct Baz;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-17 12:22:14 -05:00
|
|
|
|
2021-07-20 09:49:02 -05:00
|
|
|
use foo::bar::baz::*$0;
|
2020-08-17 12:22:14 -05:00
|
|
|
|
2021-07-20 09:49:02 -05:00
|
|
|
fn qux(baz: Baz) {}
|
|
|
|
",
|
|
|
|
);
|
2020-08-17 12:22:14 -05:00
|
|
|
}
|
|
|
|
|
2020-08-02 14:56:54 -05:00
|
|
|
#[test]
|
|
|
|
fn expanding_is_not_applicable_if_cursor_is_not_in_star_token() {
|
|
|
|
check_assist_not_applicable(
|
|
|
|
expand_glob_import,
|
|
|
|
r"
|
|
|
|
mod foo {
|
|
|
|
pub struct Bar;
|
|
|
|
pub struct Baz;
|
|
|
|
pub struct Qux;
|
|
|
|
}
|
|
|
|
|
2021-01-06 14:15:48 -06:00
|
|
|
use foo::Bar$0;
|
2020-08-02 14:56:54 -05:00
|
|
|
|
|
|
|
fn qux(bar: Bar, baz: Baz) {}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
2020-11-08 16:22:11 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn expanding_glob_import_single_nested_glob_only() {
|
|
|
|
check_assist(
|
|
|
|
expand_glob_import,
|
|
|
|
r"
|
|
|
|
mod foo {
|
|
|
|
pub struct Bar;
|
|
|
|
}
|
|
|
|
|
2021-01-06 14:15:48 -06:00
|
|
|
use foo::{*$0};
|
2020-11-08 16:22:11 -06:00
|
|
|
|
|
|
|
struct Baz {
|
|
|
|
bar: Bar
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
mod foo {
|
|
|
|
pub struct Bar;
|
|
|
|
}
|
|
|
|
|
2021-05-08 06:38:56 -05:00
|
|
|
use foo::{Bar};
|
2020-11-08 16:22:11 -06:00
|
|
|
|
|
|
|
struct Baz {
|
|
|
|
bar: Bar
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
2020-08-02 14:56:54 -05:00
|
|
|
}
|