Curley tokens

This commit is contained in:
Aleksey Kladov 2020-04-10 10:27:23 +02:00
parent 1c5d859195
commit 460c8bbdec
6 changed files with 47 additions and 131 deletions

View file

@ -3,7 +3,7 @@ use std::iter::successors;
use ra_syntax::{ use ra_syntax::{
algo::{neighbor, SyntaxRewriter}, algo::{neighbor, SyntaxRewriter},
ast::{self, edit::AstNodeEdit, make}, ast::{self, edit::AstNodeEdit, make},
AstNode, AstToken, Direction, InsertPosition, SyntaxElement, T, AstNode, Direction, InsertPosition, SyntaxElement, T,
}; };
use crate::{Assist, AssistCtx, AssistId}; use crate::{Assist, AssistCtx, AssistId};
@ -82,7 +82,7 @@ fn try_merge_trees(old: &ast::UseTree, new: &ast::UseTree) -> Option<ast::UseTre
.filter(|it| it.kind() != T!['{'] && it.kind() != T!['}']), .filter(|it| it.kind() != T!['{'] && it.kind() != T!['}']),
); );
let use_tree_list = lhs.use_tree_list()?; let use_tree_list = lhs.use_tree_list()?;
let pos = InsertPosition::Before(use_tree_list.r_curly_token()?.syntax().clone().into()); let pos = InsertPosition::Before(use_tree_list.r_curly_token()?.into());
let use_tree_list = use_tree_list.insert_children(pos, to_insert); let use_tree_list = use_tree_list.insert_children(pos, to_insert);
Some(lhs.with_use_tree_list(use_tree_list)) Some(lhs.with_use_tree_list(use_tree_list))
} }

View file

