aboutsummaryrefslogtreecommitdiff
path: root/crates/hir/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir/src')
-rw-r--r--crates/hir/src/has_source.rs66
1 files changed, 66 insertions, 0 deletions
diff --git a/crates/hir/src/has_source.rs b/crates/hir/src/has_source.rs
index a8256c181..84fbeca75 100644
--- a/crates/hir/src/has_source.rs
+++ b/crates/hir/src/has_source.rs
@@ -17,6 +17,7 @@ use crate::{
17pub trait HasSource { 17pub trait HasSource {
18 type Ast; 18 type Ast;
19 fn source_old(self, db: &dyn HirDatabase) -> InFile<Self::Ast>; 19 fn source_old(self, db: &dyn HirDatabase) -> InFile<Self::Ast>;
20 fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>>;
20} 21}
21 22
22/// NB: Module is !HasSource, because it has two source nodes at the same time: 23/// NB: Module is !HasSource, because it has two source nodes at the same time:
@@ -54,60 +55,106 @@ impl HasSource for Field {
54 Either::Right(it) => FieldSource::Named(it), 55 Either::Right(it) => FieldSource::Named(it),
55 }) 56 })
56 } 57 }
58
59 fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>> {
60 let var = VariantId::from(self.parent);
61 let src = var.child_source(db.upcast());
62 let field_source = src.map(|it| match it[self.id].clone() {
63 Either::Left(it) => FieldSource::Pos(it),
64 Either::Right(it) => FieldSource::Named(it),
65 });
66 Some(field_source)
67 }
57} 68}
58impl HasSource for Struct { 69impl HasSource for Struct {
59 type Ast = ast::Struct; 70 type Ast = ast::Struct;
60 fn source_old(self, db: &dyn HirDatabase) -> InFile<ast::Struct> { 71 fn source_old(self, db: &dyn HirDatabase) -> InFile<ast::Struct> {
61 self.id.lookup(db.upcast()).source(db.upcast()) 72 self.id.lookup(db.upcast()).source(db.upcast())
62 } 73 }
74
75 fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>> {
76 Some(self.id.lookup(db.upcast()).source(db.upcast()))
77 }
63} 78}
64impl HasSource for Union { 79impl HasSource for Union {
65 type Ast = ast::Union; 80 type Ast = ast::Union;
66 fn source_old(self, db: &dyn HirDatabase) -> InFile<ast::Union> { 81 fn source_old(self, db: &dyn HirDatabase) -> InFile<ast::Union> {
67 self.id.lookup(db.upcast()).source(db.upcast()) 82 self.id.lookup(db.upcast()).source(db.upcast())
68 } 83 }
84
85 fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>> {
86 Some(self.id.lookup(db.upcast()).source(db.upcast()))
87 }
69} 88}
70impl HasSource for Enum { 89impl HasSource for Enum {
71 type Ast = ast::Enum; 90 type Ast = ast::Enum;
72 fn source_old(self, db: &dyn HirDatabase) -> InFile<ast::Enum> { 91 fn source_old(self, db: &dyn HirDatabase) -> InFile<ast::Enum> {
73 self.id.lookup(db.upcast()).source(db.upcast()) 92 self.id.lookup(db.upcast()).source(db.upcast())
74 } 93 }
94
95 fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>> {
96 Some(self.id.lookup(db.upcast()).source(db.upcast()))
97 }
75} 98}
76impl HasSource for Variant { 99impl HasSource for Variant {
77 type Ast = ast::Variant; 100 type Ast = ast::Variant;
78 fn source_old(self, db: &dyn HirDatabase) -> InFile<ast::Variant> { 101 fn source_old(self, db: &dyn HirDatabase) -> InFile<ast::Variant> {
79 self.parent.id.child_source(db.upcast()).map(|map| map[self.id].clone()) 102 self.parent.id.child_source(db.upcast()).map(|map| map[self.id].clone())
80 } 103 }
104
105 fn source(self, db: &dyn HirDatabase) -> Option<InFile<ast::Variant>> {
106 Some(self.parent.id.child_source(db.upcast()).map(|map| map[self.id].clone()))
107 }
81} 108}
82impl HasSource for Function { 109impl HasSource for Function {
83 type Ast = ast::Fn; 110 type Ast = ast::Fn;
84 fn source_old(self, db: &dyn HirDatabase) -> InFile<ast::Fn> { 111 fn source_old(self, db: &dyn HirDatabase) -> InFile<ast::Fn> {
85 self.id.lookup(db.upcast()).source(db.upcast()) 112 self.id.lookup(db.upcast()).source(db.upcast())
86 } 113 }
114
115 fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>> {
116 Some(self.id.lookup(db.upcast()).source(db.upcast()))
117 }
87} 118}
88impl HasSource for Const { 119impl HasSource for Const {
89 type Ast = ast::Const; 120 type Ast = ast::Const;
90 fn source_old(self, db: &dyn HirDatabase) -> InFile<ast::Const> { 121 fn source_old(self, db: &dyn HirDatabase) -> InFile<ast::Const> {
91 self.id.lookup(db.upcast()).source(db.upcast()) 122 self.id.lookup(db.upcast()).source(db.upcast())
92 } 123 }
124
125 fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>> {
126 Some(self.id.lookup(db.upcast()).source(db.upcast()))
127 }
93} 128}
94impl HasSource for Static { 129impl HasSource for Static {
95 type Ast = ast::Static; 130 type Ast = ast::Static;
96 fn source_old(self, db: &dyn HirDatabase) -> InFile<ast::Static> { 131 fn source_old(self, db: &dyn HirDatabase) -> InFile<ast::Static> {
97 self.id.lookup(db.upcast()).source(db.upcast()) 132 self.id.lookup(db.upcast()).source(db.upcast())
98 } 133 }
134
135 fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>> {
136 Some(self.id.lookup(db.upcast()).source(db.upcast()))
137 }
99} 138}
100impl HasSource for Trait { 139impl HasSource for Trait {
101 type Ast = ast::Trait; 140 type Ast = ast::Trait;
102 fn source_old(self, db: &dyn HirDatabase) -> InFile<ast::Trait> { 141 fn source_old(self, db: &dyn HirDatabase) -> InFile<ast::Trait> {
103 self.id.lookup(db.upcast()).source(db.upcast()) 142 self.id.lookup(db.upcast()).source(db.upcast())
104 } 143 }
144
145 fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>> {
146 Some(self.id.lookup(db.upcast()).source(db.upcast()))
147 }
105} 148}
106impl HasSource for TypeAlias { 149impl HasSource for TypeAlias {
107 type Ast = ast::TypeAlias; 150 type Ast = ast::TypeAlias;
108 fn source_old(self, db: &dyn HirDatabase) -> InFile<ast::TypeAlias> { 151 fn source_old(self, db: &dyn HirDatabase) -> InFile<ast::TypeAlias> {
109 self.id.lookup(db.upcast()).source(db.upcast()) 152 self.id.lookup(db.upcast()).source(db.upcast())
110 } 153 }
154
155 fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>> {
156 Some(self.id.lookup(db.upcast()).source(db.upcast()))
157 }
111} 158}
112impl HasSource for MacroDef { 159impl HasSource for MacroDef {
113 type Ast = ast::Macro; 160 type Ast = ast::Macro;
@@ -117,12 +164,21 @@ impl HasSource for MacroDef {
117 value: self.id.ast_id.expect("MacroDef without ast_id").to_node(db.upcast()), 164 value: self.id.ast_id.expect("MacroDef without ast_id").to_node(db.upcast()),
118 } 165 }
119 } 166 }
167
168 fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>> {
169 let ast_id = self.id.ast_id?;
170 Some(InFile { file_id: ast_id.file_id, value: ast_id.to_node(db.upcast()) })
171 }
120} 172}
121impl HasSource for Impl { 173impl HasSource for Impl {
122 type Ast = ast::Impl; 174 type Ast = ast::Impl;
123 fn source_old(self, db: &dyn HirDatabase) -> InFile<ast::Impl> { 175 fn source_old(self, db: &dyn HirDatabase) -> InFile<ast::Impl> {
124 self.id.lookup(db.upcast()).source(db.upcast()) 176 self.id.lookup(db.upcast()).source(db.upcast())
125 } 177 }
178
179 fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>> {
180 Some(self.id.lookup(db.upcast()).source(db.upcast()))
181 }
126} 182}
127 183
128impl HasSource for TypeParam { 184impl HasSource for TypeParam {
@@ -131,6 +187,11 @@ impl HasSource for TypeParam {
131 let child_source = self.id.parent.child_source(db.upcast()); 187 let child_source = self.id.parent.child_source(db.upcast());
132 child_source.map(|it| it[self.id.local_id].clone()) 188 child_source.map(|it| it[self.id.local_id].clone())
133 } 189 }
190
191 fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>> {
192 let child_source = self.id.parent.child_source(db.upcast());
193 Some(child_source.map(|it| it[self.id.local_id].clone()))
194 }
134} 195}
135 196
136impl HasSource for LifetimeParam { 197impl HasSource for LifetimeParam {
@@ -139,6 +200,11 @@ impl HasSource for LifetimeParam {
139 let child_source = self.id.parent.child_source(db.upcast()); 200 let child_source = self.id.parent.child_source(db.upcast());
140 child_source.map(|it| it[self.id.local_id].clone()) 201 child_source.map(|it| it[self.id.local_id].clone())
141 } 202 }
203
204 fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>> {
205 let child_source = self.id.parent.child_source(db.upcast());
206 Some(child_source.map(|it| it[self.id.local_id].clone()))
207 }
142} 208}
143 209
144impl HasSource for ConstParam { 210impl HasSource for ConstParam {