some fixes, add tests

This commit is contained in:
Ekaterina Babshukova 2019-10-11 02:11:23 +03:00
parent 5b03773fbe
commit df8441b24e
3 changed files with 51 additions and 38 deletions

View File

@ -6,11 +6,11 @@
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 @@ fn from_ref(
) -> Option<Definition>;
}
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,

View File

@ -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::<Vec<_>>();
return Some(RangeInfo::new(range, ReferenceSearchResult { declaration, references }));
@ -314,6 +310,45 @@ enum Foo {
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()

View File

@ -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<TextRange>)>,
}
pub(crate) fn find_refs(
db: &RootDatabase,
def: Definition,
name: String,
) -> Vec<ReferenceDescriptor> {
pub(crate) fn find_refs(db: &RootDatabase, def: Definition, name: String) -> Vec<FileRange> {
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::<ast::NameRef>(&syntax, offset) {
let name_range = name_ref.syntax().text_range();
let 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(),
});
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 @@ pub fn scope(&self, db: &RootDatabase) -> SearchScope {
let source_root = db.source_root(source_root_id);
let mut files = source_root.walk().map(|id| (id.into(), None)).collect::<Vec<_>>();
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();