From 5e5eb6a108b00c573455d8d088742592012707be Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 11 Apr 2020 23:33:17 +0200 Subject: Align grammar for record patterns and literals The grammar now looks like this [name_ref :] pat --- crates/ra_syntax/src/ast.rs | 4 ++-- crates/ra_syntax/src/ast/extensions.rs | 30 +++++++++++++++++++++++++++++ crates/ra_syntax/src/ast/generated/nodes.rs | 2 +- 3 files changed, 33 insertions(+), 3 deletions(-) (limited to 'crates/ra_syntax/src') diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs index 99c6b7219..7fca5661e 100644 --- a/crates/ra_syntax/src/ast.rs +++ b/crates/ra_syntax/src/ast.rs @@ -18,8 +18,8 @@ use crate::{ pub use self::{ expr_extensions::{ArrayExprKind, BinOp, ElseBranch, LiteralKind, PrefixOp, RangeOp}, extensions::{ - AttrKind, FieldKind, PathSegmentKind, SelfParamKind, SlicePatComponents, StructKind, - TypeBoundKind, VisibilityKind, + AttrKind, FieldKind, NameOrNameRef, PathSegmentKind, SelfParamKind, SlicePatComponents, + StructKind, TypeBoundKind, VisibilityKind, }, generated::{nodes::*, tokens::*}, tokens::*, diff --git a/crates/ra_syntax/src/ast/extensions.rs b/crates/ra_syntax/src/ast/extensions.rs index 63e272fbf..f2ea5088e 100644 --- a/crates/ra_syntax/src/ast/extensions.rs +++ b/crates/ra_syntax/src/ast/extensions.rs @@ -1,6 +1,8 @@ //! Various extension methods to ast Nodes, which are hard to code-generate. //! Extensions for various expressions live in a sibling `expr_extensions` module. +use std::fmt; + use itertools::Itertools; use ra_parser::SyntaxKind; @@ -217,6 +219,34 @@ impl ast::RecordField { } } +pub enum NameOrNameRef { + Name(ast::Name), + NameRef(ast::NameRef), +} + +impl fmt::Display for NameOrNameRef { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + NameOrNameRef::Name(it) => fmt::Display::fmt(it, f), + NameOrNameRef::NameRef(it) => fmt::Display::fmt(it, f), + } + } +} + +impl ast::RecordFieldPat { + /// Deals with field init shorthand + pub fn field_name(&self) -> Option { + if let Some(name_ref) = self.name_ref() { + return Some(NameOrNameRef::NameRef(name_ref)); + } + if let Some(ast::Pat::BindPat(pat)) = self.pat() { + let name = pat.name()?; + return Some(NameOrNameRef::Name(name)); + } + None + } +} + impl ast::EnumVariant { pub fn parent_enum(&self) -> ast::EnumDef { self.syntax() diff --git a/crates/ra_syntax/src/ast/generated/nodes.rs b/crates/ra_syntax/src/ast/generated/nodes.rs index f1098755b..188f0df96 100644 --- a/crates/ra_syntax/src/ast/generated/nodes.rs +++ b/crates/ra_syntax/src/ast/generated/nodes.rs @@ -1806,8 +1806,8 @@ impl AstNode for RecordFieldPat { fn syntax(&self) -> &SyntaxNode { &self.syntax } } impl ast::AttrsOwner for RecordFieldPat {} -impl ast::NameOwner for RecordFieldPat {} impl RecordFieldPat { + pub fn name_ref(&self) -> Option { support::child(&self.syntax) } pub fn colon_token(&self) -> Option { support::token(&self.syntax, T![:]) } pub fn pat(&self) -> Option { support::child(&self.syntax) } } -- cgit v1.2.3