aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-06-11 16:00:08 +0100
committerAleksey Kladov <[email protected]>2019-06-11 16:28:51 +0100
commit0dcaded439ae4bd4670bc6a2fbf739cd4fce60af (patch)
tree9f97e6be462d8c3b38da74af095f4160adeda51c /crates/ra_hir/src
parentc4512fadb1b332b13bb41b0aa8a28aa964664842 (diff)
move source to a seaparate file
Diffstat (limited to 'crates/ra_hir/src')
-rw-r--r--crates/ra_hir/src/code_model.rs123
-rw-r--r--crates/ra_hir/src/code_model/src.rs120
-rw-r--r--crates/ra_hir/src/lib.rs3
3 files changed, 125 insertions, 121 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs
index 282bd15de..0cfab27dc 100644
--- a/crates/ra_hir/src/code_model.rs
+++ b/crates/ra_hir/src/code_model.rs
@@ -1,10 +1,12 @@
1pub(crate) mod src;
2
1use std::sync::Arc; 3use std::sync::Arc;
2 4
3use ra_db::{CrateId, SourceRootId, Edition, FileId}; 5use ra_db::{CrateId, SourceRootId, Edition, FileId};
4use ra_syntax::{ast::{self, NameOwner, TypeAscriptionOwner}, TreeArc}; 6use ra_syntax::{ast::{self, NameOwner, TypeAscriptionOwner}, TreeArc};
5 7
6use crate::{ 8use crate::{
7 Name, AsName, AstId, Ty, HirFileId, Either, KnownName, 9 Name, AsName, AstId, Ty, Either, KnownName, HasSource,
8 HirDatabase, DefDatabase, AstDatabase, 10 HirDatabase, DefDatabase, AstDatabase,
9 type_ref::TypeRef, 11 type_ref::TypeRef,
10 nameres::{ModuleScope, Namespace, ImportId, CrateModuleId}, 12 nameres::{ModuleScope, Namespace, ImportId, CrateModuleId},
@@ -20,22 +22,6 @@ use crate::{
20 type_ref::Mutability, 22 type_ref::Mutability,
21}; 23};
22 24
23pub struct Source<T> {
24 pub file_id: HirFileId,
25 pub ast: T,
26}
27
28impl<T> From<(HirFileId, T)> for Source<T> {
29 fn from((file_id, ast): (HirFileId, T)) -> Self {
30 Source { file_id, ast }
31 }
32}
33
34pub trait HasSource {
35 type Ast;
36 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<Self::Ast>;
37}
38
39/// hir::Crate describes a single crate. It's the main interface with which 25/// hir::Crate describes a single crate. It's the main interface with which
40/// a crate's dependencies interact. Mostly, it should be just a proxy for the 26/// a crate's dependencies interact. Mostly, it should be just a proxy for the
41/// root module. 27/// root module.
@@ -195,28 +181,6 @@ impl Module {
195 }) 181 })
196 } 182 }
197 183
198 /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items.
199 pub fn definition_source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ModuleSource> {
200 let def_map = db.crate_def_map(self.krate);
201 let decl_id = def_map[self.module_id].declaration;
202 let file_id = def_map[self.module_id].definition;
203 let module_source = ModuleSource::new(db, file_id, decl_id);
204 let file_id = file_id.map(HirFileId::from).unwrap_or_else(|| decl_id.unwrap().file_id());
205 (file_id, module_source).into()
206 }
207
208 /// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`.
209 /// `None` for the crate root.
210 pub fn declaration_source(
211 self,
212 db: &(impl DefDatabase + AstDatabase),
213 ) -> Option<Source<TreeArc<ast::Module>>> {
214 let def_map = db.crate_def_map(self.krate);
215 let decl = def_map[self.module_id].declaration?;
216 let ast = decl.to_node(db);
217 Some((decl.file_id(), ast).into())
218 }
219
220 /// Returns the syntax of the last path segment corresponding to this import 184 /// Returns the syntax of the last path segment corresponding to this import
221 pub fn import_source( 185 pub fn import_source(
222 self, 186 self,
@@ -343,13 +307,6 @@ pub enum FieldSource {
343 Pos(TreeArc<ast::PosFieldDef>), 307 Pos(TreeArc<ast::PosFieldDef>),
344} 308}
345 309
346impl HasSource for StructField {
347 type Ast = FieldSource;
348 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<FieldSource> {
349 self.source_impl(db).into()
350 }
351}
352
353impl StructField { 310impl StructField {
354 pub fn name(&self, db: &impl HirDatabase) -> Name { 311 pub fn name(&self, db: &impl HirDatabase) -> Name {
355 self.parent.variant_data(db).fields().unwrap()[self.id].name.clone() 312 self.parent.variant_data(db).fields().unwrap()[self.id].name.clone()
@@ -369,13 +326,6 @@ pub struct Struct {
369 pub(crate) id: StructId, 326 pub(crate) id: StructId,
370} 327}
371 328
372impl HasSource for Struct {
373 type Ast = TreeArc<ast::StructDef>;
374 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::StructDef>> {
375 self.id.source(db).into()
376 }
377}
378
379impl Struct { 329impl Struct {
380 pub fn module(self, db: &impl HirDatabase) -> Module { 330 pub fn module(self, db: &impl HirDatabase) -> Module {
381 self.id.module(db) 331 self.id.module(db)
@@ -430,13 +380,6 @@ pub struct Union {
430 pub(crate) id: StructId, 380 pub(crate) id: StructId,
431} 381}
432 382
433impl HasSource for Union {
434 type Ast = TreeArc<ast::StructDef>;
435 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::StructDef>> {
436 self.id.source(db).into()
437 }
438}
439
440impl Union { 383impl Union {
441 pub fn name(self, db: &impl DefDatabase) -> Option<Name> { 384 pub fn name(self, db: &impl DefDatabase) -> Option<Name> {
442 db.struct_data(Struct { id: self.id }).name.clone() 385 db.struct_data(Struct { id: self.id }).name.clone()
@@ -463,13 +406,6 @@ pub struct Enum {
463 pub(crate) id: EnumId, 406 pub(crate) id: EnumId,
464} 407}
465 408
466impl HasSource for Enum {
467 type Ast = TreeArc<ast::EnumDef>;
468 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::EnumDef>> {
469 self.id.source(db).into()
470 }
471}
472
473impl Enum { 409impl Enum {
474 pub fn module(self, db: &impl HirDatabase) -> Module { 410 pub fn module(self, db: &impl HirDatabase) -> Module {
475 self.id.module(db) 411 self.id.module(db)
@@ -513,13 +449,6 @@ pub struct EnumVariant {
513 pub(crate) id: EnumVariantId, 449 pub(crate) id: EnumVariantId,
514} 450}
515 451
516impl HasSource for EnumVariant {
517 type Ast = TreeArc<ast::EnumVariant>;
518 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::EnumVariant>> {
519 self.source_impl(db)
520 }
521}
522
523impl EnumVariant { 452impl EnumVariant {
524 pub fn module(&self, db: &impl HirDatabase) -> Module { 453 pub fn module(&self, db: &impl HirDatabase) -> Module {
525 self.parent.module(db) 454 self.parent.module(db)
@@ -589,14 +518,6 @@ pub struct Function {
589 pub(crate) id: FunctionId, 518 pub(crate) id: FunctionId,
590} 519}
591 520
592impl HasSource for Function {
593 type Ast = TreeArc<ast::FnDef>;
594
595 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::FnDef>> {
596 self.id.source(db).into()
597 }
598}
599
600/// The declared signature of a function. 521/// The declared signature of a function.
601#[derive(Debug, Clone, PartialEq, Eq)] 522#[derive(Debug, Clone, PartialEq, Eq)]
602pub struct FnSignature { 523pub struct FnSignature {
@@ -743,14 +664,6 @@ pub struct Const {
743 pub(crate) id: ConstId, 664 pub(crate) id: ConstId,
744} 665}
745 666
746impl HasSource for Const {
747 type Ast = TreeArc<ast::ConstDef>;
748
749 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::ConstDef>> {
750 self.id.source(db).into()
751 }
752}
753
754impl Const { 667impl Const {
755 pub fn module(self, db: &impl DefDatabase) -> Module { 668 pub fn module(self, db: &impl DefDatabase) -> Module {
756 self.id.module(db) 669 self.id.module(db)
@@ -827,14 +740,6 @@ pub struct Static {
827 pub(crate) id: StaticId, 740 pub(crate) id: StaticId,
828} 741}
829 742
830impl HasSource for Static {
831 type Ast = TreeArc<ast::StaticDef>;
832
833 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::StaticDef>> {
834 self.id.source(db).into()
835 }
836}
837
838impl Static { 743impl Static {
839 pub fn module(self, db: &impl DefDatabase) -> Module { 744 pub fn module(self, db: &impl DefDatabase) -> Module {
840 self.id.module(db) 745 self.id.module(db)
@@ -860,13 +765,6 @@ pub struct Trait {
860 pub(crate) id: TraitId, 765 pub(crate) id: TraitId,
861} 766}
862 767
863impl HasSource for Trait {
864 type Ast = TreeArc<ast::TraitDef>;
865 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::TraitDef>> {
866 self.id.source(db).into()
867 }
868}
869
870impl Trait { 768impl Trait {
871 pub fn module(self, db: &impl DefDatabase) -> Module { 769 pub fn module(self, db: &impl DefDatabase) -> Module {
872 self.id.module(db) 770 self.id.module(db)
@@ -906,13 +804,6 @@ pub struct TypeAlias {
906 pub(crate) id: TypeAliasId, 804 pub(crate) id: TypeAliasId,
907} 805}
908 806
909impl HasSource for TypeAlias {
910 type Ast = TreeArc<ast::TypeAliasDef>;
911 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::TypeAliasDef>> {
912 self.id.source(db).into()
913 }
914}
915
916impl TypeAlias { 807impl TypeAlias {
917 pub fn module(self, db: &impl DefDatabase) -> Module { 808 pub fn module(self, db: &impl DefDatabase) -> Module {
918 self.id.module(db) 809 self.id.module(db)
@@ -962,14 +853,6 @@ pub struct MacroDef {
962 pub(crate) id: MacroDefId, 853 pub(crate) id: MacroDefId,
963} 854}
964 855
965impl HasSource for MacroDef {
966 type Ast = TreeArc<ast::MacroCall>;
967
968 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::MacroCall>> {
969 (self.id.0.file_id(), self.id.0.to_node(db)).into()
970 }
971}
972
973impl MacroDef {} 856impl MacroDef {}
974 857
975pub enum Container { 858pub enum Container {
diff --git a/crates/ra_hir/src/code_model/src.rs b/crates/ra_hir/src/code_model/src.rs
new file mode 100644
index 000000000..7484faf04
--- /dev/null
+++ b/crates/ra_hir/src/code_model/src.rs
@@ -0,0 +1,120 @@
1use ra_syntax::{TreeArc, ast};
2
3use crate::{
4 HirFileId, DefDatabase, AstDatabase, Module, ModuleSource,
5 StructField, Struct, Enum, Union, EnumVariant, Function, Static, Trait, Const, TypeAlias,
6 FieldSource, MacroDef, ids::AstItemDef,
7};
8
9pub struct Source<T> {
10 pub file_id: HirFileId,
11 pub ast: T,
12}
13
14impl<T> From<(HirFileId, T)> for Source<T> {
15 fn from((file_id, ast): (HirFileId, T)) -> Self {
16 Source { file_id, ast }
17 }
18}
19
20pub trait HasSource {
21 type Ast;
22 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<Self::Ast>;
23}
24
25/// NB: Module is !HasSource, becase it has two source nodes at the same time:
26/// definition and declaration.
27impl Module {
28 /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items.
29 pub fn definition_source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ModuleSource> {
30 let def_map = db.crate_def_map(self.krate);
31 let decl_id = def_map[self.module_id].declaration;
32 let file_id = def_map[self.module_id].definition;
33 let module_source = ModuleSource::new(db, file_id, decl_id);
34 let file_id = file_id.map(HirFileId::from).unwrap_or_else(|| decl_id.unwrap().file_id());
35 (file_id, module_source).into()
36 }
37
38 /// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`.
39 /// `None` for the crate root.
40 pub fn declaration_source(
41 self,
42 db: &(impl DefDatabase + AstDatabase),
43 ) -> Option<Source<TreeArc<ast::Module>>> {
44 let def_map = db.crate_def_map(self.krate);
45 let decl = def_map[self.module_id].declaration?;
46 let ast = decl.to_node(db);
47 Some((decl.file_id(), ast).into())
48 }
49}
50
51impl HasSource for StructField {
52 type Ast = FieldSource;
53 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<FieldSource> {
54 self.source_impl(db).into()
55 }
56}
57impl HasSource for Struct {
58 type Ast = TreeArc<ast::StructDef>;
59 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::StructDef>> {
60 self.id.source(db).into()
61 }
62}
63impl HasSource for Union {
64 type Ast = TreeArc<ast::StructDef>;
65 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::StructDef>> {
66 self.id.source(db).into()
67 }
68}
69impl HasSource for Enum {
70 type Ast = TreeArc<ast::EnumDef>;
71 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::EnumDef>> {
72 self.id.source(db).into()
73 }
74}
75impl HasSource for EnumVariant {
76 type Ast = TreeArc<ast::EnumVariant>;
77 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::EnumVariant>> {
78 self.source_impl(db)
79 }
80}
81impl HasSource for Function {
82 type Ast = TreeArc<ast::FnDef>;
83
84 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::FnDef>> {
85 self.id.source(db).into()
86 }
87}
88impl HasSource for Const {
89 type Ast = TreeArc<ast::ConstDef>;
90
91 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::ConstDef>> {
92 self.id.source(db).into()
93 }
94}
95impl HasSource for Static {
96 type Ast = TreeArc<ast::StaticDef>;
97
98 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::StaticDef>> {
99 self.id.source(db).into()
100 }
101}
102impl HasSource for Trait {
103 type Ast = TreeArc<ast::TraitDef>;
104 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::TraitDef>> {
105 self.id.source(db).into()
106 }
107}
108impl HasSource for TypeAlias {
109 type Ast = TreeArc<ast::TypeAliasDef>;
110 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::TypeAliasDef>> {
111 self.id.source(db).into()
112 }
113}
114impl HasSource for MacroDef {
115 type Ast = TreeArc<ast::MacroCall>;
116
117 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::MacroCall>> {
118 (self.id.0.file_id(), self.id.0.to_node(db)).into()
119 }
120}
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs
index 02f5f7f40..1690296e5 100644
--- a/crates/ra_hir/src/lib.rs
+++ b/crates/ra_hir/src/lib.rs
@@ -81,5 +81,6 @@ pub use self::code_model::{
81 StructField, FieldSource, 81 StructField, FieldSource,
82 Static, Const, ConstSignature, 82 Static, Const, ConstSignature,
83 Trait, TypeAlias, MacroDef, Container, 83 Trait, TypeAlias, MacroDef, Container,
84 BuiltinType, Source, HasSource, 84 BuiltinType,
85 src::{Source, HasSource},
85}; 86};