From 38853459e3d964cc7f635829cdc66f5faee33d85 Mon Sep 17 00:00:00 2001 From: ice1000 Date: Tue, 3 Dec 2019 15:24:02 -0500 Subject: Add `ModuleSource::Block` --- crates/ra_hir/src/code_model.rs | 65 +++---------------------------------- crates/ra_hir/src/code_model/src.rs | 3 +- crates/ra_hir/src/from_source.rs | 8 ++--- crates/ra_hir/src/lib.rs | 5 +-- crates/ra_hir/src/source_binder.rs | 3 +- 5 files changed, 15 insertions(+), 69 deletions(-) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 5877afefa..7706399ae 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -11,6 +11,7 @@ use hir_def::{ builtin_type::BuiltinType, docs::Documentation, expr::{BindingAnnotation, Pat, PatId}, + nameres::ModuleSource, per_ns::PerNs, resolver::HasResolver, type_ref::{Mutability, TypeRef}, @@ -21,11 +22,11 @@ use hir_def::{ use hir_expand::{ diagnostics::DiagnosticSink, name::{self, AsName}, - AstId, MacroDefId, + MacroDefId, }; use hir_ty::expr::ExprValidator; -use ra_db::{CrateId, Edition, FileId, FilePosition}; -use ra_syntax::{ast, AstNode, SyntaxNode}; +use ra_db::{CrateId, Edition}; +use ra_syntax::ast; use crate::{ db::{DefDatabase, HirDatabase}, @@ -79,64 +80,6 @@ impl Crate { } } -pub enum ModuleSource { - SourceFile(ast::SourceFile), - Module(ast::Module), -} - -impl ModuleSource { - pub fn new( - db: &impl DefDatabase, - file_id: Option, - decl_id: Option>, - ) -> ModuleSource { - match (file_id, decl_id) { - (Some(file_id), _) => { - let source_file = db.parse(file_id).tree(); - ModuleSource::SourceFile(source_file) - } - (None, Some(item_id)) => { - let module = item_id.to_node(db); - assert!(module.item_list().is_some(), "expected inline module"); - ModuleSource::Module(module) - } - (None, None) => panic!(), - } - } - - // FIXME: this methods do not belong here - pub fn from_position(db: &impl DefDatabase, position: FilePosition) -> ModuleSource { - let parse = db.parse(position.file_id); - match &ra_syntax::algo::find_node_at_offset::( - parse.tree().syntax(), - position.offset, - ) { - Some(m) if !m.has_semi() => ModuleSource::Module(m.clone()), - _ => { - let source_file = parse.tree(); - ModuleSource::SourceFile(source_file) - } - } - } - - pub fn from_child_node(db: &impl DefDatabase, child: InFile<&SyntaxNode>) -> ModuleSource { - if let Some(m) = - child.value.ancestors().filter_map(ast::Module::cast).find(|it| !it.has_semi()) - { - ModuleSource::Module(m) - } else { - let file_id = child.file_id.original_file(db); - let source_file = db.parse(file_id).tree(); - ModuleSource::SourceFile(source_file) - } - } - - pub fn from_file_id(db: &impl DefDatabase, file_id: FileId) -> ModuleSource { - let source_file = db.parse(file_id).tree(); - ModuleSource::SourceFile(source_file) - } -} - #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct Module { pub(crate) id: ModuleId, diff --git a/crates/ra_hir/src/code_model/src.rs b/crates/ra_hir/src/code_model/src.rs index 36cfbc8f1..083946729 100644 --- a/crates/ra_hir/src/code_model/src.rs +++ b/crates/ra_hir/src/code_model/src.rs @@ -2,6 +2,7 @@ use either::Either; use hir_def::{ + nameres::ModuleSource, src::{HasChildSource, HasSource as _}, AstItemDef, Lookup, VariantId, }; @@ -9,7 +10,7 @@ use ra_syntax::ast; use crate::{ db::DefDatabase, Const, Enum, EnumVariant, FieldSource, Function, ImplBlock, Import, MacroDef, - Module, ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union, + Module, Static, Struct, StructField, Trait, TypeAlias, Union, }; pub use hir_expand::InFile; diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs index 82bf641dc..94a5e0b0d 100644 --- a/crates/ra_hir/src/from_source.rs +++ b/crates/ra_hir/src/from_source.rs @@ -1,6 +1,6 @@ //! FIXME: write short doc here -use hir_def::{AstItemDef, LocationCtx, ModuleId}; +use hir_def::{nameres::ModuleSource, AstItemDef, LocationCtx, ModuleId}; use hir_expand::{name::AsName, AstId, MacroDefId, MacroDefKind}; use ra_syntax::{ ast::{self, AstNode, NameOwner}, @@ -10,8 +10,8 @@ use ra_syntax::{ use crate::{ db::{AstDatabase, DefDatabase, HirDatabase}, AssocItem, Const, DefWithBody, Enum, EnumVariant, FieldSource, Function, HasSource, ImplBlock, - InFile, Local, MacroDef, Module, ModuleDef, ModuleSource, Static, Struct, StructField, Trait, - TypeAlias, Union, VariantDef, + InFile, Local, MacroDef, Module, ModuleDef, Static, Struct, StructField, Trait, TypeAlias, + Union, VariantDef, }; pub trait FromSource: Sized { @@ -257,7 +257,7 @@ impl Module { InFile { file_id: src.file_id, value: module.clone() }, ); } - ModuleSource::SourceFile(_) => (), + ModuleSource::SourceFile(_) | ModuleSource::Block(_) => (), }; let original_file = src.file_id.original_file(db); diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 853760cb1..f12e4ca3f 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -43,8 +43,8 @@ pub use crate::{ code_model::{ src::HasSource, Adt, AssocItem, AttrDef, Const, Container, Crate, CrateDependency, DefWithBody, Docs, Enum, EnumVariant, FieldSource, Function, GenericDef, GenericParam, - HasAttrs, ImplBlock, Import, Local, MacroDef, Module, ModuleDef, ModuleSource, ScopeDef, - Static, Struct, StructField, Trait, Type, TypeAlias, Union, VariantDef, + HasAttrs, ImplBlock, Import, Local, MacroDef, Module, ModuleDef, ScopeDef, Static, Struct, + StructField, Trait, Type, TypeAlias, Union, VariantDef, }, from_source::FromSource, source_binder::{PathResolution, ScopeEntryWithSyntax, SourceAnalyzer}, @@ -59,6 +59,7 @@ pub use hir_def::{ body::scope::ExprScopes, builtin_type::BuiltinType, docs::Documentation, + nameres::ModuleSource, path::{Path, PathKind}, type_ref::Mutability, }; diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 28d41b647..db0451059 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -14,6 +14,7 @@ use hir_def::{ BodySourceMap, }, expr::{ExprId, PatId}, + nameres::ModuleSource, path::known, resolver::{self, resolver_for_scope, HasResolver, Resolver, TypeNs, ValueNs}, AssocItemId, DefWithBodyId, @@ -46,7 +47,7 @@ fn try_get_resolver_for_node(db: &impl HirDatabase, node: InFile<&SyntaxNode>) - Some(crate::Module::from_declaration(db, src)?.id.resolver(db)) }, ast::SourceFile(it) => { - let src = node.with_value(crate::ModuleSource::SourceFile(it)); + let src = node.with_value(ModuleSource::SourceFile(it)); Some(crate::Module::from_definition(db, src)?.id.resolver(db)) }, ast::StructDef(it) => { -- cgit v1.2.3 From 5c5f90ba57c83499a44af33bf8b91b24254fb685 Mon Sep 17 00:00:00 2001 From: ice1000 Date: Tue, 3 Dec 2019 15:28:40 -0500 Subject: Confluent `ModuleSource` usage --- crates/ra_hir/src/code_model/src.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/code_model/src.rs b/crates/ra_hir/src/code_model/src.rs index 083946729..d9bccd902 100644 --- a/crates/ra_hir/src/code_model/src.rs +++ b/crates/ra_hir/src/code_model/src.rs @@ -26,11 +26,7 @@ impl Module { /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items. pub fn definition_source(self, db: &impl DefDatabase) -> InFile { let def_map = db.crate_def_map(self.id.krate); - let src = def_map[self.id.local_id].definition_source(db); - src.map(|it| match it { - Either::Left(it) => ModuleSource::SourceFile(it), - Either::Right(it) => ModuleSource::Module(it), - }) + def_map[self.id.local_id].definition_source(db) } /// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`. -- cgit v1.2.3 From 032eb3d68e07f087ac531d48f956a79948baa4b9 Mon Sep 17 00:00:00 2001 From: ice1000 Date: Tue, 3 Dec 2019 15:58:38 -0500 Subject: Remove almost unused `ModuleSource::new` --- crates/ra_hir/src/from_source.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs index 94a5e0b0d..67b127ba5 100644 --- a/crates/ra_hir/src/from_source.rs +++ b/crates/ra_hir/src/from_source.rs @@ -238,7 +238,7 @@ impl Module { _ => { let src_parent = InFile { file_id: src.file_id, - value: ModuleSource::new(db, Some(src.file_id.original_file(db)), None), + value: ModuleSource::from_file_id(db, src.file_id.original_file(db)), }; Module::from_definition(db, src_parent) } -- cgit v1.2.3 From 088f50c0ab351d5ac072547a47c1ce7eeae029f3 Mon Sep 17 00:00:00 2001 From: ice1000 Date: Wed, 4 Dec 2019 13:35:24 -0500 Subject: No block at the moment --- crates/ra_hir/src/from_source.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs index 67b127ba5..a46bba4f2 100644 --- a/crates/ra_hir/src/from_source.rs +++ b/crates/ra_hir/src/from_source.rs @@ -257,7 +257,7 @@ impl Module { InFile { file_id: src.file_id, value: module.clone() }, ); } - ModuleSource::SourceFile(_) | ModuleSource::Block(_) => (), + ModuleSource::SourceFile(_) => (), }; let original_file = src.file_id.original_file(db); -- cgit v1.2.3 From 7702f690a9592605be71104ec9d0b732af940fcc Mon Sep 17 00:00:00 2001 From: ice1000 Date: Thu, 5 Dec 2019 08:28:31 -0500 Subject: One pub function less is good! --- crates/ra_hir/src/from_source.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs index a46bba4f2..18d87f6d7 100644 --- a/crates/ra_hir/src/from_source.rs +++ b/crates/ra_hir/src/from_source.rs @@ -235,11 +235,10 @@ impl Module { let src_parent = InFile { file_id: src.file_id, value: parent_declaration }; Module::from_declaration(db, src_parent) } - _ => { - let src_parent = InFile { - file_id: src.file_id, - value: ModuleSource::from_file_id(db, src.file_id.original_file(db)), - }; + None => { + let source_file = db.parse(src.file_id.original_file(db)).tree(); + let src_parent = + InFile { file_id: src.file_id, value: ModuleSource::SourceFile(source_file) }; Module::from_definition(db, src_parent) } }?; -- cgit v1.2.3