diff options
author | Ekaterina Babshukova <[email protected]> | 2019-10-15 20:50:28 +0100 |
---|---|---|
committer | Ekaterina Babshukova <[email protected]> | 2019-10-22 21:47:31 +0100 |
commit | 55e1910d006da7961687928542c1167cc556a39f (patch) | |
tree | 8440f4a2655ab06884d20180da10134f652d403b /crates/ra_ide_api/src | |
parent | 93c179531b31786bfd50644b5f0c879afc798f7d (diff) |
classify module from declaration
Diffstat (limited to 'crates/ra_ide_api/src')
-rw-r--r-- | crates/ra_ide_api/src/references.rs | 29 | ||||
-rw-r--r-- | crates/ra_ide_api/src/references/classify.rs | 13 | ||||
-rw-r--r-- | crates/ra_ide_api/src/references/search_scope.rs | 10 |
3 files changed, 44 insertions, 8 deletions
diff --git a/crates/ra_ide_api/src/references.rs b/crates/ra_ide_api/src/references.rs index 0f1ac57fc..3d647d2cb 100644 --- a/crates/ra_ide_api/src/references.rs +++ b/crates/ra_ide_api/src/references.rs | |||
@@ -232,7 +232,7 @@ mod tests { | |||
232 | } | 232 | } |
233 | 233 | ||
234 | #[test] | 234 | #[test] |
235 | fn test_find_all_refs_modules() { | 235 | fn test_find_all_refs_two_modules() { |
236 | let code = r#" | 236 | let code = r#" |
237 | //- /lib.rs | 237 | //- /lib.rs |
238 | pub mod foo; | 238 | pub mod foo; |
@@ -270,6 +270,33 @@ mod tests { | |||
270 | assert_eq!(refs.len(), 3); | 270 | assert_eq!(refs.len(), 3); |
271 | } | 271 | } |
272 | 272 | ||
273 | |||
274 | // `mod foo;` is not in the results because `foo` is an `ast::Name`. | ||
275 | // So, there are two references: the first one is a definition of the `foo` module, | ||
276 | // which is the whole `foo.rs`, and the second one is in `use foo::Foo`. | ||
277 | #[test] | ||
278 | fn test_find_all_refs_decl_module() { | ||
279 | let code = r#" | ||
280 | //- /lib.rs | ||
281 | mod foo<|>; | ||
282 | |||
283 | use foo::Foo; | ||
284 | |||
285 | fn f() { | ||
286 | let i = Foo { n: 5 }; | ||
287 | } | ||
288 | |||
289 | //- /foo.rs | ||
290 | pub struct Foo { | ||
291 | pub n: u32, | ||
292 | } | ||
293 | "#; | ||
294 | |||
295 | let (analysis, pos) = analysis_and_position(code); | ||
296 | let refs = analysis.find_all_refs(pos).unwrap().unwrap(); | ||
297 | assert_eq!(refs.len(), 2); | ||
298 | } | ||
299 | |||
273 | fn get_all_refs(text: &str) -> ReferenceSearchResult { | 300 | fn get_all_refs(text: &str) -> ReferenceSearchResult { |
274 | let (analysis, position) = single_file_with_position(text); | 301 | let (analysis, position) = single_file_with_position(text); |
275 | analysis.find_all_refs(position).unwrap().unwrap() | 302 | analysis.find_all_refs(position).unwrap().unwrap() |
diff --git a/crates/ra_ide_api/src/references/classify.rs b/crates/ra_ide_api/src/references/classify.rs index 93e079ccc..ac9cf34eb 100644 --- a/crates/ra_ide_api/src/references/classify.rs +++ b/crates/ra_ide_api/src/references/classify.rs | |||
@@ -31,9 +31,16 @@ pub(crate) fn classify_name( | |||
31 | Some(from_struct_field(db, field)) | 31 | Some(from_struct_field(db, field)) |
32 | }, | 32 | }, |
33 | ast::Module(it) => { | 33 | ast::Module(it) => { |
34 | let ast = hir::ModuleSource::Module(it); | 34 | let def = { |
35 | let src = hir::Source { file_id, ast }; | 35 | if !it.has_semi() { |
36 | let def = hir::Module::from_definition(db, src)?; | 36 | let ast = hir::ModuleSource::Module(it); |
37 | let src = hir::Source { file_id, ast }; | ||
38 | hir::Module::from_definition(db, src) | ||
39 | } else { | ||
40 | let src = hir::Source { file_id, ast: it }; | ||
41 | hir::Module::from_declaration(db, src) | ||
42 | } | ||
43 | }?; | ||
37 | Some(from_module_def(db, def.into(), None)) | 44 | Some(from_module_def(db, def.into(), None)) |
38 | }, | 45 | }, |
39 | ast::StructDef(it) => { | 46 | ast::StructDef(it) => { |
diff --git a/crates/ra_ide_api/src/references/search_scope.rs b/crates/ra_ide_api/src/references/search_scope.rs index 680988a21..d2c966b4f 100644 --- a/crates/ra_ide_api/src/references/search_scope.rs +++ b/crates/ra_ide_api/src/references/search_scope.rs | |||
@@ -1,5 +1,7 @@ | |||
1 | //! FIXME: write short doc here | 1 | //! FIXME: write short doc here |
2 | 2 | ||
3 | use std::collections::HashSet; | ||
4 | |||
3 | use hir::{DefWithBody, HasSource, ModuleSource}; | 5 | use hir::{DefWithBody, HasSource, ModuleSource}; |
4 | use ra_db::{FileId, SourceDatabase, SourceDatabaseExt}; | 6 | use ra_db::{FileId, SourceDatabase, SourceDatabaseExt}; |
5 | use ra_syntax::{AstNode, TextRange}; | 7 | use ra_syntax::{AstNode, TextRange}; |
@@ -9,7 +11,7 @@ use crate::db::RootDatabase; | |||
9 | use super::{NameDefinition, NameKind}; | 11 | use super::{NameDefinition, NameKind}; |
10 | 12 | ||
11 | impl NameDefinition { | 13 | impl NameDefinition { |
12 | pub(crate) fn search_scope(&self, db: &RootDatabase) -> Vec<(FileId, Option<TextRange>)> { | 14 | pub(crate) fn search_scope(&self, db: &RootDatabase) -> HashSet<(FileId, Option<TextRange>)> { |
13 | let module_src = self.container.definition_source(db); | 15 | let module_src = self.container.definition_source(db); |
14 | let file_id = module_src.file_id.original_file(db); | 16 | let file_id = module_src.file_id.original_file(db); |
15 | 17 | ||
@@ -19,13 +21,13 @@ impl NameDefinition { | |||
19 | DefWithBody::Const(c) => c.source(db).ast.syntax().text_range(), | 21 | DefWithBody::Const(c) => c.source(db).ast.syntax().text_range(), |
20 | DefWithBody::Static(s) => s.source(db).ast.syntax().text_range(), | 22 | DefWithBody::Static(s) => s.source(db).ast.syntax().text_range(), |
21 | }; | 23 | }; |
22 | return vec![(file_id, Some(range))]; | 24 | return [(file_id, Some(range))].iter().cloned().collect(); |
23 | } | 25 | } |
24 | 26 | ||
25 | if let Some(ref vis) = self.visibility { | 27 | if let Some(ref vis) = self.visibility { |
26 | let source_root_id = db.file_source_root(file_id); | 28 | let source_root_id = db.file_source_root(file_id); |
27 | let source_root = db.source_root(source_root_id); | 29 | let source_root = db.source_root(source_root_id); |
28 | let mut files = source_root.walk().map(|id| (id.into(), None)).collect::<Vec<_>>(); | 30 | let mut files = source_root.walk().map(|id| (id.into(), None)).collect::<HashSet<_>>(); |
29 | 31 | ||
30 | if vis.syntax().to_string().as_str() == "pub(crate)" { | 32 | if vis.syntax().to_string().as_str() == "pub(crate)" { |
31 | return files; | 33 | return files; |
@@ -54,6 +56,6 @@ impl NameDefinition { | |||
54 | ModuleSource::Module(m) => Some(m.syntax().text_range()), | 56 | ModuleSource::Module(m) => Some(m.syntax().text_range()), |
55 | ModuleSource::SourceFile(_) => None, | 57 | ModuleSource::SourceFile(_) => None, |
56 | }; | 58 | }; |
57 | vec![(file_id, range)] | 59 | [(file_id, range)].iter().cloned().collect() |
58 | } | 60 | } |
59 | } | 61 | } |