Skip to content

Commit 2334992

Browse files
committed
Add #[sep] to several AST fields
1 parent 6d83b44 commit 2334992

File tree

3 files changed

+160
-37
lines changed

3 files changed

+160
-37
lines changed

crates/ra_syntax/src/ast/extensions.rs

+24
Original file line numberDiff line numberDiff line change
@@ -529,3 +529,27 @@ impl ast::TokenTree {
529529
self.syntax().last_child_or_token().and_then(ast::RightDelimiter::cast_element)
530530
}
531531
}
532+
533+
impl ast::Condition {
534+
pub fn pat(&self) -> Option<ast::Pat> {
535+
if self.eq().is_some() {
536+
self.syntax()
537+
.children_with_tokens()
538+
.take_while(|x| x.kind() != T![=])
539+
.find_map(ast::Pat::cast_element)
540+
} else {
541+
None
542+
}
543+
}
544+
545+
pub fn expr(&self) -> Option<ast::Expr> {
546+
if self.eq().is_some() {
547+
self.syntax()
548+
.children_with_tokens()
549+
.skip_while(|x| x.kind() != T![=])
550+
.find_map(ast::Expr::cast_element)
551+
} else {
552+
child_opt(self)
553+
}
554+
}
555+
}

crates/ra_syntax/src/ast/generated.rs

