aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEkaterina Babshukova <[email protected]>2019-10-11 00:11:23 +0100
committerEkaterina Babshukova <[email protected]>2019-10-22 21:47:31 +0100
commitdf8441b24e24c438d82b93931474faf7e30d17df (patch)
tree9d2a02b36e4cbec8bc0c865e244955f2856479a6
parent5b03773fbeea55d86f64e5fb69a0d0f1d6a4f7e8 (diff)
some fixes, add tests
-rw-r--r--crates/ra_ide_api/src/name_kind.rs14
-rw-r--r--crates/ra_ide_api/src/references.rs43
-rw-r--r--crates/ra_ide_api/src/search_scope.rs34
3 files changed, 52 insertions, 39 deletions
diff --git a/crates/ra_ide_api/src/name_kind.rs b/crates/ra_ide_api/src/name_kind.rs
index 8eef540f6..2f944fb04 100644
--- a/crates/ra_ide_api/src/name_kind.rs
+++ b/crates/ra_ide_api/src/name_kind.rs
@@ -6,11 +6,11 @@ use hir::{
6 SourceAnalyzer, StructField, Ty, VariantDef, 6 SourceAnalyzer, StructField, Ty, VariantDef,
7}; 7};
8use ra_db::FileId; 8use ra_db::FileId;
9use ra_syntax::{ast, ast::VisibilityOwner, AstNode, AstPtr}; 9use ra_syntax::{ast, ast::VisibilityOwner, match_ast, AstNode, AstPtr};
10 10
11use crate::db::RootDatabase; 11use crate::db::RootDatabase;
12 12
13#[derive(PartialEq, Eq)] 13#[derive(Debug, PartialEq, Eq)]
14pub enum NameKind { 14pub enum NameKind {
15 Macro(MacroDef), 15 Macro(MacroDef),
16 FieldAccess(StructField), 16 FieldAccess(StructField),
@@ -42,16 +42,6 @@ trait HasDefinition {
42 ) -> Option<Definition>; 42 ) -> Option<Definition>;
43} 43}
44 44
45macro_rules! match_ast {
46 (match $node:ident {
47 $( ast::$ast:ident($it:ident) => $res:block, )*
48 _ => $catch_all:expr,
49 }) => {{
50 $( if let Some($it) = ast::$ast::cast($node.clone()) $res else )*
51 { $catch_all }
52 }};
53}
54
55pub(crate) fn classify_name_ref( 45pub(crate) fn classify_name_ref(
56 db: &RootDatabase, 46 db: &RootDatabase,
57 file_id: FileId, 47 file_id: FileId,
diff --git a/crates/ra_ide_api/src/references.rs b/crates/ra_ide_api/src/references.rs
index 24cd7e827..e640b92cf 100644
--- a/crates/ra_ide_api/src/references.rs
+++ b/crates/ra_ide_api/src/references.rs
@@ -81,10 +81,6 @@ pub(crate) fn find_all_refs(
81 // _ => vec![], 81 // _ => vec![],
82 // }; 82 // };
83 let references = find_refs(db, def, name); 83 let references = find_refs(db, def, name);
84 let references = references
85 .into_iter()
86 .map(move |ref_desc| FileRange { file_id: position.file_id, range: ref_desc.range })
87 .collect::<Vec<_>>();
88 84
89 return Some(RangeInfo::new(range, ReferenceSearchResult { declaration, references })); 85 return Some(RangeInfo::new(range, ReferenceSearchResult { declaration, references }));
90 86
@@ -314,6 +310,45 @@ mod tests {
314 assert_eq!(refs.len(), 1); 310 assert_eq!(refs.len(), 1);
315 } 311 }
316 312
313 #[test]
314 fn test_find_all_refs_modules() {
315 let code = r#"
316 //- /lib.rs
317 pub mod foo;
318 pub mod bar;
319
320 fn f() {
321 let i = foo::Foo { n: 5 };
322 }
323
324 //- /foo.rs
325 use crate::bar;
326
327 pub struct Foo {
328 pub n: u32,
329 }
330
331 fn f() {
332 let i = bar::Bar { n: 5 };
333 }
334
335 //- /bar.rs
336 use crate::foo;
337
338 pub struct Bar {
339 pub n: u32,
340 }
341
342 fn f() {
343 let i = foo::Foo<|> { n: 5 };
344 }
345 "#;
346
347 let (analysis, pos) = analysis_and_position(code);
348 let refs = analysis.find_all_refs(pos).unwrap().unwrap();
349 assert_eq!(refs.len(), 3);
350 }
351
317 fn get_all_refs(text: &str) -> ReferenceSearchResult { 352 fn get_all_refs(text: &str) -> ReferenceSearchResult {
318 let (analysis, position) = single_file_with_position(text); 353 let (analysis, position) = single_file_with_position(text);
319 analysis.find_all_refs(position).unwrap().unwrap() 354 analysis.find_all_refs(position).unwrap().unwrap()
diff --git a/crates/ra_ide_api/src/search_scope.rs b/crates/ra_ide_api/src/search_scope.rs
index 9fcbdcc3a..1590a09c4 100644
--- a/crates/ra_ide_api/src/search_scope.rs
+++ b/crates/ra_ide_api/src/search_scope.rs
@@ -1,7 +1,5 @@
1use hir::{ 1use hir::{DefWithBody, HasSource, ModuleSource, SourceAnalyzer};
2 source_binder::ReferenceDescriptor, DefWithBody, HasSource, ModuleSource, SourceAnalyzer, 2use ra_db::{FileId, FileRange, SourceDatabase};
3};
4use ra_db::{FileId, SourceDatabase};
5use ra_syntax::{algo::find_node_at_offset, ast, AstNode, SourceFile, TextRange, TextUnit}; 3use ra_syntax::{algo::find_node_at_offset, ast, AstNode, SourceFile, TextRange, TextUnit};
6 4
7use crate::{ 5use crate::{
@@ -13,11 +11,7 @@ pub(crate) struct SearchScope {
13 pub scope: Vec<(FileId, Option<TextRange>)>, 11 pub scope: Vec<(FileId, Option<TextRange>)>,
14} 12}
15 13
16pub(crate) fn find_refs( 14pub(crate) fn find_refs(db: &RootDatabase, def: Definition, name: String) -> Vec<FileRange> {
17 db: &RootDatabase,
18 def: Definition,
19 name: String,
20) -> Vec<ReferenceDescriptor> {
21 let pat = name.as_str(); 15 let pat = name.as_str();
22 let scope = def.scope(db).scope; 16 let scope = def.scope(db).scope;
23 let mut refs = vec![]; 17 let mut refs = vec![];
@@ -40,20 +34,14 @@ pub(crate) fn find_refs(
40 for (idx, _) in text.match_indices(pat) { 34 for (idx, _) in text.match_indices(pat) {
41 let offset = TextUnit::from_usize(idx); 35 let offset = TextUnit::from_usize(idx);
42 if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(&syntax, offset) { 36 if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(&syntax, offset) {
43 let name_range = name_ref.syntax().text_range(); 37 let range = name_ref.syntax().text_range();
44 38
45 if let Some(range) = text_range { 39 if let Some(text_range) = text_range {
46 if name_range.is_subrange(&range) && is_match(file_id, &name_ref) { 40 if range.is_subrange(&text_range) && is_match(file_id, &name_ref) {
47 refs.push(ReferenceDescriptor { 41 refs.push(FileRange { file_id, range });
48 name: name_ref.text().to_string(),
49 range: name_ref.syntax().text_range(),
50 });
51 } 42 }
52 } else if is_match(file_id, &name_ref) { 43 } else if is_match(file_id, &name_ref) {
53 refs.push(ReferenceDescriptor { 44 refs.push(FileRange { file_id, range });
54 name: name_ref.text().to_string(),
55 range: name_ref.syntax().text_range(),
56 });
57 } 45 }
58 } 46 }
59 } 47 }
@@ -81,10 +69,10 @@ impl Definition {
81 let source_root = db.source_root(source_root_id); 69 let source_root = db.source_root(source_root_id);
82 let mut files = source_root.walk().map(|id| (id.into(), None)).collect::<Vec<_>>(); 70 let mut files = source_root.walk().map(|id| (id.into(), None)).collect::<Vec<_>>();
83 71
84 if vis.syntax().text() == "pub(crate)" { 72 if vis.syntax().to_string().as_str() == "pub(crate)" {
85 return SearchScope { scope: files }; 73 return SearchScope { scope: files };
86 } 74 }
87 if vis.syntax().text() == "pub" { 75 if vis.syntax().to_string().as_str() == "pub" {
88 let krate = self.container.krate(db).unwrap(); 76 let krate = self.container.krate(db).unwrap();
89 let crate_graph = db.crate_graph(); 77 let crate_graph = db.crate_graph();
90 78