diff options
author | Seivan Heidari <[email protected]> | 2019-10-31 16:40:39 +0000 |
---|---|---|
committer | Seivan Heidari <[email protected]> | 2019-10-31 16:40:39 +0000 |
commit | 1b6c68e51fcbba59c2e99c31d1400cbd6d44f928 (patch) | |
tree | bdf28d44b2908a62b49a3f2bb1995187f45e42c4 /crates/ra_hir/src/code_model | |
parent | a437f0e5849d8ce0a14d40a55f2ac2166f5ec343 (diff) | |
parent | 5451bfb9a1c6482822bdd7883434b9230affd2ea (diff) |
Merge branch 'master' into feature/themes
Diffstat (limited to 'crates/ra_hir/src/code_model')
-rw-r--r-- | crates/ra_hir/src/code_model/src.rs | 42 |
1 files changed, 40 insertions, 2 deletions
diff --git a/crates/ra_hir/src/code_model/src.rs b/crates/ra_hir/src/code_model/src.rs index 5c7f61eef..0f4c78df7 100644 --- a/crates/ra_hir/src/code_model/src.rs +++ b/crates/ra_hir/src/code_model/src.rs | |||
@@ -3,6 +3,7 @@ | |||
3 | use ra_syntax::ast::{self, AstNode}; | 3 | use ra_syntax::ast::{self, AstNode}; |
4 | 4 | ||
5 | use crate::{ | 5 | use crate::{ |
6 | adt::VariantDef, | ||
6 | db::{AstDatabase, DefDatabase, HirDatabase}, | 7 | db::{AstDatabase, DefDatabase, HirDatabase}, |
7 | ids::AstItemDef, | 8 | ids::AstItemDef, |
8 | Const, Either, Enum, EnumVariant, FieldSource, Function, HasBody, HirFileId, MacroDef, Module, | 9 | Const, Either, Enum, EnumVariant, FieldSource, Function, HasBody, HirFileId, MacroDef, Module, |
@@ -45,7 +46,33 @@ impl Module { | |||
45 | impl HasSource for StructField { | 46 | impl HasSource for StructField { |
46 | type Ast = FieldSource; | 47 | type Ast = FieldSource; |
47 | fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<FieldSource> { | 48 | fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<FieldSource> { |
48 | self.source_impl(db) | 49 | let var_data = self.parent.variant_data(db); |
50 | let fields = var_data.fields().unwrap(); | ||
51 | let ss; | ||
52 | let es; | ||
53 | let (file_id, struct_kind) = match self.parent { | ||
54 | VariantDef::Struct(s) => { | ||
55 | ss = s.source(db); | ||
56 | (ss.file_id, ss.ast.kind()) | ||
57 | } | ||
58 | VariantDef::EnumVariant(e) => { | ||
59 | es = e.source(db); | ||
60 | (es.file_id, es.ast.kind()) | ||
61 | } | ||
62 | }; | ||
63 | |||
64 | let field_sources = match struct_kind { | ||
65 | ast::StructKind::Tuple(fl) => fl.fields().map(|it| FieldSource::Pos(it)).collect(), | ||
66 | ast::StructKind::Named(fl) => fl.fields().map(|it| FieldSource::Named(it)).collect(), | ||
67 | ast::StructKind::Unit => Vec::new(), | ||
68 | }; | ||
69 | let ast = field_sources | ||
70 | .into_iter() | ||
71 | .zip(fields.iter()) | ||
72 | .find(|(_syntax, (id, _))| *id == self.id) | ||
73 | .unwrap() | ||
74 | .0; | ||
75 | Source { file_id, ast } | ||
49 | } | 76 | } |
50 | } | 77 | } |
51 | impl HasSource for Struct { | 78 | impl HasSource for Struct { |
@@ -69,7 +96,18 @@ impl HasSource for Enum { | |||
69 | impl HasSource for EnumVariant { | 96 | impl HasSource for EnumVariant { |
70 | type Ast = ast::EnumVariant; | 97 | type Ast = ast::EnumVariant; |
71 | fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ast::EnumVariant> { | 98 | fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ast::EnumVariant> { |
72 | self.source_impl(db) | 99 | let enum_data = db.enum_data(self.parent.id); |
100 | let src = self.parent.id.source(db); | ||
101 | let ast = src | ||
102 | .ast | ||
103 | .variant_list() | ||
104 | .into_iter() | ||
105 | .flat_map(|it| it.variants()) | ||
106 | .zip(enum_data.variants.iter()) | ||
107 | .find(|(_syntax, (id, _))| *id == self.id) | ||
108 | .unwrap() | ||
109 | .0; | ||
110 | Source { file_id: src.file_id, ast } | ||
73 | } | 111 | } |
74 | } | 112 | } |
75 | impl HasSource for Function { | 113 | impl HasSource for Function { |