diff options
Diffstat (limited to 'crates/ra_ide_api/src/completion/complete_scope.rs')
-rw-r--r-- | crates/ra_ide_api/src/completion/complete_scope.rs | 55 |
1 files changed, 50 insertions, 5 deletions
diff --git a/crates/ra_ide_api/src/completion/complete_scope.rs b/crates/ra_ide_api/src/completion/complete_scope.rs index fd256fc3b..63d475823 100644 --- a/crates/ra_ide_api/src/completion/complete_scope.rs +++ b/crates/ra_ide_api/src/completion/complete_scope.rs | |||
@@ -1,12 +1,57 @@ | |||
1 | use crate::completion::{Completions, CompletionContext}; | 1 | use ra_text_edit::TextEditBuilder; |
2 | use ra_syntax::SmolStr; | ||
3 | use ra_assists::auto_import; | ||
4 | use crate::completion::{CompletionItem, Completions, CompletionKind, CompletionContext}; | ||
2 | 5 | ||
3 | pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) { | 6 | pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) { |
4 | if !ctx.is_trivial_path { | 7 | if ctx.is_trivial_path { |
5 | return; | 8 | let names = ctx.analyzer.all_names(ctx.db); |
9 | names.into_iter().for_each(|(name, res)| acc.add_resolution(ctx, name.to_string(), &res)); | ||
6 | } | 10 | } |
7 | let names = ctx.analyzer.all_names(ctx.db); | ||
8 | 11 | ||
9 | names.into_iter().for_each(|(name, res)| acc.add_resolution(ctx, name.to_string(), &res)); | 12 | if let Some(name) = ctx.path_ident.as_ref() { |
13 | let import_names = ctx.analyzer.all_import_names(ctx.db, name); | ||
14 | import_names.into_iter().for_each(|(name, path)| { | ||
15 | let edit = { | ||
16 | let mut builder = TextEditBuilder::default(); | ||
17 | builder.replace(ctx.source_range(), name.to_string()); | ||
18 | auto_import::auto_import_text_edit( | ||
19 | ctx.token.parent(), | ||
20 | ctx.token.parent(), | ||
21 | &path, | ||
22 | &mut builder, | ||
23 | ); | ||
24 | builder.finish() | ||
25 | }; | ||
26 | CompletionItem::new( | ||
27 | CompletionKind::Reference, | ||
28 | ctx.source_range(), | ||
29 | build_import_label(&name, &path), | ||
30 | ) | ||
31 | .text_edit(edit) | ||
32 | .add_to(acc) | ||
33 | }); | ||
34 | } | ||
35 | } | ||
36 | |||
37 | fn build_import_label(name: &str, path: &Vec<SmolStr>) -> String { | ||
38 | let mut buf = String::with_capacity(64); | ||
39 | buf.push_str(name); | ||
40 | buf.push_str(" ("); | ||
41 | fmt_import_path(path, &mut buf); | ||
42 | buf.push_str(")"); | ||
43 | buf | ||
44 | } | ||
45 | |||
46 | fn fmt_import_path(path: &Vec<SmolStr>, buf: &mut String) { | ||
47 | let mut segments = path.iter(); | ||
48 | if let Some(s) = segments.next() { | ||
49 | buf.push_str(&s); | ||
50 | } | ||
51 | for s in segments { | ||
52 | buf.push_str("::"); | ||
53 | buf.push_str(&s); | ||
54 | } | ||
10 | } | 55 | } |
11 | 56 | ||
12 | #[cfg(test)] | 57 | #[cfg(test)] |