aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_analysis/src/completion
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_analysis/src/completion')
-rw-r--r--crates/ra_analysis/src/completion/mod.rs13
-rw-r--r--crates/ra_analysis/src/completion/reference_completion.rs44
2 files changed, 28 insertions, 29 deletions
diff --git a/crates/ra_analysis/src/completion/mod.rs b/crates/ra_analysis/src/completion/mod.rs
index d7acca044..124da486a 100644
--- a/crates/ra_analysis/src/completion/mod.rs
+++ b/crates/ra_analysis/src/completion/mod.rs
@@ -7,13 +7,11 @@ use ra_syntax::{
7 AstNode, AtomEdit, 7 AstNode, AtomEdit,
8 SyntaxNodeRef, 8 SyntaxNodeRef,
9}; 9};
10use ra_db::SyntaxDatabase;
10use rustc_hash::{FxHashMap}; 11use rustc_hash::{FxHashMap};
11 12
12use crate::{ 13use crate::{
13 db::{self, SyntaxDatabase}, 14 db,
14 descriptors::{
15 module::{ModuleDescriptor}
16 },
17 Cancelable, FilePosition 15 Cancelable, FilePosition
18}; 16};
19 17
@@ -31,17 +29,14 @@ pub(crate) fn completions(
31 db: &db::RootDatabase, 29 db: &db::RootDatabase,
32 position: FilePosition, 30 position: FilePosition,
33) -> Cancelable<Option<Vec<CompletionItem>>> { 31) -> Cancelable<Option<Vec<CompletionItem>>> {
34 let original_file = db.file_syntax(position.file_id); 32 let original_file = db.source_file(position.file_id);
35 // Insert a fake ident to get a valid parse tree 33 // Insert a fake ident to get a valid parse tree
36 let file = { 34 let file = {
37 let edit = AtomEdit::insert(position.offset, "intellijRulezz".to_string()); 35 let edit = AtomEdit::insert(position.offset, "intellijRulezz".to_string());
38 original_file.reparse(&edit) 36 original_file.reparse(&edit)
39 }; 37 };
40 38
41 let module = match ModuleDescriptor::guess_from_position(db, position)? { 39 let module = ctry!(hir::Module::guess_from_position(db, position)?);
42 None => return Ok(None),
43 Some(it) => it,
44 };
45 40
46 let mut res = Vec::new(); 41 let mut res = Vec::new();
47 let mut has_completions = false; 42 let mut has_completions = false;
diff --git a/crates/ra_analysis/src/completion/reference_completion.rs b/crates/ra_analysis/src/completion/reference_completion.rs
index e52062107..8ea7478a8 100644
--- a/crates/ra_analysis/src/completion/reference_completion.rs
+++ b/crates/ra_analysis/src/completion/reference_completion.rs
@@ -6,22 +6,23 @@ use ra_syntax::{
6 ast::{self, LoopBodyOwner}, 6 ast::{self, LoopBodyOwner},
7 SyntaxKind::*, 7 SyntaxKind::*,
8}; 8};
9use hir::{
10 self,
11 FnScopes,
12 Def,
13 Path,
14};
9 15
10use crate::{ 16use crate::{
11 db::RootDatabase, 17 db::RootDatabase,
12 completion::CompletionItem, 18 completion::CompletionItem,
13 descriptors::{
14 module::{ModuleDescriptor},
15 function::FnScopes,
16 Path,
17 },
18 Cancelable 19 Cancelable
19}; 20};
20 21
21pub(super) fn completions( 22pub(super) fn completions(
22 acc: &mut Vec<CompletionItem>, 23 acc: &mut Vec<CompletionItem>,
23 db: &RootDatabase, 24 db: &RootDatabase,
24 module: &ModuleDescriptor, 25 module: &hir::Module,
25 file: &SourceFileNode, 26 file: &SourceFileNode,
26 name_ref: ast::NameRef, 27 name_ref: ast::NameRef,
27) -> Cancelable<()> { 28) -> Cancelable<()> {
@@ -42,13 +43,15 @@ pub(super) fn completions(
42 let module_scope = module.scope(db)?; 43 let module_scope = module.scope(db)?;
43 acc.extend( 44 acc.extend(
44 module_scope 45 module_scope
45 .items 46 .entries()
46 .iter()
47 .filter(|(_name, res)| { 47 .filter(|(_name, res)| {
48 // Don't expose this item 48 // Don't expose this item
49 match res.import_name { 49 match res.import {
50 None => true, 50 None => true,
51 Some(ptr) => !ptr.range().is_subrange(&name_ref.syntax().range()), 51 Some(import) => {
52 let range = import.range(db, module.source().file_id());
53 !range.is_subrange(&name_ref.syntax().range())
54 }
52 } 55 }
53 }) 56 })
54 .map(|(name, _res)| CompletionItem { 57 .map(|(name, _res)| CompletionItem {
@@ -147,26 +150,27 @@ fn complete_fn(name_ref: ast::NameRef, scopes: &FnScopes, acc: &mut Vec<Completi
147fn complete_path( 150fn complete_path(
148 acc: &mut Vec<CompletionItem>, 151 acc: &mut Vec<CompletionItem>,
149 db: &RootDatabase, 152 db: &RootDatabase,
150 module: &ModuleDescriptor, 153 module: &hir::Module,
151 mut path: Path, 154 mut path: Path,
152) -> Cancelable<()> { 155) -> Cancelable<()> {
153 if path.segments.is_empty() { 156 if path.segments.is_empty() {
154 return Ok(()); 157 return Ok(());
155 } 158 }
156 path.segments.pop(); 159 path.segments.pop();
157 let target_module = match module.resolve_path(path) { 160 let def_id = match module.resolve_path(db, path)? {
158 None => return Ok(()), 161 None => return Ok(()),
159 Some(it) => it, 162 Some(it) => it,
160 }; 163 };
164 let target_module = match def_id.resolve(db)? {
165 Def::Module(it) => it,
166 Def::Item => return Ok(()),
167 };
161 let module_scope = target_module.scope(db)?; 168 let module_scope = target_module.scope(db)?;
162 let completions = module_scope 169 let completions = module_scope.entries().map(|(name, _res)| CompletionItem {
163 .items 170 label: name.to_string(),
164 .iter() 171 lookup: None,
165 .map(|(name, _res)| CompletionItem { 172 snippet: None,
166 label: name.to_string(), 173 });
167 lookup: None,
168 snippet: None,
169 });
170 acc.extend(completions); 174 acc.extend(completions);
171 Ok(()) 175 Ok(())
172} 176}