diff options
Diffstat (limited to 'crates/ra_syntax/src')
-rw-r--r-- | crates/ra_syntax/src/ast/extensions.rs | 30 | ||||
-rw-r--r-- | crates/ra_syntax/src/lib.rs | 5 |
2 files changed, 35 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() |
diff --git a/crates/ra_syntax/src/lib.rs b/crates/ra_syntax/src/lib.rs index f0e16dc2b..a796e78b1 100644 --- a/crates/ra_syntax/src/lib.rs +++ b/crates/ra_syntax/src/lib.rs | |||
@@ -19,6 +19,11 @@ | |||
19 | //! [RFC]: <https://github.com/rust-lang/rfcs/pull/2256> | 19 | //! [RFC]: <https://github.com/rust-lang/rfcs/pull/2256> |
20 | //! [Swift]: <https://github.com/apple/swift/blob/13d593df6f359d0cb2fc81cfaac273297c539455/lib/Syntax/README.md> | 20 | //! [Swift]: <https://github.com/apple/swift/blob/13d593df6f359d0cb2fc81cfaac273297c539455/lib/Syntax/README.md> |
21 | 21 | ||
22 | #[allow(unused)] | ||
23 | macro_rules! eprintln { | ||
24 | ($($tt:tt)*) => { stdx::eprintln!($($tt)*) }; | ||
25 | } | ||
26 | |||
22 | mod syntax_node; | 27 | mod syntax_node; |
23 | mod syntax_error; | 28 | mod syntax_error; |
24 | mod parsing; | 29 | mod parsing; |