From d26d0ada50fd0063c03e28bc2673f9f63fd23d95 Mon Sep 17 00:00:00 2001 From: Ekaterina Babshukova Date: Sat, 12 Oct 2019 18:47:17 +0300 Subject: restructure a bit --- crates/ra_ide_api/src/references/classify.rs | 143 +++++++ crates/ra_ide_api/src/references/definition.rs | 177 +++++++++ crates/ra_ide_api/src/references/rename.rs | 467 +++++++++++++++++++++++ crates/ra_ide_api/src/references/search_scope.rs | 61 +++ 4 files changed, 848 insertions(+) create mode 100644 crates/ra_ide_api/src/references/classify.rs create mode 100644 crates/ra_ide_api/src/references/definition.rs create mode 100644 crates/ra_ide_api/src/references/rename.rs create mode 100644 crates/ra_ide_api/src/references/search_scope.rs (limited to 'crates/ra_ide_api/src/references') diff --git a/crates/ra_ide_api/src/references/classify.rs b/crates/ra_ide_api/src/references/classify.rs new file mode 100644 index 000000000..0b604a5cf --- /dev/null +++ b/crates/ra_ide_api/src/references/classify.rs @@ -0,0 +1,143 @@ +use hir::{ + AssocItem, Either, EnumVariant, FromSource, Module, ModuleDef, ModuleSource, Path, + PathResolution, Source, SourceAnalyzer, StructField, +}; +use ra_db::FileId; +use ra_syntax::{ast, match_ast, AstNode, AstPtr}; + +use super::{definition::HasDefinition, Definition, NameKind}; +use crate::db::RootDatabase; + +use hir::{db::AstDatabase, HirFileId}; + +pub(crate) fn classify_name( + db: &RootDatabase, + file_id: FileId, + name: &ast::Name, +) -> Option { + let parent = name.syntax().parent()?; + let file_id = file_id.into(); + + match_ast! { + match parent { + ast::BindPat(it) => { + decl_from_pat(db, file_id, AstPtr::new(&it)) + }, + ast::RecordFieldDef(it) => { + StructField::from_def(db, file_id, it) + }, + ast::ImplItem(it) => { + AssocItem::from_def(db, file_id, it.clone()).or_else(|| { + match it { + ast::ImplItem::FnDef(f) => ModuleDef::from_def(db, file_id, f.into()), + ast::ImplItem::ConstDef(c) => ModuleDef::from_def(db, file_id, c.into()), + ast::ImplItem::TypeAliasDef(a) => ModuleDef::from_def(db, file_id, a.into()), + } + }) + }, + ast::EnumVariant(it) => { + let src = hir::Source { file_id, ast: it.clone() }; + let def: ModuleDef = EnumVariant::from_source(db, src)?.into(); + Some(def.definition(db)) + }, + ast::ModuleItem(it) => { + ModuleDef::from_def(db, file_id, it) + }, + _ => None, + } + } +} + +pub(crate) fn classify_name_ref( + db: &RootDatabase, + file_id: FileId, + name_ref: &ast::NameRef, +) -> Option { + let analyzer = SourceAnalyzer::new(db, file_id, name_ref.syntax(), None); + let parent = name_ref.syntax().parent()?; + match_ast! { + match parent { + ast::MethodCallExpr(it) => { + return AssocItem::from_ref(db, &analyzer, it); + }, + ast::FieldExpr(it) => { + if let Some(field) = analyzer.resolve_field(&it) { + return Some(field.definition(db)); + } + }, + ast::RecordField(it) => { + if let Some(record_lit) = it.syntax().ancestors().find_map(ast::RecordLit::cast) { + let variant_def = analyzer.resolve_record_literal(&record_lit)?; + let hir_path = Path::from_name_ref(name_ref); + let hir_name = hir_path.as_ident()?; + let field = variant_def.field(db, hir_name)?; + return Some(field.definition(db)); + } + }, + _ => (), + } + } + + let ast = ModuleSource::from_child_node(db, file_id, &parent); + let file_id = file_id.into(); + let container = Module::from_definition(db, Source { file_id, ast })?; + let visibility = None; + + if let Some(macro_call) = + parent.parent().and_then(|node| node.parent()).and_then(ast::MacroCall::cast) + { + if let Some(mac) = analyzer.resolve_macro_call(db, ¯o_call) { + return Some(Definition { item: NameKind::Macro(mac), container, visibility }); + } + } + + // General case, a path or a local: + let path = name_ref.syntax().ancestors().find_map(ast::Path::cast)?; + let resolved = analyzer.resolve_path(db, &path)?; + match resolved { + PathResolution::Def(def) => Some(def.definition(db)), + PathResolution::LocalBinding(Either::A(pat)) => decl_from_pat(db, file_id, pat), + PathResolution::LocalBinding(Either::B(par)) => { + Some(Definition { item: NameKind::SelfParam(par), container, visibility }) + } + PathResolution::GenericParam(par) => { + // FIXME: get generic param def + Some(Definition { item: NameKind::GenericParam(par), container, visibility }) + } + PathResolution::Macro(def) => { + Some(Definition { item: NameKind::Macro(def), container, visibility }) + } + PathResolution::SelfType(impl_block) => { + let ty = impl_block.target_ty(db); + let container = impl_block.module(); + Some(Definition { item: NameKind::SelfType(ty), container, visibility }) + } + PathResolution::AssocItem(assoc) => Some(assoc.definition(db)), + } +} + +fn decl_from_pat( + db: &RootDatabase, + file_id: HirFileId, + pat: AstPtr, +) -> Option { + let root = db.parse_or_expand(file_id)?; + // FIXME: use match_ast! + let def = pat.to_node(&root).syntax().ancestors().find_map(|node| { + if let Some(it) = ast::FnDef::cast(node.clone()) { + let src = hir::Source { file_id, ast: it }; + Some(hir::Function::from_source(db, src)?.into()) + } else if let Some(it) = ast::ConstDef::cast(node.clone()) { + let src = hir::Source { file_id, ast: it }; + Some(hir::Const::from_source(db, src)?.into()) + } else if let Some(it) = ast::StaticDef::cast(node.clone()) { + let src = hir::Source { file_id, ast: it }; + Some(hir::Static::from_source(db, src)?.into()) + } else { + None + } + })?; + let item = NameKind::Pat((def, pat)); + let container = def.module(db); + Some(Definition { item, container, visibility: None }) +} diff --git a/crates/ra_ide_api/src/references/definition.rs b/crates/ra_ide_api/src/references/definition.rs new file mode 100644 index 000000000..65b1f8dd7 --- /dev/null +++ b/crates/ra_ide_api/src/references/definition.rs @@ -0,0 +1,177 @@ +use hir::{ + db::AstDatabase, Adt, AssocItem, DefWithBody, FromSource, HasSource, HirFileId, MacroDef, + Module, ModuleDef, SourceAnalyzer, StructField, Ty, VariantDef, +}; +use ra_syntax::{ast, ast::VisibilityOwner, AstNode, AstPtr}; + +use crate::db::RootDatabase; + +#[derive(Debug, PartialEq, Eq)] +pub enum NameKind { + Macro(MacroDef), + FieldAccess(StructField), + AssocItem(AssocItem), + Def(ModuleDef), + SelfType(Ty), + Pat((DefWithBody, AstPtr)), + SelfParam(AstPtr), + GenericParam(u32), +} + +#[derive(PartialEq, Eq)] +pub(crate) struct Definition { + pub visibility: Option, + pub container: Module, + pub item: NameKind, +} + +pub(super) trait HasDefinition { + type Def; + type Ref; + + fn definition(self, db: &RootDatabase) -> Definition; + fn from_def(db: &RootDatabase, file_id: HirFileId, def: Self::Def) -> Option; + fn from_ref( + db: &RootDatabase, + analyzer: &SourceAnalyzer, + refer: Self::Ref, + ) -> Option; +} + +// fn decl_from_pat( +// db: &RootDatabase, +// file_id: HirFileId, +// pat: AstPtr, +// ) -> Option { +// let root = db.parse_or_expand(file_id)?; +// // FIXME: use match_ast! +// let def = pat.to_node(&root).syntax().ancestors().find_map(|node| { +// if let Some(it) = ast::FnDef::cast(node.clone()) { +// let src = hir::Source { file_id, ast: it }; +// Some(hir::Function::from_source(db, src)?.into()) +// } else if let Some(it) = ast::ConstDef::cast(node.clone()) { +// let src = hir::Source { file_id, ast: it }; +// Some(hir::Const::from_source(db, src)?.into()) +// } else if let Some(it) = ast::StaticDef::cast(node.clone()) { +// let src = hir::Source { file_id, ast: it }; +// Some(hir::Static::from_source(db, src)?.into()) +// } else { +// None +// } +// })?; +// let item = NameKind::Pat((def, pat)); +// let container = def.module(db); +// Some(Definition { item, container, visibility: None }) +// } + +impl HasDefinition for StructField { + type Def = ast::RecordFieldDef; + type Ref = ast::FieldExpr; + + fn definition(self, db: &RootDatabase) -> Definition { + let item = NameKind::FieldAccess(self); + let parent = self.parent_def(db); + let container = parent.module(db); + let visibility = match parent { + VariantDef::Struct(s) => s.source(db).ast.visibility(), + VariantDef::EnumVariant(e) => e.source(db).ast.parent_enum().visibility(), + }; + Definition { item, container, visibility } + } + + fn from_def(db: &RootDatabase, file_id: HirFileId, def: Self::Def) -> Option { + let src = hir::Source { file_id, ast: hir::FieldSource::Named(def) }; + let field = StructField::from_source(db, src)?; + Some(field.definition(db)) + } + + fn from_ref( + db: &RootDatabase, + analyzer: &SourceAnalyzer, + refer: Self::Ref, + ) -> Option { + let field = analyzer.resolve_field(&refer)?; + Some(field.definition(db)) + } +} + +impl HasDefinition for AssocItem { + type Def = ast::ImplItem; + type Ref = ast::MethodCallExpr; + + fn definition(self, db: &RootDatabase) -> Definition { + let item = NameKind::AssocItem(self); + let container = self.module(db); + let visibility = match self { + AssocItem::Function(f) => f.source(db).ast.visibility(), + AssocItem::Const(c) => c.source(db).ast.visibility(), + AssocItem::TypeAlias(a) => a.source(db).ast.visibility(), + }; + Definition { item, container, visibility } + } + + fn from_def(db: &RootDatabase, file_id: HirFileId, def: Self::Def) -> Option { + if def.syntax().parent().and_then(ast::ItemList::cast).is_none() { + return None; + } + let src = hir::Source { file_id, ast: def }; + let item = AssocItem::from_source(db, src)?; + Some(item.definition(db)) + } + + fn from_ref( + db: &RootDatabase, + analyzer: &SourceAnalyzer, + refer: Self::Ref, + ) -> Option { + let func: AssocItem = analyzer.resolve_method_call(&refer)?.into(); + Some(func.definition(db)) + } +} + +impl HasDefinition for ModuleDef { + type Def = ast::ModuleItem; + type Ref = ast::Path; + + fn definition(self, db: &RootDatabase) -> Definition { + let (container, visibility) = match self { + ModuleDef::Module(it) => { + let container = it.parent(db).or_else(|| Some(it)).unwrap(); + let visibility = it.declaration_source(db).and_then(|s| s.ast.visibility()); + (container, visibility) + } + ModuleDef::EnumVariant(it) => { + let container = it.module(db); + let visibility = it.source(db).ast.parent_enum().visibility(); + (container, visibility) + } + ModuleDef::Function(it) => (it.module(db), it.source(db).ast.visibility()), + ModuleDef::Const(it) => (it.module(db), it.source(db).ast.visibility()), + ModuleDef::Static(it) => (it.module(db), it.source(db).ast.visibility()), + ModuleDef::Trait(it) => (it.module(db), it.source(db).ast.visibility()), + ModuleDef::TypeAlias(it) => (it.module(db), it.source(db).ast.visibility()), + ModuleDef::Adt(Adt::Struct(it)) => (it.module(db), it.source(db).ast.visibility()), + ModuleDef::Adt(Adt::Union(it)) => (it.module(db), it.source(db).ast.visibility()), + ModuleDef::Adt(Adt::Enum(it)) => (it.module(db), it.source(db).ast.visibility()), + ModuleDef::BuiltinType(..) => unreachable!(), + }; + let item = NameKind::Def(self); + Definition { item, container, visibility } + } + + fn from_def(db: &RootDatabase, file_id: HirFileId, def: Self::Def) -> Option { + let src = hir::Source { file_id, ast: def }; + let def = ModuleDef::from_source(db, src)?; + Some(def.definition(db)) + } + + fn from_ref( + db: &RootDatabase, + analyzer: &SourceAnalyzer, + refer: Self::Ref, + ) -> Option { + None + } +} + +// FIXME: impl HasDefinition for hir::MacroDef diff --git a/crates/ra_ide_api/src/references/rename.rs b/crates/ra_ide_api/src/references/rename.rs new file mode 100644 index 000000000..7e564a40e --- /dev/null +++ b/crates/ra_ide_api/src/references/rename.rs @@ -0,0 +1,467 @@ +use hir::ModuleSource; +use ra_db::SourceDatabase; +use ra_syntax::{algo::find_node_at_offset, ast, AstNode, SyntaxNode}; +use relative_path::{RelativePath, RelativePathBuf}; + +use crate::{ + db::RootDatabase, FileId, FilePosition, FileSystemEdit, RangeInfo, SourceChange, + SourceFileEdit, TextRange, +}; + +use super::find_all_refs; + +pub(crate) fn rename( + db: &RootDatabase, + position: FilePosition, + new_name: &str, +) -> Option> { + let parse = db.parse(position.file_id); + if let Some((ast_name, ast_module)) = + find_name_and_module_at_offset(parse.tree().syntax(), position) + { + let range = ast_name.syntax().text_range(); + rename_mod(db, &ast_name, &ast_module, position, new_name) + .map(|info| RangeInfo::new(range, info)) + } else { + rename_reference(db, position, new_name) + } +} + +fn find_name_and_module_at_offset( + syntax: &SyntaxNode, + position: FilePosition, +) -> Option<(ast::Name, ast::Module)> { + let ast_name = find_node_at_offset::(syntax, position.offset)?; + let ast_module = ast::Module::cast(ast_name.syntax().parent()?)?; + Some((ast_name, ast_module)) +} + +fn source_edit_from_file_id_range( + file_id: FileId, + range: TextRange, + new_name: &str, +) -> SourceFileEdit { + SourceFileEdit { + file_id, + edit: { + let mut builder = ra_text_edit::TextEditBuilder::default(); + builder.replace(range, new_name.into()); + builder.finish() + }, + } +} + +fn rename_mod( + db: &RootDatabase, + ast_name: &ast::Name, + ast_module: &ast::Module, + position: FilePosition, + new_name: &str, +) -> Option { + let mut source_file_edits = Vec::new(); + let mut file_system_edits = Vec::new(); + let module_src = hir::Source { file_id: position.file_id.into(), ast: ast_module.clone() }; + if let Some(module) = hir::Module::from_declaration(db, module_src) { + let src = module.definition_source(db); + let file_id = src.file_id.original_file(db); + match src.ast { + ModuleSource::SourceFile(..) => { + let mod_path: RelativePathBuf = db.file_relative_path(file_id); + // mod is defined in path/to/dir/mod.rs + let dst_path = if mod_path.file_stem() == Some("mod") { + mod_path + .parent() + .and_then(|p| p.parent()) + .or_else(|| Some(RelativePath::new(""))) + .map(|p| p.join(new_name).join("mod.rs")) + } else { + Some(mod_path.with_file_name(new_name).with_extension("rs")) + }; + if let Some(path) = dst_path { + let move_file = FileSystemEdit::MoveFile { + src: file_id, + dst_source_root: db.file_source_root(position.file_id), + dst_path: path, + }; + file_system_edits.push(move_file); + } + } + ModuleSource::Module(..) => {} + } + } + + let edit = SourceFileEdit { + file_id: position.file_id, + edit: { + let mut builder = ra_text_edit::TextEditBuilder::default(); + builder.replace(ast_name.syntax().text_range(), new_name.into()); + builder.finish() + }, + }; + source_file_edits.push(edit); + + Some(SourceChange::from_edits("rename", source_file_edits, file_system_edits)) +} + +fn rename_reference( + db: &RootDatabase, + position: FilePosition, + new_name: &str, +) -> Option> { + let RangeInfo { range, info: refs } = find_all_refs(db, position)?; + + let edit = refs + .into_iter() + .map(|range| source_edit_from_file_id_range(range.file_id, range.range, new_name)) + .collect::>(); + + if edit.is_empty() { + return None; + } + + Some(RangeInfo::new(range, SourceChange::source_file_edits("rename", edit))) +} + +#[cfg(test)] +mod tests { + use crate::{ + mock_analysis::analysis_and_position, mock_analysis::single_file_with_position, FileId, + ReferenceSearchResult, + }; + use insta::assert_debug_snapshot; + use test_utils::assert_eq_text; + + #[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); + } + + #[test] + fn test_find_all_refs_field_name() { + let code = r#" + //- /lib.rs + struct Foo { + pub spam<|>: u32, + } + + fn main(s: Foo) { + let f = s.spam; + } + "#; + + let refs = get_all_refs(code); + assert_eq!(refs.len(), 2); + } + + #[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); + } + + #[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() + } + + #[test] + fn test_rename_for_local() { + test_rename( + r#" + fn main() { + let mut i = 1; + let j = 1; + i = i<|> + j; + + { + i = 0; + } + + i = 5; + }"#, + "k", + r#" + fn main() { + let mut k = 1; + let j = 1; + k = k + j; + + { + k = 0; + } + + k = 5; + }"#, + ); + } + + #[test] + fn test_rename_for_param_inside() { + test_rename( + r#" + fn foo(i : u32) -> u32 { + i<|> + }"#, + "j", + r#" + fn foo(j : u32) -> u32 { + j + }"#, + ); + } + + #[test] + fn test_rename_refs_for_fn_param() { + test_rename( + r#" + fn foo(i<|> : u32) -> u32 { + i + }"#, + "new_name", + r#" + fn foo(new_name : u32) -> u32 { + new_name + }"#, + ); + } + + #[test] + fn test_rename_for_mut_param() { + test_rename( + r#" + fn foo(mut i<|> : u32) -> u32 { + i + }"#, + "new_name", + r#" + fn foo(mut new_name : u32) -> u32 { + new_name + }"#, + ); + } + + #[test] + fn test_rename_mod() { + let (analysis, position) = analysis_and_position( + " + //- /lib.rs + mod bar; + + //- /bar.rs + mod foo<|>; + + //- /bar/foo.rs + // emtpy + ", + ); + let new_name = "foo2"; + let source_change = analysis.rename(position, new_name).unwrap(); + assert_debug_snapshot!(&source_change, +@r###" + Some( + RangeInfo { + range: [4; 7), + info: SourceChange { + label: "rename", + source_file_edits: [ + SourceFileEdit { + file_id: FileId( + 2, + ), + edit: TextEdit { + atoms: [ + AtomTextEdit { + delete: [4; 7), + insert: "foo2", + }, + ], + }, + }, + ], + file_system_edits: [ + MoveFile { + src: FileId( + 3, + ), + dst_source_root: SourceRootId( + 0, + ), + dst_path: "bar/foo2.rs", + }, + ], + cursor_position: None, + }, + }, + ) + "###); + } + + #[test] + fn test_rename_mod_in_dir() { + let (analysis, position) = analysis_and_position( + " + //- /lib.rs + mod fo<|>o; + //- /foo/mod.rs + // emtpy + ", + ); + let new_name = "foo2"; + let source_change = analysis.rename(position, new_name).unwrap(); + assert_debug_snapshot!(&source_change, + @r###" + Some( + RangeInfo { + range: [4; 7), + info: SourceChange { + label: "rename", + source_file_edits: [ + SourceFileEdit { + file_id: FileId( + 1, + ), + edit: TextEdit { + atoms: [ + AtomTextEdit { + delete: [4; 7), + insert: "foo2", + }, + ], + }, + }, + ], + file_system_edits: [ + MoveFile { + src: FileId( + 2, + ), + dst_source_root: SourceRootId( + 0, + ), + dst_path: "foo2/mod.rs", + }, + ], + cursor_position: None, + }, + }, + ) + "### + ); + } + + fn test_rename(text: &str, new_name: &str, expected: &str) { + let (analysis, position) = single_file_with_position(text); + let source_change = analysis.rename(position, new_name).unwrap(); + let mut text_edit_builder = ra_text_edit::TextEditBuilder::default(); + let mut file_id: Option = None; + if let Some(change) = source_change { + for edit in change.info.source_file_edits { + file_id = Some(edit.file_id); + for atom in edit.edit.as_atoms() { + text_edit_builder.replace(atom.delete, atom.insert.clone()); + } + } + } + let result = + text_edit_builder.finish().apply(&*analysis.file_text(file_id.unwrap()).unwrap()); + assert_eq_text!(expected, &*result); + } +} diff --git a/crates/ra_ide_api/src/references/search_scope.rs b/crates/ra_ide_api/src/references/search_scope.rs new file mode 100644 index 000000000..557ee7b57 --- /dev/null +++ b/crates/ra_ide_api/src/references/search_scope.rs @@ -0,0 +1,61 @@ +use hir::{DefWithBody, HasSource, ModuleSource}; +use ra_db::{FileId, SourceDatabase}; +use ra_syntax::{AstNode, TextRange}; + +use crate::db::RootDatabase; + +use super::{Definition, NameKind}; + +pub(crate) struct SearchScope { + pub scope: Vec<(FileId, Option)>, +} + +impl Definition { + pub fn scope(&self, db: &RootDatabase) -> SearchScope { + let module_src = self.container.definition_source(db); + let file_id = module_src.file_id.original_file(db); + + if let NameKind::Pat((def, _)) = self.item { + let range = match def { + DefWithBody::Function(f) => f.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(), + }; + return SearchScope { scope: vec![(file_id, Some(range))] }; + } + + if let Some(ref vis) = self.visibility { + let source_root_id = db.file_source_root(file_id); + let source_root = db.source_root(source_root_id); + let mut files = source_root.walk().map(|id| (id.into(), None)).collect::>(); + + if vis.syntax().to_string().as_str() == "pub(crate)" { + return SearchScope { scope: files }; + } + if vis.syntax().to_string().as_str() == "pub" { + let krate = self.container.krate(db).unwrap(); + let crate_graph = db.crate_graph(); + + for crate_id in crate_graph.iter() { + let mut crate_deps = crate_graph.dependencies(crate_id); + + if crate_deps.any(|dep| dep.crate_id() == krate.crate_id()) { + let root_file = crate_graph.crate_root(crate_id); + let source_root_id = db.file_source_root(root_file); + let source_root = db.source_root(source_root_id); + files.extend(source_root.walk().map(|id| (id.into(), None))); + } + } + + return SearchScope { scope: files }; + } + // FIXME: "pub(super)", "pub(in path)" + } + + let range = match module_src.ast { + ModuleSource::Module(m) => Some(m.syntax().text_range()), + ModuleSource::SourceFile(_) => None, + }; + SearchScope { scope: vec![(file_id, range)] } + } +} -- cgit v1.2.3 From 835173d065dbe1fdd7369ea49336c0b785be8cb8 Mon Sep 17 00:00:00 2001 From: Ekaterina Babshukova Date: Sat, 12 Oct 2019 20:30:53 +0300 Subject: replace trait by a bunch of functions --- crates/ra_ide_api/src/references/classify.rs | 196 ++++++++++++--------- crates/ra_ide_api/src/references/definition.rs | 177 ------------------- .../ra_ide_api/src/references/name_definition.rs | 104 +++++++++++ crates/ra_ide_api/src/references/search_scope.rs | 4 +- 4 files changed, 217 insertions(+), 264 deletions(-) delete mode 100644 crates/ra_ide_api/src/references/definition.rs create mode 100644 crates/ra_ide_api/src/references/name_definition.rs (limited to 'crates/ra_ide_api/src/references') diff --git a/crates/ra_ide_api/src/references/classify.rs b/crates/ra_ide_api/src/references/classify.rs index 0b604a5cf..2ba1cf71c 100644 --- a/crates/ra_ide_api/src/references/classify.rs +++ b/crates/ra_ide_api/src/references/classify.rs @@ -1,47 +1,90 @@ -use hir::{ - AssocItem, Either, EnumVariant, FromSource, Module, ModuleDef, ModuleSource, Path, - PathResolution, Source, SourceAnalyzer, StructField, -}; +use hir::{Either, FromSource, Module, ModuleSource, Path, PathResolution, Source, SourceAnalyzer}; use ra_db::FileId; use ra_syntax::{ast, match_ast, AstNode, AstPtr}; -use super::{definition::HasDefinition, Definition, NameKind}; +use super::{ + name_definition::{from_assoc_item, from_module_def, from_pat, from_struct_field}, + NameDefinition, NameKind, +}; use crate::db::RootDatabase; -use hir::{db::AstDatabase, HirFileId}; - pub(crate) fn classify_name( db: &RootDatabase, file_id: FileId, name: &ast::Name, -) -> Option { +) -> Option { let parent = name.syntax().parent()?; let file_id = file_id.into(); + // FIXME: add ast::MacroCall(it) match_ast! { match parent { ast::BindPat(it) => { - decl_from_pat(db, file_id, AstPtr::new(&it)) + from_pat(db, file_id, AstPtr::new(&it)) }, ast::RecordFieldDef(it) => { - StructField::from_def(db, file_id, it) + let ast = hir::FieldSource::Named(it); + let src = hir::Source { file_id, ast }; + let field = hir::StructField::from_source(db, src)?; + Some(from_struct_field(db, field)) + }, + ast::Module(it) => { + let ast = hir::ModuleSource::Module(it); + let src = hir::Source { file_id, ast }; + let def = hir::Module::from_definition(db, src)?; + Some(from_module_def(db, def.into())) }, - ast::ImplItem(it) => { - AssocItem::from_def(db, file_id, it.clone()).or_else(|| { - match it { - ast::ImplItem::FnDef(f) => ModuleDef::from_def(db, file_id, f.into()), - ast::ImplItem::ConstDef(c) => ModuleDef::from_def(db, file_id, c.into()), - ast::ImplItem::TypeAliasDef(a) => ModuleDef::from_def(db, file_id, a.into()), - } - }) + ast::StructDef(it) => { + let src = hir::Source { file_id, ast: it }; + let def = hir::Struct::from_source(db, src)?; + Some(from_module_def(db, def.into())) + }, + ast::EnumDef(it) => { + let src = hir::Source { file_id, ast: it }; + let def = hir::Enum::from_source(db, src)?; + Some(from_module_def(db, def.into())) + }, + ast::TraitDef(it) => { + let src = hir::Source { file_id, ast: it }; + let def = hir::Trait::from_source(db, src)?; + Some(from_module_def(db, def.into())) + }, + ast::StaticDef(it) => { + let src = hir::Source { file_id, ast: it }; + let def = hir::Static::from_source(db, src)?; + Some(from_module_def(db, def.into())) }, ast::EnumVariant(it) => { - let src = hir::Source { file_id, ast: it.clone() }; - let def: ModuleDef = EnumVariant::from_source(db, src)?.into(); - Some(def.definition(db)) + let src = hir::Source { file_id, ast: it }; + let def = hir::EnumVariant::from_source(db, src)?; + Some(from_module_def(db, def.into())) + }, + ast::FnDef(it) => { + let src = hir::Source { file_id, ast: it }; + let def = hir::Function::from_source(db, src)?; + if parent.parent().and_then(ast::ItemList::cast).is_some() { + Some(from_assoc_item(db, def.into())) + } else { + Some(from_module_def(db, def.into())) + } + }, + ast::ConstDef(it) => { + let src = hir::Source { file_id, ast: it }; + let def = hir::Const::from_source(db, src)?; + if parent.parent().and_then(ast::ItemList::cast).is_some() { + Some(from_assoc_item(db, def.into())) + } else { + Some(from_module_def(db, def.into())) + } }, - ast::ModuleItem(it) => { - ModuleDef::from_def(db, file_id, it) + ast::TypeAliasDef(it) => { + let src = hir::Source { file_id, ast: it }; + let def = hir::TypeAlias::from_source(db, src)?; + if parent.parent().and_then(ast::ItemList::cast).is_some() { + Some(from_assoc_item(db, def.into())) + } else { + Some(from_module_def(db, def.into())) + } }, _ => None, } @@ -52,92 +95,75 @@ pub(crate) fn classify_name_ref( db: &RootDatabase, file_id: FileId, name_ref: &ast::NameRef, -) -> Option { - let analyzer = SourceAnalyzer::new(db, file_id, name_ref.syntax(), None); +) -> Option { + use PathResolution::*; + let parent = name_ref.syntax().parent()?; - match_ast! { - match parent { - ast::MethodCallExpr(it) => { - return AssocItem::from_ref(db, &analyzer, it); - }, - ast::FieldExpr(it) => { - if let Some(field) = analyzer.resolve_field(&it) { - return Some(field.definition(db)); - } - }, - ast::RecordField(it) => { - if let Some(record_lit) = it.syntax().ancestors().find_map(ast::RecordLit::cast) { - let variant_def = analyzer.resolve_record_literal(&record_lit)?; - let hir_path = Path::from_name_ref(name_ref); - let hir_name = hir_path.as_ident()?; - let field = variant_def.field(db, hir_name)?; - return Some(field.definition(db)); - } - }, - _ => (), + let analyzer = SourceAnalyzer::new(db, file_id, name_ref.syntax(), None); + + if let Some(method_call) = ast::MethodCallExpr::cast(parent.clone()) { + let func = analyzer.resolve_method_call(&method_call)?; + return Some(from_assoc_item(db, func.into())); + } + + if let Some(field_expr) = ast::FieldExpr::cast(parent.clone()) { + if let Some(field) = analyzer.resolve_field(&field_expr) { + return Some(from_struct_field(db, field)); + } + } + + if let Some(record_field) = ast::RecordField::cast(parent.clone()) { + if let Some(record_lit) = record_field.syntax().ancestors().find_map(ast::RecordLit::cast) { + let variant_def = analyzer.resolve_record_literal(&record_lit)?; + let hir_path = Path::from_name_ref(name_ref); + let hir_name = hir_path.as_ident()?; + let field = variant_def.field(db, hir_name)?; + return Some(from_struct_field(db, field)); } } let ast = ModuleSource::from_child_node(db, file_id, &parent); let file_id = file_id.into(); + // FIXME: find correct container and visibility for each case let container = Module::from_definition(db, Source { file_id, ast })?; let visibility = None; if let Some(macro_call) = parent.parent().and_then(|node| node.parent()).and_then(ast::MacroCall::cast) { - if let Some(mac) = analyzer.resolve_macro_call(db, ¯o_call) { - return Some(Definition { item: NameKind::Macro(mac), container, visibility }); + if let Some(macro_def) = analyzer.resolve_macro_call(db, ¯o_call) { + return Some(NameDefinition { + item: NameKind::Macro(macro_def), + container, + visibility, + }); } } - // General case, a path or a local: let path = name_ref.syntax().ancestors().find_map(ast::Path::cast)?; let resolved = analyzer.resolve_path(db, &path)?; match resolved { - PathResolution::Def(def) => Some(def.definition(db)), - PathResolution::LocalBinding(Either::A(pat)) => decl_from_pat(db, file_id, pat), - PathResolution::LocalBinding(Either::B(par)) => { - Some(Definition { item: NameKind::SelfParam(par), container, visibility }) + Def(def) => Some(from_module_def(db, def)), + AssocItem(item) => Some(from_assoc_item(db, item)), + LocalBinding(Either::A(pat)) => from_pat(db, file_id, pat), + LocalBinding(Either::B(par)) => { + let item = NameKind::SelfParam(par); + Some(NameDefinition { item, container, visibility }) } - PathResolution::GenericParam(par) => { + GenericParam(par) => { // FIXME: get generic param def - Some(Definition { item: NameKind::GenericParam(par), container, visibility }) + let item = NameKind::GenericParam(par); + Some(NameDefinition { item, container, visibility }) } - PathResolution::Macro(def) => { - Some(Definition { item: NameKind::Macro(def), container, visibility }) + Macro(def) => { + let item = NameKind::Macro(def); + Some(NameDefinition { item, container, visibility }) } - PathResolution::SelfType(impl_block) => { + SelfType(impl_block) => { let ty = impl_block.target_ty(db); + let item = NameKind::SelfType(ty); let container = impl_block.module(); - Some(Definition { item: NameKind::SelfType(ty), container, visibility }) + Some(NameDefinition { item, container, visibility }) } - PathResolution::AssocItem(assoc) => Some(assoc.definition(db)), } } - -fn decl_from_pat( - db: &RootDatabase, - file_id: HirFileId, - pat: AstPtr, -) -> Option { - let root = db.parse_or_expand(file_id)?; - // FIXME: use match_ast! - let def = pat.to_node(&root).syntax().ancestors().find_map(|node| { - if let Some(it) = ast::FnDef::cast(node.clone()) { - let src = hir::Source { file_id, ast: it }; - Some(hir::Function::from_source(db, src)?.into()) - } else if let Some(it) = ast::ConstDef::cast(node.clone()) { - let src = hir::Source { file_id, ast: it }; - Some(hir::Const::from_source(db, src)?.into()) - } else if let Some(it) = ast::StaticDef::cast(node.clone()) { - let src = hir::Source { file_id, ast: it }; - Some(hir::Static::from_source(db, src)?.into()) - } else { - None - } - })?; - let item = NameKind::Pat((def, pat)); - let container = def.module(db); - Some(Definition { item, container, visibility: None }) -} diff --git a/crates/ra_ide_api/src/references/definition.rs b/crates/ra_ide_api/src/references/definition.rs deleted file mode 100644 index 65b1f8dd7..000000000 --- a/crates/ra_ide_api/src/references/definition.rs +++ /dev/null @@ -1,177 +0,0 @@ -use hir::{ - db::AstDatabase, Adt, AssocItem, DefWithBody, FromSource, HasSource, HirFileId, MacroDef, - Module, ModuleDef, SourceAnalyzer, StructField, Ty, VariantDef, -}; -use ra_syntax::{ast, ast::VisibilityOwner, AstNode, AstPtr}; - -use crate::db::RootDatabase; - -#[derive(Debug, PartialEq, Eq)] -pub enum NameKind { - Macro(MacroDef), - FieldAccess(StructField), - AssocItem(AssocItem), - Def(ModuleDef), - SelfType(Ty), - Pat((DefWithBody, AstPtr)), - SelfParam(AstPtr), - GenericParam(u32), -} - -#[derive(PartialEq, Eq)] -pub(crate) struct Definition { - pub visibility: Option, - pub container: Module, - pub item: NameKind, -} - -pub(super) trait HasDefinition { - type Def; - type Ref; - - fn definition(self, db: &RootDatabase) -> Definition; - fn from_def(db: &RootDatabase, file_id: HirFileId, def: Self::Def) -> Option; - fn from_ref( - db: &RootDatabase, - analyzer: &SourceAnalyzer, - refer: Self::Ref, - ) -> Option; -} - -// fn decl_from_pat( -// db: &RootDatabase, -// file_id: HirFileId, -// pat: AstPtr, -// ) -> Option { -// let root = db.parse_or_expand(file_id)?; -// // FIXME: use match_ast! -// let def = pat.to_node(&root).syntax().ancestors().find_map(|node| { -// if let Some(it) = ast::FnDef::cast(node.clone()) { -// let src = hir::Source { file_id, ast: it }; -// Some(hir::Function::from_source(db, src)?.into()) -// } else if let Some(it) = ast::ConstDef::cast(node.clone()) { -// let src = hir::Source { file_id, ast: it }; -// Some(hir::Const::from_source(db, src)?.into()) -// } else if let Some(it) = ast::StaticDef::cast(node.clone()) { -// let src = hir::Source { file_id, ast: it }; -// Some(hir::Static::from_source(db, src)?.into()) -// } else { -// None -// } -// })?; -// let item = NameKind::Pat((def, pat)); -// let container = def.module(db); -// Some(Definition { item, container, visibility: None }) -// } - -impl HasDefinition for StructField { - type Def = ast::RecordFieldDef; - type Ref = ast::FieldExpr; - - fn definition(self, db: &RootDatabase) -> Definition { - let item = NameKind::FieldAccess(self); - let parent = self.parent_def(db); - let container = parent.module(db); - let visibility = match parent { - VariantDef::Struct(s) => s.source(db).ast.visibility(), - VariantDef::EnumVariant(e) => e.source(db).ast.parent_enum().visibility(), - }; - Definition { item, container, visibility } - } - - fn from_def(db: &RootDatabase, file_id: HirFileId, def: Self::Def) -> Option { - let src = hir::Source { file_id, ast: hir::FieldSource::Named(def) }; - let field = StructField::from_source(db, src)?; - Some(field.definition(db)) - } - - fn from_ref( - db: &RootDatabase, - analyzer: &SourceAnalyzer, - refer: Self::Ref, - ) -> Option { - let field = analyzer.resolve_field(&refer)?; - Some(field.definition(db)) - } -} - -impl HasDefinition for AssocItem { - type Def = ast::ImplItem; - type Ref = ast::MethodCallExpr; - - fn definition(self, db: &RootDatabase) -> Definition { - let item = NameKind::AssocItem(self); - let container = self.module(db); - let visibility = match self { - AssocItem::Function(f) => f.source(db).ast.visibility(), - AssocItem::Const(c) => c.source(db).ast.visibility(), - AssocItem::TypeAlias(a) => a.source(db).ast.visibility(), - }; - Definition { item, container, visibility } - } - - fn from_def(db: &RootDatabase, file_id: HirFileId, def: Self::Def) -> Option { - if def.syntax().parent().and_then(ast::ItemList::cast).is_none() { - return None; - } - let src = hir::Source { file_id, ast: def }; - let item = AssocItem::from_source(db, src)?; - Some(item.definition(db)) - } - - fn from_ref( - db: &RootDatabase, - analyzer: &SourceAnalyzer, - refer: Self::Ref, - ) -> Option { - let func: AssocItem = analyzer.resolve_method_call(&refer)?.into(); - Some(func.definition(db)) - } -} - -impl HasDefinition for ModuleDef { - type Def = ast::ModuleItem; - type Ref = ast::Path; - - fn definition(self, db: &RootDatabase) -> Definition { - let (container, visibility) = match self { - ModuleDef::Module(it) => { - let container = it.parent(db).or_else(|| Some(it)).unwrap(); - let visibility = it.declaration_source(db).and_then(|s| s.ast.visibility()); - (container, visibility) - } - ModuleDef::EnumVariant(it) => { - let container = it.module(db); - let visibility = it.source(db).ast.parent_enum().visibility(); - (container, visibility) - } - ModuleDef::Function(it) => (it.module(db), it.source(db).ast.visibility()), - ModuleDef::Const(it) => (it.module(db), it.source(db).ast.visibility()), - ModuleDef::Static(it) => (it.module(db), it.source(db).ast.visibility()), - ModuleDef::Trait(it) => (it.module(db), it.source(db).ast.visibility()), - ModuleDef::TypeAlias(it) => (it.module(db), it.source(db).ast.visibility()), - ModuleDef::Adt(Adt::Struct(it)) => (it.module(db), it.source(db).ast.visibility()), - ModuleDef::Adt(Adt::Union(it)) => (it.module(db), it.source(db).ast.visibility()), - ModuleDef::Adt(Adt::Enum(it)) => (it.module(db), it.source(db).ast.visibility()), - ModuleDef::BuiltinType(..) => unreachable!(), - }; - let item = NameKind::Def(self); - Definition { item, container, visibility } - } - - fn from_def(db: &RootDatabase, file_id: HirFileId, def: Self::Def) -> Option { - let src = hir::Source { file_id, ast: def }; - let def = ModuleDef::from_source(db, src)?; - Some(def.definition(db)) - } - - fn from_ref( - db: &RootDatabase, - analyzer: &SourceAnalyzer, - refer: Self::Ref, - ) -> Option { - None - } -} - -// FIXME: impl HasDefinition for hir::MacroDef diff --git a/crates/ra_ide_api/src/references/name_definition.rs b/crates/ra_ide_api/src/references/name_definition.rs new file mode 100644 index 000000000..19702eba0 --- /dev/null +++ b/crates/ra_ide_api/src/references/name_definition.rs @@ -0,0 +1,104 @@ +use hir::{ + db::AstDatabase, Adt, AssocItem, DefWithBody, FromSource, HasSource, HirFileId, MacroDef, + Module, ModuleDef, StructField, Ty, VariantDef, +}; +use ra_syntax::{ast, ast::VisibilityOwner, match_ast, AstNode, AstPtr}; + +use crate::db::RootDatabase; + +#[derive(Debug, PartialEq, Eq)] +pub enum NameKind { + Macro(MacroDef), + Field(StructField), + AssocItem(AssocItem), + Def(ModuleDef), + SelfType(Ty), + Pat((DefWithBody, AstPtr)), + SelfParam(AstPtr), + GenericParam(u32), +} + +#[derive(PartialEq, Eq)] +pub(crate) struct NameDefinition { + pub visibility: Option, + pub container: Module, + pub item: NameKind, +} + +pub(super) fn from_pat( + db: &RootDatabase, + file_id: HirFileId, + pat: AstPtr, +) -> Option { + let root = db.parse_or_expand(file_id)?; + let def = pat.to_node(&root).syntax().ancestors().find_map(|node| { + match_ast! { + match node { + ast::FnDef(it) => { + let src = hir::Source { file_id, ast: it }; + Some(hir::Function::from_source(db, src)?.into()) + }, + ast::ConstDef(it) => { + let src = hir::Source { file_id, ast: it }; + Some(hir::Const::from_source(db, src)?.into()) + }, + ast::StaticDef(it) => { + let src = hir::Source { file_id, ast: it }; + Some(hir::Static::from_source(db, src)?.into()) + }, + _ => None, + } + } + })?; + let item = NameKind::Pat((def, pat)); + let container = def.module(db); + Some(NameDefinition { item, container, visibility: None }) +} + +pub(super) fn from_assoc_item(db: &RootDatabase, item: AssocItem) -> NameDefinition { + let container = item.module(db); + let visibility = match item { + AssocItem::Function(f) => f.source(db).ast.visibility(), + AssocItem::Const(c) => c.source(db).ast.visibility(), + AssocItem::TypeAlias(a) => a.source(db).ast.visibility(), + }; + let item = NameKind::AssocItem(item); + NameDefinition { item, container, visibility } +} + +pub(super) fn from_struct_field(db: &RootDatabase, field: StructField) -> NameDefinition { + let item = NameKind::Field(field); + let parent = field.parent_def(db); + let container = parent.module(db); + let visibility = match parent { + VariantDef::Struct(s) => s.source(db).ast.visibility(), + VariantDef::EnumVariant(e) => e.source(db).ast.parent_enum().visibility(), + }; + NameDefinition { item, container, visibility } +} + +pub(super) fn from_module_def(db: &RootDatabase, def: ModuleDef) -> NameDefinition { + let item = NameKind::Def(def); + let (container, visibility) = match def { + ModuleDef::Module(it) => { + let container = it.parent(db).or_else(|| Some(it)).unwrap(); + let visibility = it.declaration_source(db).and_then(|s| s.ast.visibility()); + (container, visibility) + } + ModuleDef::EnumVariant(it) => { + let container = it.module(db); + let visibility = it.source(db).ast.parent_enum().visibility(); + (container, visibility) + } + ModuleDef::Function(it) => (it.module(db), it.source(db).ast.visibility()), + ModuleDef::Const(it) => (it.module(db), it.source(db).ast.visibility()), + ModuleDef::Static(it) => (it.module(db), it.source(db).ast.visibility()), + ModuleDef::Trait(it) => (it.module(db), it.source(db).ast.visibility()), + ModuleDef::TypeAlias(it) => (it.module(db), it.source(db).ast.visibility()), + ModuleDef::Adt(Adt::Struct(it)) => (it.module(db), it.source(db).ast.visibility()), + ModuleDef::Adt(Adt::Union(it)) => (it.module(db), it.source(db).ast.visibility()), + ModuleDef::Adt(Adt::Enum(it)) => (it.module(db), it.source(db).ast.visibility()), + ModuleDef::BuiltinType(..) => unreachable!(), + }; + NameDefinition { item, container, visibility } +} diff --git a/crates/ra_ide_api/src/references/search_scope.rs b/crates/ra_ide_api/src/references/search_scope.rs index 557ee7b57..346815d31 100644 --- a/crates/ra_ide_api/src/references/search_scope.rs +++ b/crates/ra_ide_api/src/references/search_scope.rs @@ -4,13 +4,13 @@ use ra_syntax::{AstNode, TextRange}; use crate::db::RootDatabase; -use super::{Definition, NameKind}; +use super::{NameDefinition, NameKind}; pub(crate) struct SearchScope { pub scope: Vec<(FileId, Option)>, } -impl Definition { +impl NameDefinition { pub fn scope(&self, db: &RootDatabase) -> SearchScope { let module_src = self.container.definition_source(db); let file_id = module_src.file_id.original_file(db); -- cgit v1.2.3 From 19fbf2c16b5c1f39e23c720a2655cfdb49c25135 Mon Sep 17 00:00:00 2001 From: Ekaterina Babshukova Date: Sat, 12 Oct 2019 20:40:49 +0300 Subject: remove `unreachable!()` --- crates/ra_ide_api/src/references/classify.rs | 20 ++++++++++---------- crates/ra_ide_api/src/references/name_definition.rs | 8 ++++++-- 2 files changed, 16 insertions(+), 12 deletions(-) (limited to 'crates/ra_ide_api/src/references') diff --git a/crates/ra_ide_api/src/references/classify.rs b/crates/ra_ide_api/src/references/classify.rs index 2ba1cf71c..5cb194c0e 100644 --- a/crates/ra_ide_api/src/references/classify.rs +++ b/crates/ra_ide_api/src/references/classify.rs @@ -32,32 +32,32 @@ pub(crate) fn classify_name( let ast = hir::ModuleSource::Module(it); let src = hir::Source { file_id, ast }; let def = hir::Module::from_definition(db, src)?; - Some(from_module_def(db, def.into())) + Some(from_module_def(db, def.into(), None)) }, ast::StructDef(it) => { let src = hir::Source { file_id, ast: it }; let def = hir::Struct::from_source(db, src)?; - Some(from_module_def(db, def.into())) + Some(from_module_def(db, def.into(), None)) }, ast::EnumDef(it) => { let src = hir::Source { file_id, ast: it }; let def = hir::Enum::from_source(db, src)?; - Some(from_module_def(db, def.into())) + Some(from_module_def(db, def.into(), None)) }, ast::TraitDef(it) => { let src = hir::Source { file_id, ast: it }; let def = hir::Trait::from_source(db, src)?; - Some(from_module_def(db, def.into())) + Some(from_module_def(db, def.into(), None)) }, ast::StaticDef(it) => { let src = hir::Source { file_id, ast: it }; let def = hir::Static::from_source(db, src)?; - Some(from_module_def(db, def.into())) + Some(from_module_def(db, def.into(), None)) }, ast::EnumVariant(it) => { let src = hir::Source { file_id, ast: it }; let def = hir::EnumVariant::from_source(db, src)?; - Some(from_module_def(db, def.into())) + Some(from_module_def(db, def.into(), None)) }, ast::FnDef(it) => { let src = hir::Source { file_id, ast: it }; @@ -65,7 +65,7 @@ pub(crate) fn classify_name( if parent.parent().and_then(ast::ItemList::cast).is_some() { Some(from_assoc_item(db, def.into())) } else { - Some(from_module_def(db, def.into())) + Some(from_module_def(db, def.into(), None)) } }, ast::ConstDef(it) => { @@ -74,7 +74,7 @@ pub(crate) fn classify_name( if parent.parent().and_then(ast::ItemList::cast).is_some() { Some(from_assoc_item(db, def.into())) } else { - Some(from_module_def(db, def.into())) + Some(from_module_def(db, def.into(), None)) } }, ast::TypeAliasDef(it) => { @@ -83,7 +83,7 @@ pub(crate) fn classify_name( if parent.parent().and_then(ast::ItemList::cast).is_some() { Some(from_assoc_item(db, def.into())) } else { - Some(from_module_def(db, def.into())) + Some(from_module_def(db, def.into(), None)) } }, _ => None, @@ -143,7 +143,7 @@ pub(crate) fn classify_name_ref( let path = name_ref.syntax().ancestors().find_map(ast::Path::cast)?; let resolved = analyzer.resolve_path(db, &path)?; match resolved { - Def(def) => Some(from_module_def(db, def)), + Def(def) => Some(from_module_def(db, def, Some(container))), AssocItem(item) => Some(from_assoc_item(db, item)), LocalBinding(Either::A(pat)) => from_pat(db, file_id, pat), LocalBinding(Either::B(par)) => { diff --git a/crates/ra_ide_api/src/references/name_definition.rs b/crates/ra_ide_api/src/references/name_definition.rs index 19702eba0..58baf3686 100644 --- a/crates/ra_ide_api/src/references/name_definition.rs +++ b/crates/ra_ide_api/src/references/name_definition.rs @@ -77,7 +77,11 @@ pub(super) fn from_struct_field(db: &RootDatabase, field: StructField) -> NameDe NameDefinition { item, container, visibility } } -pub(super) fn from_module_def(db: &RootDatabase, def: ModuleDef) -> NameDefinition { +pub(super) fn from_module_def( + db: &RootDatabase, + def: ModuleDef, + module: Option, +) -> NameDefinition { let item = NameKind::Def(def); let (container, visibility) = match def { ModuleDef::Module(it) => { @@ -98,7 +102,7 @@ pub(super) fn from_module_def(db: &RootDatabase, def: ModuleDef) -> NameDefiniti ModuleDef::Adt(Adt::Struct(it)) => (it.module(db), it.source(db).ast.visibility()), ModuleDef::Adt(Adt::Union(it)) => (it.module(db), it.source(db).ast.visibility()), ModuleDef::Adt(Adt::Enum(it)) => (it.module(db), it.source(db).ast.visibility()), - ModuleDef::BuiltinType(..) => unreachable!(), + ModuleDef::BuiltinType(..) => (module.unwrap(), None), }; NameDefinition { item, container, visibility } } -- cgit v1.2.3 From 88ff88d3189de9dd9b0d88bdda3da769254c2b8e Mon Sep 17 00:00:00 2001 From: Ekaterina Babshukova Date: Mon, 14 Oct 2019 14:59:02 +0300 Subject: use Lazy, some fixes --- crates/ra_ide_api/src/references/classify.rs | 34 ++++++++++------------ .../ra_ide_api/src/references/name_definition.rs | 20 +++++++------ crates/ra_ide_api/src/references/rename.rs | 2 ++ crates/ra_ide_api/src/references/search_scope.rs | 16 +++++----- 4 files changed, 38 insertions(+), 34 deletions(-) (limited to 'crates/ra_ide_api/src/references') diff --git a/crates/ra_ide_api/src/references/classify.rs b/crates/ra_ide_api/src/references/classify.rs index 5cb194c0e..93e079ccc 100644 --- a/crates/ra_ide_api/src/references/classify.rs +++ b/crates/ra_ide_api/src/references/classify.rs @@ -1,3 +1,5 @@ +//! FIXME: write short doc here + use hir::{Either, FromSource, Module, ModuleSource, Path, PathResolution, Source, SourceAnalyzer}; use ra_db::FileId; use ra_syntax::{ast, match_ast, AstNode, AstPtr}; @@ -102,8 +104,9 @@ pub(crate) fn classify_name_ref( let analyzer = SourceAnalyzer::new(db, file_id, name_ref.syntax(), None); if let Some(method_call) = ast::MethodCallExpr::cast(parent.clone()) { - let func = analyzer.resolve_method_call(&method_call)?; - return Some(from_assoc_item(db, func.into())); + if let Some(func) = analyzer.resolve_method_call(&method_call) { + return Some(from_assoc_item(db, func.into())); + } } if let Some(field_expr) = ast::FieldExpr::cast(parent.clone()) { @@ -128,15 +131,10 @@ pub(crate) fn classify_name_ref( let container = Module::from_definition(db, Source { file_id, ast })?; let visibility = None; - if let Some(macro_call) = - parent.parent().and_then(|node| node.parent()).and_then(ast::MacroCall::cast) - { + if let Some(macro_call) = parent.ancestors().find_map(ast::MacroCall::cast) { if let Some(macro_def) = analyzer.resolve_macro_call(db, ¯o_call) { - return Some(NameDefinition { - item: NameKind::Macro(macro_def), - container, - visibility, - }); + let kind = NameKind::Macro(macro_def); + return Some(NameDefinition { kind, container, visibility }); } } @@ -147,23 +145,23 @@ pub(crate) fn classify_name_ref( AssocItem(item) => Some(from_assoc_item(db, item)), LocalBinding(Either::A(pat)) => from_pat(db, file_id, pat), LocalBinding(Either::B(par)) => { - let item = NameKind::SelfParam(par); - Some(NameDefinition { item, container, visibility }) + let kind = NameKind::SelfParam(par); + Some(NameDefinition { kind, container, visibility }) } GenericParam(par) => { // FIXME: get generic param def - let item = NameKind::GenericParam(par); - Some(NameDefinition { item, container, visibility }) + let kind = NameKind::GenericParam(par); + Some(NameDefinition { kind, container, visibility }) } Macro(def) => { - let item = NameKind::Macro(def); - Some(NameDefinition { item, container, visibility }) + let kind = NameKind::Macro(def); + Some(NameDefinition { kind, container, visibility }) } SelfType(impl_block) => { let ty = impl_block.target_ty(db); - let item = NameKind::SelfType(ty); + let kind = NameKind::SelfType(ty); let container = impl_block.module(); - Some(NameDefinition { item, container, visibility }) + Some(NameDefinition { kind, container, visibility }) } } } diff --git a/crates/ra_ide_api/src/references/name_definition.rs b/crates/ra_ide_api/src/references/name_definition.rs index 58baf3686..723d97237 100644 --- a/crates/ra_ide_api/src/references/name_definition.rs +++ b/crates/ra_ide_api/src/references/name_definition.rs @@ -1,3 +1,5 @@ +//! FIXME: write short doc here + use hir::{ db::AstDatabase, Adt, AssocItem, DefWithBody, FromSource, HasSource, HirFileId, MacroDef, Module, ModuleDef, StructField, Ty, VariantDef, @@ -22,7 +24,7 @@ pub enum NameKind { pub(crate) struct NameDefinition { pub visibility: Option, pub container: Module, - pub item: NameKind, + pub kind: NameKind, } pub(super) fn from_pat( @@ -50,9 +52,9 @@ pub(super) fn from_pat( } } })?; - let item = NameKind::Pat((def, pat)); + let kind = NameKind::Pat((def, pat)); let container = def.module(db); - Some(NameDefinition { item, container, visibility: None }) + Some(NameDefinition { kind, container, visibility: None }) } pub(super) fn from_assoc_item(db: &RootDatabase, item: AssocItem) -> NameDefinition { @@ -62,19 +64,19 @@ pub(super) fn from_assoc_item(db: &RootDatabase, item: AssocItem) -> NameDefinit AssocItem::Const(c) => c.source(db).ast.visibility(), AssocItem::TypeAlias(a) => a.source(db).ast.visibility(), }; - let item = NameKind::AssocItem(item); - NameDefinition { item, container, visibility } + let kind = NameKind::AssocItem(item); + NameDefinition { kind, container, visibility } } pub(super) fn from_struct_field(db: &RootDatabase, field: StructField) -> NameDefinition { - let item = NameKind::Field(field); + let kind = NameKind::Field(field); let parent = field.parent_def(db); let container = parent.module(db); let visibility = match parent { VariantDef::Struct(s) => s.source(db).ast.visibility(), VariantDef::EnumVariant(e) => e.source(db).ast.parent_enum().visibility(), }; - NameDefinition { item, container, visibility } + NameDefinition { kind, container, visibility } } pub(super) fn from_module_def( @@ -82,7 +84,7 @@ pub(super) fn from_module_def( def: ModuleDef, module: Option, ) -> NameDefinition { - let item = NameKind::Def(def); + let kind = NameKind::Def(def); let (container, visibility) = match def { ModuleDef::Module(it) => { let container = it.parent(db).or_else(|| Some(it)).unwrap(); @@ -104,5 +106,5 @@ pub(super) fn from_module_def( ModuleDef::Adt(Adt::Enum(it)) => (it.module(db), it.source(db).ast.visibility()), ModuleDef::BuiltinType(..) => (module.unwrap(), None), }; - NameDefinition { item, container, visibility } + NameDefinition { kind, container, visibility } } diff --git a/crates/ra_ide_api/src/references/rename.rs b/crates/ra_ide_api/src/references/rename.rs index 7e564a40e..c91dada46 100644 --- a/crates/ra_ide_api/src/references/rename.rs +++ b/crates/ra_ide_api/src/references/rename.rs @@ -1,3 +1,5 @@ +//! FIXME: write short doc here + use hir::ModuleSource; use ra_db::SourceDatabase; use ra_syntax::{algo::find_node_at_offset, ast, AstNode, SyntaxNode}; diff --git a/crates/ra_ide_api/src/references/search_scope.rs b/crates/ra_ide_api/src/references/search_scope.rs index 346815d31..aae9db13b 100644 --- a/crates/ra_ide_api/src/references/search_scope.rs +++ b/crates/ra_ide_api/src/references/search_scope.rs @@ -1,3 +1,5 @@ +//! FIXME: write short doc here + use hir::{DefWithBody, HasSource, ModuleSource}; use ra_db::{FileId, SourceDatabase}; use ra_syntax::{AstNode, TextRange}; @@ -7,21 +9,21 @@ use crate::db::RootDatabase; use super::{NameDefinition, NameKind}; pub(crate) struct SearchScope { - pub scope: Vec<(FileId, Option)>, + pub files: Vec<(FileId, Option)>, } impl NameDefinition { - pub fn scope(&self, db: &RootDatabase) -> SearchScope { + pub(crate) fn scope(&self, db: &RootDatabase) -> SearchScope { let module_src = self.container.definition_source(db); let file_id = module_src.file_id.original_file(db); - if let NameKind::Pat((def, _)) = self.item { + if let NameKind::Pat((def, _)) = self.kind { let range = match def { DefWithBody::Function(f) => f.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(), }; - return SearchScope { scope: vec![(file_id, Some(range))] }; + return SearchScope { files: vec![(file_id, Some(range))] }; } if let Some(ref vis) = self.visibility { @@ -30,7 +32,7 @@ impl NameDefinition { let mut files = source_root.walk().map(|id| (id.into(), None)).collect::>(); if vis.syntax().to_string().as_str() == "pub(crate)" { - return SearchScope { scope: files }; + return SearchScope { files }; } if vis.syntax().to_string().as_str() == "pub" { let krate = self.container.krate(db).unwrap(); @@ -47,7 +49,7 @@ impl NameDefinition { } } - return SearchScope { scope: files }; + return SearchScope { files }; } // FIXME: "pub(super)", "pub(in path)" } @@ -56,6 +58,6 @@ impl NameDefinition { ModuleSource::Module(m) => Some(m.syntax().text_range()), ModuleSource::SourceFile(_) => None, }; - SearchScope { scope: vec![(file_id, range)] } + SearchScope { files: vec![(file_id, range)] } } } -- cgit v1.2.3 From 328be5721ad0d53d6d296fec08fbcc569634ecf7 Mon Sep 17 00:00:00 2001 From: Ekaterina Babshukova Date: Mon, 14 Oct 2019 15:49:32 +0300 Subject: remove SearchScope --- crates/ra_ide_api/src/references/search_scope.rs | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) (limited to 'crates/ra_ide_api/src/references') diff --git a/crates/ra_ide_api/src/references/search_scope.rs b/crates/ra_ide_api/src/references/search_scope.rs index aae9db13b..dfea18a19 100644 --- a/crates/ra_ide_api/src/references/search_scope.rs +++ b/crates/ra_ide_api/src/references/search_scope.rs @@ -8,12 +8,8 @@ use crate::db::RootDatabase; use super::{NameDefinition, NameKind}; -pub(crate) struct SearchScope { - pub files: Vec<(FileId, Option)>, -} - impl NameDefinition { - pub(crate) fn scope(&self, db: &RootDatabase) -> SearchScope { + pub(crate) fn search_scope(&self, db: &RootDatabase) -> Vec<(FileId, Option)> { let module_src = self.container.definition_source(db); let file_id = module_src.file_id.original_file(db); @@ -23,7 +19,7 @@ impl NameDefinition { DefWithBody::Const(c) => c.source(db).ast.syntax().text_range(), DefWithBody::Static(s) => s.source(db).ast.syntax().text_range(), }; - return SearchScope { files: vec![(file_id, Some(range))] }; + return vec![(file_id, Some(range))]; } if let Some(ref vis) = self.visibility { @@ -32,7 +28,7 @@ impl NameDefinition { let mut files = source_root.walk().map(|id| (id.into(), None)).collect::>(); if vis.syntax().to_string().as_str() == "pub(crate)" { - return SearchScope { files }; + return files; } if vis.syntax().to_string().as_str() == "pub" { let krate = self.container.krate(db).unwrap(); @@ -49,7 +45,7 @@ impl NameDefinition { } } - return SearchScope { files }; + return files; } // FIXME: "pub(super)", "pub(in path)" } @@ -58,6 +54,6 @@ impl NameDefinition { ModuleSource::Module(m) => Some(m.syntax().text_range()), ModuleSource::SourceFile(_) => None, }; - SearchScope { files: vec![(file_id, range)] } + vec![(file_id, range)] } } -- cgit v1.2.3 From 93c179531b31786bfd50644b5f0c879afc798f7d Mon Sep 17 00:00:00 2001 From: Ekaterina Babshukova Date: Tue, 15 Oct 2019 19:25:57 +0300 Subject: fix highlighting --- crates/ra_ide_api/src/references/rename.rs | 2 +- crates/ra_ide_api/src/references/search_scope.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'crates/ra_ide_api/src/references') diff --git a/crates/ra_ide_api/src/references/rename.rs b/crates/ra_ide_api/src/references/rename.rs index c91dada46..0e2e088e0 100644 --- a/crates/ra_ide_api/src/references/rename.rs +++ b/crates/ra_ide_api/src/references/rename.rs @@ -1,7 +1,7 @@ //! FIXME: write short doc here use hir::ModuleSource; -use ra_db::SourceDatabase; +use ra_db::{SourceDatabase, SourceDatabaseExt}; use ra_syntax::{algo::find_node_at_offset, ast, AstNode, SyntaxNode}; use relative_path::{RelativePath, RelativePathBuf}; diff --git a/crates/ra_ide_api/src/references/search_scope.rs b/crates/ra_ide_api/src/references/search_scope.rs index dfea18a19..680988a21 100644 --- a/crates/ra_ide_api/src/references/search_scope.rs +++ b/crates/ra_ide_api/src/references/search_scope.rs @@ -1,7 +1,7 @@ //! FIXME: write short doc here use hir::{DefWithBody, HasSource, ModuleSource}; -use ra_db::{FileId, SourceDatabase}; +use ra_db::{FileId, SourceDatabase, SourceDatabaseExt}; use ra_syntax::{AstNode, TextRange}; use crate::db::RootDatabase; -- cgit v1.2.3 From 55e1910d006da7961687928542c1167cc556a39f Mon Sep 17 00:00:00 2001 From: Ekaterina Babshukova Date: Tue, 15 Oct 2019 22:50:28 +0300 Subject: classify module from declaration --- crates/ra_ide_api/src/references/classify.rs | 13 ++++++++++--- crates/ra_ide_api/src/references/search_scope.rs | 10 ++++++---- 2 files changed, 16 insertions(+), 7 deletions(-) (limited to 'crates/ra_ide_api/src/references') diff --git a/crates/ra_ide_api/src/references/classify.rs b/crates/ra_ide_api/src/references/classify.rs index 93e079ccc..ac9cf34eb 100644 --- a/crates/ra_ide_api/src/references/classify.rs +++ b/crates/ra_ide_api/src/references/classify.rs @@ -31,9 +31,16 @@ pub(crate) fn classify_name( Some(from_struct_field(db, field)) }, ast::Module(it) => { - let ast = hir::ModuleSource::Module(it); - let src = hir::Source { file_id, ast }; - let def = hir::Module::from_definition(db, src)?; + let def = { + if !it.has_semi() { + let ast = hir::ModuleSource::Module(it); + let src = hir::Source { file_id, ast }; + hir::Module::from_definition(db, src) + } else { + let src = hir::Source { file_id, ast: it }; + hir::Module::from_declaration(db, src) + } + }?; Some(from_module_def(db, def.into(), None)) }, ast::StructDef(it) => { diff --git a/crates/ra_ide_api/src/references/search_scope.rs b/crates/ra_ide_api/src/references/search_scope.rs index 680988a21..d2c966b4f 100644 --- a/crates/ra_ide_api/src/references/search_scope.rs +++ b/crates/ra_ide_api/src/references/search_scope.rs @@ -1,5 +1,7 @@ //! FIXME: write short doc here +use std::collections::HashSet; + use hir::{DefWithBody, HasSource, ModuleSource}; use ra_db::{FileId, SourceDatabase, SourceDatabaseExt}; use ra_syntax::{AstNode, TextRange}; @@ -9,7 +11,7 @@ use crate::db::RootDatabase; use super::{NameDefinition, NameKind}; impl NameDefinition { - pub(crate) fn search_scope(&self, db: &RootDatabase) -> Vec<(FileId, Option)> { + pub(crate) fn search_scope(&self, db: &RootDatabase) -> HashSet<(FileId, Option)> { let module_src = self.container.definition_source(db); let file_id = module_src.file_id.original_file(db); @@ -19,13 +21,13 @@ impl NameDefinition { DefWithBody::Const(c) => c.source(db).ast.syntax().text_range(), DefWithBody::Static(s) => s.source(db).ast.syntax().text_range(), }; - return vec![(file_id, Some(range))]; + return [(file_id, Some(range))].iter().cloned().collect(); } if let Some(ref vis) = self.visibility { let source_root_id = db.file_source_root(file_id); let source_root = db.source_root(source_root_id); - let mut files = source_root.walk().map(|id| (id.into(), None)).collect::>(); + let mut files = source_root.walk().map(|id| (id.into(), None)).collect::>(); if vis.syntax().to_string().as_str() == "pub(crate)" { return files; @@ -54,6 +56,6 @@ impl NameDefinition { ModuleSource::Module(m) => Some(m.syntax().text_range()), ModuleSource::SourceFile(_) => None, }; - vec![(file_id, range)] + [(file_id, range)].iter().cloned().collect() } } -- cgit v1.2.3 From b5a3ee93e24931c8bba628ddc7be4f098a19a326 Mon Sep 17 00:00:00 2001 From: Ekaterina Babshukova Date: Wed, 16 Oct 2019 16:49:35 +0300 Subject: support items that visible to the parent module --- crates/ra_ide_api/src/references/classify.rs | 1 + crates/ra_ide_api/src/references/search_scope.rs | 44 ++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 3 deletions(-) (limited to 'crates/ra_ide_api/src/references') diff --git a/crates/ra_ide_api/src/references/classify.rs b/crates/ra_ide_api/src/references/classify.rs index ac9cf34eb..3beab9861 100644 --- a/crates/ra_ide_api/src/references/classify.rs +++ b/crates/ra_ide_api/src/references/classify.rs @@ -152,6 +152,7 @@ pub(crate) fn classify_name_ref( AssocItem(item) => Some(from_assoc_item(db, item)), LocalBinding(Either::A(pat)) => from_pat(db, file_id, pat), LocalBinding(Either::B(par)) => { + // Not really supported let kind = NameKind::SelfParam(par); Some(NameDefinition { kind, container, visibility }) } diff --git a/crates/ra_ide_api/src/references/search_scope.rs b/crates/ra_ide_api/src/references/search_scope.rs index d2c966b4f..8495a92a5 100644 --- a/crates/ra_ide_api/src/references/search_scope.rs +++ b/crates/ra_ide_api/src/references/search_scope.rs @@ -25,14 +25,53 @@ impl NameDefinition { } if let Some(ref vis) = self.visibility { + let vis = vis.syntax().to_string(); + + // FIXME: add "pub(in path)" + + if vis.as_str() == "pub(super)" { + if let Some(parent_module) = self.container.parent(db) { + let mut files = HashSet::new(); + + let parent_src = parent_module.definition_source(db); + let file_id = parent_src.file_id.original_file(db); + + match parent_src.ast { + ModuleSource::Module(m) => { + let range = Some(m.syntax().text_range()); + files.insert((file_id, range)); + } + ModuleSource::SourceFile(_) => { + files.insert((file_id, None)); + files.extend( + parent_module + .children(db) + .map(|m| { + let src = m.definition_source(db); + (src.file_id.original_file(db), None) + }) + .collect::>(), + ); + } + } + return files; + } else { + let range = match module_src.ast { + ModuleSource::Module(m) => Some(m.syntax().text_range()), + ModuleSource::SourceFile(_) => None, + }; + return [(file_id, range)].iter().cloned().collect(); + } + } + let source_root_id = db.file_source_root(file_id); let source_root = db.source_root(source_root_id); let mut files = source_root.walk().map(|id| (id.into(), None)).collect::>(); - if vis.syntax().to_string().as_str() == "pub(crate)" { + if vis.as_str() == "pub(crate)" { return files; } - if vis.syntax().to_string().as_str() == "pub" { + if vis.as_str() == "pub" { let krate = self.container.krate(db).unwrap(); let crate_graph = db.crate_graph(); @@ -49,7 +88,6 @@ impl NameDefinition { return files; } - // FIXME: "pub(super)", "pub(in path)" } let range = match module_src.ast { -- cgit v1.2.3 From decfd28bd14b56befa17257694caacc57a090939 Mon Sep 17 00:00:00 2001 From: Ekaterina Babshukova Date: Tue, 22 Oct 2019 23:46:53 +0300 Subject: some fixes, add docs --- crates/ra_ide_api/src/references/classify.rs | 8 ++- .../ra_ide_api/src/references/name_definition.rs | 5 +- crates/ra_ide_api/src/references/search_scope.rs | 81 ++++++++++------------ 3 files changed, 47 insertions(+), 47 deletions(-) (limited to 'crates/ra_ide_api/src/references') diff --git a/crates/ra_ide_api/src/references/classify.rs b/crates/ra_ide_api/src/references/classify.rs index 3beab9861..c8daff9b1 100644 --- a/crates/ra_ide_api/src/references/classify.rs +++ b/crates/ra_ide_api/src/references/classify.rs @@ -1,8 +1,9 @@ -//! FIXME: write short doc here +//! Functions that are used to classify an element from its definition or reference. use hir::{Either, FromSource, Module, ModuleSource, Path, PathResolution, Source, SourceAnalyzer}; use ra_db::FileId; use ra_syntax::{ast, match_ast, AstNode, AstPtr}; +use test_utils::tested_by; use super::{ name_definition::{from_assoc_item, from_module_def, from_pat, from_struct_field}, @@ -111,18 +112,21 @@ pub(crate) fn classify_name_ref( let analyzer = SourceAnalyzer::new(db, file_id, name_ref.syntax(), None); if let Some(method_call) = ast::MethodCallExpr::cast(parent.clone()) { + tested_by!(goto_definition_works_for_methods); if let Some(func) = analyzer.resolve_method_call(&method_call) { return Some(from_assoc_item(db, func.into())); } } if let Some(field_expr) = ast::FieldExpr::cast(parent.clone()) { + tested_by!(goto_definition_works_for_fields); if let Some(field) = analyzer.resolve_field(&field_expr) { return Some(from_struct_field(db, field)); } } if let Some(record_field) = ast::RecordField::cast(parent.clone()) { + tested_by!(goto_definition_works_for_record_fields); if let Some(record_lit) = record_field.syntax().ancestors().find_map(ast::RecordLit::cast) { let variant_def = analyzer.resolve_record_literal(&record_lit)?; let hir_path = Path::from_name_ref(name_ref); @@ -139,6 +143,7 @@ pub(crate) fn classify_name_ref( let visibility = None; if let Some(macro_call) = parent.ancestors().find_map(ast::MacroCall::cast) { + tested_by!(goto_definition_works_for_macros); if let Some(macro_def) = analyzer.resolve_macro_call(db, ¯o_call) { let kind = NameKind::Macro(macro_def); return Some(NameDefinition { kind, container, visibility }); @@ -152,7 +157,6 @@ pub(crate) fn classify_name_ref( AssocItem(item) => Some(from_assoc_item(db, item)), LocalBinding(Either::A(pat)) => from_pat(db, file_id, pat), LocalBinding(Either::B(par)) => { - // Not really supported let kind = NameKind::SelfParam(par); Some(NameDefinition { kind, container, visibility }) } diff --git a/crates/ra_ide_api/src/references/name_definition.rs b/crates/ra_ide_api/src/references/name_definition.rs index 723d97237..4580bc789 100644 --- a/crates/ra_ide_api/src/references/name_definition.rs +++ b/crates/ra_ide_api/src/references/name_definition.rs @@ -1,4 +1,7 @@ -//! FIXME: write short doc here +//! `NameDefinition` keeps information about the element we want to search references for. +//! The element is represented by `NameKind`. It's located inside some `container` and +//! has a `visibility`, which defines a search scope. +//! Note that the reference search is possible for not all of the classified items. use hir::{ db::AstDatabase, Adt, AssocItem, DefWithBody, FromSource, HasSource, HirFileId, MacroDef, diff --git a/crates/ra_ide_api/src/references/search_scope.rs b/crates/ra_ide_api/src/references/search_scope.rs index 8495a92a5..5cb69b8fc 100644 --- a/crates/ra_ide_api/src/references/search_scope.rs +++ b/crates/ra_ide_api/src/references/search_scope.rs @@ -1,72 +1,66 @@ -//! FIXME: write short doc here - -use std::collections::HashSet; +//! Generally, `search_scope` returns files that might contain references for the element. +//! For `pub(crate)` things it's a crate, for `pub` things it's a crate and dependant crates. +//! In some cases, the location of the references is known to within a `TextRange`, +//! e.g. for things like local variables. use hir::{DefWithBody, HasSource, ModuleSource}; use ra_db::{FileId, SourceDatabase, SourceDatabaseExt}; use ra_syntax::{AstNode, TextRange}; +use rustc_hash::FxHashSet; use crate::db::RootDatabase; use super::{NameDefinition, NameKind}; impl NameDefinition { - pub(crate) fn search_scope(&self, db: &RootDatabase) -> HashSet<(FileId, Option)> { + pub(crate) fn search_scope(&self, db: &RootDatabase) -> FxHashSet<(FileId, Option)> { let module_src = self.container.definition_source(db); let file_id = module_src.file_id.original_file(db); if let NameKind::Pat((def, _)) = self.kind { + let mut res = FxHashSet::default(); let range = match def { DefWithBody::Function(f) => f.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(), }; - return [(file_id, Some(range))].iter().cloned().collect(); + res.insert((file_id, Some(range))); + return res; } - if let Some(ref vis) = self.visibility { - let vis = vis.syntax().to_string(); - - // FIXME: add "pub(in path)" - - if vis.as_str() == "pub(super)" { - if let Some(parent_module) = self.container.parent(db) { - let mut files = HashSet::new(); + let vis = + self.visibility.as_ref().map(|v| v.syntax().to_string()).unwrap_or("".to_string()); - let parent_src = parent_module.definition_source(db); - let file_id = parent_src.file_id.original_file(db); + if vis.as_str() == "pub(super)" { + if let Some(parent_module) = self.container.parent(db) { + let mut files = FxHashSet::default(); + let parent_src = parent_module.definition_source(db); + let file_id = parent_src.file_id.original_file(db); - match parent_src.ast { - ModuleSource::Module(m) => { - let range = Some(m.syntax().text_range()); - files.insert((file_id, range)); - } - ModuleSource::SourceFile(_) => { - files.insert((file_id, None)); - files.extend( - parent_module - .children(db) - .map(|m| { - let src = m.definition_source(db); - (src.file_id.original_file(db), None) - }) - .collect::>(), - ); - } + match parent_src.ast { + ModuleSource::Module(m) => { + let range = Some(m.syntax().text_range()); + files.insert((file_id, range)); + } + ModuleSource::SourceFile(_) => { + files.insert((file_id, None)); + files.extend(parent_module.children(db).map(|m| { + let src = m.definition_source(db); + (src.file_id.original_file(db), None) + })); } - return files; - } else { - let range = match module_src.ast { - ModuleSource::Module(m) => Some(m.syntax().text_range()), - ModuleSource::SourceFile(_) => None, - }; - return [(file_id, range)].iter().cloned().collect(); } + return files; } + } + if vis.as_str() != "" { let source_root_id = db.file_source_root(file_id); let source_root = db.source_root(source_root_id); - let mut files = source_root.walk().map(|id| (id.into(), None)).collect::>(); + let mut files = + source_root.walk().map(|id| (id.into(), None)).collect::>(); + + // FIXME: add "pub(in path)" if vis.as_str() == "pub(crate)" { return files; @@ -74,10 +68,8 @@ impl NameDefinition { if vis.as_str() == "pub" { let krate = self.container.krate(db).unwrap(); let crate_graph = db.crate_graph(); - for crate_id in crate_graph.iter() { let mut crate_deps = crate_graph.dependencies(crate_id); - if crate_deps.any(|dep| dep.crate_id() == krate.crate_id()) { let root_file = crate_graph.crate_root(crate_id); let source_root_id = db.file_source_root(root_file); @@ -85,15 +77,16 @@ impl NameDefinition { files.extend(source_root.walk().map(|id| (id.into(), None))); } } - return files; } } + let mut res = FxHashSet::default(); let range = match module_src.ast { ModuleSource::Module(m) => Some(m.syntax().text_range()), ModuleSource::SourceFile(_) => None, }; - [(file_id, range)].iter().cloned().collect() + res.insert((file_id, range)); + res } } -- cgit v1.2.3