From 460c8bbdec8561a581d035f9767b7dd1ba36cfd7 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 10 Apr 2020 10:27:23 +0200 Subject: Curley tokens --- crates/ra_assists/src/handlers/merge_imports.rs | 4 +- crates/ra_syntax/src/ast/edit.rs | 4 +- crates/ra_syntax/src/ast/extensions.rs | 18 +++-- crates/ra_syntax/src/ast/generated/nodes.rs | 36 +++++----- crates/ra_syntax/src/ast/generated/tokens.rs | 88 ------------------------- xtask/src/ast_src.rs | 32 +++++---- 6 files changed, 49 insertions(+), 133 deletions(-) diff --git a/crates/ra_assists/src/handlers/merge_imports.rs b/crates/ra_assists/src/handlers/merge_imports.rs index 936d50ab4..0958f52f1 100644 --- a/crates/ra_assists/src/handlers/merge_imports.rs +++ b/crates/ra_assists/src/handlers/merge_imports.rs @@ -3,7 +3,7 @@ use std::iter::successors; use ra_syntax::{ algo::{neighbor, SyntaxRewriter}, ast::{self, edit::AstNodeEdit, make}, - AstNode, AstToken, Direction, InsertPosition, SyntaxElement, T, + AstNode, Direction, InsertPosition, SyntaxElement, T, }; use crate::{Assist, AssistCtx, AssistId}; @@ -82,7 +82,7 @@ fn try_merge_trees(old: &ast::UseTree, new: &ast::UseTree) -> Option match self.l_curly_token() { Some(it) => ( " ".to_string() + &leading_indent(self.syntax()).unwrap_or_default(), - InsertPosition::After(it.syntax().clone().into()), + InsertPosition::After(it.into()), ), None => return self.clone(), }, @@ -142,7 +142,7 @@ impl ast::RecordFieldList { macro_rules! after_l_curly { () => {{ let anchor = match self.l_curly_token() { - Some(it) => it.syntax().clone().into(), + Some(it) => it.into(), None => return self.clone(), }; InsertPosition::After(anchor) diff --git a/crates/ra_syntax/src/ast/extensions.rs b/crates/ra_syntax/src/ast/extensions.rs index 11ec70bc0..eee98ebc2 100644 --- a/crates/ra_syntax/src/ast/extensions.rs +++ b/crates/ra_syntax/src/ast/extensions.rs @@ -416,11 +416,17 @@ impl ast::RangePat { } impl ast::TokenTree { - pub fn left_delimiter(&self) -> Option { - self.syntax().first_child_or_token()?.into_token().and_then(ast::LeftDelimiter::cast) - } - - pub fn right_delimiter(&self) -> Option { - self.syntax().last_child_or_token()?.into_token().and_then(ast::RightDelimiter::cast) + pub fn left_delimiter_token(&self) -> Option { + self.syntax().first_child_or_token()?.into_token().filter(|it| match it.kind() { + T!['{'] | T!['('] | T!['['] => true, + _ => false, + }) + } + + pub fn right_delimiter_token(&self) -> Option { + self.syntax().last_child_or_token()?.into_token().filter(|it| match it.kind() { + T!['{'] | T!['('] | T!['['] => true, + _ => false, + }) } } diff --git a/crates/ra_syntax/src/ast/generated/nodes.rs b/crates/ra_syntax/src/ast/generated/nodes.rs index fae76a7a8..3c917257c 100644 --- a/crates/ra_syntax/src/ast/generated/nodes.rs +++ b/crates/ra_syntax/src/ast/generated/nodes.rs @@ -146,9 +146,9 @@ impl AstNode for RecordFieldDefList { fn syntax(&self) -> &SyntaxNode { &self.syntax } } impl RecordFieldDefList { - pub fn l_curly_token(&self) -> Option { support::token(&self.syntax) } + pub fn l_curly_token(&self) -> Option { support::token2(&self.syntax, T!['{']) } pub fn fields(&self) -> AstChildren { support::children(&self.syntax) } - pub fn r_curly_token(&self) -> Option { support::token(&self.syntax) } + pub fn r_curly_token(&self) -> Option { support::token2(&self.syntax, T!['}']) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct RecordFieldDef { @@ -251,9 +251,9 @@ impl AstNode for EnumVariantList { fn syntax(&self) -> &SyntaxNode { &self.syntax } } impl EnumVariantList { - pub fn l_curly_token(&self) -> Option { support::token(&self.syntax) } + pub fn l_curly_token(&self) -> Option { support::token2(&self.syntax, T!['{']) } pub fn variants(&self) -> AstChildren { support::children(&self.syntax) } - pub fn r_curly_token(&self) -> Option { support::token(&self.syntax) } + pub fn r_curly_token(&self) -> Option { support::token2(&self.syntax, T!['}']) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct EnumVariant { @@ -347,9 +347,9 @@ impl AstNode for ItemList { } impl ast::ModuleItemOwner for ItemList {} impl ItemList { - pub fn l_curly_token(&self) -> Option { support::token(&self.syntax) } + pub fn l_curly_token(&self) -> Option { support::token2(&self.syntax, T!['{']) } pub fn impl_items(&self) -> AstChildren { support::children(&self.syntax) } - pub fn r_curly_token(&self) -> Option { support::token(&self.syntax) } + pub fn r_curly_token(&self) -> Option { support::token2(&self.syntax, T!['}']) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct ConstDef { @@ -1337,9 +1337,9 @@ impl AstNode for MatchArmList { } impl ast::AttrsOwner for MatchArmList {} impl MatchArmList { - pub fn l_curly_token(&self) -> Option { support::token(&self.syntax) } + pub fn l_curly_token(&self) -> Option { support::token2(&self.syntax, T!['{']) } pub fn arms(&self) -> AstChildren { support::children(&self.syntax) } - pub fn r_curly_token(&self) -> Option { support::token(&self.syntax) } + pub fn r_curly_token(&self) -> Option { support::token2(&self.syntax, T!['}']) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct MatchArm { @@ -1417,11 +1417,11 @@ impl AstNode for RecordFieldList { fn syntax(&self) -> &SyntaxNode { &self.syntax } } impl RecordFieldList { - pub fn l_curly_token(&self) -> Option { support::token(&self.syntax) } + pub fn l_curly_token(&self) -> Option { support::token2(&self.syntax, T!['{']) } pub fn fields(&self) -> AstChildren { support::children(&self.syntax) } pub fn dotdot_token(&self) -> Option { support::token(&self.syntax) } pub fn spread(&self) -> Option { support::child(&self.syntax) } - pub fn r_curly_token(&self) -> Option { support::token(&self.syntax) } + pub fn r_curly_token(&self) -> Option { support::token2(&self.syntax, T!['}']) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct RecordField { @@ -1709,14 +1709,14 @@ impl AstNode for RecordFieldPatList { fn syntax(&self) -> &SyntaxNode { &self.syntax } } impl RecordFieldPatList { - pub fn l_curly_token(&self) -> Option { support::token(&self.syntax) } + pub fn l_curly_token(&self) -> Option { support::token2(&self.syntax, T!['{']) } pub fn pats(&self) -> AstChildren { support::children(&self.syntax) } pub fn record_field_pats(&self) -> AstChildren { support::children(&self.syntax) } pub fn bind_pats(&self) -> AstChildren { support::children(&self.syntax) } pub fn dotdot_token(&self) -> Option { support::token(&self.syntax) } - pub fn r_curly_token(&self) -> Option { support::token(&self.syntax) } + pub fn r_curly_token(&self) -> Option { support::token2(&self.syntax, T!['}']) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct RecordFieldPat { @@ -2165,10 +2165,10 @@ impl AstNode for Block { impl ast::AttrsOwner for Block {} impl ast::ModuleItemOwner for Block {} impl Block { - pub fn l_curly_token(&self) -> Option { support::token(&self.syntax) } + pub fn l_curly_token(&self) -> Option { support::token2(&self.syntax, T!['{']) } pub fn statements(&self) -> AstChildren { support::children(&self.syntax) } pub fn expr(&self) -> Option { support::child(&self.syntax) } - pub fn r_curly_token(&self) -> Option { support::token(&self.syntax) } + pub fn r_curly_token(&self) -> Option { support::token2(&self.syntax, T!['}']) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct ParamList { @@ -2311,9 +2311,9 @@ impl AstNode for UseTreeList { fn syntax(&self) -> &SyntaxNode { &self.syntax } } impl UseTreeList { - pub fn l_curly_token(&self) -> Option { support::token(&self.syntax) } + pub fn l_curly_token(&self) -> Option { support::token2(&self.syntax, T!['{']) } pub fn use_trees(&self) -> AstChildren { support::children(&self.syntax) } - pub fn r_curly_token(&self) -> Option { support::token(&self.syntax) } + pub fn r_curly_token(&self) -> Option { support::token2(&self.syntax, T!['}']) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct ExternCrateItem { @@ -2557,9 +2557,9 @@ impl AstNode for ExternItemList { } impl ast::ModuleItemOwner for ExternItemList {} impl ExternItemList { - pub fn l_curly_token(&self) -> Option { support::token(&self.syntax) } + pub fn l_curly_token(&self) -> Option { support::token2(&self.syntax, T!['{']) } pub fn extern_items(&self) -> AstChildren { support::children(&self.syntax) } - pub fn r_curly_token(&self) -> Option { support::token(&self.syntax) } + pub fn r_curly_token(&self) -> Option { support::token2(&self.syntax, T!['}']) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct ExternBlock { diff --git a/crates/ra_syntax/src/ast/generated/tokens.rs b/crates/ra_syntax/src/ast/generated/tokens.rs index 7344b0e49..4cbc1d793 100644 --- a/crates/ra_syntax/src/ast/generated/tokens.rs +++ b/crates/ra_syntax/src/ast/generated/tokens.rs @@ -1366,94 +1366,6 @@ impl AstToken for RDollar { fn syntax(&self) -> &SyntaxToken { &self.syntax } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub enum LeftDelimiter { - LParen(LParen), - LBrack(LBrack), - LCurly(LCurly), -} -impl From for LeftDelimiter { - fn from(node: LParen) -> LeftDelimiter { LeftDelimiter::LParen(node) } -} -impl From for LeftDelimiter { - fn from(node: LBrack) -> LeftDelimiter { LeftDelimiter::LBrack(node) } -} -impl From for LeftDelimiter { - fn from(node: LCurly) -> LeftDelimiter { LeftDelimiter::LCurly(node) } -} -impl std::fmt::Display for LeftDelimiter { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - std::fmt::Display::fmt(self.syntax(), f) - } -} -impl AstToken for LeftDelimiter { - fn can_cast(kind: SyntaxKind) -> bool { - match kind { - L_PAREN | L_BRACK | L_CURLY => true, - _ => false, - } - } - fn cast(syntax: SyntaxToken) -> Option { - let res = match syntax.kind() { - L_PAREN => LeftDelimiter::LParen(LParen { syntax }), - L_BRACK => LeftDelimiter::LBrack(LBrack { syntax }), - L_CURLY => LeftDelimiter::LCurly(LCurly { syntax }), - _ => return None, - }; - Some(res) - } - fn syntax(&self) -> &SyntaxToken { - match self { - LeftDelimiter::LParen(it) => &it.syntax, - LeftDelimiter::LBrack(it) => &it.syntax, - LeftDelimiter::LCurly(it) => &it.syntax, - } - } -} -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub enum RightDelimiter { - RParen(RParen), - RBrack(RBrack), - RCurly(RCurly), -} -impl From for RightDelimiter { - fn from(node: RParen) -> RightDelimiter { RightDelimiter::RParen(node) } -} -impl From for RightDelimiter { - fn from(node: RBrack) -> RightDelimiter { RightDelimiter::RBrack(node) } -} -impl From for RightDelimiter { - fn from(node: RCurly) -> RightDelimiter { RightDelimiter::RCurly(node) } -} -impl std::fmt::Display for RightDelimiter { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - std::fmt::Display::fmt(self.syntax(), f) - } -} -impl AstToken for RightDelimiter { - fn can_cast(kind: SyntaxKind) -> bool { - match kind { - R_PAREN | R_BRACK | R_CURLY => true, - _ => false, - } - } - fn cast(syntax: SyntaxToken) -> Option { - let res = match syntax.kind() { - R_PAREN => RightDelimiter::RParen(RParen { syntax }), - R_BRACK => RightDelimiter::RBrack(RBrack { syntax }), - R_CURLY => RightDelimiter::RCurly(RCurly { syntax }), - _ => return None, - }; - Some(res) - } - fn syntax(&self) -> &SyntaxToken { - match self { - RightDelimiter::RParen(it) => &it.syntax, - RightDelimiter::RBrack(it) => &it.syntax, - RightDelimiter::RCurly(it) => &it.syntax, - } - } -} -#[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum RangeSeparator { Dotdot(Dotdot), Dotdotdot(Dotdotdot), diff --git a/xtask/src/ast_src.rs b/xtask/src/ast_src.rs index 8c83450e6..1483dc08e 100644 --- a/xtask/src/ast_src.rs +++ b/xtask/src/ast_src.rs @@ -335,7 +335,7 @@ pub(crate) const AST_SRC: AstSrc = AstSrc { RecordFieldDefList, } - struct RecordFieldDefList { LCurly, fields: [RecordFieldDef], RCurly } + struct RecordFieldDefList { T!['{'], fields: [RecordFieldDef], T!['}'] } struct RecordFieldDef: VisibilityOwner, NameOwner, AttrsOwner, DocCommentsOwner, TypeAscriptionOwner { } struct TupleFieldDefList { LParen, fields: [TupleFieldDef], RParen } @@ -348,9 +348,9 @@ pub(crate) const AST_SRC: AstSrc = AstSrc { variant_list: EnumVariantList, } struct EnumVariantList { - LCurly, + T!['{'], variants: [EnumVariant], - RCurly + T!['}'] } struct EnumVariant: VisibilityOwner, NameOwner, DocCommentsOwner, AttrsOwner { FieldDefList, @@ -372,9 +372,9 @@ pub(crate) const AST_SRC: AstSrc = AstSrc { } struct ItemList: ModuleItemOwner { - LCurly, + T!['{'], impl_items: [ImplItem], - RCurly + T!['}'] } struct ConstDef: VisibilityOwner, NameOwner, TypeParamsOwner, AttrsOwner, DocCommentsOwner, TypeAscriptionOwner { @@ -469,7 +469,7 @@ pub(crate) const AST_SRC: AstSrc = AstSrc { struct Literal { LiteralToken } struct MatchExpr: AttrsOwner { T![match], Expr, MatchArmList } - struct MatchArmList: AttrsOwner { LCurly, arms: [MatchArm], RCurly } + struct MatchArmList: AttrsOwner { T!['{'], arms: [MatchArm], T!['}'] } struct MatchArm: AttrsOwner { pat: Pat, guard: MatchGuard, @@ -480,11 +480,11 @@ pub(crate) const AST_SRC: AstSrc = AstSrc { struct RecordLit { Path, RecordFieldList} struct RecordFieldList { - LCurly, + T!['{'], fields: [RecordField], Dotdot, spread: Expr, - RCurly + T!['}'] } struct RecordField: AttrsOwner { NameRef, Colon, Expr } @@ -503,12 +503,12 @@ pub(crate) const AST_SRC: AstSrc = AstSrc { struct RecordPat { RecordFieldPatList, Path } struct RecordFieldPatList { - LCurly, + T!['{'], pats: [RecordInnerPat], record_field_pats: [RecordFieldPat], bind_pats: [BindPat], Dotdot, - RCurly + T!['}'] } struct RecordFieldPat: AttrsOwner, NameOwner { Colon, Pat } @@ -556,10 +556,10 @@ pub(crate) const AST_SRC: AstSrc = AstSrc { } struct Condition { T![let], Pat, Eq, Expr } struct Block: AttrsOwner, ModuleItemOwner { - LCurly, + T!['{'], statements: [Stmt], Expr, - RCurly, + T!['}'], } struct ParamList { LParen, @@ -580,7 +580,7 @@ pub(crate) const AST_SRC: AstSrc = AstSrc { Path, Star, UseTreeList, Alias } struct Alias: NameOwner { T![as] } - struct UseTreeList { LCurly, use_trees: [UseTree], RCurly } + struct UseTreeList { T!['{'], use_trees: [UseTree], T!['}'] } struct ExternCrateItem: AttrsOwner, VisibilityOwner { T![extern], T![crate], NameRef, Alias, } @@ -619,9 +619,9 @@ pub(crate) const AST_SRC: AstSrc = AstSrc { } struct ExternItemList: ModuleItemOwner { - LCurly, + T!['{'], extern_items: [ExternItem], - RCurly + T!['}'] } struct ExternBlock { @@ -769,8 +769,6 @@ pub(crate) const AST_SRC: AstSrc = AstSrc { }, token_enums: &ast_enums! { - enum LeftDelimiter { LParen, LBrack, LCurly } - enum RightDelimiter { RParen, RBrack, RCurly } enum RangeSeparator { Dotdot, Dotdotdot, Dotdoteq} enum BinOp { -- cgit v1.2.3