diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2018-10-30 23:10:48 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2018-10-30 23:10:48 +0000 |
commit | 032d15c392e8de7936c8729e03e6229313f3b054 (patch) | |
tree | 61cfa26801e456d5fdcd2619238c05bce62a1ac2 /crates/ra_analysis/src/completion.rs | |
parent | 23cad90fe98dcca7f4b6905d9fd3ed52d2896c8c (diff) | |
parent | fbbee537228538f448a335bb0b2dabec2b3d443e (diff) |
Merge #171
171: Query-based module scopes r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_analysis/src/completion.rs')
-rw-r--r-- | crates/ra_analysis/src/completion.rs | 35 |
1 files changed, 23 insertions, 12 deletions
diff --git a/crates/ra_analysis/src/completion.rs b/crates/ra_analysis/src/completion.rs index 0a2f99575..0141d132e 100644 --- a/crates/ra_analysis/src/completion.rs +++ b/crates/ra_analysis/src/completion.rs | |||
@@ -1,40 +1,51 @@ | |||
1 | use ra_editor::{CompletionItem, find_node_at_offset, complete_module_items}; | 1 | use ra_editor::{CompletionItem, find_node_at_offset}; |
2 | use ra_syntax::{ | 2 | use ra_syntax::{ |
3 | AtomEdit, File, TextUnit, AstNode, | 3 | AtomEdit, File, TextUnit, AstNode, |
4 | ast::{self, ModuleItemOwner}, | 4 | ast::{self, ModuleItemOwner, AstChildren}, |
5 | }; | 5 | }; |
6 | 6 | ||
7 | use crate::{ | 7 | use crate::{ |
8 | FileId, Cancelable, | 8 | FileId, Cancelable, |
9 | input::FilesDatabase, | 9 | input::FilesDatabase, |
10 | db::{self, SyntaxDatabase}, | 10 | db::{self, SyntaxDatabase}, |
11 | descriptors::module::{ModulesDatabase, ModuleTree, ModuleId}, | 11 | descriptors::module::{ModulesDatabase, ModuleTree, ModuleId, scope::ModuleScope}, |
12 | }; | 12 | }; |
13 | 13 | ||
14 | pub(crate) fn resolve_based_completion(db: &db::RootDatabase, file_id: FileId, offset: TextUnit) -> Cancelable<Option<Vec<CompletionItem>>> { | 14 | pub(crate) fn resolve_based_completion(db: &db::RootDatabase, file_id: FileId, offset: TextUnit) -> Cancelable<Option<Vec<CompletionItem>>> { |
15 | let source_root_id = db.file_source_root(file_id); | 15 | let source_root_id = db.file_source_root(file_id); |
16 | let file = db.file_syntax(file_id); | 16 | let file = db.file_syntax(file_id); |
17 | let module_tree = db.module_tree(source_root_id)?; | 17 | let module_tree = db.module_tree(source_root_id)?; |
18 | let module_id = match module_tree.any_module_for_file(file_id) { | ||
19 | None => return Ok(None), | ||
20 | Some(it) => it, | ||
21 | }; | ||
18 | let file = { | 22 | let file = { |
19 | let edit = AtomEdit::insert(offset, "intellijRulezz".to_string()); | 23 | let edit = AtomEdit::insert(offset, "intellijRulezz".to_string()); |
20 | file.reparse(&edit) | 24 | file.reparse(&edit) |
21 | }; | 25 | }; |
22 | let target_file = match find_target_module(&module_tree, file_id, &file, offset) { | 26 | let target_module_id = match find_target_module(&module_tree, module_id, &file, offset) { |
23 | None => return Ok(None), | 27 | None => return Ok(None), |
24 | Some(target_module) => { | 28 | Some(it) => it, |
25 | let file_id = target_module.file_id(&module_tree); | ||
26 | db.file_syntax(file_id) | ||
27 | } | ||
28 | }; | 29 | }; |
29 | let mut res = Vec::new(); | 30 | let module_scope = db.module_scope(source_root_id, target_module_id)?; |
30 | complete_module_items(target_file.ast().items(), None, &mut res); | 31 | let res: Vec<_> = module_scope |
32 | .entries() | ||
33 | .iter() | ||
34 | .map(|entry| CompletionItem { | ||
35 | label: entry.name().to_string(), | ||
36 | lookup: None, | ||
37 | snippet: None, | ||
38 | }) | ||
39 | .collect(); | ||
31 | Ok(Some(res)) | 40 | Ok(Some(res)) |
32 | } | 41 | } |
33 | 42 | ||
34 | pub(crate) fn find_target_module(module_tree: &ModuleTree, file_id: FileId, file: &File, offset: TextUnit) -> Option<ModuleId> { | 43 | |
44 | |||
45 | pub(crate) fn find_target_module(module_tree: &ModuleTree, module_id: ModuleId, file: &File, offset: TextUnit) -> Option<ModuleId> { | ||
35 | let name_ref: ast::NameRef = find_node_at_offset(file.syntax(), offset)?; | 46 | let name_ref: ast::NameRef = find_node_at_offset(file.syntax(), offset)?; |
36 | let mut crate_path = crate_path(name_ref)?; | 47 | let mut crate_path = crate_path(name_ref)?; |
37 | let module_id = module_tree.any_module_for_file(file_id)?; | 48 | |
38 | crate_path.pop(); | 49 | crate_path.pop(); |
39 | let mut target_module = module_id.root(&module_tree); | 50 | let mut target_module = module_id.root(&module_tree); |
40 | for name in crate_path { | 51 | for name in crate_path { |