From df8441b24e24c438d82b93931474faf7e30d17df Mon Sep 17 00:00:00 2001 From: Ekaterina Babshukova Date: Fri, 11 Oct 2019 02:11:23 +0300 Subject: some fixes, add tests --- crates/ra_ide_api/src/name_kind.rs | 14 ++---------- crates/ra_ide_api/src/references.rs | 43 +++++++++++++++++++++++++++++++---- crates/ra_ide_api/src/search_scope.rs | 34 +++++++++------------------ 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::{ SourceAnalyzer, StructField, Ty, VariantDef, }; use ra_db::FileId; -use ra_syntax::{ast, ast::VisibilityOwner, AstNode, AstPtr}; +use ra_syntax::{ast, ast::VisibilityOwner, match_ast, AstNode, AstPtr}; use crate::db::RootDatabase; -#[derive(PartialEq, Eq)] +#[derive(Debug, PartialEq, Eq)] pub enum NameKind { Macro(MacroDef), FieldAccess(StructField), @@ -42,16 +42,6 @@ trait HasDefinition { ) -> Option; } -macro_rules! match_ast { - (match $node:ident { - $( ast::$ast:ident($it:ident) => $res:block, )* - _ => $catch_all:expr, - }) => {{ - $( if let Some($it) = ast::$ast::cast($node.clone()) $res else )* - { $catch_all } - }}; -} - pub(crate) fn classify_name_ref( db: &RootDatabase, 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( // _ => vec![], // }; let references = find_refs(db, def, name); - let references = references - .into_iter() - .map(move |ref_desc| FileRange { file_id: position.file_id, range: ref_desc.range }) - .collect::>(); return Some(RangeInfo::new(range, ReferenceSearchResult { declaration, references })); @@ -314,6 +310,45 @@ mod tests { assert_eq!(refs.len(), 1); } + #[test] + fn test_find_all_refs_modules() { + let code = r#" + //- /lib.rs + pub mod foo; + pub mod bar; + + fn f() { + let i = foo::Foo { n: 5 }; + } + + //- /foo.rs + use crate::bar; + + pub struct Foo { + pub n: u32, + } + + fn f() { + let i = bar::Bar { n: 5 }; + } + + //- /bar.rs + use crate::foo; + + pub struct Bar { + pub n: u32, + } + + fn f() { + let i = foo::Foo<|> { n: 5 }; + } + "#; + + let (analysis, pos) = analysis_and_position(code); + let refs = analysis.find_all_refs(pos).unwrap().unwrap(); + assert_eq!(refs.len(), 3); + } + fn get_all_refs(text: &str) -> ReferenceSearchResult { let (analysis, position) = single_file_with_position(text); 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 @@ -use hir::{ - source_binder::ReferenceDescriptor, DefWithBody, HasSource, ModuleSource, SourceAnalyzer, -}; -use ra_db::{FileId, SourceDatabase}; +use hir::{DefWithBody, HasSource, ModuleSource, SourceAnalyzer}; +use ra_db::{FileId, FileRange, SourceDatabase}; use ra_syntax::{algo::find_node_at_offset, ast, AstNode, SourceFile, TextRange, TextUnit}; use crate::{ @@ -13,11 +11,7 @@ pub(crate) struct SearchScope { pub scope: Vec<(FileId, Option)>, } -pub(crate) fn find_refs( - db: &RootDatabase, - def: Definition, - name: String, -) -> Vec { +pub(crate) fn find_refs(db: &RootDatabase, def: Definition, name: String) -> Vec { let pat = name.as_str(); let scope = def.scope(db).scope; let mut refs = vec![]; @@ -40,20 +34,14 @@ pub(crate) fn find_refs( for (idx, _) in text.match_indices(pat) { let offset = TextUnit::from_usize(idx); if let Some(name_ref) = find_node_at_offset::(&syntax, offset) { - let name_range = name_ref.syntax().text_range(); - - if let Some(range) = text_range { - if name_range.is_subrange(&range) && is_match(file_id, &name_ref) { - refs.push(ReferenceDescriptor { - name: name_ref.text().to_string(), - range: name_ref.syntax().text_range(), - }); + let range = name_ref.syntax().text_range(); + + if let Some(text_range) = text_range { + if range.is_subrange(&text_range) && is_match(file_id, &name_ref) { + refs.push(FileRange { file_id, range }); } } else if is_match(file_id, &name_ref) { - refs.push(ReferenceDescriptor { - name: name_ref.text().to_string(), - range: name_ref.syntax().text_range(), - }); + refs.push(FileRange { file_id, range }); } } } @@ -81,10 +69,10 @@ impl Definition { let source_root = db.source_root(source_root_id); let mut files = source_root.walk().map(|id| (id.into(), None)).collect::>(); - if vis.syntax().text() == "pub(crate)" { + if vis.syntax().to_string().as_str() == "pub(crate)" { return SearchScope { scope: files }; } - if vis.syntax().text() == "pub" { + if vis.syntax().to_string().as_str() == "pub" { let krate = self.container.krate(db).unwrap(); let crate_graph = db.crate_graph(); -- cgit v1.2.3