aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorFlorian Diebold <[email protected]>2019-02-13 19:53:42 +0000
committerFlorian Diebold <[email protected]>2019-02-13 20:13:08 +0000
commit911e32bca9b73e66eceb6bbee3768c82e94597d5 (patch)
tree489b3497b2762dcabf60d2674031585431e16959 /crates
parent92c595a6a6f7624092432d28ffd7e0ffd189cbda (diff)
Complete names from prelude
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir/src/nameres.rs2
-rw-r--r--crates/ra_hir/src/resolve.rs12
-rw-r--r--crates/ra_ide_api/src/completion/complete_scope.rs21
-rw-r--r--crates/ra_ide_api/src/completion/snapshots/completion_item__completes_prelude.snap54
4 files changed, 84 insertions, 5 deletions
diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs
index cd7b41cff..e35b4b129 100644
--- a/crates/ra_hir/src/nameres.rs
+++ b/crates/ra_hir/src/nameres.rs
@@ -40,7 +40,7 @@ pub struct ItemMap {
40 /// The prelude module for this crate. This either comes from an import 40 /// The prelude module for this crate. This either comes from an import
41 /// marked with the `prelude_import` attribute, or (in the normal case) from 41 /// marked with the `prelude_import` attribute, or (in the normal case) from
42 /// a dependency (`std` or `core`). 42 /// a dependency (`std` or `core`).
43 prelude: Option<Module>, 43 pub(crate) prelude: Option<Module>,
44 pub(crate) extern_prelude: FxHashMap<Name, ModuleDef>, 44 pub(crate) extern_prelude: FxHashMap<Name, ModuleDef>,
45 per_module: ArenaMap<ModuleId, ModuleScope>, 45 per_module: ArenaMap<ModuleId, ModuleScope>,
46} 46}
diff --git a/crates/ra_hir/src/resolve.rs b/crates/ra_hir/src/resolve.rs
index fde4d6580..91a531801 100644
--- a/crates/ra_hir/src/resolve.rs
+++ b/crates/ra_hir/src/resolve.rs
@@ -82,10 +82,10 @@ impl Resolver {
82 } 82 }
83 } 83 }
84 84
85 pub fn all_names(&self) -> FxHashMap<Name, PerNs<Resolution>> { 85 pub fn all_names(&self, db: &impl HirDatabase) -> FxHashMap<Name, PerNs<Resolution>> {
86 let mut names = FxHashMap::default(); 86 let mut names = FxHashMap::default();
87 for scope in self.scopes.iter().rev() { 87 for scope in self.scopes.iter().rev() {
88 scope.collect_names(&mut |name, res| { 88 scope.collect_names(db, &mut |name, res| {
89 let current: &mut PerNs<Resolution> = names.entry(name).or_default(); 89 let current: &mut PerNs<Resolution> = names.entry(name).or_default();
90 if current.types.is_none() { 90 if current.types.is_none() {
91 current.types = res.types; 91 current.types = res.types;
@@ -174,7 +174,7 @@ impl Scope {
174 } 174 }
175 } 175 }
176 176
177 fn collect_names(&self, f: &mut dyn FnMut(Name, PerNs<Resolution>)) { 177 fn collect_names(&self, db: &impl HirDatabase, f: &mut dyn FnMut(Name, PerNs<Resolution>)) {
178 match self { 178 match self {
179 Scope::ModuleScope(m) => { 179 Scope::ModuleScope(m) => {
180 // TODO: should we provide `self` here? 180 // TODO: should we provide `self` here?
@@ -190,6 +190,12 @@ impl Scope {
190 m.item_map.extern_prelude.iter().for_each(|(name, def)| { 190 m.item_map.extern_prelude.iter().for_each(|(name, def)| {
191 f(name.clone(), PerNs::types(Resolution::Def(*def))); 191 f(name.clone(), PerNs::types(Resolution::Def(*def)));
192 }); 192 });
193 if let Some(prelude) = m.item_map.prelude {
194 let prelude_item_map = db.item_map(prelude.krate);
195 prelude_item_map[prelude.module_id].entries().for_each(|(name, res)| {
196 f(name.clone(), res.def.map(Resolution::Def));
197 });
198 }
193 } 199 }
194 Scope::GenericParams(gp) => { 200 Scope::GenericParams(gp) => {
195 for param in &gp.params { 201 for param in &gp.params {
diff --git a/crates/ra_ide_api/src/completion/complete_scope.rs b/crates/ra_ide_api/src/completion/complete_scope.rs
index 445788407..eeaf26d93 100644
--- a/crates/ra_ide_api/src/completion/complete_scope.rs
+++ b/crates/ra_ide_api/src/completion/complete_scope.rs
@@ -4,7 +4,7 @@ pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) {
4 if !ctx.is_trivial_path { 4 if !ctx.is_trivial_path {
5 return; 5 return;
6 } 6 }
7 let names = ctx.resolver.all_names(); 7 let names = ctx.resolver.all_names(ctx.db);
8 8
9 names.into_iter().for_each(|(name, res)| { 9 names.into_iter().for_each(|(name, res)| {
10 CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.to_string()) 10 CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.to_string())
@@ -165,4 +165,23 @@ mod tests {
165 fn completes_self_in_methods() { 165 fn completes_self_in_methods() {
166 check_reference_completion("self_in_methods", r"impl S { fn foo(&self) { <|> } }") 166 check_reference_completion("self_in_methods", r"impl S { fn foo(&self) { <|> } }")
167 } 167 }
168
169 #[test]
170 fn completes_prelude() {
171 check_reference_completion(
172 "completes_prelude",
173 "
174 //- /main.rs
175 fn foo() { let x: <|> }
176
177 //- /std/lib.rs
178 #[prelude_import]
179 use prelude::*;
180
181 mod prelude {
182 struct Option;
183 }
184 ",
185 );
186 }
168} 187}
diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__completes_prelude.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__completes_prelude.snap
new file mode 100644
index 000000000..2b5a1a8ea
--- /dev/null
+++ b/crates/ra_ide_api/src/completion/snapshots/completion_item__completes_prelude.snap
@@ -0,0 +1,54 @@
1---
2created: "2019-02-13T19:52:43.734834624Z"
3creator: [email protected]
4source: crates/ra_ide_api/src/completion/completion_item.rs
5expression: kind_completions
6---
7[
8 CompletionItem {
9 completion_kind: Reference,
10 label: "Option",
11 kind: Some(
12 Struct
13 ),
14 detail: None,
15 documentation: None,
16 lookup: None,
17 insert_text: None,
18 insert_text_format: PlainText,
19 source_range: [18; 18),
20 text_edit: None
21 },
22 CompletionItem {
23 completion_kind: Reference,
24 label: "foo",
25 kind: Some(
26 Function
27 ),
28 detail: Some(
29 "fn foo()"
30 ),
31 documentation: None,
32 lookup: None,
33 insert_text: Some(
34 "foo()$0"
35 ),
36 insert_text_format: Snippet,
37 source_range: [18; 18),
38 text_edit: None
39 },
40 CompletionItem {
41 completion_kind: Reference,
42 label: "std",
43 kind: Some(
44 Module
45 ),
46 detail: None,
47 documentation: None,
48 lookup: None,
49 insert_text: None,
50 insert_text_format: PlainText,
51 source_range: [18; 18),
52 text_edit: None
53 }
54]