diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-04-11 18:37:02 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2020-04-11 18:37:02 +0100 |
commit | 1a1c09ed3e3a34c0a8750f98ece9ad85595395d2 (patch) | |
tree | 44136a4ce738d388bb9b0ce9f5569cf68465cbe2 /crates/ra_syntax/src/ast | |
parent | 11d400b63b07d3cffbe8d1363b802a2d52f5d786 (diff) | |
parent | 0aece75cdd40daa4d48484103cfcd36ba13ba076 (diff) |
Merge #3951
3951: Simplify records grammar r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_syntax/src/ast')
-rw-r--r-- | crates/ra_syntax/src/ast/extensions.rs | 30 |
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 | ||
190 | impl 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 | |||
190 | impl ast::EnumVariant { | 220 | impl ast::EnumVariant { |
191 | pub fn parent_enum(&self) -> ast::EnumDef { | 221 | pub fn parent_enum(&self) -> ast::EnumDef { |
192 | self.syntax() | 222 | self.syntax() |