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::{
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<ast::UseTre
.filter(|it| it.kind() != T!['{'] && it.kind() != T!['}']),
);
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);
Some(lhs.with_use_tree_list(use_tree_list))
}

View file

@ -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)

View file

@ -416,11 +416,17 @@ impl ast::RangePat {
}
impl ast::TokenTree {
pub fn left_delimiter(&self) -> Option<ast::LeftDelimiter> {
self.syntax().first_child_or_token()?.into_token().and_then(ast::LeftDelimiter::cast)
pub fn left_delimiter_token(&self) -> Option<SyntaxToken> {
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> {
self.syntax().last_child_or_token()?.into_token().and_then(ast::RightDelimiter::cast)
pub fn right_delimiter_token(&self) -> Option<SyntaxToken> {
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 }
}
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 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)]
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<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 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)]
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<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 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)]
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<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 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)]
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<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 dotdot_token(&self) -> Option<Dotdot> { support::token(&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)]
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<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 record_field_pats(&self) -> AstChildren<RecordFieldPat> {
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 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)]
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<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 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)]
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<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 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)]
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<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 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)]
pub struct ExternBlock {

View file

@ -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<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 {
Dotdot(Dotdot),
Dotdotdot(Dotdotdot),

View file

@ -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 {