mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 12:29:21 +00:00
some fixes, add tests
This commit is contained in:
parent
5b03773fbe
commit
df8441b24e
3 changed files with 51 additions and 38 deletions
|
@ -6,11 +6,11 @@ use hir::{
|
||||||
SourceAnalyzer, StructField, Ty, VariantDef,
|
SourceAnalyzer, StructField, Ty, VariantDef,
|
||||||
};
|
};
|
||||||
use ra_db::FileId;
|
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;
|
use crate::db::RootDatabase;
|
||||||
|
|
||||||
#[derive(PartialEq, Eq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
pub enum NameKind {
|
pub enum NameKind {
|
||||||
Macro(MacroDef),
|
Macro(MacroDef),
|
||||||
FieldAccess(StructField),
|
FieldAccess(StructField),
|
||||||
|
@ -42,16 +42,6 @@ trait HasDefinition {
|
||||||
) -> Option<Definition>;
|
) -> 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(
|
pub(crate) fn classify_name_ref(
|
||||||
db: &RootDatabase,
|
db: &RootDatabase,
|
||||||
file_id: FileId,
|
file_id: FileId,
|
||||||
|
|
|
@ -81,10 +81,6 @@ pub(crate) fn find_all_refs(
|
||||||
// _ => vec![],
|
// _ => vec![],
|
||||||
// };
|
// };
|
||||||
let references = find_refs(db, def, name);
|
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 }));
|
return Some(RangeInfo::new(range, ReferenceSearchResult { declaration, references }));
|
||||||
|
|
||||||
|
@ -314,6 +310,45 @@ mod tests {
|
||||||
assert_eq!(refs.len(), 1);
|
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 {
|
fn get_all_refs(text: &str) -> ReferenceSearchResult {
|
||||||
let (analysis, position) = single_file_with_position(text);
|
let (analysis, position) = single_file_with_position(text);
|
||||||
analysis.find_all_refs(position).unwrap().unwrap()
|
analysis.find_all_refs(position).unwrap().unwrap()
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
use hir::{
|
use hir::{DefWithBody, HasSource, ModuleSource, SourceAnalyzer};
|
||||||
source_binder::ReferenceDescriptor, DefWithBody, HasSource, ModuleSource, SourceAnalyzer,
|
use ra_db::{FileId, FileRange, SourceDatabase};
|
||||||
};
|
|
||||||
use ra_db::{FileId, SourceDatabase};
|
|
||||||
use ra_syntax::{algo::find_node_at_offset, ast, AstNode, SourceFile, TextRange, TextUnit};
|
use ra_syntax::{algo::find_node_at_offset, ast, AstNode, SourceFile, TextRange, TextUnit};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
@ -13,11 +11,7 @@ pub(crate) struct SearchScope {
|
||||||
pub scope: Vec<(FileId, Option<TextRange>)>,
|
pub scope: Vec<(FileId, Option<TextRange>)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn find_refs(
|
pub(crate) fn find_refs(db: &RootDatabase, def: Definition, name: String) -> Vec<FileRange> {
|
||||||
db: &RootDatabase,
|
|
||||||
def: Definition,
|
|
||||||
name: String,
|
|
||||||
) -> Vec<ReferenceDescriptor> {
|
|
||||||
let pat = name.as_str();
|
let pat = name.as_str();
|
||||||
let scope = def.scope(db).scope;
|
let scope = def.scope(db).scope;
|
||||||
let mut refs = vec![];
|
let mut refs = vec![];
|
||||||
|
@ -40,20 +34,14 @@ pub(crate) fn find_refs(
|
||||||
for (idx, _) in text.match_indices(pat) {
|
for (idx, _) in text.match_indices(pat) {
|
||||||
let offset = TextUnit::from_usize(idx);
|
let offset = TextUnit::from_usize(idx);
|
||||||
if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(&syntax, offset) {
|
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 let Some(text_range) = text_range {
|
||||||
if name_range.is_subrange(&range) && is_match(file_id, &name_ref) {
|
if range.is_subrange(&text_range) && is_match(file_id, &name_ref) {
|
||||||
refs.push(ReferenceDescriptor {
|
refs.push(FileRange { file_id, range });
|
||||||
name: name_ref.text().to_string(),
|
|
||||||
range: name_ref.syntax().text_range(),
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
} else if is_match(file_id, &name_ref) {
|
} else if is_match(file_id, &name_ref) {
|
||||||
refs.push(ReferenceDescriptor {
|
refs.push(FileRange { file_id, range });
|
||||||
name: name_ref.text().to_string(),
|
|
||||||
range: name_ref.syntax().text_range(),
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -81,10 +69,10 @@ impl Definition {
|
||||||
let source_root = db.source_root(source_root_id);
|
let source_root = db.source_root(source_root_id);
|
||||||
let mut files = source_root.walk().map(|id| (id.into(), None)).collect::<Vec<_>>();
|
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 };
|
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 krate = self.container.krate(db).unwrap();
|
||||||
let crate_graph = db.crate_graph();
|
let crate_graph = db.crate_graph();
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue