rust/crates/ra_ide_api/src/references.rs

305 lines
8.1 KiB
Rust
Raw Normal View History

//! FIXME: write short doc here
2019-10-12 10:47:17 -05:00
mod classify;
2019-10-12 12:30:53 -05:00
mod name_definition;
2019-10-12 10:47:17 -05:00
mod rename;
mod search_scope;
2019-10-14 06:59:02 -05:00
use once_cell::unsync::Lazy;
use ra_db::{SourceDatabase, SourceDatabaseExt};
2019-10-12 10:47:17 -05:00
use ra_syntax::{algo::find_node_at_offset, ast, AstNode, SourceFile, SyntaxNode, TextUnit};
use crate::{db::RootDatabase, FileId, FilePosition, FileRange, NavigationTarget, RangeInfo};
pub(crate) use self::{
classify::{classify_name, classify_name_ref},
2019-10-12 12:30:53 -05:00
name_definition::{NameDefinition, NameKind},
2019-10-12 10:47:17 -05:00
rename::rename,
2019-01-16 07:39:01 -06:00
};
2019-02-08 05:06:26 -06:00
#[derive(Debug, Clone)]
pub struct ReferenceSearchResult {
declaration: NavigationTarget,
references: Vec<FileRange>,
}
impl ReferenceSearchResult {
pub fn declaration(&self) -> &NavigationTarget {
&self.declaration
}
pub fn references(&self) -> &[FileRange] {
&self.references
}
/// Total number of references
/// At least 1 since all valid references should
/// Have a declaration
pub fn len(&self) -> usize {
self.references.len() + 1
}
}
// allow turning ReferenceSearchResult into an iterator
// over FileRanges
impl IntoIterator for ReferenceSearchResult {
type Item = FileRange;
2019-02-17 09:25:19 -06:00
type IntoIter = std::vec::IntoIter<FileRange>;
fn into_iter(mut self) -> Self::IntoIter {
let mut v = Vec::with_capacity(self.len());
v.push(FileRange { file_id: self.declaration.file_id(), range: self.declaration.range() });
v.append(&mut self.references);
v.into_iter()
}
}
pub(crate) fn find_all_refs(
db: &RootDatabase,
position: FilePosition,
2019-09-05 13:36:40 -05:00
) -> Option<RangeInfo<ReferenceSearchResult>> {
let parse = db.parse(position.file_id);
2019-09-14 06:38:10 -05:00
let syntax = parse.tree().syntax().clone();
2019-10-10 10:51:51 -05:00
let RangeInfo { range, info: (name, def) } = find_name(db, &syntax, position)?;
2019-09-14 06:38:10 -05:00
2019-10-14 06:59:02 -05:00
let declaration = match def.kind {
2019-10-12 10:47:17 -05:00
NameKind::Macro(mac) => NavigationTarget::from_macro_def(db, mac),
2019-10-12 12:30:53 -05:00
NameKind::Field(field) => NavigationTarget::from_field(db, field),
2019-10-12 10:47:17 -05:00
NameKind::AssocItem(assoc) => NavigationTarget::from_assoc_item(db, assoc),
NameKind::Def(def) => NavigationTarget::from_def(db, def)?,
NameKind::SelfType(ref ty) => match ty.as_adt() {
2019-09-14 06:38:10 -05:00
Some((def_id, _)) => NavigationTarget::from_adt_def(db, def_id),
None => return None,
},
2019-10-12 10:47:17 -05:00
NameKind::Pat((_, pat)) => NavigationTarget::from_pat(db, position.file_id, pat),
NameKind::SelfParam(par) => NavigationTarget::from_self_param(position.file_id, par),
NameKind::GenericParam(_) => return None,
2019-09-14 06:38:10 -05:00
};
2019-02-08 05:06:26 -06:00
2019-10-12 10:47:17 -05:00
let references = process_definition(db, def, name);
2019-09-14 06:38:10 -05:00
2019-10-12 10:47:17 -05:00
Some(RangeInfo::new(range, ReferenceSearchResult { declaration, references }))
2019-01-16 07:39:01 -06:00
}
2019-10-12 10:47:17 -05:00
fn find_name<'a>(
db: &RootDatabase,
2019-01-16 07:39:01 -06:00
syntax: &SyntaxNode,
position: FilePosition,
2019-10-12 12:30:53 -05:00
) -> Option<RangeInfo<(String, NameDefinition)>> {
2019-10-12 10:47:17 -05:00
if let Some(name) = find_node_at_offset::<ast::Name>(&syntax, position.offset) {
let def = classify_name(db, position.file_id, &name)?;
let range = name.syntax().text_range();
return Some(RangeInfo::new(range, (name.text().to_string(), def)));
}
2019-10-12 10:47:17 -05:00
let name_ref = find_node_at_offset::<ast::NameRef>(&syntax, position.offset)?;
let def = classify_name_ref(db, position.file_id, &name_ref)?;
let range = name_ref.syntax().text_range();
Some(RangeInfo::new(range, (name_ref.text().to_string(), def)))
}
2019-10-12 12:30:53 -05:00
fn process_definition(db: &RootDatabase, def: NameDefinition, name: String) -> Vec<FileRange> {
2019-10-12 10:47:17 -05:00
let pat = name.as_str();
2019-10-14 07:49:32 -05:00
let scope = def.search_scope(db);
2019-10-12 10:47:17 -05:00
let mut refs = vec![];
let is_match = |file_id: FileId, name_ref: &ast::NameRef| -> bool {
let classified = classify_name_ref(db, file_id, &name_ref);
if let Some(d) = classified {
d == def
} else {
false
2019-01-16 07:39:01 -06:00
}
};
2019-10-12 10:47:17 -05:00
for (file_id, text_range) in scope {
let text = db.file_text(file_id);
2019-10-14 06:59:02 -05:00
let parse = Lazy::new(|| SourceFile::parse(&text));
2019-10-12 10:47:17 -05:00
for (idx, _) in text.match_indices(pat) {
let offset = TextUnit::from_usize(idx);
2019-10-14 06:59:02 -05:00
if let Some(name_ref) =
find_node_at_offset::<ast::NameRef>(parse.tree().syntax(), offset)
{
2019-10-12 10:47:17 -05:00
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(FileRange { file_id, range });
}
}
}
2019-01-16 07:39:01 -06:00
}
2019-10-12 10:47:17 -05:00
return refs;
2019-01-16 07:39:01 -06:00
}
2019-01-18 02:29:09 -06:00
#[cfg(test)]
mod tests {
use crate::{
2019-10-12 10:47:17 -05:00
mock_analysis::analysis_and_position, mock_analysis::single_file_with_position,
ReferenceSearchResult,
};
2019-01-18 02:29:09 -06:00
2019-03-25 15:03:32 -05:00
#[test]
fn test_find_all_refs_for_local() {
let code = r#"
fn main() {
let mut i = 1;
let j = 1;
i = i<|> + j;
{
i = 0;
}
i = 5;
}"#;
let refs = get_all_refs(code);
assert_eq!(refs.len(), 5);
}
#[test]
fn test_find_all_refs_for_param_inside() {
let code = r#"
fn foo(i : u32) -> u32 {
i<|>
}"#;
let refs = get_all_refs(code);
assert_eq!(refs.len(), 2);
}
#[test]
fn test_find_all_refs_for_fn_param() {
let code = r#"
fn foo(i<|> : u32) -> u32 {
i
}"#;
let refs = get_all_refs(code);
assert_eq!(refs.len(), 2);
}
2019-09-14 06:38:10 -05:00
#[test]
fn test_find_all_refs_field_name() {
let code = r#"
//- /lib.rs
struct Foo {
2019-10-10 10:51:51 -05:00
pub spam<|>: u32,
}
fn main(s: Foo) {
let f = s.spam;
2019-09-14 06:38:10 -05:00
}
"#;
let refs = get_all_refs(code);
2019-10-10 10:51:51 -05:00
assert_eq!(refs.len(), 2);
2019-09-14 06:38:10 -05:00
}
#[test]
fn test_find_all_refs_impl_item_name() {
let code = r#"
//- /lib.rs
struct Foo;
impl Foo {
fn f<|>(&self) { }
}
"#;
let refs = get_all_refs(code);
assert_eq!(refs.len(), 1);
}
#[test]
fn test_find_all_refs_enum_var_name() {
let code = r#"
//- /lib.rs
enum Foo {
A,
B<|>,
C,
}
"#;
let refs = get_all_refs(code);
assert_eq!(refs.len(), 1);
}
2019-10-10 18:11:23 -05:00
#[test]
2019-10-15 14:50:28 -05:00
fn test_find_all_refs_two_modules() {
2019-10-10 18:11:23 -05:00
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);
}
2019-10-15 14:50:28 -05:00
// `mod foo;` is not in the results because `foo` is an `ast::Name`.
// So, there are two references: the first one is a definition of the `foo` module,
// which is the whole `foo.rs`, and the second one is in `use foo::Foo`.
#[test]
fn test_find_all_refs_decl_module() {
let code = r#"
//- /lib.rs
mod foo<|>;
use foo::Foo;
fn f() {
let i = Foo { n: 5 };
}
//- /foo.rs
pub struct Foo {
pub n: u32,
}
"#;
let (analysis, pos) = analysis_and_position(code);
let refs = analysis.find_all_refs(pos).unwrap().unwrap();
assert_eq!(refs.len(), 2);
}
2019-03-25 15:03:32 -05:00
fn get_all_refs(text: &str) -> ReferenceSearchResult {
let (analysis, position) = single_file_with_position(text);
analysis.find_all_refs(position).unwrap().unwrap()
}
2019-01-18 02:29:09 -06:00
}