Skip to content

Commit ee99620

Browse files
Move autoimport completion into the unqialified_path module
1 parent 3b0fc4d commit ee99620

File tree

5 files changed

+139
-156
lines changed

5 files changed

+139
-156
lines changed

crates/completion/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ text_edit = { path = "../text_edit", version = "0.0.0" }
2222
base_db = { path = "../base_db", version = "0.0.0" }
2323
ide_db = { path = "../ide_db", version = "0.0.0" }
2424
profile = { path = "../profile", version = "0.0.0" }
25-
assists = { path = "../assists", version = "0.0.0" }
2625
test_utils = { path = "../test_utils", version = "0.0.0" }
2726

2827
# completions crate should depend only on the top-level `hir` package. if you need

crates/completion/src/completions.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ pub(crate) mod postfix;
1313
pub(crate) mod macro_in_item_position;
1414
pub(crate) mod trait_impl;
1515
pub(crate) mod mod_;
16-
pub(crate) mod magic;
1716

1817
use hir::{ModPath, ScopeDef, Type};
1918

crates/completion/src/completions/magic.rs

Lines changed: 0 additions & 151 deletions
This file was deleted.

crates/completion/src/completions/unqualified_path.rs

Lines changed: 139 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
//! Completion of names from the current scope, e.g. locals and imported items.
22
3+
use assists::utils::{insert_use, mod_path_to_ast, ImportScope};
4+
use either::Either;
35
use hir::{Adt, ModuleDef, ScopeDef, Type};
4-
use syntax::AstNode;
6+
use ide_db::imports_locator;
7+
use syntax::{algo, AstNode};
58
use test_utils::mark;
69

7-
use crate::{CompletionContext, Completions};
10+
use crate::{
11+
render::{render_resolution, RenderContext},
12+
CompletionContext, Completions,
13+
};
814

915
pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionContext) {
1016
if !(ctx.is_trivial_path || ctx.is_pat_binding_or_const) {
@@ -37,6 +43,56 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
3743
}
3844
acc.add_resolution(ctx, name.to_string(), &res)
3945
});
46+
47+
fuzzy_completion(acc, ctx).unwrap_or_default()
48+
}
49+
50+
// TODO kb add a setting toggle for this feature?
51+
fn fuzzy_completion(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
52+
let _p = profile::span("fuzzy_completion®");
53+
let current_module = ctx.scope.module()?;
54+
let anchor = ctx.name_ref_syntax.as_ref()?;
55+
let import_scope = ImportScope::find_insert_use_container(anchor.syntax(), &ctx.sema)?;
56+
57+
let potential_import_name = ctx.token.to_string();
58+
59+
let possible_imports =
60+
imports_locator::find_similar_imports(&ctx.sema, ctx.krate?, &potential_import_name, 400)
61+
.filter_map(|import_candidate| match import_candidate {
62+
// when completing outside the use declaration, modules are pretty useless
63+
// and tend to bloat the completion suggestions a lot
64+
Either::Left(ModuleDef::Module(_)) => None,
65+
Either::Left(module_def) => Some((
66+
current_module.find_use_path(ctx.db, module_def)?,
67+
ScopeDef::ModuleDef(module_def),
68+
)),
69+
Either::Right(macro_def) => Some((
70+
current_module.find_use_path(ctx.db, macro_def)?,
71+
ScopeDef::MacroDef(macro_def),
72+
)),
73+
})
74+
.filter_map(|(mod_path, definition)| {
75+
let mut resolution_with_missing_import = render_resolution(
76+
RenderContext::new(ctx),
77+
mod_path.segments.last()?.to_string(),
78+
&definition,
79+
)?;
80+
81+
let mut text_edits =
82+
resolution_with_missing_import.text_edit().to_owned().into_builder();
83+
84+
let rewriter =
85+
insert_use(&import_scope, mod_path_to_ast(&mod_path), ctx.config.merge);
86+
let old_ast = rewriter.rewrite_root()?;
87+
algo::diff(&old_ast, &rewriter.rewrite(&old_ast)).into_text_edit(&mut text_edits);
88+
89+
resolution_with_missing_import.update_text_edit(text_edits.finish());
90+
91+
Some(resolution_with_missing_import)
92+
});
93+
94+
acc.add_all(possible_imports);
95+
Some(())
4096
}
4197

4298
fn complete_enum_variants(acc: &mut Completions, ctx: &CompletionContext, ty: &Type) {
@@ -676,4 +732,85 @@ impl My<|>
676732
"#]],
677733
)
678734
}
735+
736+
#[test]
737+
fn function_magic_completion() {
738+
check_edit(
739+
"stdin",
740+
r#"
741+
//- /lib.rs crate:dep
742+
pub mod io {
743+
pub fn stdin() {}
744+
};
745+
746+
//- /main.rs crate:main deps:dep
747+
fn main() {
748+
stdi<|>
749+
}
750+
"#,
751+
r#"
752+
use dep::io::stdin;
753+
754+
fn main() {
755+
stdin()$0
756+
}
757+
"#,
758+
);
759+
}
760+
761+
#[test]
762+
fn macro_magic_completion() {
763+
check_edit(
764+
"macro_with_curlies!",
765+
r#"
766+
//- /lib.rs crate:dep
767+
/// Please call me as macro_with_curlies! {}
768+
#[macro_export]
769+
macro_rules! macro_with_curlies {
770+
() => {}
771+
}
772+
773+
//- /main.rs crate:main deps:dep
774+
fn main() {
775+
curli<|>
776+
}
777+
"#,
778+
r#"
779+
use dep::macro_with_curlies;
780+
781+
fn main() {
782+
macro_with_curlies! {$0}
783+
}
784+
"#,
785+
);
786+
}
787+
788+
#[test]
789+
fn case_insensitive_magic_completion_works() {
790+
check_edit(
791+
"ThirdStruct",
792+
r#"
793+
//- /lib.rs crate:dep
794+
pub struct FirstStruct;
795+
pub mod some_module {
796+
pub struct SecondStruct;
797+
pub struct ThirdStruct;
798+
}
799+
800+
//- /main.rs crate:main deps:dep
801+
use dep::{FirstStruct, some_module::SecondStruct};
802+
803+
fn main() {
804+
this<|>
805+
}
806+
"#,
807+
r#"
808+
use dep::{FirstStruct, some_module::{SecondStruct, ThirdStruct}};
809+
810+
fn main() {
811+
ThirdStruct
812+
}
813+
"#,
814+
);
815+
}
679816
}

crates/completion/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ pub fn completions(
118118
completions::macro_in_item_position::complete_macro_in_item_position(&mut acc, &ctx);
119119
completions::trait_impl::complete_trait_impl(&mut acc, &ctx);
120120
completions::mod_::complete_mod(&mut acc, &ctx);
121-
completions::magic::complete_magic(&mut acc, &ctx);
122121

123122
Some(acc)
124123
}

0 commit comments

Comments
 (0)