diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-12-05 13:38:39 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2019-12-05 13:38:39 +0000 |
commit | 4c0bd068da39e74c66104206e27c270454e3562e (patch) | |
tree | 25e6c7005e04c3a63344f23a003614409cfda4c9 /crates/ra_hir/src | |
parent | ace661bb10dbfd596a444c235f80fa43a2845239 (diff) | |
parent | d15f300268fa955f8e9c04d51ca1dc528766f742 (diff) |
Merge #2474
2474: Move `ModuleSource`, create `ModuleOrigin` r=matklad a=ice1000
As title. This comes right after #2473
Co-authored-by: ice1000 <[email protected]>
Diffstat (limited to 'crates/ra_hir/src')
-rw-r--r-- | crates/ra_hir/src/code_model.rs | 65 | ||||
-rw-r--r-- | crates/ra_hir/src/code_model/src.rs | 9 | ||||
-rw-r--r-- | crates/ra_hir/src/from_source.rs | 15 | ||||
-rw-r--r-- | crates/ra_hir/src/lib.rs | 5 | ||||
-rw-r--r-- | crates/ra_hir/src/source_binder.rs | 3 |
5 files changed, 19 insertions, 78 deletions
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::{ | |||
11 | builtin_type::BuiltinType, | 11 | builtin_type::BuiltinType, |
12 | docs::Documentation, | 12 | docs::Documentation, |
13 | expr::{BindingAnnotation, Pat, PatId}, | 13 | expr::{BindingAnnotation, Pat, PatId}, |
14 | nameres::ModuleSource, | ||
14 | per_ns::PerNs, | 15 | per_ns::PerNs, |
15 | resolver::HasResolver, | 16 | resolver::HasResolver, |
16 | type_ref::{Mutability, TypeRef}, | 17 | type_ref::{Mutability, TypeRef}, |
@@ -21,11 +22,11 @@ use hir_def::{ | |||
21 | use hir_expand::{ | 22 | use hir_expand::{ |
22 | diagnostics::DiagnosticSink, | 23 | diagnostics::DiagnosticSink, |
23 | name::{self, AsName}, | 24 | name::{self, AsName}, |
24 | AstId, MacroDefId, | 25 | MacroDefId, |
25 | }; | 26 | }; |
26 | use hir_ty::expr::ExprValidator; | 27 | use hir_ty::expr::ExprValidator; |
27 | use ra_db::{CrateId, Edition, FileId, FilePosition}; | 28 | use ra_db::{CrateId, Edition}; |
28 | use ra_syntax::{ast, AstNode, SyntaxNode}; | 29 | use ra_syntax::ast; |
29 | 30 | ||
30 | use crate::{ | 31 | use crate::{ |
31 | db::{DefDatabase, HirDatabase}, | 32 | db::{DefDatabase, HirDatabase}, |
@@ -79,64 +80,6 @@ impl Crate { | |||
79 | } | 80 | } |
80 | } | 81 | } |
81 | 82 | ||
82 | pub enum ModuleSource { | ||
83 | SourceFile(ast::SourceFile), | ||
84 | Module(ast::Module), | ||
85 | } | ||
86 | |||
87 | impl ModuleSource { | ||
88 | pub fn new( | ||
89 | db: &impl DefDatabase, | ||
90 | file_id: Option<FileId>, | ||
91 | decl_id: Option<AstId<ast::Module>>, | ||
92 | ) -> ModuleSource { | ||
93 | match (file_id, decl_id) { | ||
94 | (Some(file_id), _) => { | ||
95 | let source_file = db.parse(file_id).tree(); | ||
96 | ModuleSource::SourceFile(source_file) | ||
97 | } | ||
98 | (None, Some(item_id)) => { | ||
99 | let module = item_id.to_node(db); | ||
100 | assert!(module.item_list().is_some(), "expected inline module"); | ||
101 | ModuleSource::Module(module) | ||
102 | } | ||
103 | (None, None) => panic!(), | ||
104 | } | ||
105 | } | ||
106 | |||
107 | // FIXME: this methods do not belong here | ||
108 | pub fn from_position(db: &impl DefDatabase, position: FilePosition) -> ModuleSource { | ||
109 | let parse = db.parse(position.file_id); | ||
110 | match &ra_syntax::algo::find_node_at_offset::<ast::Module>( | ||
111 | parse.tree().syntax(), | ||
112 | position.offset, | ||
113 | ) { | ||
114 | Some(m) if !m.has_semi() => ModuleSource::Module(m.clone()), | ||
115 | _ => { | ||
116 | let source_file = parse.tree(); | ||
117 | ModuleSource::SourceFile(source_file) | ||
118 | } | ||
119 | } | ||
120 | } | ||
121 | |||
122 | pub fn from_child_node(db: &impl DefDatabase, child: InFile<&SyntaxNode>) -> ModuleSource { | ||
123 | if let Some(m) = | ||
124 | child.value.ancestors().filter_map(ast::Module::cast).find(|it| !it.has_semi()) | ||
125 | { | ||
126 | ModuleSource::Module(m) | ||
127 | } else { | ||
128 | let file_id = child.file_id.original_file(db); | ||
129 | let source_file = db.parse(file_id).tree(); | ||
130 | ModuleSource::SourceFile(source_file) | ||
131 | } | ||
132 | } | ||
133 | |||
134 | pub fn from_file_id(db: &impl DefDatabase, file_id: FileId) -> ModuleSource { | ||
135 | let source_file = db.parse(file_id).tree(); | ||
136 | ModuleSource::SourceFile(source_file) | ||
137 | } | ||
138 | } | ||
139 | |||
140 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 83 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
141 | pub struct Module { | 84 | pub struct Module { |
142 | pub(crate) id: ModuleId, | 85 | 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..d9bccd902 100644 --- a/crates/ra_hir/src/code_model/src.rs +++ b/crates/ra_hir/src/code_model/src.rs | |||
@@ -2,6 +2,7 @@ | |||
2 | 2 | ||
3 | use either::Either; | 3 | use either::Either; |
4 | use hir_def::{ | 4 | use hir_def::{ |
5 | nameres::ModuleSource, | ||
5 | src::{HasChildSource, HasSource as _}, | 6 | src::{HasChildSource, HasSource as _}, |
6 | AstItemDef, Lookup, VariantId, | 7 | AstItemDef, Lookup, VariantId, |
7 | }; | 8 | }; |
@@ -9,7 +10,7 @@ use ra_syntax::ast; | |||
9 | 10 | ||
10 | use crate::{ | 11 | use crate::{ |
11 | db::DefDatabase, Const, Enum, EnumVariant, FieldSource, Function, ImplBlock, Import, MacroDef, | 12 | db::DefDatabase, Const, Enum, EnumVariant, FieldSource, Function, ImplBlock, Import, MacroDef, |
12 | Module, ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union, | 13 | Module, Static, Struct, StructField, Trait, TypeAlias, Union, |
13 | }; | 14 | }; |
14 | 15 | ||
15 | pub use hir_expand::InFile; | 16 | pub use hir_expand::InFile; |
@@ -25,11 +26,7 @@ impl Module { | |||
25 | /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items. | 26 | /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items. |
26 | pub fn definition_source(self, db: &impl DefDatabase) -> InFile<ModuleSource> { | 27 | pub fn definition_source(self, db: &impl DefDatabase) -> InFile<ModuleSource> { |
27 | let def_map = db.crate_def_map(self.id.krate); | 28 | let def_map = db.crate_def_map(self.id.krate); |
28 | let src = def_map[self.id.local_id].definition_source(db); | 29 | def_map[self.id.local_id].definition_source(db) |
29 | src.map(|it| match it { | ||
30 | Either::Left(it) => ModuleSource::SourceFile(it), | ||
31 | Either::Right(it) => ModuleSource::Module(it), | ||
32 | }) | ||
33 | } | 30 | } |
34 | 31 | ||
35 | /// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`. | 32 | /// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`. |
diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs index 82bf641dc..18d87f6d7 100644 --- a/crates/ra_hir/src/from_source.rs +++ b/crates/ra_hir/src/from_source.rs | |||
@@ -1,6 +1,6 @@ | |||
1 | //! FIXME: write short doc here | 1 | //! FIXME: write short doc here |
2 | 2 | ||
3 | use hir_def::{AstItemDef, LocationCtx, ModuleId}; | 3 | use hir_def::{nameres::ModuleSource, AstItemDef, LocationCtx, ModuleId}; |
4 | use hir_expand::{name::AsName, AstId, MacroDefId, MacroDefKind}; | 4 | use hir_expand::{name::AsName, AstId, MacroDefId, MacroDefKind}; |
5 | use ra_syntax::{ | 5 | use ra_syntax::{ |
6 | ast::{self, AstNode, NameOwner}, | 6 | ast::{self, AstNode, NameOwner}, |
@@ -10,8 +10,8 @@ use ra_syntax::{ | |||
10 | use crate::{ | 10 | use crate::{ |
11 | db::{AstDatabase, DefDatabase, HirDatabase}, | 11 | db::{AstDatabase, DefDatabase, HirDatabase}, |
12 | AssocItem, Const, DefWithBody, Enum, EnumVariant, FieldSource, Function, HasSource, ImplBlock, | 12 | AssocItem, Const, DefWithBody, Enum, EnumVariant, FieldSource, Function, HasSource, ImplBlock, |
13 | InFile, Local, MacroDef, Module, ModuleDef, ModuleSource, Static, Struct, StructField, Trait, | 13 | InFile, Local, MacroDef, Module, ModuleDef, Static, Struct, StructField, Trait, TypeAlias, |
14 | TypeAlias, Union, VariantDef, | 14 | Union, VariantDef, |
15 | }; | 15 | }; |
16 | 16 | ||
17 | pub trait FromSource: Sized { | 17 | pub trait FromSource: Sized { |
@@ -235,11 +235,10 @@ impl Module { | |||
235 | let src_parent = InFile { file_id: src.file_id, value: parent_declaration }; | 235 | let src_parent = InFile { file_id: src.file_id, value: parent_declaration }; |
236 | Module::from_declaration(db, src_parent) | 236 | Module::from_declaration(db, src_parent) |
237 | } | 237 | } |
238 | _ => { | 238 | None => { |
239 | let src_parent = InFile { | 239 | let source_file = db.parse(src.file_id.original_file(db)).tree(); |
240 | file_id: src.file_id, | 240 | let src_parent = |
241 | value: ModuleSource::new(db, Some(src.file_id.original_file(db)), None), | 241 | InFile { file_id: src.file_id, value: ModuleSource::SourceFile(source_file) }; |
242 | }; | ||
243 | Module::from_definition(db, src_parent) | 242 | Module::from_definition(db, src_parent) |
244 | } | 243 | } |
245 | }?; | 244 | }?; |
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::{ | |||
43 | code_model::{ | 43 | code_model::{ |
44 | src::HasSource, Adt, AssocItem, AttrDef, Const, Container, Crate, CrateDependency, | 44 | src::HasSource, Adt, AssocItem, AttrDef, Const, Container, Crate, CrateDependency, |
45 | DefWithBody, Docs, Enum, EnumVariant, FieldSource, Function, GenericDef, GenericParam, | 45 | DefWithBody, Docs, Enum, EnumVariant, FieldSource, Function, GenericDef, GenericParam, |
46 | HasAttrs, ImplBlock, Import, Local, MacroDef, Module, ModuleDef, ModuleSource, ScopeDef, | 46 | HasAttrs, ImplBlock, Import, Local, MacroDef, Module, ModuleDef, ScopeDef, Static, Struct, |
47 | Static, Struct, StructField, Trait, Type, TypeAlias, Union, VariantDef, | 47 | StructField, Trait, Type, TypeAlias, Union, VariantDef, |
48 | }, | 48 | }, |
49 | from_source::FromSource, | 49 | from_source::FromSource, |
50 | source_binder::{PathResolution, ScopeEntryWithSyntax, SourceAnalyzer}, | 50 | source_binder::{PathResolution, ScopeEntryWithSyntax, SourceAnalyzer}, |
@@ -59,6 +59,7 @@ pub use hir_def::{ | |||
59 | body::scope::ExprScopes, | 59 | body::scope::ExprScopes, |
60 | builtin_type::BuiltinType, | 60 | builtin_type::BuiltinType, |
61 | docs::Documentation, | 61 | docs::Documentation, |
62 | nameres::ModuleSource, | ||
62 | path::{Path, PathKind}, | 63 | path::{Path, PathKind}, |
63 | type_ref::Mutability, | 64 | type_ref::Mutability, |
64 | }; | 65 | }; |
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::{ | |||
14 | BodySourceMap, | 14 | BodySourceMap, |
15 | }, | 15 | }, |
16 | expr::{ExprId, PatId}, | 16 | expr::{ExprId, PatId}, |
17 | nameres::ModuleSource, | ||
17 | path::known, | 18 | path::known, |
18 | resolver::{self, resolver_for_scope, HasResolver, Resolver, TypeNs, ValueNs}, | 19 | resolver::{self, resolver_for_scope, HasResolver, Resolver, TypeNs, ValueNs}, |
19 | AssocItemId, DefWithBodyId, | 20 | AssocItemId, DefWithBodyId, |
@@ -46,7 +47,7 @@ fn try_get_resolver_for_node(db: &impl HirDatabase, node: InFile<&SyntaxNode>) - | |||
46 | Some(crate::Module::from_declaration(db, src)?.id.resolver(db)) | 47 | Some(crate::Module::from_declaration(db, src)?.id.resolver(db)) |
47 | }, | 48 | }, |
48 | ast::SourceFile(it) => { | 49 | ast::SourceFile(it) => { |
49 | let src = node.with_value(crate::ModuleSource::SourceFile(it)); | 50 | let src = node.with_value(ModuleSource::SourceFile(it)); |
50 | Some(crate::Module::from_definition(db, src)?.id.resolver(db)) | 51 | Some(crate::Module::from_definition(db, src)?.id.resolver(db)) |
51 | }, | 52 | }, |
52 | ast::StructDef(it) => { | 53 | ast::StructDef(it) => { |