2020-10-06 14:19:18 +00:00
|
|
|
use crate::{
|
2020-10-12 16:11:36 +00:00
|
|
|
utils::import_assets::{ImportAssets, ImportCandidate},
|
|
|
|
utils::{insert_use, mod_path_to_ast, ImportScope},
|
|
|
|
AssistContext, AssistId, AssistKind, Assists, GroupLabel,
|
2020-10-06 14:19:18 +00:00
|
|
|
};
|
2019-12-24 00:19:09 +00:00
|
|
|
|
|
|
|
// Assist: auto_import
|
|
|
|
//
|
|
|
|
// If the name is unresolved, provides all possible imports for it.
|
|
|
|
//
|
|
|
|
// ```
|
|
|
|
// fn main() {
|
|
|
|
// let map = HashMap<|>::new();
|
|
|
|
// }
|
2020-02-06 17:10:25 +00:00
|
|
|
// # pub mod std { pub mod collections { pub struct HashMap { } } }
|
2019-12-24 00:19:09 +00:00
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
|
|
|
// use std::collections::HashMap;
|
|
|
|
//
|
|
|
|
// fn main() {
|
2020-02-06 17:10:25 +00:00
|
|
|
// let map = HashMap::new();
|
2019-12-24 00:19:09 +00:00
|
|
|
// }
|
2020-02-06 17:10:25 +00:00
|
|
|
// # pub mod std { pub mod collections { pub struct HashMap { } } }
|
2019-12-24 00:19:09 +00:00
|
|
|
// ```
|
2020-05-06 16:45:35 +00:00
|
|
|
pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
2020-10-12 16:11:36 +00:00
|
|
|
let auto_import_assets = ImportAssets::new(&ctx)?;
|
|
|
|
let proposed_imports = auto_import_assets.search_for_imports(&ctx.sema, &ctx.config.insert_use);
|
2019-12-24 00:19:09 +00:00
|
|
|
if proposed_imports.is_empty() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2020-10-12 16:11:36 +00:00
|
|
|
let range = ctx.sema.original_range(auto_import_assets.syntax_under_caret()).range;
|
|
|
|
let group = import_group_message(auto_import_assets.import_candidate());
|
2020-09-03 14:23:33 +00:00
|
|
|
let scope =
|
2020-10-12 16:11:36 +00:00
|
|
|
ImportScope::find_insert_use_container(auto_import_assets.syntax_under_caret(), ctx)?;
|
2020-09-03 14:23:33 +00:00
|
|
|
let syntax = scope.as_syntax_node();
|
2020-02-09 13:30:27 +00:00
|
|
|
for import in proposed_imports {
|
2020-05-06 16:45:35 +00:00
|
|
|
acc.add_group(
|
|
|
|
&group,
|
2020-07-02 21:48:35 +00:00
|
|
|
AssistId("auto_import", AssistKind::QuickFix),
|
2020-05-06 16:45:35 +00:00
|
|
|
format!("Import `{}`", &import),
|
|
|
|
range,
|
|
|
|
|builder| {
|
2020-09-16 19:29:48 +00:00
|
|
|
let new_syntax =
|
2020-10-06 14:19:18 +00:00
|
|
|
insert_use(&scope, mod_path_to_ast(&import), ctx.config.insert_use.merge);
|
2020-09-03 12:22:22 +00:00
|
|
|
builder.replace(syntax.text_range(), new_syntax.to_string())
|
2020-05-06 16:45:35 +00:00
|
|
|
},
|
|
|
|
);
|
2020-02-09 13:30:27 +00:00
|
|
|
}
|
2020-05-06 16:45:35 +00:00
|
|
|
Some(())
|
2019-12-24 00:19:09 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 16:11:36 +00:00
|
|
|
fn import_group_message(import_candidate: &ImportCandidate) -> GroupLabel {
|
|
|
|
let name = match import_candidate {
|
|
|
|
ImportCandidate::UnqualifiedName(name) => format!("Import {}", name),
|
|
|
|
ImportCandidate::QualifierStart(qualifier_start) => format!("Import {}", qualifier_start),
|
|
|
|
ImportCandidate::TraitAssocItem(_, trait_assoc_item_name) => {
|
|
|
|
format!("Import a trait for item {}", trait_assoc_item_name)
|
2020-02-12 12:41:34 +00:00
|
|
|
}
|
2020-10-12 16:11:36 +00:00
|
|
|
ImportCandidate::TraitMethod(_, trait_method_name) => {
|
|
|
|
format!("Import a trait for method {}", trait_method_name)
|
2020-02-10 14:55:20 +00:00
|
|
|
}
|
2020-10-12 16:11:36 +00:00
|
|
|
};
|
|
|
|
GroupLabel(name)
|
2020-02-11 16:24:43 +00:00
|
|
|
}
|
|
|
|
|
2019-12-24 00:19:09 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-02-07 14:12:51 +00:00
|
|
|
use super::*;
|
2020-05-06 08:16:55 +00:00
|
|
|
use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target};
|
2020-02-07 14:12:51 +00:00
|
|
|
|
2020-09-25 13:19:22 +00: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;
|
|
|
|
|
|
|
|
<|>Formatter
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
mod std {
|
|
|
|
pub mod fmt {
|
|
|
|
pub struct Formatter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
use std::fmt::{self, Formatter};
|
|
|
|
|
|
|
|
Formatter
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-01-26 22:16:18 +00:00
|
|
|
#[test]
|
|
|
|
fn applicable_when_found_an_import() {
|
2020-02-06 16:17:51 +00:00
|
|
|
check_assist(
|
2020-01-26 22:16:18 +00:00
|
|
|
auto_import,
|
|
|
|
r"
|
2020-01-29 12:57:44 +00:00
|
|
|
<|>PubStruct
|
2019-12-24 00:19:09 +00:00
|
|
|
|
2020-01-26 22:16:18 +00:00
|
|
|
pub mod PubMod {
|
|
|
|
pub struct PubStruct;
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
2020-05-20 20:55:37 +00:00
|
|
|
use PubMod::PubStruct;
|
2019-12-24 00:19:09 +00:00
|
|
|
|
2020-01-29 12:57:44 +00:00
|
|
|
PubStruct
|
2020-01-26 22:16:18 +00:00
|
|
|
|
|
|
|
pub mod PubMod {
|
|
|
|
pub struct PubStruct;
|
2019-12-24 00:19:09 +00:00
|
|
|
}
|
2020-01-26 22:16:18 +00:00
|
|
|
",
|
|
|
|
);
|
2019-12-24 00:19:09 +00:00
|
|
|
}
|
|
|
|
|
2020-05-02 19:24:55 +00:00
|
|
|
#[test]
|
|
|
|
fn applicable_when_found_an_import_in_macros() {
|
|
|
|
check_assist(
|
|
|
|
auto_import,
|
|
|
|
r"
|
|
|
|
macro_rules! foo {
|
|
|
|
($i:ident) => { fn foo(a: $i) {} }
|
|
|
|
}
|
|
|
|
foo!(Pub<|>Struct);
|
|
|
|
|
|
|
|
pub mod PubMod {
|
|
|
|
pub struct PubStruct;
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
use PubMod::PubStruct;
|
|
|
|
|
|
|
|
macro_rules! foo {
|
|
|
|
($i:ident) => { fn foo(a: $i) {} }
|
|
|
|
}
|
2020-05-20 20:55:37 +00:00
|
|
|
foo!(PubStruct);
|
2020-05-02 19:24:55 +00:00
|
|
|
|
|
|
|
pub mod PubMod {
|
|
|
|
pub struct PubStruct;
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-02-02 08:28:16 +00:00
|
|
|
#[test]
|
|
|
|
fn auto_imports_are_merged() {
|
2020-02-06 16:17:51 +00:00
|
|
|
check_assist(
|
2020-02-02 08:28:16 +00:00
|
|
|
auto_import,
|
|
|
|
r"
|
|
|
|
use PubMod::PubStruct1;
|
|
|
|
|
2020-02-02 21:24:43 +00:00
|
|
|
struct Test {
|
|
|
|
test: Pub<|>Struct2<u8>,
|
|
|
|
}
|
2020-02-02 08:28:16 +00:00
|
|
|
|
|
|
|
pub mod PubMod {
|
|
|
|
pub struct PubStruct1;
|
2020-02-02 21:24:43 +00:00
|
|
|
pub struct PubStruct2<T> {
|
|
|
|
_t: T,
|
|
|
|
}
|
2020-02-02 08:28:16 +00:00
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
2020-09-03 12:22:22 +00:00
|
|
|
use PubMod::{PubStruct1, PubStruct2};
|
2020-02-02 08:28:16 +00:00
|
|
|
|
2020-02-02 21:24:43 +00:00
|
|
|
struct Test {
|
2020-05-20 20:55:37 +00:00
|
|
|
test: PubStruct2<u8>,
|
2020-02-02 21:24:43 +00:00
|
|
|
}
|
2020-02-02 08:28:16 +00:00
|
|
|
|
|
|
|
pub mod PubMod {
|
|
|
|
pub struct PubStruct1;
|
2020-02-02 21:24:43 +00:00
|
|
|
pub struct PubStruct2<T> {
|
|
|
|
_t: T,
|
|
|
|
}
|
2020-02-02 08:28:16 +00:00
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-12-24 00:19:09 +00:00
|
|
|
#[test]
|
2020-01-26 22:16:18 +00:00
|
|
|
fn applicable_when_found_multiple_imports() {
|
2020-02-06 16:17:51 +00:00
|
|
|
check_assist(
|
2019-12-24 00:19:09 +00:00
|
|
|
auto_import,
|
2020-01-26 22:16:18 +00:00
|
|
|
r"
|
2020-01-29 12:57:44 +00:00
|
|
|
PubSt<|>ruct
|
2020-01-26 22:16:18 +00:00
|
|
|
|
|
|
|
pub mod PubMod1 {
|
|
|
|
pub struct PubStruct;
|
|
|
|
}
|
|
|
|
pub mod PubMod2 {
|
|
|
|
pub struct PubStruct;
|
|
|
|
}
|
|
|
|
pub mod PubMod3 {
|
|
|
|
pub struct PubStruct;
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
2020-05-06 16:45:35 +00:00
|
|
|
use PubMod3::PubStruct;
|
2020-01-26 22:16:18 +00:00
|
|
|
|
2020-05-20 20:55:37 +00:00
|
|
|
PubStruct
|
2020-01-26 22:16:18 +00:00
|
|
|
|
|
|
|
pub mod PubMod1 {
|
|
|
|
pub struct PubStruct;
|
|
|
|
}
|
|
|
|
pub mod PubMod2 {
|
|
|
|
pub struct PubStruct;
|
2019-12-24 00:19:09 +00:00
|
|
|
}
|
2020-01-26 22:16:18 +00:00
|
|
|
pub mod PubMod3 {
|
|
|
|
pub struct PubStruct;
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn not_applicable_for_already_imported_types() {
|
2020-02-06 16:17:51 +00:00
|
|
|
check_assist_not_applicable(
|
2020-01-26 22:16:18 +00:00
|
|
|
auto_import,
|
|
|
|
r"
|
|
|
|
use PubMod::PubStruct;
|
|
|
|
|
|
|
|
PubStruct<|>
|
2019-12-24 00:19:09 +00:00
|
|
|
|
2020-01-26 22:16:18 +00:00
|
|
|
pub mod PubMod {
|
|
|
|
pub struct PubStruct;
|
|
|
|
}
|
|
|
|
",
|
2019-12-24 00:19:09 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-01-26 22:16:18 +00:00
|
|
|
fn not_applicable_for_types_with_private_paths() {
|
2020-02-06 16:17:51 +00:00
|
|
|
check_assist_not_applicable(
|
2019-12-24 00:19:09 +00:00
|
|
|
auto_import,
|
2020-01-26 22:16:18 +00:00
|
|
|
r"
|
|
|
|
PrivateStruct<|>
|
|
|
|
|
|
|
|
pub mod PubMod {
|
|
|
|
struct PrivateStruct;
|
2019-12-24 00:19:09 +00:00
|
|
|
}
|
2020-01-26 22:16:18 +00:00
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
2019-12-24 00:19:09 +00:00
|
|
|
|
2020-01-26 22:16:18 +00:00
|
|
|
#[test]
|
|
|
|
fn not_applicable_when_no_imports_found() {
|
2020-02-06 16:17:51 +00:00
|
|
|
check_assist_not_applicable(
|
2020-01-26 22:16:18 +00:00
|
|
|
auto_import,
|
|
|
|
"
|
|
|
|
PubStruct<|>",
|
2019-12-24 00:19:09 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn not_applicable_in_import_statements() {
|
2020-02-06 16:17:51 +00:00
|
|
|
check_assist_not_applicable(
|
2019-12-24 00:19:09 +00:00
|
|
|
auto_import,
|
2020-01-26 22:16:18 +00:00
|
|
|
r"
|
|
|
|
use PubStruct<|>;
|
|
|
|
|
|
|
|
pub mod PubMod {
|
|
|
|
pub struct PubStruct;
|
|
|
|
}",
|
2019-12-24 00:19:09 +00:00
|
|
|
);
|
|
|
|
}
|
2020-02-01 20:13:02 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn function_import() {
|
2020-02-06 16:17:51 +00:00
|
|
|
check_assist(
|
2020-02-01 20:13:02 +00:00
|
|
|
auto_import,
|
|
|
|
r"
|
|
|
|
test_function<|>
|
|
|
|
|
|
|
|
pub mod PubMod {
|
|
|
|
pub fn test_function() {};
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
use PubMod::test_function;
|
|
|
|
|
2020-05-20 20:55:37 +00:00
|
|
|
test_function
|
2020-02-01 20:13:02 +00:00
|
|
|
|
|
|
|
pub mod PubMod {
|
|
|
|
pub fn test_function() {};
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
2020-02-09 15:13:29 +00:00
|
|
|
|
2020-03-23 21:23:26 +00:00
|
|
|
#[test]
|
|
|
|
fn macro_import() {
|
|
|
|
check_assist(
|
|
|
|
auto_import,
|
|
|
|
r"
|
2020-06-23 20:27:24 +00:00
|
|
|
//- /lib.rs crate:crate_with_macro
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! foo {
|
|
|
|
() => ()
|
|
|
|
}
|
2020-03-23 21:23:26 +00:00
|
|
|
|
2020-06-23 20:27:24 +00:00
|
|
|
//- /main.rs crate:main deps:crate_with_macro
|
|
|
|
fn main() {
|
|
|
|
foo<|>
|
|
|
|
}
|
|
|
|
",
|
2020-03-23 21:23:26 +00:00
|
|
|
r"use crate_with_macro::foo;
|
|
|
|
|
|
|
|
fn main() {
|
2020-05-20 20:55:37 +00:00
|
|
|
foo
|
2020-03-23 21:23:26 +00:00
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-02-09 15:13:29 +00:00
|
|
|
#[test]
|
|
|
|
fn auto_import_target() {
|
|
|
|
check_assist_target(
|
|
|
|
auto_import,
|
|
|
|
r"
|
|
|
|
struct AssistInfo {
|
|
|
|
group_label: Option<<|>GroupLabel>,
|
|
|
|
}
|
|
|
|
|
|
|
|
mod m { pub struct GroupLabel; }
|
|
|
|
",
|
|
|
|
"GroupLabel",
|
|
|
|
)
|
|
|
|
}
|
2020-02-08 23:56:17 +00: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() {
|
|
|
|
mod2::mod3::TestStruct<|>
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
2020-02-09 22:22:12 +00: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() {
|
|
|
|
test_function<|>
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
2020-02-09 22:30:00 +00: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() {
|
|
|
|
TestStruct::test_function<|>
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
use test_mod::TestStruct;
|
|
|
|
|
|
|
|
mod test_mod {
|
|
|
|
pub struct TestStruct {}
|
|
|
|
impl TestStruct {
|
|
|
|
pub fn test_function() {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2020-05-20 20:55:37 +00:00
|
|
|
TestStruct::test_function
|
2020-02-09 22:30:00 +00:00
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-02-12 20:38:19 +00: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() {
|
|
|
|
TestStruct::TEST_CONST<|>
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
use test_mod::TestStruct;
|
|
|
|
|
|
|
|
mod test_mod {
|
|
|
|
pub struct TestStruct {}
|
|
|
|
impl TestStruct {
|
|
|
|
const TEST_CONST: u8 = 42;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2020-05-20 20:55:37 +00:00
|
|
|
TestStruct::TEST_CONST
|
2020-02-12 20:38:19 +00:00
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-02-09 22:30:00 +00: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() {
|
|
|
|
test_mod::TestStruct::test_function<|>
|
|
|
|
}
|
|
|
|
",
|
|
|
|
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 20:55:37 +00:00
|
|
|
test_mod::TestStruct::test_function
|
2020-02-09 22:30:00 +00:00
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-02-11 16:24:43 +00:00
|
|
|
fn not_applicable_for_imported_trait_for_function() {
|
2020-02-11 13:21:12 +00: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() {
|
|
|
|
test_mod::TestEnum::test_function<|>;
|
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-02-12 20:38:19 +00: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() {
|
|
|
|
test_mod::TestStruct::TEST_CONST<|>
|
|
|
|
}
|
|
|
|
",
|
|
|
|
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 20:55:37 +00:00
|
|
|
test_mod::TestStruct::TEST_CONST
|
2020-02-12 20:38:19 +00: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() {
|
|
|
|
test_mod::TestEnum::TEST_CONST<|>;
|
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-02-11 13:21:12 +00:00
|
|
|
#[test]
|
2020-02-09 22:30:00 +00: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 {};
|
2020-02-11 16:24:43 +00:00
|
|
|
test_struct.test_meth<|>od()
|
2020-02-09 22:30:00 +00: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 20:55:37 +00:00
|
|
|
test_struct.test_method()
|
2020-02-09 22:30:00 +00:00
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
2020-02-11 16:24:43 +00:00
|
|
|
|
2020-07-02 10:22:25 +00: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 {};
|
|
|
|
test_struct.test_meth<|>od()
|
|
|
|
}
|
|
|
|
//- /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() {
|
|
|
|
dep::test_mod::TestStruct::test_func<|>tion
|
|
|
|
}
|
|
|
|
//- /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() {
|
|
|
|
dep::test_mod::TestStruct::CONST<|>
|
|
|
|
}
|
|
|
|
//- /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 {};
|
|
|
|
test_struct.test_func<|>tion()
|
|
|
|
}
|
|
|
|
//- /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 {};
|
|
|
|
test_struct.test_meth<|>od()
|
|
|
|
}
|
|
|
|
//- /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 16:24:43 +00: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;
|
|
|
|
one.test<|>_method();
|
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
2020-06-09 16:48:44 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn dep_import() {
|
|
|
|
check_assist(
|
|
|
|
auto_import,
|
|
|
|
r"
|
2020-06-23 20:27:24 +00:00
|
|
|
//- /lib.rs crate:dep
|
|
|
|
pub struct Struct;
|
2020-06-09 16:48:44 +00:00
|
|
|
|
2020-06-23 20:27:24 +00:00
|
|
|
//- /main.rs crate:main deps:dep
|
|
|
|
fn main() {
|
|
|
|
Struct<|>
|
|
|
|
}
|
|
|
|
",
|
2020-06-09 16:48:44 +00:00
|
|
|
r"use dep::Struct;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
Struct
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn whole_segment() {
|
2020-06-10 09:39:06 +00:00
|
|
|
// Tests that only imports whose last segment matches the identifier get suggested.
|
2020-06-09 16:48:44 +00:00
|
|
|
check_assist(
|
|
|
|
auto_import,
|
|
|
|
r"
|
2020-06-23 20:27:24 +00:00
|
|
|
//- /lib.rs crate:dep
|
|
|
|
pub mod fmt {
|
|
|
|
pub trait Display {}
|
|
|
|
}
|
2020-06-09 16:48:44 +00:00
|
|
|
|
2020-06-23 20:27:24 +00:00
|
|
|
pub fn panic_fmt() {}
|
2020-06-09 16:48:44 +00:00
|
|
|
|
2020-06-23 20:27:24 +00:00
|
|
|
//- /main.rs crate:main deps:dep
|
|
|
|
struct S;
|
2020-06-09 16:48:44 +00:00
|
|
|
|
2020-06-23 20:27:24 +00:00
|
|
|
impl f<|>mt::Display for S {}
|
|
|
|
",
|
2020-06-09 16:48:44 +00:00
|
|
|
r"use dep::fmt;
|
|
|
|
|
|
|
|
struct S;
|
2020-06-23 20:27:24 +00:00
|
|
|
|
2020-06-09 16:48:44 +00:00
|
|
|
impl fmt::Display for S {}
|
2020-06-10 09:39:06 +00:00
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn macro_generated() {
|
|
|
|
// Tests that macro-generated items are suggested from external crates.
|
|
|
|
check_assist(
|
|
|
|
auto_import,
|
|
|
|
r"
|
2020-06-23 20:27:24 +00:00
|
|
|
//- /lib.rs crate:dep
|
|
|
|
macro_rules! mac {
|
|
|
|
() => {
|
|
|
|
pub struct Cheese;
|
|
|
|
};
|
|
|
|
}
|
2020-06-10 09:39:06 +00:00
|
|
|
|
2020-06-23 20:27:24 +00:00
|
|
|
mac!();
|
2020-06-10 09:39:06 +00:00
|
|
|
|
2020-06-23 20:27:24 +00:00
|
|
|
//- /main.rs crate:main deps:dep
|
|
|
|
fn main() {
|
|
|
|
Cheese<|>;
|
|
|
|
}
|
|
|
|
",
|
2020-06-10 09:39:06 +00:00
|
|
|
r"use dep::Cheese;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
Cheese;
|
|
|
|
}
|
2020-06-10 14:04:55 +00: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 20:27:24 +00:00
|
|
|
//- /lib.rs crate:dep
|
|
|
|
pub struct FMT;
|
|
|
|
pub struct fmt;
|
2020-06-10 14:04:55 +00:00
|
|
|
|
2020-06-23 20:27:24 +00:00
|
|
|
//- /main.rs crate:main deps:dep
|
|
|
|
fn main() {
|
|
|
|
FMT<|>;
|
|
|
|
}
|
|
|
|
",
|
2020-06-10 14:04:55 +00:00
|
|
|
r"use dep::FMT;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
FMT;
|
|
|
|
}
|
2020-06-09 16:48:44 +00:00
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
2019-12-24 00:19:09 +00:00
|
|
|
}
|