@ -98,7 +98,7 @@ impl ast::ItemList {
None => match self.l_curly_token() { None => match self.l_curly_token() {
Some(it) => ( Some(it) => (
" ".to_string() + &leading_indent(self.syntax()).unwrap_or_default(), " ".to_string() + &leading_indent(self.syntax()).unwrap_or_default(),
InsertPosition::After(it.syntax().clone().into()), InsertPosition::After(it.into()),
), ),
None => return self.clone(), None => return self.clone(),
}, },
@ -142,7 +142,7 @@ impl ast::RecordFieldList {
macro_rules! after_l_curly { macro_rules! after_l_curly {
() => {{ () => {{
let anchor = match self.l_curly_token() { let anchor = match self.l_curly_token() {
Some(it) => it.syntax().clone().into(), Some(it) => it.into(),
None => return self.clone(), None => return self.clone(),
}; };
InsertPosition::After(anchor) InsertPosition::After(anchor)

View file

@ -416,11 +416,17 @@ impl ast::RangePat {
} }
impl ast::TokenTree { impl ast::TokenTree {
pub fn left_delimiter(&self) -> Option<ast::LeftDelimiter> { pub fn left_delimiter_token(&self) -> Option<SyntaxToken> {
self.syntax().first_child_or_token()?.into_token().and_then(ast::LeftDelimiter::cast) self.syntax().first_child_or_token()?.into_token().filter(|it| match it.kind() {
T!['{'] | T!['('] | T!['['] => true,
_ => false,
})
} }
pub fn right_delimiter(&self) -> Option<ast::RightDelimiter> { pub fn right_delimiter_token(&self) -> Option<SyntaxToken> {
self.syntax().last_child_or_token()?.into_token().and_then(ast::RightDelimiter::cast) self.syntax().last_child_or_token()?.into_token().filter(|it| match it.kind() {
T!['{'] | T!['('] | T!['['] => true,
_ => false,
})
} }
} }

View file

@ -146,9 +146,9 @@ impl AstNode for RecordFieldDefList {
fn syntax(&self) -> &SyntaxNode { &self.syntax } fn syntax(&self) -> &SyntaxNode { &self.syntax }
} }
impl RecordFieldDefList { impl RecordFieldDefList {
pub fn l_curly_token(&self) -> Option<LCurly> { support::token(&self.syntax) } pub fn l_curly_token(&self) -> Option<SyntaxToken> { support::token2(&self.syntax, T!['{']) }
pub fn fields(&self) -> AstChildren<RecordFieldDef> { support::children(&self.syntax) } pub fn fields(&self) -> AstChildren<RecordFieldDef> { support::children(&self.syntax) }
pub fn r_curly_token(&self) -> Option<RCurly> { support::token(&self.syntax) } pub fn r_curly_token(&self) -> Option<SyntaxToken> { support::token2(&self.syntax, T!['}']) }
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct RecordFieldDef { pub struct RecordFieldDef {
@ -251,9 +251,9 @@ impl AstNode for EnumVariantList {
fn syntax(&self) -> &SyntaxNode { &self.syntax } fn syntax(&self) -> &SyntaxNode { &self.syntax }
} }
impl EnumVariantList { impl EnumVariantList {
pub fn l_curly_token(&self) -> Option<LCurly> { support::token(&self.syntax) } pub fn l_curly_token(&self) -> Option<SyntaxToken> { support::token2(&self.syntax, T!['{']) }
pub fn variants(&self) -> AstChildren<EnumVariant> { support::children(&self.syntax) } pub fn variants(&self) -> AstChildren<EnumVariant> { support::children(&self.syntax) }
pub fn r_curly_token(&self) -> Option<RCurly> { support::token(&self.syntax) } pub fn r_curly_token(&self) -> Option<SyntaxToken> { support::token2(&self.syntax, T!['}']) }
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct EnumVariant { pub struct EnumVariant {
@ -347,9 +347,9 @@ impl AstNode for ItemList {
} }
impl ast::ModuleItemOwner for ItemList {} impl ast::ModuleItemOwner for ItemList {}
impl ItemList { impl ItemList {
pub fn l_curly_token(&self) -> Option<LCurly> { support::token(&self.syntax) } pub fn l_curly_token(&self) -> Option<SyntaxToken> { support::token2(&self.syntax, T!['{']) }
pub fn impl_items(&self) -> AstChildren<ImplItem> { support::children(&self.syntax) } pub fn impl_items(&self) -> AstChildren<ImplItem> { support::children(&self.syntax) }
pub fn r_curly_token(&self) -> Option<RCurly> { support::token(&self.syntax) } pub fn r_curly_token(&self) -> Option<SyntaxToken> { support::token2(&self.syntax, T!['}']) }
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ConstDef { pub struct ConstDef {
@ -1337,9 +1337,9 @@ impl AstNode for MatchArmList {
} }
impl ast::AttrsOwner for MatchArmList {} impl ast::AttrsOwner for MatchArmList {}
impl MatchArmList { impl MatchArmList {
pub fn l_curly_token(&self) -> Option<LCurly> { support::token(&self.syntax) } pub fn l_curly_token(&self) -> Option<SyntaxToken> { support::token2(&self.syntax, T!['{']) }
pub fn arms(&self) -> AstChildren<MatchArm> { support::children(&self.syntax) } pub fn arms(&self) -> AstChildren<MatchArm> { support::children(&self.syntax) }
pub fn r_curly_token(&self) -> Option<RCurly> { support::token(&self.syntax) } pub fn r_curly_token(&self) -> Option<SyntaxToken> { support::token2(&self.syntax, T!['}']) }
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct MatchArm { pub struct MatchArm {
@ -1417,11 +1417,11 @@ impl AstNode for RecordFieldList {
fn syntax(&self) -> &SyntaxNode { &self.syntax } fn syntax(&self) -> &SyntaxNode { &self.syntax }
} }
impl RecordFieldList { impl RecordFieldList {
pub fn l_curly_token(&self) -> Option<LCurly> { support::token(&self.syntax) } pub fn l_curly_token(&self) -> Option<SyntaxToken> { support::token2(&self.syntax, T!['{']) }
pub fn fields(&self) -> AstChildren<RecordField> { support::children(&self.syntax) } pub fn fields(&self) -> AstChildren<RecordField> { support::children(&self.syntax) }
pub fn dotdot_token(&self) -> Option<Dotdot> { support::token(&self.syntax) } pub fn dotdot_token(&self) -> Option<Dotdot> { support::token(&self.syntax) }
pub fn spread(&self) -> Option<Expr> { support::child(&self.syntax) } pub fn spread(&self) -> Option<Expr> { support::child(&self.syntax) }
pub fn r_curly_token(&self) -> Option<RCurly> { support::token(&self.syntax) } pub fn r_curly_token(&self) -> Option<SyntaxToken> { support::token2(&self.syntax, T!['}']) }
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct RecordField { pub struct RecordField {
@ -1709,14 +1709,14 @@ impl AstNode for RecordFieldPatList {
fn syntax(&self) -> &SyntaxNode { &self.syntax } fn syntax(&self) -> &SyntaxNode { &self.syntax }
} }
impl RecordFieldPatList { impl RecordFieldPatList {
pub fn l_curly_token(&self) -> Option<LCurly> { support::token(&self.syntax) } pub fn l_curly_token(&self) -> Option<SyntaxToken> { support::token2(&self.syntax, T!['{']) }
pub fn pats(&self) -> AstChildren<RecordInnerPat> { support::children(&self.syntax) } pub fn pats(&self) -> AstChildren<RecordInnerPat> { support::children(&self.syntax) }
pub fn record_field_pats(&self) -> AstChildren<RecordFieldPat> { pub fn record_field_pats(&self) -> AstChildren<RecordFieldPat> {
support::children(&self.syntax) support::children(&self.syntax)
} }
pub fn bind_pats(&self) -> AstChildren<BindPat> { support::children(&self.syntax) } pub fn bind_pats(&self) -> AstChildren<BindPat> { support::children(&self.syntax) }
pub fn dotdot_token(&self) -> Option<Dotdot> { support::token(&self.syntax) } pub fn dotdot_token(&self) -> Option<Dotdot> { support::token(&self.syntax) }
pub fn r_curly_token(&self) -> Option<RCurly> { support::token(&self.syntax) } pub fn r_curly_token(&self) -> Option<SyntaxToken> { support::token2(&self.syntax, T!['}']) }
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct RecordFieldPat { pub struct RecordFieldPat {
@ -2165,10 +2165,10 @@ impl AstNode for Block {
impl ast::AttrsOwner for Block {} impl ast::AttrsOwner for Block {}
impl ast::ModuleItemOwner for Block {} impl ast::ModuleItemOwner for Block {}
impl Block { impl Block {
pub fn l_curly_token(&self) -> Option<LCurly> { support::token(&self.syntax) } pub fn l_curly_token(&self) -> Option<SyntaxToken> { support::token2(&self.syntax, T!['{']) }
pub fn statements(&self) -> AstChildren<Stmt> { support::children(&self.syntax) } pub fn statements(&self) -> AstChildren<Stmt> { support::children(&self.syntax) }
pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) } pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) }
pub fn r_curly_token(&self) -> Option<RCurly> { support::token(&self.syntax) } pub fn r_curly_token(&self) -> Option<SyntaxToken> { support::token2(&self.syntax, T!['}']) }
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ParamList { pub struct ParamList {
@ -2311,9 +2311,9 @@ impl AstNode for UseTreeList {
fn syntax(&self) -> &SyntaxNode { &self.syntax } fn syntax(&self) -> &SyntaxNode { &self.syntax }
} }
impl UseTreeList { impl UseTreeList {
pub fn l_curly_token(&self) -> Option<LCurly> { support::token(&self.syntax) } pub fn l_curly_token(&self) -> Option<SyntaxToken> { support::token2(&self.syntax, T!['{']) }
pub fn use_trees(&self) -> AstChildren<UseTree> { support::children(&self.syntax) } pub fn use_trees(&self) -> AstChildren<UseTree> { support::children(&self.syntax) }
pub fn r_curly_token(&self) -> Option<RCurly> { support::token(&self.syntax) } pub fn r_curly_token(&self) -> Option<SyntaxToken> { support::token2(&self.syntax, T!['}']) }
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ExternCrateItem { pub struct ExternCrateItem {
@ -2557,9 +2557,9 @@ impl AstNode for ExternItemList {
} }
impl ast::ModuleItemOwner for ExternItemList {} impl ast::ModuleItemOwner for ExternItemList {}
impl ExternItemList { impl ExternItemList {
pub fn l_curly_token(&self) -> Option<LCurly> { support::token(&self.syntax) } pub fn l_curly_token(&self) -> Option<SyntaxToken> { support::token2(&self.syntax, T!['{']) }
pub fn extern_items(&self) -> AstChildren<ExternItem> { support::children(&self.syntax) } pub fn extern_items(&self) -> AstChildren<ExternItem> { support::children(&self.syntax) }
pub fn r_curly_token(&self) -> Option<RCurly> { support::token(&self.syntax) } pub fn r_curly_token(&self) -> Option<SyntaxToken> { support::token2(&self.syntax, T!['}']) }
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ExternBlock { pub struct ExternBlock {

View file

@ -1366,94 +1366,6 @@ impl AstToken for RDollar {
fn syntax(&self) -> &SyntaxToken { &self.syntax } fn syntax(&self) -> &SyntaxToken { &self.syntax }
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum LeftDelimiter {
LParen(LParen),
LBrack(LBrack),
LCurly(LCurly),
}
impl From<LParen> for LeftDelimiter {
fn from(node: LParen) -> LeftDelimiter { LeftDelimiter::LParen(node) }
}
impl From<LBrack> for LeftDelimiter {
fn from(node: LBrack) -> LeftDelimiter { LeftDelimiter::LBrack(node) }
}
impl From<LCurly> 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<Self> {
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<RParen> for RightDelimiter {
fn from(node: RParen) -> RightDelimiter { RightDelimiter::RParen(node) }
}
impl From<RBrack> for RightDelimiter {
fn from(node: RBrack) -> RightDelimiter { RightDelimiter::RBrack(node) }
}
impl From<RCurly> 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<Self> {
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 { pub enum RangeSeparator {
Dotdot(Dotdot), Dotdot(Dotdot),
Dotdotdot(Dotdotdot), Dotdotdot(Dotdotdot),

View file

@ -335,7 +335,7 @@ pub(crate) const AST_SRC: AstSrc = AstSrc {
RecordFieldDefList, RecordFieldDefList,
} }
struct RecordFieldDefList { LCurly, fields: [RecordFieldDef], RCurly } struct RecordFieldDefList { T!['{'], fields: [RecordFieldDef], T!['}'] }
struct RecordFieldDef: VisibilityOwner, NameOwner, AttrsOwner, DocCommentsOwner, TypeAscriptionOwner { } struct RecordFieldDef: VisibilityOwner, NameOwner, AttrsOwner, DocCommentsOwner, TypeAscriptionOwner { }
struct TupleFieldDefList { LParen, fields: [TupleFieldDef], RParen } struct TupleFieldDefList { LParen, fields: [TupleFieldDef], RParen }
@ -348,9 +348,9 @@ pub(crate) const AST_SRC: AstSrc = AstSrc {
variant_list: EnumVariantList, variant_list: EnumVariantList,
} }
struct EnumVariantList { struct EnumVariantList {
LCurly, T!['{'],
variants: [EnumVariant], variants: [EnumVariant],
RCurly T!['}']
} }
struct EnumVariant: VisibilityOwner, NameOwner, DocCommentsOwner, AttrsOwner { struct EnumVariant: VisibilityOwner, NameOwner, DocCommentsOwner, AttrsOwner {
FieldDefList, FieldDefList,
@ -372,9 +372,9 @@ pub(crate) const AST_SRC: AstSrc = AstSrc {
} }
struct ItemList: ModuleItemOwner { struct ItemList: ModuleItemOwner {
LCurly, T!['{'],
impl_items: [ImplItem], impl_items: [ImplItem],
RCurly T!['}']
} }
struct ConstDef: VisibilityOwner, NameOwner, TypeParamsOwner, AttrsOwner, DocCommentsOwner, TypeAscriptionOwner { struct ConstDef: VisibilityOwner, NameOwner, TypeParamsOwner, AttrsOwner, DocCommentsOwner, TypeAscriptionOwner {
@ -469,7 +469,7 @@ pub(crate) const AST_SRC: AstSrc = AstSrc {
struct Literal { LiteralToken } struct Literal { LiteralToken }
struct MatchExpr: AttrsOwner { T![match], Expr, MatchArmList } struct MatchExpr: AttrsOwner { T![match], Expr, MatchArmList }
struct MatchArmList: AttrsOwner { LCurly, arms: [MatchArm], RCurly } struct MatchArmList: AttrsOwner { T!['{'], arms: [MatchArm], T!['}'] }
struct MatchArm: AttrsOwner { struct MatchArm: AttrsOwner {
pat: Pat, pat: Pat,
guard: MatchGuard, guard: MatchGuard,
@ -480,11 +480,11 @@ pub(crate) const AST_SRC: AstSrc = AstSrc {
struct RecordLit { Path, RecordFieldList} struct RecordLit { Path, RecordFieldList}
struct RecordFieldList { struct RecordFieldList {
LCurly, T!['{'],
fields: [RecordField], fields: [RecordField],
Dotdot, Dotdot,
spread: Expr, spread: Expr,
RCurly T!['}']
} }
struct RecordField: AttrsOwner { NameRef, Colon, Expr } struct RecordField: AttrsOwner { NameRef, Colon, Expr }
@ -503,12 +503,12 @@ pub(crate) const AST_SRC: AstSrc = AstSrc {
struct RecordPat { RecordFieldPatList, Path } struct RecordPat { RecordFieldPatList, Path }
struct RecordFieldPatList { struct RecordFieldPatList {
LCurly, T!['{'],
pats: [RecordInnerPat], pats: [RecordInnerPat],
record_field_pats: [RecordFieldPat], record_field_pats: [RecordFieldPat],
bind_pats: [BindPat], bind_pats: [BindPat],
Dotdot, Dotdot,
RCurly T!['}']
} }
struct RecordFieldPat: AttrsOwner, NameOwner { Colon, Pat } 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 Condition { T![let], Pat, Eq, Expr }
struct Block: AttrsOwner, ModuleItemOwner { struct Block: AttrsOwner, ModuleItemOwner {
LCurly, T!['{'],
statements: [Stmt], statements: [Stmt],
Expr, Expr,
RCurly, T!['}'],
} }
struct ParamList { struct ParamList {
LParen, LParen,
@ -580,7 +580,7 @@ pub(crate) const AST_SRC: AstSrc = AstSrc {
Path, Star, UseTreeList, Alias Path, Star, UseTreeList, Alias
} }
struct Alias: NameOwner { T![as] } struct Alias: NameOwner { T![as] }
struct UseTreeList { LCurly, use_trees: [UseTree], RCurly } struct UseTreeList { T!['{'], use_trees: [UseTree], T!['}'] }
struct ExternCrateItem: AttrsOwner, VisibilityOwner { struct ExternCrateItem: AttrsOwner, VisibilityOwner {
T![extern], T![crate], NameRef, Alias, T![extern], T![crate], NameRef, Alias,
} }
@ -619,9 +619,9 @@ pub(crate) const AST_SRC: AstSrc = AstSrc {
} }
struct ExternItemList: ModuleItemOwner { struct ExternItemList: ModuleItemOwner {
LCurly, T!['{'],
extern_items: [ExternItem], extern_items: [ExternItem],
RCurly T!['}']
} }
struct ExternBlock { struct ExternBlock {
@ -769,8 +769,6 @@ pub(crate) const AST_SRC: AstSrc = AstSrc {
}, },
token_enums: &ast_enums! { token_enums: &ast_enums! {
enum LeftDelimiter { LParen, LBrack, LCurly }
enum RightDelimiter { RParen, RBrack, RCurly }
enum RangeSeparator { Dotdot, Dotdotdot, Dotdoteq} enum RangeSeparator { Dotdot, Dotdotdot, Dotdoteq}
enum BinOp { enum BinOp {