From 2867c40925e8f7b440ff50a421a2d3726b9ff334 Mon Sep 17 00:00:00 2001 From: Ekaterina Babshukova Date: Mon, 16 Sep 2019 13:48:54 +0300 Subject: introduce FromSource trait --- crates/ra_hir/src/code_model.rs | 2 +- crates/ra_hir/src/from_source.rs | 211 +++++++++++++++++++++++++ crates/ra_hir/src/lib.rs | 3 + crates/ra_hir/src/mock.rs | 6 +- crates/ra_hir/src/nameres/tests/incremental.rs | 12 +- crates/ra_hir/src/source_binder.rs | 124 ++------------- 6 files changed, 247 insertions(+), 111 deletions(-) create mode 100644 crates/ra_hir/src/from_source.rs (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 706d24c32..28002f740 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -313,7 +313,7 @@ pub struct StructField { pub(crate) id: StructFieldId, } -#[derive(Debug)] +#[derive(Debug, PartialEq, Eq)] pub enum FieldSource { Named(ast::RecordFieldDef), Pos(ast::TupleFieldDef), diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs new file mode 100644 index 000000000..c6ad85fc7 --- /dev/null +++ b/crates/ra_hir/src/from_source.rs @@ -0,0 +1,211 @@ +use ra_db::{FileId, FilePosition}; +use ra_syntax::{ + algo::find_node_at_offset, + ast::{self, AstNode, NameOwner}, + SyntaxNode, +}; + +use crate::{ + db::{AstDatabase, DefDatabase, HirDatabase}, + ids::{AstItemDef, LocationCtx}, + name::AsName, + Const, Crate, Enum, EnumVariant, FieldSource, Function, HasSource, ImplBlock, Module, + ModuleSource, Source, Static, Struct, StructField, Trait, TypeAlias, Union, VariantDef, +}; + +pub trait FromSource: Sized { + type Ast; + fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source) -> Option; +} + +impl FromSource for Struct { + type Ast = ast::StructDef; + fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source) -> Option { + let id = from_source(db, src)?; + Some(Struct { id }) + } +} +impl FromSource for Union { + type Ast = ast::StructDef; + fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source) -> Option { + let id = from_source(db, src)?; + Some(Union { id }) + } +} +impl FromSource for Enum { + type Ast = ast::EnumDef; + fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source) -> Option { + let id = from_source(db, src)?; + Some(Enum { id }) + } +} +impl FromSource for Trait { + type Ast = ast::TraitDef; + fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source) -> Option { + let id = from_source(db, src)?; + Some(Trait { id }) + } +} +impl FromSource for Function { + type Ast = ast::FnDef; + fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source) -> Option { + let id = from_source(db, src)?; + Some(Function { id }) + } +} +impl FromSource for Const { + type Ast = ast::ConstDef; + fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source) -> Option { + let id = from_source(db, src)?; + Some(Const { id }) + } +} +impl FromSource for Static { + type Ast = ast::StaticDef; + fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source) -> Option { + let id = from_source(db, src)?; + Some(Static { id }) + } +} +impl FromSource for TypeAlias { + type Ast = ast::TypeAliasDef; + fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source) -> Option { + let id = from_source(db, src)?; + Some(TypeAlias { id }) + } +} +// FIXME: add impl FromSource for MacroDef + +impl FromSource for ImplBlock { + type Ast = ast::ImplBlock; + fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source) -> Option { + let module_src = crate::ModuleSource::from_child_node( + db, + src.file_id.original_file(db), + &src.ast.syntax(), + ); + let module = Module::from_definition(db, Source { file_id: src.file_id, ast: module_src })?; + let impls = module.impl_blocks(db); + impls.into_iter().find(|b| b.source(db) == src) + } +} + +impl FromSource for EnumVariant { + type Ast = ast::EnumVariant; + fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source) -> Option { + let parent_enum = src.ast.parent_enum(); + let src_enum = Source { file_id: src.file_id, ast: parent_enum }; + let variants = Enum::from_source(db, src_enum)?.variants(db); + variants.into_iter().find(|v| v.source(db) == src) + } +} + +impl FromSource for StructField { + type Ast = FieldSource; + fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source) -> Option { + let variant_def: VariantDef = match src.ast { + FieldSource::Named(ref field) => { + let ast = field.syntax().ancestors().find_map(ast::StructDef::cast)?; + let src = Source { file_id: src.file_id, ast }; + let def = Struct::from_source(db, src)?; + VariantDef::from(def) + } + FieldSource::Pos(ref field) => { + let ast = field.syntax().ancestors().find_map(ast::EnumVariant::cast)?; + let src = Source { file_id: src.file_id, ast }; + let def = EnumVariant::from_source(db, src)?; + VariantDef::from(def) + } + }; + variant_def + .variant_data(db) + .fields() + .into_iter() + .flat_map(|it| it.iter()) + .map(|(id, _)| StructField { parent: variant_def.clone(), id }) + .find(|f| f.source(db) == src) + } +} + +// FIXME: simplify it +impl ModuleSource { + pub fn from_position( + db: &(impl DefDatabase + AstDatabase), + position: FilePosition, + ) -> ModuleSource { + let parse = db.parse(position.file_id); + match &find_node_at_offset::(parse.tree().syntax(), position.offset) { + Some(m) if !m.has_semi() => ModuleSource::Module(m.clone()), + _ => { + let source_file = parse.tree().to_owned(); + ModuleSource::SourceFile(source_file) + } + } + } + + pub fn from_child_node( + db: &(impl DefDatabase + AstDatabase), + file_id: FileId, + child: &SyntaxNode, + ) -> ModuleSource { + if let Some(m) = child.ancestors().filter_map(ast::Module::cast).find(|it| !it.has_semi()) { + ModuleSource::Module(m.clone()) + } else { + let source_file = db.parse(file_id).tree().to_owned(); + ModuleSource::SourceFile(source_file) + } + } + + pub fn from_file_id(db: &(impl DefDatabase + AstDatabase), file_id: FileId) -> ModuleSource { + let source_file = db.parse(file_id).tree().to_owned(); + ModuleSource::SourceFile(source_file) + } +} + +impl Module { + pub fn from_declaration(db: &impl HirDatabase, src: Source) -> Option { + let src_parent = Source { + file_id: src.file_id, + ast: ModuleSource::new(db, Some(src.file_id.original_file(db)), None), + }; + let parent_module = Module::from_definition(db, src_parent)?; + let child_name = src.ast.name()?; + parent_module.child(db, &child_name.as_name()) + } + + pub fn from_definition( + db: &(impl DefDatabase + AstDatabase), + src: Source, + ) -> Option { + let decl_id = match src.ast { + ModuleSource::Module(ref module) => { + assert!(!module.has_semi()); + let ast_id_map = db.ast_id_map(src.file_id); + let item_id = ast_id_map.ast_id(module).with_file_id(src.file_id); + Some(item_id) + } + ModuleSource::SourceFile(_) => None, + }; + + let source_root_id = db.file_source_root(src.file_id.original_file(db)); + db.source_root_crates(source_root_id).iter().map(|&crate_id| Crate { crate_id }).find_map( + |krate| { + let def_map = db.crate_def_map(krate); + let module_id = def_map.find_module_by_source(src.file_id, decl_id)?; + Some(Module { krate, module_id }) + }, + ) + } +} + +fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source) -> Option +where + N: AstNode, + DEF: AstItemDef, +{ + let module_src = + crate::ModuleSource::from_child_node(db, src.file_id.original_file(db), &src.ast.syntax()); + let module = Module::from_definition(db, Source { file_id: src.file_id, ast: module_src })?; + let ctx = LocationCtx::new(db, module, src.file_id); + Some(DEF::from_ast(ctx, &src.ast)) +} diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index e7a576aa0..a9de9fb6b 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -53,6 +53,8 @@ pub mod diagnostics; mod code_model; +pub mod from_source; + #[cfg(test)] mod marks; @@ -67,6 +69,7 @@ pub use self::{ adt::VariantDef, either::Either, expr::ExprScopes, + from_source::FromSource, generics::{GenericParam, GenericParams, HasGenericParams}, ids::{HirFileId, MacroCallId, MacroCallLoc, MacroDefId, MacroFile}, impl_block::ImplBlock, diff --git a/crates/ra_hir/src/mock.rs b/crates/ra_hir/src/mock.rs index 8dcea5071..cb405091e 100644 --- a/crates/ra_hir/src/mock.rs +++ b/crates/ra_hir/src/mock.rs @@ -93,7 +93,11 @@ impl MockDatabase { let mut files: Vec = self.files.values().copied().collect(); files.sort(); for file in files { - let module = crate::source_binder::module_from_file_id(self, file).unwrap(); + let src = crate::Source { + file_id: file.into(), + ast: crate::ModuleSource::new(self, Some(file), None), + }; + let module = crate::Module::from_definition(self, src).unwrap(); module.diagnostics( self, &mut DiagnosticSink::new(|d| { diff --git a/crates/ra_hir/src/nameres/tests/incremental.rs b/crates/ra_hir/src/nameres/tests/incremental.rs index bc721f6e0..c41862a0b 100644 --- a/crates/ra_hir/src/nameres/tests/incremental.rs +++ b/crates/ra_hir/src/nameres/tests/incremental.rs @@ -114,7 +114,11 @@ fn typing_inside_a_macro_should_not_invalidate_def_map() { ); { let events = db.log_executed(|| { - let module = crate::source_binder::module_from_file_id(&db, pos.file_id).unwrap(); + let src = crate::Source { + file_id: pos.file_id.into(), + ast: crate::ModuleSource::new(&db, Some(pos.file_id), None), + }; + let module = crate::Module::from_definition(&db, src).unwrap(); let decls = module.declarations(&db); assert_eq!(decls.len(), 18); }); @@ -124,7 +128,11 @@ fn typing_inside_a_macro_should_not_invalidate_def_map() { { let events = db.log_executed(|| { - let module = crate::source_binder::module_from_file_id(&db, pos.file_id).unwrap(); + let src = crate::Source { + file_id: pos.file_id.into(), + ast: crate::ModuleSource::new(&db, Some(pos.file_id), None), + }; + let module = crate::Module::from_definition(&db, src).unwrap(); let decls = module.declarations(&db); assert_eq!(decls.len(), 18); }); diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 4d895f0a1..296acc364 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -7,10 +7,9 @@ /// purely for "IDE needs". use std::sync::Arc; -use ra_db::{FileId, FilePosition}; +use ra_db::FileId; use ra_syntax::{ - algo::find_node_at_offset, - ast::{self, AstNode, NameOwner}, + ast::{self, AstNode}, AstPtr, SyntaxKind::*, SyntaxNode, SyntaxNodePtr, TextRange, TextUnit, @@ -28,119 +27,28 @@ use crate::{ path::known, resolve::{ScopeDef, TypeNs, ValueNs}, ty::method_resolution::implements_trait, - AsName, AstId, Const, Crate, DefWithBody, Either, Enum, Function, HasBody, HirFileId, MacroDef, - Module, Name, Path, Resolver, Static, Struct, Trait, Ty, + AsName, Const, DefWithBody, Either, Enum, FromSource, Function, HasBody, HirFileId, MacroDef, + Module, Name, Path, Resolver, Static, Struct, Ty, }; -/// Locates the module by `FileId`. Picks topmost module in the file. -pub fn module_from_file_id(db: &impl HirDatabase, file_id: FileId) -> Option { - module_from_source(db, file_id.into(), None) -} - -/// Locates the child module by `mod child;` declaration. -pub fn module_from_declaration( - db: &impl HirDatabase, - file_id: FileId, - decl: ast::Module, -) -> Option { - let parent_module = module_from_file_id(db, file_id); - let child_name = decl.name(); - match (parent_module, child_name) { - (Some(parent_module), Some(child_name)) => parent_module.child(db, &child_name.as_name()), - _ => None, - } -} - -/// Locates the module by position in the source code. -pub fn module_from_position(db: &impl HirDatabase, position: FilePosition) -> Option { - let parse = db.parse(position.file_id); - match &find_node_at_offset::(parse.tree().syntax(), position.offset) { - Some(m) if !m.has_semi() => module_from_inline(db, position.file_id, m.clone()), - _ => module_from_file_id(db, position.file_id), - } -} - -fn module_from_inline( - db: &impl HirDatabase, - file_id: FileId, - module: ast::Module, -) -> Option { - assert!(!module.has_semi()); - let file_id = file_id.into(); - let ast_id_map = db.ast_id_map(file_id); - let item_id = ast_id_map.ast_id(&module).with_file_id(file_id); - module_from_source(db, file_id, Some(item_id)) -} - -/// Locates the module by child syntax element within the module -pub fn module_from_child_node( - db: &impl HirDatabase, - file_id: FileId, - child: &SyntaxNode, -) -> Option { - if let Some(m) = child.ancestors().filter_map(ast::Module::cast).find(|it| !it.has_semi()) { - module_from_inline(db, file_id, m) - } else { - module_from_file_id(db, file_id) - } -} - -fn module_from_source( - db: &impl HirDatabase, - file_id: HirFileId, - decl_id: Option>, -) -> Option { - let source_root_id = db.file_source_root(file_id.as_original_file()); - db.source_root_crates(source_root_id).iter().map(|&crate_id| Crate { crate_id }).find_map( - |krate| { - let def_map = db.crate_def_map(krate); - let module_id = def_map.find_module_by_source(file_id, decl_id)?; - Some(Module { krate, module_id }) - }, - ) -} - -pub fn struct_from_module( - db: &impl HirDatabase, - module: Module, - struct_def: &ast::StructDef, -) -> Struct { - let file_id = module.definition_source(db).file_id; - let ctx = LocationCtx::new(db, module, file_id); - Struct { id: ctx.to_def(struct_def) } -} - -pub fn enum_from_module(db: &impl HirDatabase, module: Module, enum_def: &ast::EnumDef) -> Enum { - let file_id = module.definition_source(db).file_id; - let ctx = LocationCtx::new(db, module, file_id); - Enum { id: ctx.to_def(enum_def) } -} - -pub fn trait_from_module( - db: &impl HirDatabase, - module: Module, - trait_def: &ast::TraitDef, -) -> Trait { - let file_id = module.definition_source(db).file_id; - let ctx = LocationCtx::new(db, module, file_id); - Trait { id: ctx.to_def(trait_def) } -} - fn try_get_resolver_for_node( db: &impl HirDatabase, file_id: FileId, node: &SyntaxNode, ) -> Option { if let Some(module) = ast::Module::cast(node.clone()) { - Some(module_from_declaration(db, file_id, module)?.resolver(db)) - } else if let Some(_) = ast::SourceFile::cast(node.clone()) { - Some(module_from_source(db, file_id.into(), None)?.resolver(db)) + let src = crate::Source { file_id: file_id.into(), ast: module }; + Some(crate::Module::from_declaration(db, src)?.resolver(db)) + } else if let Some(file) = ast::SourceFile::cast(node.clone()) { + let src = + crate::Source { file_id: file_id.into(), ast: crate::ModuleSource::SourceFile(file) }; + Some(crate::Module::from_definition(db, src)?.resolver(db)) } else if let Some(s) = ast::StructDef::cast(node.clone()) { - let module = module_from_child_node(db, file_id, s.syntax())?; - Some(struct_from_module(db, module, &s).resolver(db)) + let src = crate::Source { file_id: file_id.into(), ast: s }; + Some(Struct::from_source(db, src)?.resolver(db)) } else if let Some(e) = ast::EnumDef::cast(node.clone()) { - let module = module_from_child_node(db, file_id, e.syntax())?; - Some(enum_from_module(db, module, &e).resolver(db)) + let src = crate::Source { file_id: file_id.into(), ast: e }; + Some(Enum::from_source(db, src)?.resolver(db)) } else if node.kind() == FN_DEF || node.kind() == CONST_DEF || node.kind() == STATIC_DEF { Some(def_with_body_from_child_node(db, file_id, node)?.resolver(db)) } else { @@ -154,8 +62,10 @@ fn def_with_body_from_child_node( file_id: FileId, node: &SyntaxNode, ) -> Option { - let module = module_from_child_node(db, file_id, node)?; + let src = crate::ModuleSource::from_child_node(db, file_id, node); + let module = Module::from_definition(db, crate::Source { file_id: file_id.into(), ast: src })?; let ctx = LocationCtx::new(db, module, file_id.into()); + node.ancestors().find_map(|node| { if let Some(def) = ast::FnDef::cast(node.clone()) { return Some(Function { id: ctx.to_def(&def) }.into()); -- cgit v1.2.3