aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/ast
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_syntax/src/ast')
-rw-r--r--crates/ra_syntax/src/ast/extensions.rs30
1 files changed, 30 insertions, 0 deletions
diff --git a/crates/ra_syntax/src/ast/extensions.rs b/crates/ra_syntax/src/ast/extensions.rs
index 76b7655e6..63e272fbf 100644
--- a/crates/ra_syntax/src/ast/extensions.rs
+++ b/crates/ra_syntax/src/ast/extensions.rs
@@ -187,6 +187,36 @@ impl ast::StructDef {
187 } 187 }
188} 188}
189 189
190impl ast::RecordField {
191 pub fn for_field_name(field_name: &ast::NameRef) -> Option<ast::RecordField> {
192 let candidate =
193 field_name.syntax().parent().and_then(ast::RecordField::cast).or_else(|| {
194 field_name.syntax().ancestors().nth(4).and_then(ast::RecordField::cast)
195 })?;
196 if candidate.field_name().as_ref() == Some(field_name) {
197 Some(candidate)
198 } else {
199 None
200 }
201 }
202
203 /// Deals with field init shorthand
204 pub fn field_name(&self) -> Option<ast::NameRef> {
205 if let Some(name_ref) = self.name_ref() {
206 return Some(name_ref);
207 }
208 if let Some(ast::Expr::PathExpr(expr)) = self.expr() {
209 let path = expr.path()?;
210 let segment = path.segment()?;
211 let name_ref = segment.name_ref()?;
212 if path.qualifier().is_none() {
213 return Some(name_ref);
214 }
215 }
216 None
217 }
218}
219
190impl ast::EnumVariant { 220impl ast::EnumVariant {
191 pub fn parent_enum(&self) -> ast::EnumDef { 221 pub fn parent_enum(&self) -> ast::EnumDef {
192 self.syntax() 222 self.syntax()