diff options
Diffstat (limited to 'crates/completion')
-rw-r--r-- | crates/completion/Cargo.toml | 2 | ||||
-rw-r--r-- | crates/completion/src/completions.rs | 1 | ||||
-rw-r--r-- | crates/completion/src/completions/complete_magic.rs | 114 | ||||
-rw-r--r-- | crates/completion/src/item.rs | 1 | ||||
-rw-r--r-- | crates/completion/src/lib.rs | 1 |
5 files changed, 119 insertions, 0 deletions
diff --git a/crates/completion/Cargo.toml b/crates/completion/Cargo.toml index 3015ec9e0..799b4a3d5 100644 --- a/crates/completion/Cargo.toml +++ b/crates/completion/Cargo.toml | |||
@@ -13,6 +13,7 @@ doctest = false | |||
13 | itertools = "0.9.0" | 13 | itertools = "0.9.0" |
14 | log = "0.4.8" | 14 | log = "0.4.8" |
15 | rustc-hash = "1.1.0" | 15 | rustc-hash = "1.1.0" |
16 | either = "1.6.1" | ||
16 | 17 | ||
17 | assists = { path = "../assists", version = "0.0.0" } | 18 | assists = { path = "../assists", version = "0.0.0" } |
18 | stdx = { path = "../stdx", version = "0.0.0" } | 19 | stdx = { path = "../stdx", version = "0.0.0" } |
@@ -21,6 +22,7 @@ text_edit = { path = "../text_edit", version = "0.0.0" } | |||
21 | base_db = { path = "../base_db", version = "0.0.0" } | 22 | base_db = { path = "../base_db", version = "0.0.0" } |
22 | ide_db = { path = "../ide_db", version = "0.0.0" } | 23 | ide_db = { path = "../ide_db", version = "0.0.0" } |
23 | profile = { path = "../profile", version = "0.0.0" } | 24 | profile = { path = "../profile", version = "0.0.0" } |
25 | assists = { path = "../assists", version = "0.0.0" } | ||
24 | test_utils = { path = "../test_utils", version = "0.0.0" } | 26 | test_utils = { path = "../test_utils", version = "0.0.0" } |
25 | 27 | ||
26 | # completions crate should depend only on the top-level `hir` package. if you need | 28 | # completions crate should depend only on the top-level `hir` package. if you need |
diff --git a/crates/completion/src/completions.rs b/crates/completion/src/completions.rs index 75dbb1a23..99db5f998 100644 --- a/crates/completion/src/completions.rs +++ b/crates/completion/src/completions.rs | |||
@@ -13,6 +13,7 @@ pub(crate) mod postfix; | |||
13 | pub(crate) mod macro_in_item_position; | 13 | pub(crate) mod macro_in_item_position; |
14 | pub(crate) mod trait_impl; | 14 | pub(crate) mod trait_impl; |
15 | pub(crate) mod mod_; | 15 | pub(crate) mod mod_; |
16 | pub(crate) mod complete_magic; | ||
16 | 17 | ||
17 | use hir::{ModPath, ScopeDef, Type}; | 18 | use hir::{ModPath, ScopeDef, Type}; |
18 | 19 | ||
diff --git a/crates/completion/src/completions/complete_magic.rs b/crates/completion/src/completions/complete_magic.rs new file mode 100644 index 000000000..857a0b620 --- /dev/null +++ b/crates/completion/src/completions/complete_magic.rs | |||
@@ -0,0 +1,114 @@ | |||
1 | //! TODO kb move this into the complete_unqualified_path when starts to work properly | ||
2 | |||
3 | use assists::utils::{insert_use, mod_path_to_ast, ImportScope, MergeBehaviour}; | ||
4 | use hir::Query; | ||
5 | use itertools::Itertools; | ||
6 | use syntax::AstNode; | ||
7 | use text_edit::TextEdit; | ||
8 | |||
9 | use crate::{context::CompletionContext, item::CompletionKind, CompletionItem, CompletionItemKind}; | ||
10 | |||
11 | use super::Completions; | ||
12 | |||
13 | pub(crate) fn complete_magic(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> { | ||
14 | if !(ctx.is_trivial_path || ctx.is_pat_binding_or_const) { | ||
15 | return None; | ||
16 | } | ||
17 | let current_module = ctx.scope.module()?; | ||
18 | let anchor = ctx.name_ref_syntax.as_ref()?; | ||
19 | let import_scope = ImportScope::find_insert_use_container(anchor.syntax(), &ctx.sema)?; | ||
20 | // TODO kb now this is the whole file, which is not disjoint with any other change in the same file, fix it | ||
21 | // otherwise it's impossible to correctly add the use statement and also change the completed text into something more meaningful | ||
22 | let import_syntax = import_scope.as_syntax_node(); | ||
23 | |||
24 | // TODO kb consider heuristics, such as "don't show `hash_map` import if `HashMap` is the import for completion" | ||
25 | // TODO kb module functions are not completed, consider `std::io::stdin` one | ||
26 | let potential_import_name = ctx.token.to_string(); | ||
27 | |||
28 | let possible_imports = ctx | ||
29 | .krate? | ||
30 | // TODO kb use imports_locator instead? | ||
31 | .query_external_importables(ctx.db, Query::new(&potential_import_name).limit(40)) | ||
32 | .unique() | ||
33 | .filter_map(|import_candidate| match import_candidate { | ||
34 | either::Either::Left(module_def) => current_module.find_use_path(ctx.db, module_def), | ||
35 | either::Either::Right(macro_def) => current_module.find_use_path(ctx.db, macro_def), | ||
36 | }) | ||
37 | .filter_map(|mod_path| { | ||
38 | let correct_qualifier = mod_path.segments.last()?.to_string(); | ||
39 | let rewriter = | ||
40 | insert_use(&import_scope, mod_path_to_ast(&mod_path), Some(MergeBehaviour::Full)); | ||
41 | let rewritten_node = rewriter.rewrite(import_syntax); | ||
42 | let insert_use_edit = | ||
43 | TextEdit::replace(import_syntax.text_range(), rewritten_node.to_string()); | ||
44 | let mut completion_edit = | ||
45 | TextEdit::replace(anchor.syntax().text_range(), correct_qualifier); | ||
46 | completion_edit.union(insert_use_edit).expect("TODO kb"); | ||
47 | |||
48 | let completion_item: CompletionItem = CompletionItem::new( | ||
49 | CompletionKind::Magic, | ||
50 | ctx.source_range(), | ||
51 | mod_path.to_string(), | ||
52 | ) | ||
53 | .kind(CompletionItemKind::Struct) | ||
54 | .text_edit(completion_edit) | ||
55 | .into(); | ||
56 | Some(completion_item) | ||
57 | }); | ||
58 | acc.add_all(possible_imports); | ||
59 | |||
60 | Some(()) | ||
61 | } | ||
62 | |||
63 | #[cfg(test)] | ||
64 | mod tests { | ||
65 | use expect_test::{expect, Expect}; | ||
66 | |||
67 | use crate::{ | ||
68 | item::CompletionKind, | ||
69 | test_utils::{check_edit, completion_list}, | ||
70 | }; | ||
71 | |||
72 | fn check(ra_fixture: &str, expect: Expect) { | ||
73 | let actual = completion_list(ra_fixture, CompletionKind::Magic); | ||
74 | expect.assert_eq(&actual) | ||
75 | } | ||
76 | |||
77 | #[test] | ||
78 | fn case_insensitive_magic_completion_works() { | ||
79 | check( | ||
80 | r#" | ||
81 | //- /lib.rs crate:dep | ||
82 | pub struct TestStruct; | ||
83 | |||
84 | //- /main.rs crate:main deps:dep | ||
85 | fn main() { | ||
86 | teru<|> | ||
87 | } | ||
88 | "#, | ||
89 | expect![[r#" | ||
90 | st dep::TestStruct | ||
91 | "#]], | ||
92 | ); | ||
93 | |||
94 | check_edit( | ||
95 | "dep::TestStruct", | ||
96 | r#" | ||
97 | //- /lib.rs crate:dep | ||
98 | pub struct TestStruct; | ||
99 | |||
100 | //- /main.rs crate:main deps:dep | ||
101 | fn main() { | ||
102 | teru<|> | ||
103 | } | ||
104 | "#, | ||
105 | r#" | ||
106 | use dep::TestStruct; | ||
107 | |||
108 | fn main() { | ||
109 | TestStruct | ||
110 | } | ||
111 | "#, | ||
112 | ); | ||
113 | } | ||
114 | } | ||
diff --git a/crates/completion/src/item.rs b/crates/completion/src/item.rs index 6d1d085f4..f23913935 100644 --- a/crates/completion/src/item.rs +++ b/crates/completion/src/item.rs | |||
@@ -31,6 +31,7 @@ pub struct CompletionItem { | |||
31 | /// | 31 | /// |
32 | /// Typically, replaces `source_range` with new identifier. | 32 | /// Typically, replaces `source_range` with new identifier. |
33 | text_edit: TextEdit, | 33 | text_edit: TextEdit, |
34 | |||
34 | insert_text_format: InsertTextFormat, | 35 | insert_text_format: InsertTextFormat, |
35 | 36 | ||
36 | /// What item (struct, function, etc) are we completing. | 37 | /// What item (struct, function, etc) are we completing. |
diff --git a/crates/completion/src/lib.rs b/crates/completion/src/lib.rs index cb6e0554e..e920fa6b5 100644 --- a/crates/completion/src/lib.rs +++ b/crates/completion/src/lib.rs | |||
@@ -118,6 +118,7 @@ pub fn completions( | |||
118 | completions::macro_in_item_position::complete_macro_in_item_position(&mut acc, &ctx); | 118 | completions::macro_in_item_position::complete_macro_in_item_position(&mut acc, &ctx); |
119 | completions::trait_impl::complete_trait_impl(&mut acc, &ctx); | 119 | completions::trait_impl::complete_trait_impl(&mut acc, &ctx); |
120 | completions::mod_::complete_mod(&mut acc, &ctx); | 120 | completions::mod_::complete_mod(&mut acc, &ctx); |
121 | completions::complete_magic::complete_magic(&mut acc, &ctx); | ||
121 | 122 | ||
122 | Some(acc) | 123 | Some(acc) |
123 | } | 124 | } |