|
1 | 1 | //! Completion for derives
|
2 |
| -use hir::{HasAttrs, Macro}; |
| 2 | +use hir::{HasAttrs, ScopeDef}; |
3 | 3 | use ide_db::SymbolKind;
|
4 | 4 | use itertools::Itertools;
|
5 | 5 | use syntax::SmolStr;
|
6 | 6 |
|
7 | 7 | use crate::{
|
8 |
| - context::{CompletionContext, PathCompletionCtx, PathKind}, |
| 8 | + context::{CompletionContext, PathCompletionCtx, PathKind, PathQualifierCtx}, |
9 | 9 | item::CompletionItem,
|
10 | 10 | Completions,
|
11 | 11 | };
|
12 | 12 |
|
13 | 13 | pub(crate) fn complete_derive(acc: &mut Completions, ctx: &CompletionContext) {
|
14 |
| - match ctx.path_context { |
15 |
| - // FIXME: Enable qualified completions |
16 |
| - Some(PathCompletionCtx { kind: Some(PathKind::Derive), qualifier: None, .. }) => (), |
| 14 | + let (qualifier, is_absolute_path) = match ctx.path_context { |
| 15 | + Some(PathCompletionCtx { |
| 16 | + kind: Some(PathKind::Derive), |
| 17 | + ref qualifier, |
| 18 | + is_absolute_path, |
| 19 | + .. |
| 20 | + }) => (qualifier, is_absolute_path), |
17 | 21 | _ => return,
|
18 |
| - } |
| 22 | + }; |
19 | 23 |
|
20 | 24 | let core = ctx.famous_defs().core();
|
21 | 25 |
|
22 |
| - for (name, mac) in get_derives_in_scope(ctx) { |
23 |
| - if ctx.existing_derives.contains(&mac) { |
24 |
| - continue; |
25 |
| - } |
| 26 | + match qualifier { |
| 27 | + Some(PathQualifierCtx { resolution, is_super_chain, .. }) => { |
| 28 | + if *is_super_chain { |
| 29 | + acc.add_keyword(ctx, "super::"); |
| 30 | + } |
26 | 31 |
|
27 |
| - let name = name.to_smol_str(); |
28 |
| - let (label, lookup) = match (core, mac.module(ctx.db).krate()) { |
29 |
| - // show derive dependencies for `core`/`std` derives |
30 |
| - (Some(core), mac_krate) if core == mac_krate => { |
31 |
| - if let Some(derive_completion) = DEFAULT_DERIVE_DEPENDENCIES |
32 |
| - .iter() |
33 |
| - .find(|derive_completion| derive_completion.label == name) |
34 |
| - { |
35 |
| - let mut components = vec![derive_completion.label]; |
36 |
| - components.extend(derive_completion.dependencies.iter().filter( |
37 |
| - |&&dependency| { |
38 |
| - !ctx.existing_derives |
39 |
| - .iter() |
40 |
| - .map(|it| it.name(ctx.db)) |
41 |
| - .any(|it| it.to_smol_str() == dependency) |
42 |
| - }, |
43 |
| - )); |
44 |
| - let lookup = components.join(", "); |
45 |
| - let label = Itertools::intersperse(components.into_iter().rev(), ", "); |
46 |
| - (SmolStr::from_iter(label), Some(lookup)) |
47 |
| - } else { |
48 |
| - (name, None) |
| 32 | + let module = match resolution { |
| 33 | + Some(hir::PathResolution::Def(hir::ModuleDef::Module(it))) => it, |
| 34 | + _ => return, |
| 35 | + }; |
| 36 | + |
| 37 | + for (name, def) in module.scope(ctx.db, ctx.module) { |
| 38 | + let add_def = match def { |
| 39 | + ScopeDef::ModuleDef(hir::ModuleDef::Macro(mac)) => { |
| 40 | + !ctx.existing_derives.contains(&mac) && mac.is_derive(ctx.db) |
| 41 | + } |
| 42 | + ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) => true, |
| 43 | + _ => false, |
| 44 | + }; |
| 45 | + if add_def { |
| 46 | + acc.add_resolution(ctx, name, def); |
49 | 47 | }
|
50 | 48 | }
|
51 |
| - _ => (name, None), |
52 |
| - }; |
53 |
| - |
54 |
| - let mut item = CompletionItem::new(SymbolKind::Derive, ctx.source_range(), label); |
55 |
| - if let Some(docs) = mac.docs(ctx.db) { |
56 |
| - item.documentation(docs); |
| 49 | + return; |
57 | 50 | }
|
58 |
| - if let Some(lookup) = lookup { |
59 |
| - item.lookup_by(lookup); |
60 |
| - } |
61 |
| - item.add_to(acc); |
62 |
| - } |
63 |
| -} |
| 51 | + None if is_absolute_path => acc.add_crate_roots(ctx), |
| 52 | + // only show modules in a fresh UseTree |
| 53 | + None => { |
| 54 | + ctx.process_all_names(&mut |name, def| { |
| 55 | + let mac = match def { |
| 56 | + ScopeDef::ModuleDef(hir::ModuleDef::Macro(mac)) |
| 57 | + if !ctx.existing_derives.contains(&mac) && mac.is_derive(ctx.db) => |
| 58 | + { |
| 59 | + mac |
| 60 | + } |
| 61 | + ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) => { |
| 62 | + return acc.add_resolution(ctx, name, def); |
| 63 | + } |
| 64 | + _ => return, |
| 65 | + }; |
64 | 66 |
|
65 |
| -fn get_derives_in_scope(ctx: &CompletionContext) -> Vec<(hir::Name, Macro)> { |
66 |
| - let mut result = Vec::default(); |
67 |
| - ctx.process_all_names(&mut |name, scope_def| { |
68 |
| - if let hir::ScopeDef::ModuleDef(hir::ModuleDef::Macro(mac)) = scope_def { |
69 |
| - if mac.kind(ctx.db) == hir::MacroKind::Derive { |
70 |
| - result.push((name, mac)); |
71 |
| - } |
| 67 | + match (core, mac.module(ctx.db).krate()) { |
| 68 | + // show derive dependencies for `core`/`std` derives |
| 69 | + (Some(core), mac_krate) if core == mac_krate && qualifier.is_none() => {} |
| 70 | + _ => return acc.add_resolution(ctx, name, def), |
| 71 | + }; |
| 72 | + |
| 73 | + let name_ = name.to_smol_str(); |
| 74 | + let find = DEFAULT_DERIVE_DEPENDENCIES |
| 75 | + .iter() |
| 76 | + .find(|derive_completion| derive_completion.label == name_); |
| 77 | + |
| 78 | + match find { |
| 79 | + Some(derive_completion) => { |
| 80 | + let mut components = vec![derive_completion.label]; |
| 81 | + components.extend(derive_completion.dependencies.iter().filter( |
| 82 | + |&&dependency| { |
| 83 | + !ctx.existing_derives |
| 84 | + .iter() |
| 85 | + .map(|it| it.name(ctx.db)) |
| 86 | + .any(|it| it.to_smol_str() == dependency) |
| 87 | + }, |
| 88 | + )); |
| 89 | + let lookup = components.join(", "); |
| 90 | + let label = Itertools::intersperse(components.into_iter().rev(), ", "); |
| 91 | + |
| 92 | + let mut item = CompletionItem::new( |
| 93 | + SymbolKind::Derive, |
| 94 | + ctx.source_range(), |
| 95 | + SmolStr::from_iter(label), |
| 96 | + ); |
| 97 | + if let Some(docs) = mac.docs(ctx.db) { |
| 98 | + item.documentation(docs); |
| 99 | + } |
| 100 | + item.lookup_by(lookup); |
| 101 | + item.add_to(acc); |
| 102 | + } |
| 103 | + None => acc.add_resolution(ctx, name, def), |
| 104 | + } |
| 105 | + }); |
| 106 | + acc.add_nameref_keywords(ctx); |
72 | 107 | }
|
73 |
| - }); |
74 |
| - result |
| 108 | + } |
75 | 109 | }
|
76 | 110 |
|
77 | 111 | struct DeriveDependencies {
|
|
0 commit comments