+125-29
Original file line numberDiff line numberDiff line change
@@ -6303,13 +6303,22 @@ impl ast::DocCommentsOwner for EnumVariant {}
63036303
impl ast::AttrsOwner for EnumVariant {}
63046304
impl EnumVariant {
63056305
pub fn field_def_list(&self) -> Option<FieldDefList> {
6306-
self.syntax.children().filter_map(FieldDefList::cast).next()
6306+
self.syntax
6307+
.children_with_tokens()
6308+
.take_while(|x| !Eq::can_cast_element(x.kind()))
6309+
.filter_map(FieldDefList::cast_element)
6310+
.next()
63076311
}
63086312
pub fn eq(&self) -> Option<Eq> {
63096313
self.syntax.children_with_tokens().filter_map(Eq::cast_element).next()
63106314
}
63116315
pub fn expr(&self) -> Option<Expr> {
6312-
self.syntax.children().filter_map(Expr::cast).next()
6316+
self.syntax
6317+
.children_with_tokens()
6318+
.skip_while(|x| !Eq::can_cast_element(x.kind()))
6319+
.skip(1)
6320+
.filter_map(Expr::cast_element)
6321+
.next()
63136322
}
63146323
}
63156324
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -6572,19 +6581,37 @@ impl ast::DocCommentsOwner for ConstDef {}
65726581
impl ast::TypeAscriptionOwner for ConstDef {}
65736582
impl ConstDef {
65746583
pub fn default_kw(&self) -> Option<DefaultKw> {
6575-
self.syntax.children_with_tokens().filter_map(DefaultKw::cast_element).next()
6584+
self.syntax
6585+
.children_with_tokens()
6586+
.take_while(|x| !Eq::can_cast_element(x.kind()))
6587+
.filter_map(DefaultKw::cast_element)
6588+
.next()
65766589
}
65776590
pub fn const_kw(&self) -> Option<ConstKw> {
6578-
self.syntax.children_with_tokens().filter_map(ConstKw::cast_element).next()
6591+
self.syntax
6592+
.children_with_tokens()
6593+
.take_while(|x| !Eq::can_cast_element(x.kind()))
6594+
.filter_map(ConstKw::cast_element)
6595+
.next()
65796596
}
65806597
pub fn eq(&self) -> Option<Eq> {
65816598
self.syntax.children_with_tokens().filter_map(Eq::cast_element).next()
65826599
}
65836600
pub fn body(&self) -> Option<Expr> {
6584-
self.syntax.children().filter_map(Expr::cast).next()
6601+
self.syntax
6602+
.children_with_tokens()
6603+
.skip_while(|x| !Eq::can_cast_element(x.kind()))
6604+
.skip(1)
6605+
.filter_map(Expr::cast_element)
6606+
.next()
65856607
}
65866608
pub fn semi(&self) -> Option<Semi> {
6587-
self.syntax.children_with_tokens().filter_map(Semi::cast_element).next()
6609+
self.syntax
6610+
.children_with_tokens()
6611+
.skip_while(|x| !Eq::can_cast_element(x.kind()))
6612+
.skip(1)
6613+
.filter_map(Semi::cast_element)
6614+
.next()
65886615
}
65896616
}
65906617
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -6646,19 +6673,37 @@ impl ast::DocCommentsOwner for StaticDef {}
66466673
impl ast::TypeAscriptionOwner for StaticDef {}
66476674
impl StaticDef {
66486675
pub fn static_kw(&self) -> Option<StaticKw> {
6649-
self.syntax.children_with_tokens().filter_map(StaticKw::cast_element).next()
6676+
self.syntax
6677+
.children_with_tokens()
6678+
.take_while(|x| !Eq::can_cast_element(x.kind()))
6679+
.filter_map(StaticKw::cast_element)
6680+
.next()
66506681
}
66516682
pub fn mut_kw(&self) -> Option<MutKw> {
6652-
self.syntax.children_with_tokens().filter_map(MutKw::cast_element).next()
6683+
self.syntax
6684+
.children_with_tokens()
6685+
.take_while(|x| !Eq::can_cast_element(x.kind()))
6686+
.filter_map(MutKw::cast_element)
6687+
.next()
66536688
}
66546689
pub fn eq(&self) -> Option<Eq> {
66556690
self.syntax.children_with_tokens().filter_map(Eq::cast_element).next()
66566691
}
66576692
pub fn body(&self) -> Option<Expr> {
6658-
self.syntax.children().filter_map(Expr::cast).next()
6693+
self.syntax
6694+
.children_with_tokens()
6695+
.skip_while(|x| !Eq::can_cast_element(x.kind()))
6696+
.skip(1)
6697+
.filter_map(Expr::cast_element)
6698+
.next()
66596699
}
66606700
pub fn semi(&self) -> Option<Semi> {
6661-
self.syntax.children_with_tokens().filter_map(Semi::cast_element).next()
6701+
self.syntax
6702+
.children_with_tokens()
6703+
.skip_while(|x| !Eq::can_cast_element(x.kind()))
6704+
.skip(1)
6705+
.filter_map(Semi::cast_element)
6706+
.next()
66626707
}
66636708
}
66646709
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -6720,19 +6765,37 @@ impl ast::DocCommentsOwner for TypeAliasDef {}
67206765
impl ast::TypeBoundsOwner for TypeAliasDef {}
67216766
impl TypeAliasDef {
67226767
pub fn default_kw(&self) -> Option<DefaultKw> {
6723-
self.syntax.children_with_tokens().filter_map(DefaultKw::cast_element).next()
6768+
self.syntax
6769+
.children_with_tokens()
6770+
.take_while(|x| !Eq::can_cast_element(x.kind()))
6771+
.filter_map(DefaultKw::cast_element)
6772+
.next()
67246773
}
67256774
pub fn type_kw(&self) -> Option<TypeKw> {
6726-
self.syntax.children_with_tokens().filter_map(TypeKw::cast_element).next()
6775+
self.syntax
6776+
.children_with_tokens()
6777+
.take_while(|x| !Eq::can_cast_element(x.kind()))
6778+
.filter_map(TypeKw::cast_element)
6779+
.next()
67276780
}
67286781
pub fn eq(&self) -> Option<Eq> {
67296782
self.syntax.children_with_tokens().filter_map(Eq::cast_element).next()
67306783
}
67316784
pub fn type_ref(&self) -> Option<TypeRef> {
6732-
self.syntax.children().filter_map(TypeRef::cast).next()
6785+
self.syntax
6786+
.children_with_tokens()
6787+
.skip_while(|x| !Eq::can_cast_element(x.kind()))
6788+
.skip(1)
6789+
.filter_map(TypeRef::cast_element)
6790+
.next()
67336791
}
67346792
pub fn semi(&self) -> Option<Semi> {
6735-
self.syntax.children_with_tokens().filter_map(Semi::cast_element).next()
6793+
self.syntax
6794+
.children_with_tokens()
6795+
.skip_while(|x| !Eq::can_cast_element(x.kind()))
6796+
.skip(1)
6797+
.filter_map(Semi::cast_element)
6798+
.next()
67366799
}
67376800
}
67386801
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -8157,16 +8220,29 @@ impl ast::AttrsOwner for ForExpr {}
81578220
impl ast::LoopBodyOwner for ForExpr {}
81588221
impl ForExpr {
81598222
pub fn for_kw(&self) -> Option<ForKw> {
8160-
self.syntax.children_with_tokens().filter_map(ForKw::cast_element).next()
8223+
self.syntax
8224+
.children_with_tokens()
8225+
.take_while(|x| !InKw::can_cast_element(x.kind()))
8226+
.filter_map(ForKw::cast_element)
8227+
.next()
81618228
}
81628229
pub fn pat(&self) -> Option<Pat> {
8163-
self.syntax.children().filter_map(Pat::cast).next()
8230+
self.syntax
8231+
.children_with_tokens()
8232+
.take_while(|x| !InKw::can_cast_element(x.kind()))
8233+
.filter_map(Pat::cast_element)
8234+
.next()
81648235
}
81658236
pub fn in_kw(&self) -> Option<InKw> {
81668237
self.syntax.children_with_tokens().filter_map(InKw::cast_element).next()
81678238
}
81688239
pub fn iterable(&self) -> Option<Expr> {
8169-
self.syntax.children().filter_map(Expr::cast).next()
8240+
self.syntax
8241+
.children_with_tokens()
8242+
.skip_while(|x| !InKw::can_cast_element(x.kind()))
8243+
.skip(1)
8244+
.filter_map(Expr::cast_element)
8245+
.next()
81708246
}
81718247
}
81728248
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -9498,16 +9574,29 @@ impl AstElement for MatchArm {
94989574
impl ast::AttrsOwner for MatchArm {}
94999575
impl MatchArm {
95009576
pub fn pat(&self) -> Option<Pat> {
9501-
self.syntax.children().filter_map(Pat::cast).next()
9577+
self.syntax
9578+
.children_with_tokens()
9579+
.take_while(|x| !FatArrow::can_cast_element(x.kind()))
9580+
.filter_map(Pat::cast_element)
9581+
.next()
95029582
}
95039583
pub fn guard(&self) -> Option<MatchGuard> {
9504-
self.syntax.children().filter_map(MatchGuard::cast).next()
9584+
self.syntax
9585+
.children_with_tokens()
9586+
.take_while(|x| !FatArrow::can_cast_element(x.kind()))
9587+
.filter_map(MatchGuard::cast_element)
9588+
.next()
95059589
}
95069590
pub fn fat_arrow(&self) -> Option<FatArrow> {
95079591
self.syntax.children_with_tokens().filter_map(FatArrow::cast_element).next()
95089592
}
95099593
pub fn expr(&self) -> Option<Expr> {
9510-
self.syntax.children().filter_map(Expr::cast).next()
9594+
self.syntax
9595+
.children_with_tokens()
9596+
.skip_while(|x| !FatArrow::can_cast_element(x.kind()))
9597+
.skip(1)
9598+
.filter_map(Expr::cast_element)
9599+
.next()
95119600
}
95129601
}
95139602
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -11753,16 +11842,29 @@ impl ast::AttrsOwner for LetStmt {}
1175311842
impl ast::TypeAscriptionOwner for LetStmt {}
1175411843
impl LetStmt {
1175511844
pub fn let_kw(&self) -> Option<LetKw> {
11756-
self.syntax.children_with_tokens().filter_map(LetKw::cast_element).next()
11845+
self.syntax
11846+
.children_with_tokens()
11847+
.take_while(|x| !Eq::can_cast_element(x.kind()))
11848+
.filter_map(LetKw::cast_element)
11849+
.next()
1175711850
}
1175811851
pub fn pat(&self) -> Option<Pat> {
11759-
self.syntax.children().filter_map(Pat::cast).next()
11852+
self.syntax
11853+
.children_with_tokens()
11854+
.take_while(|x| !Eq::can_cast_element(x.kind()))
11855+
.filter_map(Pat::cast_element)
11856+
.next()
1176011857
}
1176111858
pub fn eq(&self) -> Option<Eq> {
1176211859
self.syntax.children_with_tokens().filter_map(Eq::cast_element).next()
1176311860
}
1176411861
pub fn initializer(&self) -> Option<Expr> {
11765-
self.syntax.children().filter_map(Expr::cast).next()
11862+
self.syntax
11863+
.children_with_tokens()
11864+
.skip_while(|x| !Eq::can_cast_element(x.kind()))
11865+
.skip(1)
11866+
.filter_map(Expr::cast_element)
11867+
.next()
1176611868
}
1176711869
}
1176811870
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -11820,15 +11922,9 @@ impl Condition {
1182011922
pub fn let_kw(&self) -> Option<LetKw> {
1182111923
self.syntax.children_with_tokens().filter_map(LetKw::cast_element).next()
1182211924
}
11823-
pub fn pat(&self) -> Option<Pat> {
11824-
self.syntax.children().filter_map(Pat::cast).next()
11825-
}
1182611925
pub fn eq(&self) -> Option<Eq> {
1182711926
self.syntax.children_with_tokens().filter_map(Eq::cast_element).next()
1182811927
}
11829-
pub fn expr(&self) -> Option<Expr> {
11830-
self.syntax.children().filter_map(Expr::cast).next()
11831-
}
1183211928
}
1183311929
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1183411930
pub struct Block {

xtask/src/ast_src.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ pub(crate) const AST_SRC: AstSrc = AstSrc {
353353
}
354354
struct EnumVariant: VisibilityOwner, NameOwner, DocCommentsOwner, AttrsOwner {
355355
FieldDefList,
356-
Eq,
356+
#[sep] Eq,
357357
Expr
358358
}
359359

@@ -379,23 +379,23 @@ pub(crate) const AST_SRC: AstSrc = AstSrc {
379379
struct ConstDef: VisibilityOwner, NameOwner, TypeParamsOwner, AttrsOwner, DocCommentsOwner, TypeAscriptionOwner {
380380
DefaultKw,
381381
ConstKw,
382-
Eq,
382+
#[sep] Eq,
383383
body: Expr,
384384
Semi
385385
}
386386

387387
struct StaticDef: VisibilityOwner, NameOwner, TypeParamsOwner, AttrsOwner, DocCommentsOwner, TypeAscriptionOwner {
388388
StaticKw,
389389
MutKw,
390-
Eq,
390+
#[sep] Eq,
391391
body: Expr,
392392
Semi
393393
}
394394

395395
struct TypeAliasDef: VisibilityOwner, NameOwner, TypeParamsOwner, AttrsOwner, DocCommentsOwner, TypeBoundsOwner {
396396
DefaultKw,
397397
TypeKw,
398-
Eq,
398+
#[sep] Eq,
399399
TypeRef,
400400
Semi
401401
}
@@ -442,7 +442,7 @@ pub(crate) const AST_SRC: AstSrc = AstSrc {
442442
struct ForExpr: AttrsOwner, LoopBodyOwner {
443443
ForKw,
444444
Pat,
445-
InKw,
445+
#[sep] InKw,
446446
iterable: Expr,
447447
}
448448
struct WhileExpr: AttrsOwner, LoopBodyOwner { WhileKw, Condition }
@@ -472,7 +472,7 @@ pub(crate) const AST_SRC: AstSrc = AstSrc {
472472
struct MatchArm: AttrsOwner {
473473
pat: Pat,
474474
guard: MatchGuard,
475-
FatArrow,
475+
#[sep] FatArrow,
476476
Expr,
477477
}
478478
struct MatchGuard { IfKw, Expr }
@@ -548,10 +548,13 @@ pub(crate) const AST_SRC: AstSrc = AstSrc {
548548
struct LetStmt: AttrsOwner, TypeAscriptionOwner {
549549
LetKw,
550550
Pat,
551-
Eq,
551+
#[sep] Eq,
552552
initializer: Expr,
553553
}
554-
struct Condition { LetKw, Pat, Eq, Expr }
554+
struct Condition {
555+
LetKw,
556+
Eq
557+
}
555558
struct Block: AttrsOwner, ModuleItemOwner {
556559
LCurly,
557560
statements: [Stmt],

0 commit comments

Comments
 (0)