2022-05-20 17:00:47 -05:00
|
|
|
use std::cmp::Reverse;
|
|
|
|
|
|
|
|
use hir::{db::HirDatabase, Module};
|
2022-03-06 12:01:30 -06:00
|
|
|
use ide_db::{
|
|
|
|
helpers::mod_path_to_ast,
|
|
|
|
imports::{
|
2022-05-20 17:00:47 -05:00
|
|
|
import_assets::{ImportAssets, ImportCandidate, LocatedImport},
|
2022-03-06 12:01:30 -06:00
|
|
|
insert_use::{insert_use, ImportScope},
|
|
|
|
},
|
2020-11-24 15:25:13 -06:00
|
|
|
};
|
2022-02-21 05:57:57 -06:00
|
|
|
use syntax::{ast, AstNode, NodeOrToken, SyntaxElement};
|
2020-10-13 11:52:56 -05:00
|
|
|
|
2021-01-16 11:33:36 -06:00
|
|
|
use crate::{AssistContext, AssistId, AssistKind, Assists, GroupLabel};
|
2019-12-23 18:19:09 -06:00
|
|
|
|
2020-10-19 08:53:48 -05:00
|
|
|
// Feature: Auto Import
|
2020-10-15 07:44:46 -05:00
|
|
|
//
|
|
|
|
// Using the `auto-import` assist it is possible to insert missing imports for unresolved items.
|
|
|
|
// When inserting an import it will do so in a structured manner by keeping imports grouped,
|
|
|
|
// separated by a newline in the following order:
|
|
|
|
//
|
|
|
|
// - `std` and `core`
|
|
|
|
// - External Crates
|
|
|
|
// - Current Crate, paths prefixed by `crate`
|
|
|
|
// - Current Module, paths prefixed by `self`
|
|
|
|
// - Super Module, paths prefixed by `super`
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
// ```rust
|
|
|
|
// use std::fs::File;
|
|
|
|
//
|
|
|
|
// use itertools::Itertools;
|
|
|
|
// use syntax::ast;
|
|
|
|
//
|
|
|
|
// use crate::utils::insert_use;
|
|
|
|
//
|
|
|
|
// use self::auto_import;
|
|
|
|
//
|
|
|
|
// use super::AssistContext;
|
|
|
|
// ```
|
|
|
|
//
|
2021-05-20 08:29:21 -05:00
|
|
|
// .Import Granularity
|
2020-10-15 07:44:46 -05:00
|
|
|
//
|
2022-07-11 01:42:49 -05:00
|
|
|
// It is possible to configure how use-trees are merged with the `imports.granularity.group` setting.
|
2020-10-15 07:44:46 -05:00
|
|
|
// It has the following configurations:
|
|
|
|
//
|
2021-05-20 08:29:21 -05:00
|
|
|
// - `crate`: Merge imports from the same crate into a single use statement. This kind of
|
2020-10-15 07:44:46 -05:00
|
|
|
// nesting is only supported in Rust versions later than 1.24.
|
2021-05-20 08:29:21 -05:00
|
|
|
// - `module`: Merge imports from the same module into a single use statement.
|
|
|
|
// - `item`: Don't merge imports at all, creating one import per item.
|
|
|
|
// - `preserve`: Do not change the granularity of any imports. For auto-import this has the same
|
|
|
|
// effect as `item`.
|
2020-10-15 07:44:46 -05:00
|
|
|
//
|
2022-07-11 01:42:49 -05:00
|
|
|
// In `VS Code` the configuration for this is `rust-analyzer.imports.granularity.group`.
|
2020-10-15 07:44:46 -05:00
|
|
|
//
|
|
|
|
// .Import Prefix
|
|
|
|
//
|
2022-07-11 01:42:49 -05:00
|
|
|
// The style of imports in the same crate is configurable through the `imports.prefix` setting.
|
2020-10-15 07:44:46 -05:00
|
|
|
// It has the following configurations:
|
|
|
|
//
|
2022-03-30 05:45:14 -05:00
|
|
|
// - `crate`: This setting will force paths to be always absolute, starting with the `crate`
|
2020-10-15 07:44:46 -05:00
|
|
|
// prefix, unless the item is defined outside of the current crate.
|
2022-03-30 05:45:14 -05:00
|
|
|
// - `self`: This setting will force paths that are relative to the current module to always
|
2020-10-15 07:44:46 -05:00
|
|
|
// start with `self`. This will result in paths that always start with either `crate`, `self`,
|
|
|
|
// `super` or an extern crate identifier.
|
|
|
|
// - `plain`: This setting does not impose any restrictions in imports.
|
|
|
|
//
|
2022-07-11 01:42:49 -05:00
|
|
|
// In `VS Code` the configuration for this is `rust-analyzer.imports.prefix`.
|
2021-03-30 18:08:10 -05:00
|
|
|
//
|
|
|
|
// image::https://user-images.githubusercontent.com/48062697/113020673-b85be580-917a-11eb-9022-59585f35d4f8.gif[]
|
2020-10-15 07:44:46 -05:00
|
|
|
|
2019-12-23 18:19:09 -06:00
|
|
|
// Assist: auto_import
|
|
|
|
//
|
|
|
|
// If the name is unresolved, provides all possible imports for it.
|
|
|
|
//
|
|
|
|
// ```
|
|
|
|
// fn main() {
|
2021-01-06 14:15:48 -06:00
|
|
|
// let map = HashMap$0::new();
|
2019-12-23 18:19:09 -06:00
|
|
|
// }
|
2020-02-06 11:10:25 -06:00
|
|
|
// # pub mod std { pub mod collections { pub struct HashMap { } } }
|
2019-12-23 18:19:09 -06:00
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
|
|
|
// use std::collections::HashMap;
|
|
|
|
//
|
|
|
|
// fn main() {
|
2020-02-06 11:10:25 -06:00
|
|
|
// let map = HashMap::new();
|
2019-12-23 18:19:09 -06:00
|
|
|
// }
|
2020-02-06 11:10:25 -06:00
|
|
|
// # pub mod std { pub mod collections { pub struct HashMap { } } }
|
2019-12-23 18:19:09 -06:00
|
|
|
// ```
|
2022-07-20 08:02:08 -05:00
|
|
|
pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
|
2021-01-05 02:34:03 -06:00
|
|
|
let (import_assets, syntax_under_caret) = find_importable_node(ctx)?;
|
2022-09-09 13:04:56 -05:00
|
|
|
let mut proposed_imports = import_assets.search_for_imports(
|
|
|
|
&ctx.sema,
|
|
|
|
ctx.config.insert_use.prefix_kind,
|
2022-09-13 08:09:40 -05:00
|
|
|
ctx.config.prefer_no_std,
|
2022-09-09 13:04:56 -05:00
|
|
|
);
|
2019-12-23 18:19:09 -06:00
|
|
|
if proposed_imports.is_empty() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2021-10-28 11:40:38 -05:00
|
|
|
let range = match &syntax_under_caret {
|
|
|
|
NodeOrToken::Node(node) => ctx.sema.original_range(node).range,
|
|
|
|
NodeOrToken::Token(token) => token.text_range(),
|
|
|
|
};
|
2021-03-10 03:30:25 -06:00
|
|
|
let group_label = group_label(import_assets.import_candidate());
|
2021-11-03 15:12:36 -05:00
|
|
|
let scope = ImportScope::find_insert_use_container(
|
2021-10-28 11:40:38 -05:00
|
|
|
&match syntax_under_caret {
|
|
|
|
NodeOrToken::Node(it) => it,
|
|
|
|
NodeOrToken::Token(it) => it.parent()?,
|
|
|
|
},
|
|
|
|
&ctx.sema,
|
|
|
|
)?;
|
2021-09-01 14:51:28 -05:00
|
|
|
|
|
|
|
// we aren't interested in different namespaces
|
|
|
|
proposed_imports.dedup_by(|a, b| a.import_path == b.import_path);
|
2022-05-20 17:00:47 -05:00
|
|
|
|
2022-05-28 04:27:28 -05:00
|
|
|
let current_node = match ctx.covering_element() {
|
|
|
|
NodeOrToken::Node(node) => Some(node),
|
|
|
|
NodeOrToken::Token(token) => token.parent(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let current_module =
|
|
|
|
current_node.as_ref().and_then(|node| ctx.sema.scope(node)).map(|scope| scope.module());
|
|
|
|
|
2022-05-20 17:00:47 -05:00
|
|
|
// prioritize more relevant imports
|
2022-05-28 04:27:28 -05:00
|
|
|
proposed_imports
|
|
|
|
.sort_by_key(|import| Reverse(relevance_score(ctx, import, current_module.as_ref())));
|
2022-05-20 17:00:47 -05:00
|
|
|
|
2021-02-24 17:06:31 -06:00
|
|
|
for import in proposed_imports {
|
2022-10-10 10:04:38 -05:00
|
|
|
let import_path = import.import_path;
|
|
|
|
|
2020-05-06 11:45:35 -05:00
|
|
|
acc.add_group(
|
2021-03-10 03:30:25 -06:00
|
|
|
&group_label,
|
2020-07-02 16:48:35 -05:00
|
|
|
AssistId("auto_import", AssistKind::QuickFix),
|
2022-10-10 10:04:38 -05:00
|
|
|
format!("Import `{import_path}`"),
|
2020-05-06 11:45:35 -05:00
|
|
|
range,
|
|
|
|
|builder| {
|
2021-04-19 19:05:22 -05:00
|
|
|
let scope = match scope.clone() {
|
2021-05-16 06:18:49 -05:00
|
|
|
ImportScope::File(it) => ImportScope::File(builder.make_mut(it)),
|
|
|
|
ImportScope::Module(it) => ImportScope::Module(builder.make_mut(it)),
|
2021-06-18 16:53:41 -05:00
|
|
|
ImportScope::Block(it) => ImportScope::Block(builder.make_mut(it)),
|
2021-04-19 19:05:22 -05:00
|
|
|
};
|
2022-10-10 10:04:38 -05:00
|
|
|
insert_use(&scope, mod_path_to_ast(&import_path), &ctx.config.insert_use);
|
2020-05-06 11:45:35 -05:00
|
|
|
},
|
|
|
|
);
|
2020-02-09 07:30:27 -06:00
|
|
|
}
|
2020-05-06 11:45:35 -05:00
|
|
|
Some(())
|
2019-12-23 18:19:09 -06:00
|
|
|
}
|
|
|
|
|
2022-07-20 08:06:15 -05:00
|
|
|
pub(super) fn find_importable_node(
|
|
|
|
ctx: &AssistContext<'_>,
|
|
|
|
) -> Option<(ImportAssets, SyntaxElement)> {
|
2021-01-05 02:34:03 -06:00
|
|
|
if let Some(path_under_caret) = ctx.find_node_at_offset_with_descend::<ast::Path>() {
|
|
|
|
ImportAssets::for_exact_path(&path_under_caret, &ctx.sema)
|
2021-10-28 11:40:38 -05:00
|
|
|
.zip(Some(path_under_caret.syntax().clone().into()))
|
2021-01-05 02:34:03 -06:00
|
|
|
} else if let Some(method_under_caret) =
|
|
|
|
ctx.find_node_at_offset_with_descend::<ast::MethodCallExpr>()
|
|
|
|
{
|
|
|
|
ImportAssets::for_method_call(&method_under_caret, &ctx.sema)
|
2021-10-28 11:40:38 -05:00
|
|
|
.zip(Some(method_under_caret.syntax().clone().into()))
|
2022-10-15 11:00:32 -05:00
|
|
|
} else if let Some(_) = ctx.find_node_at_offset_with_descend::<ast::Param>() {
|
|
|
|
None
|
2021-07-10 10:57:33 -05:00
|
|
|
} else if let Some(pat) = ctx
|
|
|
|
.find_node_at_offset_with_descend::<ast::IdentPat>()
|
|
|
|
.filter(ast::IdentPat::is_simple_ident)
|
|
|
|
{
|
2021-10-28 11:40:38 -05:00
|
|
|
ImportAssets::for_ident_pat(&ctx.sema, &pat).zip(Some(pat.syntax().clone().into()))
|
2021-01-05 02:34:03 -06:00
|
|
|
} else {
|
2022-02-21 05:57:57 -06:00
|
|
|
None
|
2021-01-05 02:34:03 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-10 03:30:25 -06:00
|
|
|
fn group_label(import_candidate: &ImportCandidate) -> GroupLabel {
|
2020-10-12 11:11:36 -05:00
|
|
|
let name = match import_candidate {
|
2021-01-05 02:34:03 -06:00
|
|
|
ImportCandidate::Path(candidate) => format!("Import {}", candidate.name.text()),
|
2020-10-13 13:02:14 -05:00
|
|
|
ImportCandidate::TraitAssocItem(candidate) => {
|
2021-03-08 06:09:20 -06:00
|
|
|
format!("Import a trait for item {}", candidate.assoc_item_name.text())
|
2020-02-12 06:41:34 -06:00
|
|
|
}
|
2020-10-13 13:02:14 -05:00
|
|
|
ImportCandidate::TraitMethod(candidate) => {
|
2021-03-08 06:09:20 -06:00
|
|
|
format!("Import a trait for method {}", candidate.assoc_item_name.text())
|
2020-02-10 08:55:20 -06:00
|
|
|
}
|
2020-10-12 11:11:36 -05:00
|
|
|
};
|
|
|
|
GroupLabel(name)
|
2020-02-11 10:24:43 -06:00
|
|
|
}
|
|
|
|
|
2022-05-20 17:00:47 -05:00
|
|
|
/// Determine how relevant a given import is in the current context. Higher scores are more
|
|
|
|
/// relevant.
|
2022-05-28 04:27:28 -05:00
|
|
|
fn relevance_score(
|
2022-07-20 08:02:08 -05:00
|
|
|
ctx: &AssistContext<'_>,
|
2022-05-28 04:27:28 -05:00
|
|
|
import: &LocatedImport,
|
|
|
|
current_module: Option<&Module>,
|
|
|
|
) -> i32 {
|
2022-05-20 17:00:47 -05:00
|
|
|
let mut score = 0;
|
|
|
|
|
|
|
|
let db = ctx.db();
|
|
|
|
|
|
|
|
let item_module = match import.item_to_import {
|
|
|
|
hir::ItemInNs::Types(item) | hir::ItemInNs::Values(item) => item.module(db),
|
|
|
|
hir::ItemInNs::Macros(makro) => Some(makro.module(db)),
|
|
|
|
};
|
|
|
|
|
2022-05-28 04:27:28 -05:00
|
|
|
match item_module.zip(current_module) {
|
|
|
|
// get the distance between the imported path and the current module
|
|
|
|
// (prefer items that are more local)
|
|
|
|
Some((item_module, current_module)) => {
|
|
|
|
score -= module_distance_hueristic(db, ¤t_module, &item_module) as i32;
|
2022-05-20 17:00:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// could not find relevant modules, so just use the length of the path as an estimate
|
|
|
|
None => return -(2 * import.import_path.len() as i32),
|
|
|
|
}
|
|
|
|
|
|
|
|
score
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A heuristic that gives a higher score to modules that are more separated.
|
2022-05-28 04:27:28 -05:00
|
|
|
fn module_distance_hueristic(db: &dyn HirDatabase, current: &Module, item: &Module) -> usize {
|
|
|
|
// get the path starting from the item to the respective crate roots
|
2022-05-20 17:00:47 -05:00
|
|
|
let mut current_path = current.path_to_root(db);
|
|
|
|
let mut item_path = item.path_to_root(db);
|
|
|
|
|
2022-05-28 04:27:28 -05:00
|
|
|
// we want paths going from the root to the item
|
2022-05-20 17:00:47 -05:00
|
|
|
current_path.reverse();
|
|
|
|
item_path.reverse();
|
|
|
|
|
2022-05-28 04:27:28 -05:00
|
|
|
// length of the common prefix of the two paths
|
|
|
|
let prefix_length = current_path.iter().zip(&item_path).take_while(|(a, b)| a == b).count();
|
2022-05-20 17:00:47 -05:00
|
|
|
|
2022-05-28 04:27:28 -05:00
|
|
|
// how many modules differ between the two paths (all modules, removing any duplicates)
|
|
|
|
let distinct_length = current_path.len() + item_path.len() - 2 * prefix_length;
|
2022-05-20 17:00:47 -05:00
|
|
|
|
2022-05-28 04:27:28 -05:00
|
|
|
// cost of importing from another crate
|
|
|
|
let crate_boundary_cost = if current.krate() == item.krate() {
|
|
|
|
0
|
|
|
|
} else if item.krate().is_builtin(db) {
|
|
|
|
2
|
|
|
|
} else {
|
|
|
|
4
|
|
|
|
};
|
2022-05-20 17:00:47 -05:00
|
|
|
|
2022-05-28 04:27:28 -05:00
|
|
|
distinct_length + crate_boundary_cost
|
2022-05-20 17:00:47 -05:00
|
|
|
}
|
|
|
|
|
2019-12-23 18:19:09 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-02-07 08:12:51 -06:00
|
|
|
use super::*;
|
2021-07-06 13:30:26 -05:00
|
|
|
|
2022-05-20 17:00:47 -05:00
|
|
|
use hir::Semantics;
|
|
|
|
use ide_db::{
|
|
|
|
assists::AssistResolveStrategy,
|
|
|
|
base_db::{fixture::WithFixture, FileRange},
|
|
|
|
RootDatabase,
|
|
|
|
};
|
|
|
|
|
|
|
|
use crate::tests::{
|
|
|
|
check_assist, check_assist_not_applicable, check_assist_target, TEST_CONFIG,
|
|
|
|
};
|
|
|
|
|
|
|
|
fn check_auto_import_order(before: &str, order: &[&str]) {
|
|
|
|
let (db, file_id, range_or_offset) = RootDatabase::with_range_or_offset(before);
|
|
|
|
let frange = FileRange { file_id, range: range_or_offset.into() };
|
|
|
|
|
|
|
|
let sema = Semantics::new(&db);
|
|
|
|
let config = TEST_CONFIG;
|
|
|
|
let ctx = AssistContext::new(sema, &config, frange);
|
|
|
|
let mut acc = Assists::new(&ctx, AssistResolveStrategy::All);
|
|
|
|
auto_import(&mut acc, &ctx);
|
|
|
|
let assists = acc.finish();
|
|
|
|
|
|
|
|
let labels = assists.iter().map(|assist| assist.label.to_string()).collect::<Vec<_>>();
|
|
|
|
|
|
|
|
assert_eq!(labels, order);
|
|
|
|
}
|
|
|
|
|
2022-10-15 11:00:32 -05:00
|
|
|
#[test]
|
|
|
|
fn ignore_parameter_name() {
|
|
|
|
check_assist_not_applicable(
|
|
|
|
auto_import,
|
|
|
|
r"
|
|
|
|
mod foo {
|
|
|
|
pub mod bar {}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo(bar$0: &str) {}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-05-20 17:00:47 -05:00
|
|
|
#[test]
|
|
|
|
fn prefer_shorter_paths() {
|
|
|
|
let before = r"
|
2022-05-28 04:27:28 -05:00
|
|
|
//- /main.rs crate:main deps:foo,bar
|
|
|
|
HashMap$0::new();
|
2022-05-20 17:00:47 -05:00
|
|
|
|
2022-05-28 04:27:28 -05:00
|
|
|
//- /lib.rs crate:foo
|
|
|
|
pub mod collections { pub struct HashMap; }
|
2022-05-20 17:00:47 -05:00
|
|
|
|
2022-05-28 04:27:28 -05:00
|
|
|
//- /lib.rs crate:bar
|
|
|
|
pub mod collections { pub mod hash_map { pub struct HashMap; } }
|
2022-05-20 17:00:47 -05:00
|
|
|
";
|
|
|
|
|
|
|
|
check_auto_import_order(
|
|
|
|
before,
|
|
|
|
&["Import `foo::collections::HashMap`", "Import `bar::collections::hash_map::HashMap`"],
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn prefer_same_crate() {
|
|
|
|
let before = r"
|
2022-05-28 04:27:28 -05:00
|
|
|
//- /main.rs crate:main deps:foo
|
|
|
|
HashMap$0::new();
|
2022-05-20 17:00:47 -05:00
|
|
|
|
2022-05-28 04:27:28 -05:00
|
|
|
mod collections {
|
|
|
|
pub mod hash_map {
|
|
|
|
pub struct HashMap;
|
|
|
|
}
|
|
|
|
}
|
2022-05-20 17:00:47 -05:00
|
|
|
|
2022-05-28 04:27:28 -05:00
|
|
|
//- /lib.rs crate:foo
|
|
|
|
pub struct HashMap;
|
2022-05-20 17:00:47 -05:00
|
|
|
";
|
|
|
|
|
|
|
|
check_auto_import_order(
|
|
|
|
before,
|
|
|
|
&["Import `collections::hash_map::HashMap`", "Import `foo::HashMap`"],
|
|
|
|
)
|
|
|
|
}
|
2020-02-07 08:12:51 -06:00
|
|
|
|
2021-11-03 15:12:36 -05:00
|
|
|
#[test]
|
|
|
|
fn not_applicable_if_scope_inside_macro() {
|
|
|
|
check_assist_not_applicable(
|
|
|
|
auto_import,
|
|
|
|
r"
|
|
|
|
mod bar {
|
|
|
|
pub struct Baz;
|
|
|
|
}
|
|
|
|
macro_rules! foo {
|
|
|
|
($it:ident) => {
|
|
|
|
mod __ {
|
|
|
|
fn __(x: $it) {}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
foo! {
|
|
|
|
Baz$0
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn applicable_in_attributes() {
|
|
|
|
check_assist(
|
|
|
|
auto_import,
|
|
|
|
r"
|
|
|
|
//- proc_macros: identity
|
|
|
|
#[proc_macros::identity]
|
|
|
|
mod foo {
|
|
|
|
mod bar {
|
|
|
|
const _: Baz$0 = ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mod baz {
|
|
|
|
pub struct Baz;
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
#[proc_macros::identity]
|
|
|
|
mod foo {
|
|
|
|
mod bar {
|
|
|
|
use crate::baz::Baz;
|
|
|
|
|
|
|
|
const _: Baz = ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mod baz {
|
|
|
|
pub struct Baz;
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-25 08:19:22 -05:00
|
|
|
#[test]
|
|
|
|
fn applicable_when_found_an_import_partial() {
|
|
|
|
check_assist(
|
|
|
|
auto_import,
|
|
|
|
r"
|
|
|
|
mod std {
|
|
|
|
pub mod fmt {
|
|
|
|
pub struct Formatter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
use std::fmt;
|
|
|
|
|
2021-01-06 14:15:48 -06:00
|
|
|
$0Formatter
|
2020-09-25 08:19:22 -05:00
|
|
|
",
|
|
|
|
r"
|
|
|
|
mod std {
|
|
|
|
pub mod fmt {
|
|
|
|
pub struct Formatter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
use std::fmt::{self, Formatter};
|
|
|
|
|
|
|
|
Formatter
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-01-26 16:16:18 -06:00
|
|
|
#[test]
|
|
|
|
fn applicable_when_found_an_import() {
|
2020-02-06 10:17:51 -06:00
|
|
|
check_assist(
|
2020-01-26 16:16:18 -06:00
|
|
|
auto_import,
|
|
|
|
r"
|
2021-01-06 14:15:48 -06:00
|
|
|
$0PubStruct
|
2019-12-23 18:19:09 -06:00
|
|
|
|
2020-01-26 16:16:18 -06:00
|
|
|
pub mod PubMod {
|
|
|
|
pub struct PubStruct;
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
2020-05-20 15:55:37 -05:00
|
|
|
use PubMod::PubStruct;
|
2019-12-23 18:19:09 -06:00
|
|
|
|
2020-01-29 06:57:44 -06:00
|
|
|
PubStruct
|
2020-01-26 16:16:18 -06:00
|
|
|
|
|
|
|
pub mod PubMod {
|
|
|
|
pub struct PubStruct;
|
2019-12-23 18:19:09 -06:00
|
|
|
}
|
2020-01-26 16:16:18 -06:00
|
|
|
",
|
|
|
|
);
|
2019-12-23 18:19:09 -06:00
|
|
|
}
|
|
|
|
|
2020-05-02 14:24:55 -05:00
|
|
|
#[test]
|
|
|
|
fn applicable_when_found_an_import_in_macros() {
|
|
|
|
check_assist(
|
|
|
|
auto_import,
|
|
|
|
r"
|
|
|
|
macro_rules! foo {
|
|
|
|
($i:ident) => { fn foo(a: $i) {} }
|
|
|
|
}
|
2021-01-06 14:15:48 -06:00
|
|
|
foo!(Pub$0Struct);
|
2020-05-02 14:24:55 -05:00
|
|
|
|
|
|
|
pub mod PubMod {
|
|
|
|
pub struct PubStruct;
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
use PubMod::PubStruct;
|
|
|
|
|
|
|
|
macro_rules! foo {
|
|
|
|
($i:ident) => { fn foo(a: $i) {} }
|
|
|
|
}
|
2020-05-20 15:55:37 -05:00
|
|
|
foo!(PubStruct);
|
2020-05-02 14:24:55 -05:00
|
|
|
|
|
|
|
pub mod PubMod {
|
|
|
|
pub struct PubStruct;
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-12-23 18:19:09 -06:00
|
|
|
#[test]
|
2020-01-26 16:16:18 -06:00
|
|
|
fn applicable_when_found_multiple_imports() {
|
2020-02-06 10:17:51 -06:00
|
|
|
check_assist(
|
2019-12-23 18:19:09 -06:00
|
|
|
auto_import,
|
2020-01-26 16:16:18 -06:00
|
|
|
r"
|
2021-01-06 14:15:48 -06:00
|
|
|
PubSt$0ruct
|
2020-01-26 16:16:18 -06:00
|
|
|
|
|
|
|
pub mod PubMod1 {
|
|
|
|
pub struct PubStruct;
|
|
|
|
}
|
|
|
|
pub mod PubMod2 {
|
|
|
|
pub struct PubStruct;
|
|
|
|
}
|
|
|
|
pub mod PubMod3 {
|
|
|
|
pub struct PubStruct;
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
2021-03-03 15:59:56 -06:00
|
|
|
use PubMod3::PubStruct;
|
2020-01-26 16:16:18 -06:00
|
|
|
|
2020-05-20 15:55:37 -05:00
|
|
|
PubStruct
|
2020-01-26 16:16:18 -06:00
|
|
|
|
|
|
|
pub mod PubMod1 {
|
|
|
|
pub struct PubStruct;
|
|
|
|
}
|
|
|
|
pub mod PubMod2 {
|
|
|
|
pub struct PubStruct;
|
2019-12-23 18:19:09 -06:00
|
|
|
}
|
2020-01-26 16:16:18 -06:00
|
|
|
pub mod PubMod3 {
|
|
|
|
pub struct PubStruct;
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn not_applicable_for_already_imported_types() {
|
2020-02-06 10:17:51 -06:00
|
|
|
check_assist_not_applicable(
|
2020-01-26 16:16:18 -06:00
|
|
|
auto_import,
|
|
|
|
r"
|
|
|
|
use PubMod::PubStruct;
|
|
|
|
|
2021-01-06 14:15:48 -06:00
|
|
|
PubStruct$0
|
2019-12-23 18:19:09 -06:00
|
|
|
|
2020-01-26 16:16:18 -06:00
|
|
|
pub mod PubMod {
|
|
|
|
pub struct PubStruct;
|
|
|
|
}
|
|
|
|
",
|
2019-12-23 18:19:09 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-01-26 16:16:18 -06:00
|
|
|
fn not_applicable_for_types_with_private_paths() {
|
2020-02-06 10:17:51 -06:00
|
|
|
check_assist_not_applicable(
|
2019-12-23 18:19:09 -06:00
|
|
|
auto_import,
|
2020-01-26 16:16:18 -06:00
|
|
|
r"
|
2021-01-06 14:15:48 -06:00
|
|
|
PrivateStruct$0
|
2020-01-26 16:16:18 -06:00
|
|
|
|
|
|
|
pub mod PubMod {
|
|
|
|
struct PrivateStruct;
|
2019-12-23 18:19:09 -06:00
|
|
|
}
|
2020-01-26 16:16:18 -06:00
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
2019-12-23 18:19:09 -06:00
|
|
|
|
2020-01-26 16:16:18 -06:00
|
|
|
#[test]
|
|
|
|
fn not_applicable_when_no_imports_found() {
|
2020-02-06 10:17:51 -06:00
|
|
|
check_assist_not_applicable(
|
2020-01-26 16:16:18 -06:00
|
|
|
auto_import,
|
|
|
|
"
|
2021-01-06 14:15:48 -06:00
|
|
|
PubStruct$0",
|
2019-12-23 18:19:09 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-02-01 14:13:02 -06:00
|
|
|
#[test]
|
|
|
|
fn function_import() {
|
2020-02-06 10:17:51 -06:00
|
|
|
check_assist(
|
2020-02-01 14:13:02 -06:00
|
|
|
auto_import,
|
|
|
|
r"
|
2021-01-06 14:15:48 -06:00
|
|
|
test_function$0
|
2020-02-01 14:13:02 -06:00
|
|
|
|
|
|
|
pub mod PubMod {
|
|
|
|
pub fn test_function() {};
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
use PubMod::test_function;
|
|
|
|
|
2020-05-20 15:55:37 -05:00
|
|
|
test_function
|
2020-02-01 14:13:02 -06:00
|
|
|
|
|
|
|
pub mod PubMod {
|
|
|
|
pub fn test_function() {};
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
2020-02-09 09:13:29 -06:00
|
|
|
|
2020-03-23 16:23:26 -05:00
|
|
|
#[test]
|
|
|
|
fn macro_import() {
|
|
|
|
check_assist(
|
|
|
|
auto_import,
|
|
|
|
r"
|
2020-06-23 15:27:24 -05:00
|
|
|
//- /lib.rs crate:crate_with_macro
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! foo {
|
|
|
|
() => ()
|
|
|
|
}
|
2020-03-23 16:23:26 -05:00
|
|
|
|
2020-06-23 15:27:24 -05:00
|
|
|
//- /main.rs crate:main deps:crate_with_macro
|
|
|
|
fn main() {
|
2021-01-06 14:15:48 -06:00
|
|
|
foo$0
|
2020-06-23 15:27:24 -05:00
|
|
|
}
|
|
|
|
",
|
2020-03-23 16:23:26 -05:00
|
|
|
r"use crate_with_macro::foo;
|
|
|
|
|
|
|
|
fn main() {
|
2020-05-20 15:55:37 -05:00
|
|
|
foo
|
2020-03-23 16:23:26 -05:00
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-02-09 09:13:29 -06:00
|
|
|
#[test]
|
|
|
|
fn auto_import_target() {
|
|
|
|
check_assist_target(
|
|
|
|
auto_import,
|
|
|
|
r"
|
|
|
|
struct AssistInfo {
|
2021-01-06 14:15:48 -06:00
|
|
|
group_label: Option<$0GroupLabel>,
|
2020-02-09 09:13:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
mod m { pub struct GroupLabel; }
|
|
|
|
",
|
|
|
|
"GroupLabel",
|
|
|
|
)
|
|
|
|
}
|
2020-02-08 17:56:17 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn not_applicable_when_path_start_is_imported() {
|
|
|
|
check_assist_not_applicable(
|
|
|
|
auto_import,
|
|
|
|
r"
|
|
|
|
pub mod mod1 {
|
|
|
|
pub mod mod2 {
|
|
|
|
pub mod mod3 {
|
|
|
|
pub struct TestStruct;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
use mod1::mod2;
|
|
|
|
fn main() {
|
2021-01-06 14:15:48 -06:00
|
|
|
mod2::mod3::TestStruct$0
|
2020-02-08 17:56:17 -06:00
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
2020-02-09 16:22:12 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn not_applicable_for_imported_function() {
|
|
|
|
check_assist_not_applicable(
|
|
|
|
auto_import,
|
|
|
|
r"
|
|
|
|
pub mod test_mod {
|
|
|
|
pub fn test_function() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
use test_mod::test_function;
|
|
|
|
fn main() {
|
2021-01-06 14:15:48 -06:00
|
|
|
test_function$0
|
2020-02-09 16:22:12 -06:00
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
2020-02-09 16:30:00 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn associated_struct_function() {
|
|
|
|
check_assist(
|
|
|
|
auto_import,
|
|
|
|
r"
|
|
|
|
mod test_mod {
|
|
|
|
pub struct TestStruct {}
|
|
|
|
impl TestStruct {
|
|
|
|
pub fn test_function() {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2021-01-06 14:15:48 -06:00
|
|
|
TestStruct::test_function$0
|
2020-02-09 16:30:00 -06:00
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
use test_mod::TestStruct;
|
|
|
|
|
|
|
|
mod test_mod {
|
|
|
|
pub struct TestStruct {}
|
|
|
|
impl TestStruct {
|
|
|
|
pub fn test_function() {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2020-05-20 15:55:37 -05:00
|
|
|
TestStruct::test_function
|
2020-02-09 16:30:00 -06:00
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-02-12 14:38:19 -06:00
|
|
|
#[test]
|
|
|
|
fn associated_struct_const() {
|
|
|
|
check_assist(
|
|
|
|
auto_import,
|
|
|
|
r"
|
|
|
|
mod test_mod {
|
|
|
|
pub struct TestStruct {}
|
|
|
|
impl TestStruct {
|
|
|
|
const TEST_CONST: u8 = 42;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2021-01-06 14:15:48 -06:00
|
|
|
TestStruct::TEST_CONST$0
|
2020-02-12 14:38:19 -06:00
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
use test_mod::TestStruct;
|
|
|
|
|
|
|
|
mod test_mod {
|
|
|
|
pub struct TestStruct {}
|
|
|
|
impl TestStruct {
|
|
|
|
const TEST_CONST: u8 = 42;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2020-05-20 15:55:37 -05:00
|
|
|
TestStruct::TEST_CONST
|
2020-02-12 14:38:19 -06:00
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-02-09 16:30:00 -06:00
|
|
|
#[test]
|
|
|
|
fn associated_trait_function() {
|
|
|
|
check_assist(
|
|
|
|
auto_import,
|
|
|
|
r"
|
|
|
|
mod test_mod {
|
|
|
|
pub trait TestTrait {
|
|
|
|
fn test_function();
|
|
|
|
}
|
|
|
|
pub struct TestStruct {}
|
|
|
|
impl TestTrait for TestStruct {
|
|
|
|
fn test_function() {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2021-01-06 14:15:48 -06:00
|
|
|
test_mod::TestStruct::test_function$0
|
2020-02-09 16:30:00 -06:00
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
use test_mod::TestTrait;
|
|
|
|
|
|
|
|
mod test_mod {
|
|
|
|
pub trait TestTrait {
|
|
|
|
fn test_function();
|
|
|
|
}
|
|
|
|
pub struct TestStruct {}
|
|
|
|
impl TestTrait for TestStruct {
|
|
|
|
fn test_function() {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2020-05-20 15:55:37 -05:00
|
|
|
test_mod::TestStruct::test_function
|
2020-02-09 16:30:00 -06:00
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-02-11 10:24:43 -06:00
|
|
|
fn not_applicable_for_imported_trait_for_function() {
|
2020-02-11 07:21:12 -06:00
|
|
|
check_assist_not_applicable(
|
|
|
|
auto_import,
|
|
|
|
r"
|
|
|
|
mod test_mod {
|
|
|
|
pub trait TestTrait {
|
|
|
|
fn test_function();
|
|
|
|
}
|
|
|
|
pub trait TestTrait2 {
|
|
|
|
fn test_function();
|
|
|
|
}
|
|
|
|
pub enum TestEnum {
|
|
|
|
One,
|
|
|
|
Two,
|
|
|
|
}
|
|
|
|
impl TestTrait2 for TestEnum {
|
|
|
|
fn test_function() {}
|
|
|
|
}
|
|
|
|
impl TestTrait for TestEnum {
|
|
|
|
fn test_function() {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
use test_mod::TestTrait2;
|
|
|
|
fn main() {
|
2021-01-06 14:15:48 -06:00
|
|
|
test_mod::TestEnum::test_function$0;
|
2020-02-11 07:21:12 -06:00
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-02-12 14:38:19 -06:00
|
|
|
#[test]
|
|
|
|
fn associated_trait_const() {
|
|
|
|
check_assist(
|
|
|
|
auto_import,
|
|
|
|
r"
|
|
|
|
mod test_mod {
|
|
|
|
pub trait TestTrait {
|
|
|
|
const TEST_CONST: u8;
|
|
|
|
}
|
|
|
|
pub struct TestStruct {}
|
|
|
|
impl TestTrait for TestStruct {
|
|
|
|
const TEST_CONST: u8 = 42;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2021-01-06 14:15:48 -06:00
|
|
|
test_mod::TestStruct::TEST_CONST$0
|
2020-02-12 14:38:19 -06:00
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
use test_mod::TestTrait;
|
|
|
|
|
|
|
|
mod test_mod {
|
|
|
|
pub trait TestTrait {
|
|
|
|
const TEST_CONST: u8;
|
|
|
|
}
|
|
|
|
pub struct TestStruct {}
|
|
|
|
impl TestTrait for TestStruct {
|
|
|
|
const TEST_CONST: u8 = 42;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2020-05-20 15:55:37 -05:00
|
|
|
test_mod::TestStruct::TEST_CONST
|
2020-02-12 14:38:19 -06:00
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn not_applicable_for_imported_trait_for_const() {
|
|
|
|
check_assist_not_applicable(
|
|
|
|
auto_import,
|
|
|
|
r"
|
|
|
|
mod test_mod {
|
|
|
|
pub trait TestTrait {
|
|
|
|
const TEST_CONST: u8;
|
|
|
|
}
|
|
|
|
pub trait TestTrait2 {
|
|
|
|
const TEST_CONST: f64;
|
|
|
|
}
|
|
|
|
pub enum TestEnum {
|
|
|
|
One,
|
|
|
|
Two,
|
|
|
|
}
|
|
|
|
impl TestTrait2 for TestEnum {
|
|
|
|
const TEST_CONST: f64 = 42.0;
|
|
|
|
}
|
|
|
|
impl TestTrait for TestEnum {
|
|
|
|
const TEST_CONST: u8 = 42;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
use test_mod::TestTrait2;
|
|
|
|
fn main() {
|
2021-01-06 14:15:48 -06:00
|
|
|
test_mod::TestEnum::TEST_CONST$0;
|
2020-02-12 14:38:19 -06:00
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-02-11 07:21:12 -06:00
|
|
|
#[test]
|
2020-02-09 16:30:00 -06:00
|
|
|
fn trait_method() {
|
|
|
|
check_assist(
|
|
|
|
auto_import,
|
|
|
|
r"
|
|
|
|
mod test_mod {
|
|
|
|
pub trait TestTrait {
|
|
|
|
fn test_method(&self);
|
|
|
|
}
|
|
|
|
pub struct TestStruct {}
|
|
|
|
impl TestTrait for TestStruct {
|
|
|
|
fn test_method(&self) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let test_struct = test_mod::TestStruct {};
|
2021-01-06 14:15:48 -06:00
|
|
|
test_struct.test_meth$0od()
|
2020-02-09 16:30:00 -06:00
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
use test_mod::TestTrait;
|
|
|
|
|
|
|
|
mod test_mod {
|
|
|
|
pub trait TestTrait {
|
|
|
|
fn test_method(&self);
|
|
|
|
}
|
|
|
|
pub struct TestStruct {}
|
|
|
|
impl TestTrait for TestStruct {
|
|
|
|
fn test_method(&self) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let test_struct = test_mod::TestStruct {};
|
2020-05-20 15:55:37 -05:00
|
|
|
test_struct.test_method()
|
2020-02-09 16:30:00 -06:00
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
2020-02-11 10:24:43 -06:00
|
|
|
|
2020-07-02 05:22:25 -05:00
|
|
|
#[test]
|
|
|
|
fn trait_method_cross_crate() {
|
|
|
|
check_assist(
|
|
|
|
auto_import,
|
|
|
|
r"
|
|
|
|
//- /main.rs crate:main deps:dep
|
|
|
|
fn main() {
|
|
|
|
let test_struct = dep::test_mod::TestStruct {};
|
2021-01-06 14:15:48 -06:00
|
|
|
test_struct.test_meth$0od()
|
2020-07-02 05:22:25 -05:00
|
|
|
}
|
|
|
|
//- /dep.rs crate:dep
|
|
|
|
pub mod test_mod {
|
|
|
|
pub trait TestTrait {
|
|
|
|
fn test_method(&self);
|
|
|
|
}
|
|
|
|
pub struct TestStruct {}
|
|
|
|
impl TestTrait for TestStruct {
|
|
|
|
fn test_method(&self) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
use dep::test_mod::TestTrait;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let test_struct = dep::test_mod::TestStruct {};
|
|
|
|
test_struct.test_method()
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn assoc_fn_cross_crate() {
|
|
|
|
check_assist(
|
|
|
|
auto_import,
|
|
|
|
r"
|
|
|
|
//- /main.rs crate:main deps:dep
|
|
|
|
fn main() {
|
2021-01-06 14:15:48 -06:00
|
|
|
dep::test_mod::TestStruct::test_func$0tion
|
2020-07-02 05:22:25 -05:00
|
|
|
}
|
|
|
|
//- /dep.rs crate:dep
|
|
|
|
pub mod test_mod {
|
|
|
|
pub trait TestTrait {
|
|
|
|
fn test_function();
|
|
|
|
}
|
|
|
|
pub struct TestStruct {}
|
|
|
|
impl TestTrait for TestStruct {
|
|
|
|
fn test_function() {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
use dep::test_mod::TestTrait;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
dep::test_mod::TestStruct::test_function
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn assoc_const_cross_crate() {
|
|
|
|
check_assist(
|
|
|
|
auto_import,
|
|
|
|
r"
|
|
|
|
//- /main.rs crate:main deps:dep
|
|
|
|
fn main() {
|
2021-01-06 14:15:48 -06:00
|
|
|
dep::test_mod::TestStruct::CONST$0
|
2020-07-02 05:22:25 -05:00
|
|
|
}
|
|
|
|
//- /dep.rs crate:dep
|
|
|
|
pub mod test_mod {
|
|
|
|
pub trait TestTrait {
|
|
|
|
const CONST: bool;
|
|
|
|
}
|
|
|
|
pub struct TestStruct {}
|
|
|
|
impl TestTrait for TestStruct {
|
|
|
|
const CONST: bool = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
use dep::test_mod::TestTrait;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
dep::test_mod::TestStruct::CONST
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn assoc_fn_as_method_cross_crate() {
|
|
|
|
check_assist_not_applicable(
|
|
|
|
auto_import,
|
|
|
|
r"
|
|
|
|
//- /main.rs crate:main deps:dep
|
|
|
|
fn main() {
|
|
|
|
let test_struct = dep::test_mod::TestStruct {};
|
2021-01-06 14:15:48 -06:00
|
|
|
test_struct.test_func$0tion()
|
2020-07-02 05:22:25 -05:00
|
|
|
}
|
|
|
|
//- /dep.rs crate:dep
|
|
|
|
pub mod test_mod {
|
|
|
|
pub trait TestTrait {
|
|
|
|
fn test_function();
|
|
|
|
}
|
|
|
|
pub struct TestStruct {}
|
|
|
|
impl TestTrait for TestStruct {
|
|
|
|
fn test_function() {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn private_trait_cross_crate() {
|
|
|
|
check_assist_not_applicable(
|
|
|
|
auto_import,
|
|
|
|
r"
|
|
|
|
//- /main.rs crate:main deps:dep
|
|
|
|
fn main() {
|
|
|
|
let test_struct = dep::test_mod::TestStruct {};
|
2021-01-06 14:15:48 -06:00
|
|
|
test_struct.test_meth$0od()
|
2020-07-02 05:22:25 -05:00
|
|
|
}
|
|
|
|
//- /dep.rs crate:dep
|
|
|
|
pub mod test_mod {
|
|
|
|
trait TestTrait {
|
|
|
|
fn test_method(&self);
|
|
|
|
}
|
|
|
|
pub struct TestStruct {}
|
|
|
|
impl TestTrait for TestStruct {
|
|
|
|
fn test_method(&self) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-02-11 10:24:43 -06:00
|
|
|
#[test]
|
|
|
|
fn not_applicable_for_imported_trait_for_method() {
|
|
|
|
check_assist_not_applicable(
|
|
|
|
auto_import,
|
|
|
|
r"
|
|
|
|
mod test_mod {
|
|
|
|
pub trait TestTrait {
|
|
|
|
fn test_method(&self);
|
|
|
|
}
|
|
|
|
pub trait TestTrait2 {
|
|
|
|
fn test_method(&self);
|
|
|
|
}
|
|
|
|
pub enum TestEnum {
|
|
|
|
One,
|
|
|
|
Two,
|
|
|
|
}
|
|
|
|
impl TestTrait2 for TestEnum {
|
|
|
|
fn test_method(&self) {}
|
|
|
|
}
|
|
|
|
impl TestTrait for TestEnum {
|
|
|
|
fn test_method(&self) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
use test_mod::TestTrait2;
|
|
|
|
fn main() {
|
|
|
|
let one = test_mod::TestEnum::One;
|
2021-01-06 14:15:48 -06:00
|
|
|
one.test$0_method();
|
2020-02-11 10:24:43 -06:00
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
2020-06-09 11:48:44 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn dep_import() {
|
|
|
|
check_assist(
|
|
|
|
auto_import,
|
|
|
|
r"
|
2020-06-23 15:27:24 -05:00
|
|
|
//- /lib.rs crate:dep
|
|
|
|
pub struct Struct;
|
2020-06-09 11:48:44 -05:00
|
|
|
|
2020-06-23 15:27:24 -05:00
|
|
|
//- /main.rs crate:main deps:dep
|
|
|
|
fn main() {
|
2021-01-06 14:15:48 -06:00
|
|
|
Struct$0
|
2020-06-23 15:27:24 -05:00
|
|
|
}
|
|
|
|
",
|
2020-06-09 11:48:44 -05:00
|
|
|
r"use dep::Struct;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
Struct
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn whole_segment() {
|
2020-06-10 04:39:06 -05:00
|
|
|
// Tests that only imports whose last segment matches the identifier get suggested.
|
2020-06-09 11:48:44 -05:00
|
|
|
check_assist(
|
|
|
|
auto_import,
|
|
|
|
r"
|
2020-06-23 15:27:24 -05:00
|
|
|
//- /lib.rs crate:dep
|
|
|
|
pub mod fmt {
|
|
|
|
pub trait Display {}
|
|
|
|
}
|
2020-06-09 11:48:44 -05:00
|
|
|
|
2020-06-23 15:27:24 -05:00
|
|
|
pub fn panic_fmt() {}
|
2020-06-09 11:48:44 -05:00
|
|
|
|
2020-06-23 15:27:24 -05:00
|
|
|
//- /main.rs crate:main deps:dep
|
|
|
|
struct S;
|
2020-06-09 11:48:44 -05:00
|
|
|
|
2021-01-06 14:15:48 -06:00
|
|
|
impl f$0mt::Display for S {}
|
2020-06-23 15:27:24 -05:00
|
|
|
",
|
2020-06-09 11:48:44 -05:00
|
|
|
r"use dep::fmt;
|
|
|
|
|
|
|
|
struct S;
|
2020-06-23 15:27:24 -05:00
|
|
|
|
2020-06-09 11:48:44 -05:00
|
|
|
impl fmt::Display for S {}
|
2020-06-10 04:39:06 -05:00
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn macro_generated() {
|
|
|
|
// Tests that macro-generated items are suggested from external crates.
|
|
|
|
check_assist(
|
|
|
|
auto_import,
|
|
|
|
r"
|
2020-06-23 15:27:24 -05:00
|
|
|
//- /lib.rs crate:dep
|
|
|
|
macro_rules! mac {
|
|
|
|
() => {
|
|
|
|
pub struct Cheese;
|
|
|
|
};
|
|
|
|
}
|
2020-06-10 04:39:06 -05:00
|
|
|
|
2020-06-23 15:27:24 -05:00
|
|
|
mac!();
|
2020-06-10 04:39:06 -05:00
|
|
|
|
2020-06-23 15:27:24 -05:00
|
|
|
//- /main.rs crate:main deps:dep
|
|
|
|
fn main() {
|
2021-01-06 14:15:48 -06:00
|
|
|
Cheese$0;
|
2020-06-23 15:27:24 -05:00
|
|
|
}
|
|
|
|
",
|
2020-06-10 04:39:06 -05:00
|
|
|
r"use dep::Cheese;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
Cheese;
|
|
|
|
}
|
2020-06-10 09:04:55 -05:00
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn casing() {
|
|
|
|
// Tests that differently cased names don't interfere and we only suggest the matching one.
|
|
|
|
check_assist(
|
|
|
|
auto_import,
|
|
|
|
r"
|
2020-06-23 15:27:24 -05:00
|
|
|
//- /lib.rs crate:dep
|
|
|
|
pub struct FMT;
|
|
|
|
pub struct fmt;
|
2020-06-10 09:04:55 -05:00
|
|
|
|
2020-06-23 15:27:24 -05:00
|
|
|
//- /main.rs crate:main deps:dep
|
|
|
|
fn main() {
|
2021-01-06 14:15:48 -06:00
|
|
|
FMT$0;
|
2020-06-23 15:27:24 -05:00
|
|
|
}
|
|
|
|
",
|
2020-06-10 09:04:55 -05:00
|
|
|
r"use dep::FMT;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
FMT;
|
|
|
|
}
|
2020-06-09 11:48:44 -05:00
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
2021-04-19 12:53:29 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inner_items() {
|
|
|
|
check_assist(
|
|
|
|
auto_import,
|
|
|
|
r#"
|
|
|
|
mod baz {
|
|
|
|
pub struct Foo {}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod bar {
|
|
|
|
fn bar() {
|
|
|
|
Foo$0;
|
|
|
|
println!("Hallo");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
mod baz {
|
|
|
|
pub struct Foo {}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod bar {
|
|
|
|
use crate::baz::Foo;
|
|
|
|
|
|
|
|
fn bar() {
|
|
|
|
Foo;
|
|
|
|
println!("Hallo");
|
|
|
|
}
|
|
|
|
}
|
2021-05-12 19:17:41 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn uses_abs_path_with_extern_crate_clash() {
|
2021-07-15 16:41:00 -05:00
|
|
|
cov_mark::check!(ambiguous_crate_start);
|
2021-05-12 19:17:41 -05:00
|
|
|
check_assist(
|
|
|
|
auto_import,
|
|
|
|
r#"
|
|
|
|
//- /main.rs crate:main deps:foo
|
|
|
|
mod foo {}
|
|
|
|
|
|
|
|
const _: () = {
|
|
|
|
Foo$0
|
|
|
|
};
|
|
|
|
//- /foo.rs crate:foo
|
|
|
|
pub struct Foo
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
use ::foo::Foo;
|
|
|
|
|
|
|
|
mod foo {}
|
|
|
|
|
|
|
|
const _: () = {
|
|
|
|
Foo
|
|
|
|
};
|
2021-07-10 10:57:33 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn works_on_ident_patterns() {
|
|
|
|
check_assist(
|
|
|
|
auto_import,
|
|
|
|
r#"
|
|
|
|
mod foo {
|
|
|
|
pub struct Foo {}
|
|
|
|
}
|
|
|
|
fn foo() {
|
|
|
|
let Foo$0;
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
use foo::Foo;
|
|
|
|
|
|
|
|
mod foo {
|
|
|
|
pub struct Foo {}
|
|
|
|
}
|
|
|
|
fn foo() {
|
|
|
|
let Foo;
|
|
|
|
}
|
2021-10-28 11:40:38 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn works_in_derives() {
|
|
|
|
check_assist(
|
|
|
|
auto_import,
|
|
|
|
r#"
|
|
|
|
//- minicore:derive
|
|
|
|
mod foo {
|
|
|
|
#[rustc_builtin_macro]
|
|
|
|
pub macro Copy {}
|
|
|
|
}
|
|
|
|
#[derive(Copy$0)]
|
|
|
|
struct Foo;
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
use foo::Copy;
|
|
|
|
|
|
|
|
mod foo {
|
|
|
|
#[rustc_builtin_macro]
|
|
|
|
pub macro Copy {}
|
|
|
|
}
|
|
|
|
#[derive(Copy)]
|
|
|
|
struct Foo;
|
2021-04-19 12:53:29 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
2022-05-07 08:23:25 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn works_in_use_start() {
|
|
|
|
check_assist(
|
|
|
|
auto_import,
|
|
|
|
r#"
|
|
|
|
mod bar {
|
|
|
|
pub mod foo {
|
|
|
|
pub struct Foo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
use foo$0::Foo;
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
mod bar {
|
|
|
|
pub mod foo {
|
|
|
|
pub struct Foo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
use bar::foo;
|
|
|
|
use foo::Foo;
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn not_applicable_in_non_start_use() {
|
|
|
|
check_assist_not_applicable(
|
|
|
|
auto_import,
|
|
|
|
r"
|
|
|
|
mod bar {
|
|
|
|
pub mod foo {
|
|
|
|
pub struct Foo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
use foo::Foo$0;
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
2019-12-23 18:19:09 -06:00
|
|
|
}
|