From a7ca6583fbce6f1bddce7b31ad5bb1fc0665b616 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 31 Jul 2020 15:40:48 +0200 Subject: Handwrite Stmt --- crates/ra_syntax/src/ast.rs | 2 +- crates/ra_syntax/src/ast/generated.rs | 37 ++++++++++++++++++++++++++--- crates/ra_syntax/src/ast/generated/nodes.rs | 22 ----------------- 3 files changed, 35 insertions(+), 26 deletions(-) (limited to 'crates/ra_syntax/src') diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs index 8a0e3d27b..d536bb1e7 100644 --- a/crates/ra_syntax/src/ast.rs +++ b/crates/ra_syntax/src/ast.rs @@ -17,7 +17,7 @@ use crate::{ pub use self::{ expr_ext::{ArrayExprKind, BinOp, Effect, ElseBranch, LiteralKind, PrefixOp, RangeOp}, - generated::{nodes::*, tokens::*}, + generated::*, node_ext::{ AttrKind, FieldKind, NameOrNameRef, PathSegmentKind, SelfParamKind, SlicePatComponents, StructKind, TypeBoundKind, VisibilityKind, diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs index f5199e09f..ba55f1c42 100644 --- a/crates/ra_syntax/src/ast/generated.rs +++ b/crates/ra_syntax/src/ast/generated.rs @@ -1,6 +1,37 @@ //! This file is actually hand-written, but the submodules are indeed generated. - #[rustfmt::skip] -pub(super) mod nodes; +mod nodes; #[rustfmt::skip] -pub(super) mod tokens; +mod tokens; + +use crate::{ + AstNode, + SyntaxKind::{self, *}, + SyntaxNode, +}; + +pub use {nodes::*, tokens::*}; + +// Stmt is the only nested enum, so it's easier to just hand-write it +impl AstNode for Stmt { + fn can_cast(kind: SyntaxKind) -> bool { + match kind { + LET_STMT | EXPR_STMT => true, + _ => false, + } + } + fn cast(syntax: SyntaxNode) -> Option { + let res = match syntax.kind() { + LET_STMT => Stmt::LetStmt(LetStmt { syntax }), + EXPR_STMT => Stmt::ExprStmt(ExprStmt { syntax }), + _ => return None, + }; + Some(res) + } + fn syntax(&self) -> &SyntaxNode { + match self { + Stmt::LetStmt(it) => &it.syntax, + Stmt::ExprStmt(it) => &it.syntax, + } + } +} diff --git a/crates/ra_syntax/src/ast/generated/nodes.rs b/crates/ra_syntax/src/ast/generated/nodes.rs index 286be1032..8ef72fec7 100644 --- a/crates/ra_syntax/src/ast/generated/nodes.rs +++ b/crates/ra_syntax/src/ast/generated/nodes.rs @@ -3380,28 +3380,6 @@ impl From for Stmt { impl From for Stmt { fn from(node: ExprStmt) -> Stmt { Stmt::ExprStmt(node) } } -impl AstNode for Stmt { - fn can_cast(kind: SyntaxKind) -> bool { - match kind { - LET_STMT | EXPR_STMT => true, - _ => false, - } - } - fn cast(syntax: SyntaxNode) -> Option { - let res = match syntax.kind() { - LET_STMT => Stmt::LetStmt(LetStmt { syntax }), - EXPR_STMT => Stmt::ExprStmt(ExprStmt { syntax }), - _ => return None, - }; - Some(res) - } - fn syntax(&self) -> &SyntaxNode { - match self { - Stmt::LetStmt(it) => &it.syntax, - Stmt::ExprStmt(it) => &it.syntax, - } - } -} impl std::fmt::Display for Item { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { std::fmt::Display::fmt(self.syntax(), f) -- cgit v1.2.3 From d4d986c7f850e1f535bb4c22e3a7f7fba5483628 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 31 Jul 2020 15:46:12 +0200 Subject: Item is a Stmt --- crates/ra_syntax/src/ast/generated.rs | 8 ++++++-- crates/ra_syntax/src/ast/generated/nodes.rs | 6 ++++-- 2 files changed, 10 insertions(+), 4 deletions(-) (limited to 'crates/ra_syntax/src') diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs index ba55f1c42..4a6f41ee7 100644 --- a/crates/ra_syntax/src/ast/generated.rs +++ b/crates/ra_syntax/src/ast/generated.rs @@ -17,14 +17,17 @@ impl AstNode for Stmt { fn can_cast(kind: SyntaxKind) -> bool { match kind { LET_STMT | EXPR_STMT => true, - _ => false, + _ => Item::can_cast(kind), } } fn cast(syntax: SyntaxNode) -> Option { let res = match syntax.kind() { LET_STMT => Stmt::LetStmt(LetStmt { syntax }), EXPR_STMT => Stmt::ExprStmt(ExprStmt { syntax }), - _ => return None, + _ => { + let item = Item::cast(syntax)?; + Stmt::Item(item) + } }; Some(res) } @@ -32,6 +35,7 @@ impl AstNode for Stmt { match self { Stmt::LetStmt(it) => &it.syntax, Stmt::ExprStmt(it) => &it.syntax, + Stmt::Item(it) => it.syntax(), } } } diff --git a/crates/ra_syntax/src/ast/generated/nodes.rs b/crates/ra_syntax/src/ast/generated/nodes.rs index 8ef72fec7..763fd20f4 100644 --- a/crates/ra_syntax/src/ast/generated/nodes.rs +++ b/crates/ra_syntax/src/ast/generated/nodes.rs @@ -348,7 +348,6 @@ pub struct BlockExpr { pub(crate) syntax: SyntaxNode, } impl ast::AttrsOwner for BlockExpr {} -impl ast::ModuleItemOwner for BlockExpr {} impl BlockExpr { pub fn label(&self) -> Option