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_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 ---------------------------- 4 files changed, 32 insertions(+), 114 deletions(-) (limited to 'crates/ra_syntax/src/ast') diff --git a/crates/ra_syntax/src/ast/edit.rs b/crates/ra_syntax/src/ast/edit.rs index a01f6487d..7e14349d2 100644 --- a/crates/ra_syntax/src/ast/edit.rs +++ b/crates/ra_syntax/src/ast/edit.rs @@ -98,7 +98,7 @@ impl ast::ItemList { None => 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), -- cgit v1.2.3