diff options
author | Aleksey Kladov <[email protected]> | 2019-06-11 16:00:08 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-06-11 16:28:51 +0100 |
commit | 0dcaded439ae4bd4670bc6a2fbf739cd4fce60af (patch) | |
tree | 9f97e6be462d8c3b38da74af095f4160adeda51c /crates/ra_hir/src/code_model.rs | |
parent | c4512fadb1b332b13bb41b0aa8a28aa964664842 (diff) |
move source to a seaparate file
Diffstat (limited to 'crates/ra_hir/src/code_model.rs')
-rw-r--r-- | crates/ra_hir/src/code_model.rs | 123 |
1 files changed, 3 insertions, 120 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 @@ | |||
1 | pub(crate) mod src; | ||
2 | |||
1 | use std::sync::Arc; | 3 | use std::sync::Arc; |
2 | 4 | ||
3 | use ra_db::{CrateId, SourceRootId, Edition, FileId}; | 5 | use ra_db::{CrateId, SourceRootId, Edition, FileId}; |
4 | use ra_syntax::{ast::{self, NameOwner, TypeAscriptionOwner}, TreeArc}; | 6 | use ra_syntax::{ast::{self, NameOwner, TypeAscriptionOwner}, TreeArc}; |
5 | 7 | ||
6 | use crate::{ | 8 | use 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 | ||
23 | pub struct Source<T> { | ||
24 | pub file_id: HirFileId, | ||
25 | pub ast: T, | ||
26 | } | ||
27 | |||
28 | impl<T> From<(HirFileId, T)> for Source<T> { | ||
29 | fn from((file_id, ast): (HirFileId, T)) -> Self { | ||
30 | Source { file_id, ast } | ||
31 | } | ||
32 | } | ||
33 | |||
34 | pub 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 | ||
346 | impl 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 | |||
353 | impl StructField { | 310 | impl 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 | ||
372 | impl 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 | |||
379 | impl Struct { | 329 | impl 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 | ||
433 | impl 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 | |||
440 | impl Union { | 383 | impl 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 | ||
466 | impl 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 | |||
473 | impl Enum { | 409 | impl 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 | ||
516 | impl 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 | |||
523 | impl EnumVariant { | 452 | impl 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 | ||
592 | impl 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)] |
602 | pub struct FnSignature { | 523 | pub struct FnSignature { |
@@ -743,14 +664,6 @@ pub struct Const { | |||
743 | pub(crate) id: ConstId, | 664 | pub(crate) id: ConstId, |
744 | } | 665 | } |
745 | 666 | ||
746 | impl 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 | |||
754 | impl Const { | 667 | impl 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 | ||
830 | impl 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 | |||
838 | impl Static { | 743 | impl 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 | ||
863 | impl 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 | |||
870 | impl Trait { | 768 | impl 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 | ||
909 | impl 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 | |||
916 | impl TypeAlias { | 807 | impl 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 | ||
965 | impl 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 | |||
973 | impl MacroDef {} | 856 | impl MacroDef {} |
974 | 857 | ||
975 | pub enum Container { | 858 | pub enum Container { |