mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-04 19:29:07 +00:00
implement search of references
This commit is contained in:
parent
01853e8d6c
commit
5b03773fbe
@ -10,6 +10,7 @@ use ra_syntax::{ast, ast::VisibilityOwner, AstNode, AstPtr};
|
|||||||
|
|
||||||
use crate::db::RootDatabase;
|
use crate::db::RootDatabase;
|
||||||
|
|
||||||
|
#[derive(PartialEq, Eq)]
|
||||||
pub enum NameKind {
|
pub enum NameKind {
|
||||||
Macro(MacroDef),
|
Macro(MacroDef),
|
||||||
FieldAccess(StructField),
|
FieldAccess(StructField),
|
||||||
@ -21,23 +22,24 @@ pub enum NameKind {
|
|||||||
GenericParam(u32),
|
GenericParam(u32),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) struct Declaration {
|
#[derive(PartialEq, Eq)]
|
||||||
|
pub(crate) struct Definition {
|
||||||
pub visibility: Option<ast::Visibility>,
|
pub visibility: Option<ast::Visibility>,
|
||||||
pub container: Module,
|
pub container: Module,
|
||||||
pub item: NameKind,
|
pub item: NameKind,
|
||||||
}
|
}
|
||||||
|
|
||||||
trait HasDeclaration {
|
trait HasDefinition {
|
||||||
type Def;
|
type Def;
|
||||||
type Ref;
|
type Ref;
|
||||||
|
|
||||||
fn declaration(self, db: &RootDatabase) -> Declaration;
|
fn definition(self, db: &RootDatabase) -> Definition;
|
||||||
fn from_def(db: &RootDatabase, file_id: HirFileId, def: Self::Def) -> Option<Declaration>;
|
fn from_def(db: &RootDatabase, file_id: HirFileId, def: Self::Def) -> Option<Definition>;
|
||||||
fn from_ref(
|
fn from_ref(
|
||||||
db: &RootDatabase,
|
db: &RootDatabase,
|
||||||
analyzer: &SourceAnalyzer,
|
analyzer: &SourceAnalyzer,
|
||||||
refer: Self::Ref,
|
refer: Self::Ref,
|
||||||
) -> Option<Declaration>;
|
) -> Option<Definition>;
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! match_ast {
|
macro_rules! match_ast {
|
||||||
@ -55,7 +57,7 @@ pub(crate) fn classify_name_ref(
|
|||||||
file_id: FileId,
|
file_id: FileId,
|
||||||
analyzer: &SourceAnalyzer,
|
analyzer: &SourceAnalyzer,
|
||||||
name_ref: &ast::NameRef,
|
name_ref: &ast::NameRef,
|
||||||
) -> Option<Declaration> {
|
) -> Option<Definition> {
|
||||||
let parent = name_ref.syntax().parent()?;
|
let parent = name_ref.syntax().parent()?;
|
||||||
match_ast! {
|
match_ast! {
|
||||||
match parent {
|
match parent {
|
||||||
@ -64,7 +66,7 @@ pub(crate) fn classify_name_ref(
|
|||||||
},
|
},
|
||||||
ast::FieldExpr(it) => {
|
ast::FieldExpr(it) => {
|
||||||
if let Some(field) = analyzer.resolve_field(&it) {
|
if let Some(field) = analyzer.resolve_field(&it) {
|
||||||
return Some(field.declaration(db));
|
return Some(field.definition(db));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
ast::RecordField(it) => {
|
ast::RecordField(it) => {
|
||||||
@ -73,7 +75,7 @@ pub(crate) fn classify_name_ref(
|
|||||||
let hir_path = Path::from_name_ref(name_ref);
|
let hir_path = Path::from_name_ref(name_ref);
|
||||||
let hir_name = hir_path.as_ident()?;
|
let hir_name = hir_path.as_ident()?;
|
||||||
let field = variant_def.field(db, hir_name)?;
|
let field = variant_def.field(db, hir_name)?;
|
||||||
return Some(field.declaration(db));
|
return Some(field.definition(db));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
_ => (),
|
_ => (),
|
||||||
@ -83,12 +85,13 @@ pub(crate) fn classify_name_ref(
|
|||||||
let ast = ModuleSource::from_child_node(db, file_id, &parent);
|
let ast = ModuleSource::from_child_node(db, file_id, &parent);
|
||||||
let file_id = file_id.into();
|
let file_id = file_id.into();
|
||||||
let container = Module::from_definition(db, Source { file_id, ast })?;
|
let container = Module::from_definition(db, Source { file_id, ast })?;
|
||||||
|
let visibility = None;
|
||||||
|
|
||||||
if let Some(macro_call) =
|
if let Some(macro_call) =
|
||||||
parent.parent().and_then(|node| node.parent()).and_then(ast::MacroCall::cast)
|
parent.parent().and_then(|node| node.parent()).and_then(ast::MacroCall::cast)
|
||||||
{
|
{
|
||||||
if let Some(mac) = analyzer.resolve_macro_call(db, ¯o_call) {
|
if let Some(mac) = analyzer.resolve_macro_call(db, ¯o_call) {
|
||||||
return Some(Declaration { item: NameKind::Macro(mac), container, visibility: None });
|
return Some(Definition { item: NameKind::Macro(mac), container, visibility });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -96,24 +99,24 @@ pub(crate) fn classify_name_ref(
|
|||||||
let path = name_ref.syntax().ancestors().find_map(ast::Path::cast)?;
|
let path = name_ref.syntax().ancestors().find_map(ast::Path::cast)?;
|
||||||
let resolved = analyzer.resolve_path(db, &path)?;
|
let resolved = analyzer.resolve_path(db, &path)?;
|
||||||
match resolved {
|
match resolved {
|
||||||
PathResolution::Def(def) => Some(def.declaration(db)),
|
PathResolution::Def(def) => Some(def.definition(db)),
|
||||||
PathResolution::LocalBinding(Either::A(pat)) => decl_from_pat(db, file_id, pat),
|
PathResolution::LocalBinding(Either::A(pat)) => decl_from_pat(db, file_id, pat),
|
||||||
PathResolution::LocalBinding(Either::B(par)) => {
|
PathResolution::LocalBinding(Either::B(par)) => {
|
||||||
Some(Declaration { item: NameKind::SelfParam(par), container, visibility: None })
|
Some(Definition { item: NameKind::SelfParam(par), container, visibility })
|
||||||
}
|
}
|
||||||
PathResolution::GenericParam(par) => {
|
PathResolution::GenericParam(par) => {
|
||||||
// FIXME: get generic param def
|
// FIXME: get generic param def
|
||||||
Some(Declaration { item: NameKind::GenericParam(par), container, visibility: None })
|
Some(Definition { item: NameKind::GenericParam(par), container, visibility })
|
||||||
}
|
}
|
||||||
PathResolution::Macro(def) => {
|
PathResolution::Macro(def) => {
|
||||||
Some(Declaration { item: NameKind::Macro(def), container, visibility: None })
|
Some(Definition { item: NameKind::Macro(def), container, visibility })
|
||||||
}
|
}
|
||||||
PathResolution::SelfType(impl_block) => {
|
PathResolution::SelfType(impl_block) => {
|
||||||
let ty = impl_block.target_ty(db);
|
let ty = impl_block.target_ty(db);
|
||||||
let container = impl_block.module();
|
let container = impl_block.module();
|
||||||
Some(Declaration { item: NameKind::SelfType(ty), container, visibility: None })
|
Some(Definition { item: NameKind::SelfType(ty), container, visibility })
|
||||||
}
|
}
|
||||||
PathResolution::AssocItem(assoc) => Some(assoc.declaration(db)),
|
PathResolution::AssocItem(assoc) => Some(assoc.definition(db)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -121,7 +124,7 @@ pub(crate) fn classify_name(
|
|||||||
db: &RootDatabase,
|
db: &RootDatabase,
|
||||||
file_id: FileId,
|
file_id: FileId,
|
||||||
name: &ast::Name,
|
name: &ast::Name,
|
||||||
) -> Option<Declaration> {
|
) -> Option<Definition> {
|
||||||
let parent = name.syntax().parent()?;
|
let parent = name.syntax().parent()?;
|
||||||
let file_id = file_id.into();
|
let file_id = file_id.into();
|
||||||
|
|
||||||
@ -145,7 +148,7 @@ pub(crate) fn classify_name(
|
|||||||
ast::EnumVariant(it) => {
|
ast::EnumVariant(it) => {
|
||||||
let src = hir::Source { file_id, ast: it.clone() };
|
let src = hir::Source { file_id, ast: it.clone() };
|
||||||
let def: ModuleDef = EnumVariant::from_source(db, src)?.into();
|
let def: ModuleDef = EnumVariant::from_source(db, src)?.into();
|
||||||
Some(def.declaration(db))
|
Some(def.definition(db))
|
||||||
},
|
},
|
||||||
ast::ModuleItem(it) => {
|
ast::ModuleItem(it) => {
|
||||||
ModuleDef::from_def(db, file_id, it)
|
ModuleDef::from_def(db, file_id, it)
|
||||||
@ -159,7 +162,7 @@ fn decl_from_pat(
|
|||||||
db: &RootDatabase,
|
db: &RootDatabase,
|
||||||
file_id: HirFileId,
|
file_id: HirFileId,
|
||||||
pat: AstPtr<ast::BindPat>,
|
pat: AstPtr<ast::BindPat>,
|
||||||
) -> Option<Declaration> {
|
) -> Option<Definition> {
|
||||||
let root = db.parse_or_expand(file_id)?;
|
let root = db.parse_or_expand(file_id)?;
|
||||||
// FIXME: use match_ast!
|
// FIXME: use match_ast!
|
||||||
let def = pat.to_node(&root).syntax().ancestors().find_map(|node| {
|
let def = pat.to_node(&root).syntax().ancestors().find_map(|node| {
|
||||||
@ -178,14 +181,14 @@ fn decl_from_pat(
|
|||||||
})?;
|
})?;
|
||||||
let item = NameKind::Pat((def, pat));
|
let item = NameKind::Pat((def, pat));
|
||||||
let container = def.module(db);
|
let container = def.module(db);
|
||||||
Some(Declaration { item, container, visibility: None })
|
Some(Definition { item, container, visibility: None })
|
||||||
}
|
}
|
||||||
|
|
||||||
impl HasDeclaration for StructField {
|
impl HasDefinition for StructField {
|
||||||
type Def = ast::RecordFieldDef;
|
type Def = ast::RecordFieldDef;
|
||||||
type Ref = ast::FieldExpr;
|
type Ref = ast::FieldExpr;
|
||||||
|
|
||||||
fn declaration(self, db: &RootDatabase) -> Declaration {
|
fn definition(self, db: &RootDatabase) -> Definition {
|
||||||
let item = NameKind::FieldAccess(self);
|
let item = NameKind::FieldAccess(self);
|
||||||
let parent = self.parent_def(db);
|
let parent = self.parent_def(db);
|
||||||
let container = parent.module(db);
|
let container = parent.module(db);
|
||||||
@ -193,30 +196,30 @@ impl HasDeclaration for StructField {
|
|||||||
VariantDef::Struct(s) => s.source(db).ast.visibility(),
|
VariantDef::Struct(s) => s.source(db).ast.visibility(),
|
||||||
VariantDef::EnumVariant(e) => e.source(db).ast.parent_enum().visibility(),
|
VariantDef::EnumVariant(e) => e.source(db).ast.parent_enum().visibility(),
|
||||||
};
|
};
|
||||||
Declaration { item, container, visibility }
|
Definition { item, container, visibility }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_def(db: &RootDatabase, file_id: HirFileId, def: Self::Def) -> Option<Declaration> {
|
fn from_def(db: &RootDatabase, file_id: HirFileId, def: Self::Def) -> Option<Definition> {
|
||||||
let src = hir::Source { file_id, ast: hir::FieldSource::Named(def) };
|
let src = hir::Source { file_id, ast: hir::FieldSource::Named(def) };
|
||||||
let field = StructField::from_source(db, src)?;
|
let field = StructField::from_source(db, src)?;
|
||||||
Some(field.declaration(db))
|
Some(field.definition(db))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_ref(
|
fn from_ref(
|
||||||
db: &RootDatabase,
|
db: &RootDatabase,
|
||||||
analyzer: &SourceAnalyzer,
|
analyzer: &SourceAnalyzer,
|
||||||
refer: Self::Ref,
|
refer: Self::Ref,
|
||||||
) -> Option<Declaration> {
|
) -> Option<Definition> {
|
||||||
let field = analyzer.resolve_field(&refer)?;
|
let field = analyzer.resolve_field(&refer)?;
|
||||||
Some(field.declaration(db))
|
Some(field.definition(db))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl HasDeclaration for AssocItem {
|
impl HasDefinition for AssocItem {
|
||||||
type Def = ast::ImplItem;
|
type Def = ast::ImplItem;
|
||||||
type Ref = ast::MethodCallExpr;
|
type Ref = ast::MethodCallExpr;
|
||||||
|
|
||||||
fn declaration(self, db: &RootDatabase) -> Declaration {
|
fn definition(self, db: &RootDatabase) -> Definition {
|
||||||
let item = NameKind::AssocItem(self);
|
let item = NameKind::AssocItem(self);
|
||||||
let container = self.module(db);
|
let container = self.module(db);
|
||||||
let visibility = match self {
|
let visibility = match self {
|
||||||
@ -224,30 +227,30 @@ impl HasDeclaration for AssocItem {
|
|||||||
AssocItem::Const(c) => c.source(db).ast.visibility(),
|
AssocItem::Const(c) => c.source(db).ast.visibility(),
|
||||||
AssocItem::TypeAlias(a) => a.source(db).ast.visibility(),
|
AssocItem::TypeAlias(a) => a.source(db).ast.visibility(),
|
||||||
};
|
};
|
||||||
Declaration { item, container, visibility }
|
Definition { item, container, visibility }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_def(db: &RootDatabase, file_id: HirFileId, def: Self::Def) -> Option<Declaration> {
|
fn from_def(db: &RootDatabase, file_id: HirFileId, def: Self::Def) -> Option<Definition> {
|
||||||
let src = hir::Source { file_id, ast: def };
|
let src = hir::Source { file_id, ast: def };
|
||||||
let item = AssocItem::from_source(db, src)?;
|
let item = AssocItem::from_source(db, src)?;
|
||||||
Some(item.declaration(db))
|
Some(item.definition(db))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_ref(
|
fn from_ref(
|
||||||
db: &RootDatabase,
|
db: &RootDatabase,
|
||||||
analyzer: &SourceAnalyzer,
|
analyzer: &SourceAnalyzer,
|
||||||
refer: Self::Ref,
|
refer: Self::Ref,
|
||||||
) -> Option<Declaration> {
|
) -> Option<Definition> {
|
||||||
let func: AssocItem = analyzer.resolve_method_call(&refer)?.into();
|
let func: AssocItem = analyzer.resolve_method_call(&refer)?.into();
|
||||||
Some(func.declaration(db))
|
Some(func.definition(db))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl HasDeclaration for ModuleDef {
|
impl HasDefinition for ModuleDef {
|
||||||
type Def = ast::ModuleItem;
|
type Def = ast::ModuleItem;
|
||||||
type Ref = ast::Path;
|
type Ref = ast::Path;
|
||||||
|
|
||||||
fn declaration(self, db: &RootDatabase) -> Declaration {
|
fn definition(self, db: &RootDatabase) -> Definition {
|
||||||
let (container, visibility) = match self {
|
let (container, visibility) = match self {
|
||||||
ModuleDef::Module(it) => {
|
ModuleDef::Module(it) => {
|
||||||
let container = it.parent(db).or_else(|| Some(it)).unwrap();
|
let container = it.parent(db).or_else(|| Some(it)).unwrap();
|
||||||
@ -270,22 +273,22 @@ impl HasDeclaration for ModuleDef {
|
|||||||
ModuleDef::BuiltinType(..) => unreachable!(),
|
ModuleDef::BuiltinType(..) => unreachable!(),
|
||||||
};
|
};
|
||||||
let item = NameKind::Def(self);
|
let item = NameKind::Def(self);
|
||||||
Declaration { item, container, visibility }
|
Definition { item, container, visibility }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_def(db: &RootDatabase, file_id: HirFileId, def: Self::Def) -> Option<Declaration> {
|
fn from_def(db: &RootDatabase, file_id: HirFileId, def: Self::Def) -> Option<Definition> {
|
||||||
let src = hir::Source { file_id, ast: def };
|
let src = hir::Source { file_id, ast: def };
|
||||||
let def = ModuleDef::from_source(db, src)?;
|
let def = ModuleDef::from_source(db, src)?;
|
||||||
Some(def.declaration(db))
|
Some(def.definition(db))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_ref(
|
fn from_ref(
|
||||||
db: &RootDatabase,
|
db: &RootDatabase,
|
||||||
analyzer: &SourceAnalyzer,
|
analyzer: &SourceAnalyzer,
|
||||||
refer: Self::Ref,
|
refer: Self::Ref,
|
||||||
) -> Option<Declaration> {
|
) -> Option<Definition> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: impl HasDeclaration for hir::MacroDef
|
// FIXME: impl HasDefinition for hir::MacroDef
|
||||||
|
@ -7,10 +7,8 @@ use relative_path::{RelativePath, RelativePathBuf};
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
db::RootDatabase,
|
db::RootDatabase,
|
||||||
name_kind::{
|
name_kind::{classify_name, classify_name_ref, Definition, NameKind::*},
|
||||||
classify_name, classify_name_ref,
|
search_scope::find_refs,
|
||||||
NameKind::{self, *},
|
|
||||||
},
|
|
||||||
FileId, FilePosition, FileRange, FileSystemEdit, NavigationTarget, RangeInfo, SourceChange,
|
FileId, FilePosition, FileRange, FileSystemEdit, NavigationTarget, RangeInfo, SourceChange,
|
||||||
SourceFileEdit, TextRange,
|
SourceFileEdit, TextRange,
|
||||||
};
|
};
|
||||||
@ -58,9 +56,9 @@ pub(crate) fn find_all_refs(
|
|||||||
) -> Option<RangeInfo<ReferenceSearchResult>> {
|
) -> Option<RangeInfo<ReferenceSearchResult>> {
|
||||||
let parse = db.parse(position.file_id);
|
let parse = db.parse(position.file_id);
|
||||||
let syntax = parse.tree().syntax().clone();
|
let syntax = parse.tree().syntax().clone();
|
||||||
let RangeInfo { range, info: (analyzer, name_kind) } = find_name(db, &syntax, position)?;
|
let RangeInfo { range, info: (name, def) } = find_name(db, &syntax, position)?;
|
||||||
|
|
||||||
let declaration = match name_kind {
|
let declaration = match def.item {
|
||||||
Macro(mac) => NavigationTarget::from_macro_def(db, mac),
|
Macro(mac) => NavigationTarget::from_macro_def(db, mac),
|
||||||
FieldAccess(field) => NavigationTarget::from_field(db, field),
|
FieldAccess(field) => NavigationTarget::from_field(db, field),
|
||||||
AssocItem(assoc) => NavigationTarget::from_assoc_item(db, assoc),
|
AssocItem(assoc) => NavigationTarget::from_assoc_item(db, assoc),
|
||||||
@ -74,14 +72,19 @@ pub(crate) fn find_all_refs(
|
|||||||
GenericParam(_) => return None,
|
GenericParam(_) => return None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let references = match name_kind {
|
// let references = match name_kind {
|
||||||
Pat((_, pat)) => analyzer
|
// Pat((_, pat)) => analyzer
|
||||||
.find_all_refs(&pat.to_node(&syntax))
|
// .find_all_refs(&pat.to_node(&syntax))
|
||||||
.into_iter()
|
// .into_iter()
|
||||||
.map(move |ref_desc| FileRange { file_id: position.file_id, range: ref_desc.range })
|
// .map(move |ref_desc| FileRange { file_id: position.file_id, range: ref_desc.range })
|
||||||
.collect::<Vec<_>>(),
|
// .collect::<Vec<_>>(),
|
||||||
_ => vec![],
|
// _ => 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 }));
|
return Some(RangeInfo::new(range, ReferenceSearchResult { declaration, references }));
|
||||||
|
|
||||||
@ -89,18 +92,17 @@ pub(crate) fn find_all_refs(
|
|||||||
db: &RootDatabase,
|
db: &RootDatabase,
|
||||||
syntax: &SyntaxNode,
|
syntax: &SyntaxNode,
|
||||||
position: FilePosition,
|
position: FilePosition,
|
||||||
) -> Option<RangeInfo<(hir::SourceAnalyzer, NameKind)>> {
|
) -> Option<RangeInfo<(String, Definition)>> {
|
||||||
if let Some(name) = find_node_at_offset::<ast::Name>(&syntax, position.offset) {
|
if let Some(name) = find_node_at_offset::<ast::Name>(&syntax, position.offset) {
|
||||||
let analyzer = hir::SourceAnalyzer::new(db, position.file_id, name.syntax(), None);
|
let def = classify_name(db, position.file_id, &name)?;
|
||||||
let name_kind = classify_name(db, position.file_id, &name)?.item;
|
|
||||||
let range = name.syntax().text_range();
|
let range = name.syntax().text_range();
|
||||||
return Some(RangeInfo::new(range, (analyzer, name_kind)));
|
return Some(RangeInfo::new(range, (name.text().to_string(), def)));
|
||||||
}
|
}
|
||||||
let name_ref = find_node_at_offset::<ast::NameRef>(&syntax, position.offset)?;
|
let name_ref = find_node_at_offset::<ast::NameRef>(&syntax, position.offset)?;
|
||||||
let range = name_ref.syntax().text_range();
|
let range = name_ref.syntax().text_range();
|
||||||
let analyzer = hir::SourceAnalyzer::new(db, position.file_id, name_ref.syntax(), None);
|
let analyzer = hir::SourceAnalyzer::new(db, position.file_id, name_ref.syntax(), None);
|
||||||
let name_kind = classify_name_ref(db, position.file_id, &analyzer, &name_ref)?.item;
|
let def = classify_name_ref(db, position.file_id, &analyzer, &name_ref)?;
|
||||||
Some(RangeInfo::new(range, (analyzer, name_kind)))
|
Some(RangeInfo::new(range, (name_ref.text().to_string(), def)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -271,12 +273,16 @@ mod tests {
|
|||||||
let code = r#"
|
let code = r#"
|
||||||
//- /lib.rs
|
//- /lib.rs
|
||||||
struct Foo {
|
struct Foo {
|
||||||
spam<|>: u32,
|
pub spam<|>: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main(s: Foo) {
|
||||||
|
let f = s.spam;
|
||||||
}
|
}
|
||||||
"#;
|
"#;
|
||||||
|
|
||||||
let refs = get_all_refs(code);
|
let refs = get_all_refs(code);
|
||||||
assert_eq!(refs.len(), 1);
|
assert_eq!(refs.len(), 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -1,18 +1,69 @@
|
|||||||
use hir::{DefWithBody, HasSource, ModuleSource};
|
use hir::{
|
||||||
|
source_binder::ReferenceDescriptor, DefWithBody, HasSource, ModuleSource, SourceAnalyzer,
|
||||||
|
};
|
||||||
use ra_db::{FileId, SourceDatabase};
|
use ra_db::{FileId, SourceDatabase};
|
||||||
use ra_syntax::{AstNode, TextRange};
|
use ra_syntax::{algo::find_node_at_offset, ast, AstNode, SourceFile, TextRange, TextUnit};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
db::RootDatabase,
|
db::RootDatabase,
|
||||||
name_kind::{Declaration, NameKind},
|
name_kind::{classify_name_ref, Definition, NameKind},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct SearchScope {
|
pub(crate) struct SearchScope {
|
||||||
pub scope: Vec<(FileId, Option<TextRange>)>,
|
pub scope: Vec<(FileId, Option<TextRange>)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Declaration {
|
pub(crate) fn find_refs(
|
||||||
pub fn scope(self, db: &RootDatabase) -> Option<SearchScope> {
|
db: &RootDatabase,
|
||||||
|
def: Definition,
|
||||||
|
name: String,
|
||||||
|
) -> Vec<ReferenceDescriptor> {
|
||||||
|
let pat = name.as_str();
|
||||||
|
let scope = def.scope(db).scope;
|
||||||
|
let mut refs = vec![];
|
||||||
|
|
||||||
|
let is_match = |file_id: FileId, name_ref: &ast::NameRef| -> bool {
|
||||||
|
let analyzer = SourceAnalyzer::new(db, file_id, name_ref.syntax(), None);
|
||||||
|
let classified = classify_name_ref(db, file_id, &analyzer, &name_ref);
|
||||||
|
if let Some(d) = classified {
|
||||||
|
d == def
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
for (file_id, text_range) in scope {
|
||||||
|
let text = db.file_text(file_id);
|
||||||
|
let parse = SourceFile::parse(&text);
|
||||||
|
let syntax = parse.tree().syntax().clone();
|
||||||
|
|
||||||
|
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();
|
||||||
|
|
||||||
|
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(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} else if is_match(file_id, &name_ref) {
|
||||||
|
refs.push(ReferenceDescriptor {
|
||||||
|
name: name_ref.text().to_string(),
|
||||||
|
range: name_ref.syntax().text_range(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return refs;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Definition {
|
||||||
|
pub fn scope(&self, db: &RootDatabase) -> SearchScope {
|
||||||
let module_src = self.container.definition_source(db);
|
let module_src = self.container.definition_source(db);
|
||||||
let file_id = module_src.file_id.original_file(db);
|
let file_id = module_src.file_id.original_file(db);
|
||||||
|
|
||||||
@ -22,16 +73,16 @@ impl Declaration {
|
|||||||
DefWithBody::Const(c) => c.source(db).ast.syntax().text_range(),
|
DefWithBody::Const(c) => c.source(db).ast.syntax().text_range(),
|
||||||
DefWithBody::Static(s) => s.source(db).ast.syntax().text_range(),
|
DefWithBody::Static(s) => s.source(db).ast.syntax().text_range(),
|
||||||
};
|
};
|
||||||
return Some(SearchScope { scope: vec![(file_id, Some(range))] });
|
return SearchScope { scope: vec![(file_id, Some(range))] };
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(vis) = self.visibility {
|
if let Some(ref vis) = self.visibility {
|
||||||
let source_root_id = db.file_source_root(file_id);
|
let source_root_id = db.file_source_root(file_id);
|
||||||
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().text() == "pub(crate)" {
|
||||||
return Some(SearchScope { scope: files });
|
return SearchScope { scope: files };
|
||||||
}
|
}
|
||||||
if vis.syntax().text() == "pub" {
|
if vis.syntax().text() == "pub" {
|
||||||
let krate = self.container.krate(db).unwrap();
|
let krate = self.container.krate(db).unwrap();
|
||||||
@ -48,17 +99,15 @@ impl Declaration {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Some(SearchScope { scope: files });
|
return SearchScope { scope: files };
|
||||||
}
|
}
|
||||||
// FIXME: extend to "pub(super)" and "pub(in path)" cases,
|
// FIXME: "pub(super)", "pub(in path)"
|
||||||
// then remove `Option`
|
|
||||||
return None;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let range = match module_src.ast {
|
let range = match module_src.ast {
|
||||||
ModuleSource::Module(m) => Some(m.syntax().text_range()),
|
ModuleSource::Module(m) => Some(m.syntax().text_range()),
|
||||||
ModuleSource::SourceFile(_) => None,
|
ModuleSource::SourceFile(_) => None,
|
||||||
};
|
};
|
||||||
Some(SearchScope { scope: vec![(file_id, range)] })
|
SearchScope { scope: vec![(file_id, range)] }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user