mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 04:44:57 +00:00
Curley tokens
This commit is contained in:
parent
1c5d859195
commit
460c8bbdec
6 changed files with 47 additions and 131 deletions
|
@ -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))
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue