aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-04-11 15:42:24 +0100
committerAleksey Kladov <[email protected]>2020-04-11 18:20:41 +0100
commit7a39bc3ba29351feabcd4a16e12568a9e12818ca (patch)
treea5f102f40002dd66b4fc06aa2c3474f3be184a17 /crates/ra_syntax/src
parente7a68c8f55e0770fdeae508a1710509c13aaffa1 (diff)
Make records grammar more orthogonal
We used name [: expr] grammar before, now it is [name :] expr which makes things simpler
Diffstat (limited to 'crates/ra_syntax/src')
-rw-r--r--crates/ra_syntax/src/ast/extensions.rs32
1 files changed, 32 insertions, 0 deletions
diff --git a/crates/ra_syntax/src/ast/extensions.rs b/crates/ra_syntax/src/ast/extensions.rs
index 76b7655e6..7dc024991 100644
--- a/crates/ra_syntax/src/ast/extensions.rs
+++ b/crates/ra_syntax/src/ast/extensions.rs
@@ -187,6 +187,38 @@ 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 eprintln!("field_name = {}", field_name);
193 dbg!(field_name.syntax().ancestors().nth(6));
194 let candidate =
195 field_name.syntax().parent().and_then(ast::RecordField::cast).or_else(|| {
196 field_name.syntax().ancestors().nth(4).and_then(ast::RecordField::cast)
197 })?;
198 if candidate.field_name().as_ref() == Some(field_name) {
199 Some(candidate)
200 } else {
201 None
202 }
203 }
204
205 /// Deals with field init shorthand
206 pub fn field_name(&self) -> Option<ast::NameRef> {
207 if let Some(name_ref) = self.name_ref() {
208 return Some(name_ref);
209 }
210 if let Some(ast::Expr::PathExpr(expr)) = self.expr() {
211 let path = expr.path()?;
212 let segment = path.segment()?;
213 let name_ref = segment.name_ref()?;
214 if path.qualifier().is_none() {
215 return Some(name_ref);
216 }
217 }
218 None
219 }
220}
221
190impl ast::EnumVariant { 222impl ast::EnumVariant {
191 pub fn parent_enum(&self) -> ast::EnumDef { 223 pub fn parent_enum(&self) -> ast::EnumDef {
192 self.syntax() 224 self.syntax()