aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/libeditor/src/typing.rs43
-rw-r--r--crates/libeditor/tests/test.rs11
-rw-r--r--crates/libsyntax2/src/ast/generated.rs688
-rw-r--r--crates/libsyntax2/src/grammar.ron69
-rw-r--r--crates/libsyntax2/src/lib.rs4
5 files changed, 811 insertions, 4 deletions
diff --git a/crates/libeditor/src/typing.rs b/crates/libeditor/src/typing.rs
index e7eba671f..060095f28 100644
--- a/crates/libeditor/src/typing.rs
+++ b/crates/libeditor/src/typing.rs
@@ -1,5 +1,8 @@
1use std::mem;
2
1use libsyntax2::{ 3use libsyntax2::{
2 TextUnit, TextRange, SyntaxNodeRef, File, 4 TextUnit, TextRange, SyntaxNodeRef, File, AstNode,
5 ast,
3 algo::{ 6 algo::{
4 walk::preorder, 7 walk::preorder,
5 find_covering_node, 8 find_covering_node,
@@ -60,6 +63,9 @@ fn remove_newline(
60 offset: TextUnit, 63 offset: TextUnit,
61) { 64) {
62 if node.kind() == WHITESPACE && node_text.bytes().filter(|&b| b == b'\n').count() == 1 { 65 if node.kind() == WHITESPACE && node_text.bytes().filter(|&b| b == b'\n').count() == 1 {
66 if join_lambda_body(edit, node).is_some() {
67 return
68 }
63 match (node.prev_sibling(), node.next_sibling()) { 69 match (node.prev_sibling(), node.next_sibling()) {
64 (Some(prev), Some(next)) => { 70 (Some(prev), Some(next)) => {
65 let range = TextRange::from_to(prev.range().start(), node.range().end()); 71 let range = TextRange::from_to(prev.range().start(), node.range().end());
@@ -91,6 +97,41 @@ fn remove_newline(
91 ); 97 );
92} 98}
93 99
100fn join_lambda_body(
101 edit: &mut EditBuilder,
102 node: SyntaxNodeRef,
103) -> Option<()> {
104 let block = ast::Block::cast(node.parent()?)?;
105 let block_expr = ast::BlockExpr::cast(block.syntax().parent()?)?;
106 let _lambda = ast::LambdaExpr::cast(block_expr.syntax().parent()?)?;
107 let expr = single_expr(block)?;
108 edit.replace(
109 block_expr.syntax().range(),
110 expr.syntax().text(),
111 );
112 Some(())
113}
114
115fn single_expr(block: ast::Block) -> Option<ast::Expr> {
116 let mut res = None;
117 for child in block.syntax().children() {
118 if let Some(expr) = ast::Expr::cast(child) {
119 if expr.syntax().text().contains('\n') {
120 return None;
121 }
122 if mem::replace(&mut res, Some(expr)).is_some() {
123 return None;
124 }
125 } else {
126 match child.kind() {
127 WHITESPACE | L_CURLY | R_CURLY => (),
128 _ => return None,
129 }
130 }
131 }
132 res
133}
134
94fn compute_ws(left: SyntaxNodeRef, right: SyntaxNodeRef) -> &'static str { 135fn compute_ws(left: SyntaxNodeRef, right: SyntaxNodeRef) -> &'static str {
95 match left.kind() { 136 match left.kind() {
96 L_PAREN | L_BRACK => return "", 137 L_PAREN | L_BRACK => return "",
diff --git a/crates/libeditor/tests/test.rs b/crates/libeditor/tests/test.rs
index ab325c520..7e5e75ec2 100644
--- a/crates/libeditor/tests/test.rs
+++ b/crates/libeditor/tests/test.rs
@@ -199,6 +199,17 @@ fn foo() {
199 <|>foo(1) 199 <|>foo(1)
200} 200}
201"); 201");
202 do_check(r"
203pub fn reparse(&self, edit: &AtomEdit) -> File {
204 <|>self.incremental_reparse(edit).unwrap_or_else(|| {
205 self.full_reparse(edit)
206 })
207}
208", r"
209pub fn reparse(&self, edit: &AtomEdit) -> File {
210 <|>self.incremental_reparse(edit).unwrap_or_else(|| self.full_reparse(edit))
211}
212");
202} 213}
203 214
204#[test] 215#[test]
diff --git a/crates/libsyntax2/src/ast/generated.rs b/crates/libsyntax2/src/ast/generated.rs
index 52668c37b..7d746a85b 100644
--- a/crates/libsyntax2/src/ast/generated.rs
+++ b/crates/libsyntax2/src/ast/generated.rs
@@ -4,6 +4,24 @@ use {
4 SyntaxKind::*, 4 SyntaxKind::*,
5}; 5};
6 6
7// ArrayExpr
8#[derive(Debug, Clone, Copy)]
9pub struct ArrayExpr<'a> {
10 syntax: SyntaxNodeRef<'a>,
11}
12
13impl<'a> AstNode<'a> for ArrayExpr<'a> {
14 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
15 match syntax.kind() {
16 ARRAY_EXPR => Some(ArrayExpr { syntax }),
17 _ => None,
18 }
19 }
20 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
21}
22
23impl<'a> ArrayExpr<'a> {}
24
7// ArrayType 25// ArrayType
8#[derive(Debug, Clone, Copy)] 26#[derive(Debug, Clone, Copy)]
9pub struct ArrayType<'a> { 27pub struct ArrayType<'a> {
@@ -44,6 +62,114 @@ impl<'a> Attr<'a> {
44 } 62 }
45} 63}
46 64
65// BinExpr
66#[derive(Debug, Clone, Copy)]
67pub struct BinExpr<'a> {
68 syntax: SyntaxNodeRef<'a>,
69}
70
71impl<'a> AstNode<'a> for BinExpr<'a> {
72 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
73 match syntax.kind() {
74 BIN_EXPR => Some(BinExpr { syntax }),
75 _ => None,
76 }
77 }
78 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
79}
80
81impl<'a> BinExpr<'a> {}
82
83// Block
84#[derive(Debug, Clone, Copy)]
85pub struct Block<'a> {
86 syntax: SyntaxNodeRef<'a>,
87}
88
89impl<'a> AstNode<'a> for Block<'a> {
90 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
91 match syntax.kind() {
92 BLOCK => Some(Block { syntax }),
93 _ => None,
94 }
95 }
96 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
97}
98
99impl<'a> Block<'a> {}
100
101// BlockExpr
102#[derive(Debug, Clone, Copy)]
103pub struct BlockExpr<'a> {
104 syntax: SyntaxNodeRef<'a>,
105}
106
107impl<'a> AstNode<'a> for BlockExpr<'a> {
108 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
109 match syntax.kind() {
110 BLOCK_EXPR => Some(BlockExpr { syntax }),
111 _ => None,
112 }
113 }
114 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
115}
116
117impl<'a> BlockExpr<'a> {}
118
119// BreakExpr
120#[derive(Debug, Clone, Copy)]
121pub struct BreakExpr<'a> {
122 syntax: SyntaxNodeRef<'a>,
123}
124
125impl<'a> AstNode<'a> for BreakExpr<'a> {
126 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
127 match syntax.kind() {
128 BREAK_EXPR => Some(BreakExpr { syntax }),
129 _ => None,
130 }
131 }
132 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
133}
134
135impl<'a> BreakExpr<'a> {}
136
137// CallExpr
138#[derive(Debug, Clone, Copy)]
139pub struct CallExpr<'a> {
140 syntax: SyntaxNodeRef<'a>,
141}
142
143impl<'a> AstNode<'a> for CallExpr<'a> {
144 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
145 match syntax.kind() {
146 CALL_EXPR => Some(CallExpr { syntax }),
147 _ => None,
148 }
149 }
150 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
151}
152
153impl<'a> CallExpr<'a> {}
154
155// CastExpr
156#[derive(Debug, Clone, Copy)]
157pub struct CastExpr<'a> {
158 syntax: SyntaxNodeRef<'a>,
159}
160
161impl<'a> AstNode<'a> for CastExpr<'a> {
162 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
163 match syntax.kind() {
164 CAST_EXPR => Some(CastExpr { syntax }),
165 _ => None,
166 }
167 }
168 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
169}
170
171impl<'a> CastExpr<'a> {}
172
47// ConstDef 173// ConstDef
48#[derive(Debug, Clone, Copy)] 174#[derive(Debug, Clone, Copy)]
49pub struct ConstDef<'a> { 175pub struct ConstDef<'a> {
@@ -65,6 +191,24 @@ impl<'a> ast::TypeParamsOwner<'a> for ConstDef<'a> {}
65impl<'a> ast::AttrsOwner<'a> for ConstDef<'a> {} 191impl<'a> ast::AttrsOwner<'a> for ConstDef<'a> {}
66impl<'a> ConstDef<'a> {} 192impl<'a> ConstDef<'a> {}
67 193
194// ContinueExpr
195#[derive(Debug, Clone, Copy)]
196pub struct ContinueExpr<'a> {
197 syntax: SyntaxNodeRef<'a>,
198}
199
200impl<'a> AstNode<'a> for ContinueExpr<'a> {
201 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
202 match syntax.kind() {
203 CONTINUE_EXPR => Some(ContinueExpr { syntax }),
204 _ => None,
205 }
206 }
207 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
208}
209
210impl<'a> ContinueExpr<'a> {}
211
68// DynTraitType 212// DynTraitType
69#[derive(Debug, Clone, Copy)] 213#[derive(Debug, Clone, Copy)]
70pub struct DynTraitType<'a> { 214pub struct DynTraitType<'a> {
@@ -104,6 +248,136 @@ impl<'a> ast::TypeParamsOwner<'a> for EnumDef<'a> {}
104impl<'a> ast::AttrsOwner<'a> for EnumDef<'a> {} 248impl<'a> ast::AttrsOwner<'a> for EnumDef<'a> {}
105impl<'a> EnumDef<'a> {} 249impl<'a> EnumDef<'a> {}
106 250
251// Expr
252#[derive(Debug, Clone, Copy)]
253pub enum Expr<'a> {
254 TupleExpr(TupleExpr<'a>),
255 ArrayExpr(ArrayExpr<'a>),
256 ParenExpr(ParenExpr<'a>),
257 PathExpr(PathExpr<'a>),
258 LambdaExpr(LambdaExpr<'a>),
259 IfExpr(IfExpr<'a>),
260 LoopExpr(LoopExpr<'a>),
261 ForExpr(ForExpr<'a>),
262 WhileExpr(WhileExpr<'a>),
263 ContinueExpr(ContinueExpr<'a>),
264 BreakExpr(BreakExpr<'a>),
265 Label(Label<'a>),
266 BlockExpr(BlockExpr<'a>),
267 ReturnExpr(ReturnExpr<'a>),
268 MatchExpr(MatchExpr<'a>),
269 MatchArmList(MatchArmList<'a>),
270 MatchArm(MatchArm<'a>),
271 MatchGuard(MatchGuard<'a>),
272 StructLit(StructLit<'a>),
273 NamedFieldList(NamedFieldList<'a>),
274 NamedField(NamedField<'a>),
275 CallExpr(CallExpr<'a>),
276 IndexExpr(IndexExpr<'a>),
277 MethodCallExpr(MethodCallExpr<'a>),
278 FieldExpr(FieldExpr<'a>),
279 TryExpr(TryExpr<'a>),
280 CastExpr(CastExpr<'a>),
281 RefExpr(RefExpr<'a>),
282 PrefixExpr(PrefixExpr<'a>),
283 RangeExpr(RangeExpr<'a>),
284 BinExpr(BinExpr<'a>),
285}
286
287impl<'a> AstNode<'a> for Expr<'a> {
288 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
289 match syntax.kind() {
290 TUPLE_EXPR => Some(Expr::TupleExpr(TupleExpr { syntax })),
291 ARRAY_EXPR => Some(Expr::ArrayExpr(ArrayExpr { syntax })),
292 PAREN_EXPR => Some(Expr::ParenExpr(ParenExpr { syntax })),
293 PATH_EXPR => Some(Expr::PathExpr(PathExpr { syntax })),
294 LAMBDA_EXPR => Some(Expr::LambdaExpr(LambdaExpr { syntax })),
295 IF_EXPR => Some(Expr::IfExpr(IfExpr { syntax })),
296 LOOP_EXPR => Some(Expr::LoopExpr(LoopExpr { syntax })),
297 FOR_EXPR => Some(Expr::ForExpr(ForExpr { syntax })),
298 WHILE_EXPR => Some(Expr::WhileExpr(WhileExpr { syntax })),
299 CONTINUE_EXPR => Some(Expr::ContinueExpr(ContinueExpr { syntax })),
300 BREAK_EXPR => Some(Expr::BreakExpr(BreakExpr { syntax })),
301 LABEL => Some(Expr::Label(Label { syntax })),
302 BLOCK_EXPR => Some(Expr::BlockExpr(BlockExpr { syntax })),
303 RETURN_EXPR => Some(Expr::ReturnExpr(ReturnExpr { syntax })),
304 MATCH_EXPR => Some(Expr::MatchExpr(MatchExpr { syntax })),
305 MATCH_ARM_LIST => Some(Expr::MatchArmList(MatchArmList { syntax })),
306 MATCH_ARM => Some(Expr::MatchArm(MatchArm { syntax })),
307 MATCH_GUARD => Some(Expr::MatchGuard(MatchGuard { syntax })),
308 STRUCT_LIT => Some(Expr::StructLit(StructLit { syntax })),
309 NAMED_FIELD_LIST => Some(Expr::NamedFieldList(NamedFieldList { syntax })),
310 NAMED_FIELD => Some(Expr::NamedField(NamedField { syntax })),
311 CALL_EXPR => Some(Expr::CallExpr(CallExpr { syntax })),
312 INDEX_EXPR => Some(Expr::IndexExpr(IndexExpr { syntax })),
313 METHOD_CALL_EXPR => Some(Expr::MethodCallExpr(MethodCallExpr { syntax })),
314 FIELD_EXPR => Some(Expr::FieldExpr(FieldExpr { syntax })),
315 TRY_EXPR => Some(Expr::TryExpr(TryExpr { syntax })),
316 CAST_EXPR => Some(Expr::CastExpr(CastExpr { syntax })),
317 REF_EXPR => Some(Expr::RefExpr(RefExpr { syntax })),
318 PREFIX_EXPR => Some(Expr::PrefixExpr(PrefixExpr { syntax })),
319 RANGE_EXPR => Some(Expr::RangeExpr(RangeExpr { syntax })),
320 BIN_EXPR => Some(Expr::BinExpr(BinExpr { syntax })),
321 _ => None,
322 }
323 }
324 fn syntax(self) -> SyntaxNodeRef<'a> {
325 match self {
326 Expr::TupleExpr(inner) => inner.syntax(),
327 Expr::ArrayExpr(inner) => inner.syntax(),
328 Expr::ParenExpr(inner) => inner.syntax(),
329 Expr::PathExpr(inner) => inner.syntax(),
330 Expr::LambdaExpr(inner) => inner.syntax(),
331 Expr::IfExpr(inner) => inner.syntax(),
332 Expr::LoopExpr(inner) => inner.syntax(),
333 Expr::ForExpr(inner) => inner.syntax(),
334 Expr::WhileExpr(inner) => inner.syntax(),
335 Expr::ContinueExpr(inner) => inner.syntax(),
336 Expr::BreakExpr(inner) => inner.syntax(),
337 Expr::Label(inner) => inner.syntax(),
338 Expr::BlockExpr(inner) => inner.syntax(),
339 Expr::ReturnExpr(inner) => inner.syntax(),
340 Expr::MatchExpr(inner) => inner.syntax(),
341 Expr::MatchArmList(inner) => inner.syntax(),
342 Expr::MatchArm(inner) => inner.syntax(),
343 Expr::MatchGuard(inner) => inner.syntax(),
344 Expr::StructLit(inner) => inner.syntax(),
345 Expr::NamedFieldList(inner) => inner.syntax(),
346 Expr::NamedField(inner) => inner.syntax(),
347 Expr::CallExpr(inner) => inner.syntax(),
348 Expr::IndexExpr(inner) => inner.syntax(),
349 Expr::MethodCallExpr(inner) => inner.syntax(),
350 Expr::FieldExpr(inner) => inner.syntax(),
351 Expr::TryExpr(inner) => inner.syntax(),
352 Expr::CastExpr(inner) => inner.syntax(),
353 Expr::RefExpr(inner) => inner.syntax(),
354 Expr::PrefixExpr(inner) => inner.syntax(),
355 Expr::RangeExpr(inner) => inner.syntax(),
356 Expr::BinExpr(inner) => inner.syntax(),
357 }
358 }
359}
360
361impl<'a> Expr<'a> {}
362
363// FieldExpr
364#[derive(Debug, Clone, Copy)]
365pub struct FieldExpr<'a> {
366 syntax: SyntaxNodeRef<'a>,
367}
368
369impl<'a> AstNode<'a> for FieldExpr<'a> {
370 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
371 match syntax.kind() {
372 FIELD_EXPR => Some(FieldExpr { syntax }),
373 _ => None,
374 }
375 }
376 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
377}
378
379impl<'a> FieldExpr<'a> {}
380
107// FnDef 381// FnDef
108#[derive(Debug, Clone, Copy)] 382#[derive(Debug, Clone, Copy)]
109pub struct FnDef<'a> { 383pub struct FnDef<'a> {
@@ -143,6 +417,24 @@ impl<'a> AstNode<'a> for FnPointerType<'a> {
143 417
144impl<'a> FnPointerType<'a> {} 418impl<'a> FnPointerType<'a> {}
145 419
420// ForExpr
421#[derive(Debug, Clone, Copy)]
422pub struct ForExpr<'a> {
423 syntax: SyntaxNodeRef<'a>,
424}
425
426impl<'a> AstNode<'a> for ForExpr<'a> {
427 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
428 match syntax.kind() {
429 FOR_EXPR => Some(ForExpr { syntax }),
430 _ => None,
431 }
432 }
433 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
434}
435
436impl<'a> ForExpr<'a> {}
437
146// ForType 438// ForType
147#[derive(Debug, Clone, Copy)] 439#[derive(Debug, Clone, Copy)]
148pub struct ForType<'a> { 440pub struct ForType<'a> {
@@ -161,6 +453,24 @@ impl<'a> AstNode<'a> for ForType<'a> {
161 453
162impl<'a> ForType<'a> {} 454impl<'a> ForType<'a> {}
163 455
456// IfExpr
457#[derive(Debug, Clone, Copy)]
458pub struct IfExpr<'a> {
459 syntax: SyntaxNodeRef<'a>,
460}
461
462impl<'a> AstNode<'a> for IfExpr<'a> {
463 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
464 match syntax.kind() {
465 IF_EXPR => Some(IfExpr { syntax }),
466 _ => None,
467 }
468 }
469 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
470}
471
472impl<'a> IfExpr<'a> {}
473
164// ImplItem 474// ImplItem
165#[derive(Debug, Clone, Copy)] 475#[derive(Debug, Clone, Copy)]
166pub struct ImplItem<'a> { 476pub struct ImplItem<'a> {
@@ -197,6 +507,168 @@ impl<'a> AstNode<'a> for ImplTraitType<'a> {
197 507
198impl<'a> ImplTraitType<'a> {} 508impl<'a> ImplTraitType<'a> {}
199 509
510// IndexExpr
511#[derive(Debug, Clone, Copy)]
512pub struct IndexExpr<'a> {
513 syntax: SyntaxNodeRef<'a>,
514}
515
516impl<'a> AstNode<'a> for IndexExpr<'a> {
517 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
518 match syntax.kind() {
519 INDEX_EXPR => Some(IndexExpr { syntax }),
520 _ => None,
521 }
522 }
523 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
524}
525
526impl<'a> IndexExpr<'a> {}
527
528// Label
529#[derive(Debug, Clone, Copy)]
530pub struct Label<'a> {
531 syntax: SyntaxNodeRef<'a>,
532}
533
534impl<'a> AstNode<'a> for Label<'a> {
535 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
536 match syntax.kind() {
537 LABEL => Some(Label { syntax }),
538 _ => None,
539 }
540 }
541 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
542}
543
544impl<'a> Label<'a> {}
545
546// LambdaExpr
547#[derive(Debug, Clone, Copy)]
548pub struct LambdaExpr<'a> {
549 syntax: SyntaxNodeRef<'a>,
550}
551
552impl<'a> AstNode<'a> for LambdaExpr<'a> {
553 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
554 match syntax.kind() {
555 LAMBDA_EXPR => Some(LambdaExpr { syntax }),
556 _ => None,
557 }
558 }
559 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
560}
561
562impl<'a> LambdaExpr<'a> {}
563
564// LoopExpr
565#[derive(Debug, Clone, Copy)]
566pub struct LoopExpr<'a> {
567 syntax: SyntaxNodeRef<'a>,
568}
569
570impl<'a> AstNode<'a> for LoopExpr<'a> {
571 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
572 match syntax.kind() {
573 LOOP_EXPR => Some(LoopExpr { syntax }),
574 _ => None,
575 }
576 }
577 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
578}
579
580impl<'a> LoopExpr<'a> {}
581
582// MatchArm
583#[derive(Debug, Clone, Copy)]
584pub struct MatchArm<'a> {
585 syntax: SyntaxNodeRef<'a>,
586}
587
588impl<'a> AstNode<'a> for MatchArm<'a> {
589 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
590 match syntax.kind() {
591 MATCH_ARM => Some(MatchArm { syntax }),
592 _ => None,
593 }
594 }
595 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
596}
597
598impl<'a> MatchArm<'a> {}
599
600// MatchArmList
601#[derive(Debug, Clone, Copy)]
602pub struct MatchArmList<'a> {
603 syntax: SyntaxNodeRef<'a>,
604}
605
606impl<'a> AstNode<'a> for MatchArmList<'a> {
607 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
608 match syntax.kind() {
609 MATCH_ARM_LIST => Some(MatchArmList { syntax }),
610 _ => None,
611 }
612 }
613 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
614}
615
616impl<'a> MatchArmList<'a> {}
617
618// MatchExpr
619#[derive(Debug, Clone, Copy)]
620pub struct MatchExpr<'a> {
621 syntax: SyntaxNodeRef<'a>,
622}
623
624impl<'a> AstNode<'a> for MatchExpr<'a> {
625 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
626 match syntax.kind() {
627 MATCH_EXPR => Some(MatchExpr { syntax }),
628 _ => None,
629 }
630 }
631 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
632}
633
634impl<'a> MatchExpr<'a> {}
635
636// MatchGuard
637#[derive(Debug, Clone, Copy)]
638pub struct MatchGuard<'a> {
639 syntax: SyntaxNodeRef<'a>,
640}
641
642impl<'a> AstNode<'a> for MatchGuard<'a> {
643 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
644 match syntax.kind() {
645 MATCH_GUARD => Some(MatchGuard { syntax }),
646 _ => None,
647 }
648 }
649 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
650}
651
652impl<'a> MatchGuard<'a> {}
653
654// MethodCallExpr
655#[derive(Debug, Clone, Copy)]
656pub struct MethodCallExpr<'a> {
657 syntax: SyntaxNodeRef<'a>,
658}
659
660impl<'a> AstNode<'a> for MethodCallExpr<'a> {
661 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
662 match syntax.kind() {
663 METHOD_CALL_EXPR => Some(MethodCallExpr { syntax }),
664 _ => None,
665 }
666 }
667 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
668}
669
670impl<'a> MethodCallExpr<'a> {}
671
200// Module 672// Module
201#[derive(Debug, Clone, Copy)] 673#[derive(Debug, Clone, Copy)]
202pub struct Module<'a> { 674pub struct Module<'a> {
@@ -257,6 +729,24 @@ impl<'a> AstNode<'a> for NameRef<'a> {
257 729
258impl<'a> NameRef<'a> {} 730impl<'a> NameRef<'a> {}
259 731
732// NamedField
733#[derive(Debug, Clone, Copy)]
734pub struct NamedField<'a> {
735 syntax: SyntaxNodeRef<'a>,
736}
737
738impl<'a> AstNode<'a> for NamedField<'a> {
739 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
740 match syntax.kind() {
741 NAMED_FIELD => Some(NamedField { syntax }),
742 _ => None,
743 }
744 }
745 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
746}
747
748impl<'a> NamedField<'a> {}
749
260// NamedFieldDef 750// NamedFieldDef
261#[derive(Debug, Clone, Copy)] 751#[derive(Debug, Clone, Copy)]
262pub struct NamedFieldDef<'a> { 752pub struct NamedFieldDef<'a> {
@@ -277,6 +767,24 @@ impl<'a> ast::NameOwner<'a> for NamedFieldDef<'a> {}
277impl<'a> ast::AttrsOwner<'a> for NamedFieldDef<'a> {} 767impl<'a> ast::AttrsOwner<'a> for NamedFieldDef<'a> {}
278impl<'a> NamedFieldDef<'a> {} 768impl<'a> NamedFieldDef<'a> {}
279 769
770// NamedFieldList
771#[derive(Debug, Clone, Copy)]
772pub struct NamedFieldList<'a> {
773 syntax: SyntaxNodeRef<'a>,
774}
775
776impl<'a> AstNode<'a> for NamedFieldList<'a> {
777 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
778 match syntax.kind() {
779 NAMED_FIELD_LIST => Some(NamedFieldList { syntax }),
780 _ => None,
781 }
782 }
783 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
784}
785
786impl<'a> NamedFieldList<'a> {}
787
280// NeverType 788// NeverType
281#[derive(Debug, Clone, Copy)] 789#[derive(Debug, Clone, Copy)]
282pub struct NeverType<'a> { 790pub struct NeverType<'a> {
@@ -323,6 +831,24 @@ impl<'a> ast::TypeParamsOwner<'a> for NominalDef<'a> {}
323impl<'a> ast::AttrsOwner<'a> for NominalDef<'a> {} 831impl<'a> ast::AttrsOwner<'a> for NominalDef<'a> {}
324impl<'a> NominalDef<'a> {} 832impl<'a> NominalDef<'a> {}
325 833
834// ParenExpr
835#[derive(Debug, Clone, Copy)]
836pub struct ParenExpr<'a> {
837 syntax: SyntaxNodeRef<'a>,
838}
839
840impl<'a> AstNode<'a> for ParenExpr<'a> {
841 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
842 match syntax.kind() {
843 PAREN_EXPR => Some(ParenExpr { syntax }),
844 _ => None,
845 }
846 }
847 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
848}
849
850impl<'a> ParenExpr<'a> {}
851
326// ParenType 852// ParenType
327#[derive(Debug, Clone, Copy)] 853#[derive(Debug, Clone, Copy)]
328pub struct ParenType<'a> { 854pub struct ParenType<'a> {
@@ -341,6 +867,24 @@ impl<'a> AstNode<'a> for ParenType<'a> {
341 867
342impl<'a> ParenType<'a> {} 868impl<'a> ParenType<'a> {}
343 869
870// PathExpr
871#[derive(Debug, Clone, Copy)]
872pub struct PathExpr<'a> {
873 syntax: SyntaxNodeRef<'a>,
874}
875
876impl<'a> AstNode<'a> for PathExpr<'a> {
877 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
878 match syntax.kind() {
879 PATH_EXPR => Some(PathExpr { syntax }),
880 _ => None,
881 }
882 }
883 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
884}
885
886impl<'a> PathExpr<'a> {}
887
344// PathType 888// PathType
345#[derive(Debug, Clone, Copy)] 889#[derive(Debug, Clone, Copy)]
346pub struct PathType<'a> { 890pub struct PathType<'a> {
@@ -395,6 +939,60 @@ impl<'a> AstNode<'a> for PointerType<'a> {
395 939
396impl<'a> PointerType<'a> {} 940impl<'a> PointerType<'a> {}
397 941
942// PrefixExpr
943#[derive(Debug, Clone, Copy)]
944pub struct PrefixExpr<'a> {
945 syntax: SyntaxNodeRef<'a>,
946}
947
948impl<'a> AstNode<'a> for PrefixExpr<'a> {
949 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
950 match syntax.kind() {
951 PREFIX_EXPR => Some(PrefixExpr { syntax }),
952 _ => None,
953 }
954 }
955 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
956}
957
958impl<'a> PrefixExpr<'a> {}
959
960// RangeExpr
961#[derive(Debug, Clone, Copy)]
962pub struct RangeExpr<'a> {
963 syntax: SyntaxNodeRef<'a>,
964}
965
966impl<'a> AstNode<'a> for RangeExpr<'a> {
967 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
968 match syntax.kind() {
969 RANGE_EXPR => Some(RangeExpr { syntax }),
970 _ => None,
971 }
972 }
973 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
974}
975
976impl<'a> RangeExpr<'a> {}
977
978// RefExpr
979#[derive(Debug, Clone, Copy)]
980pub struct RefExpr<'a> {
981 syntax: SyntaxNodeRef<'a>,
982}
983
984impl<'a> AstNode<'a> for RefExpr<'a> {
985 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
986 match syntax.kind() {
987 REF_EXPR => Some(RefExpr { syntax }),
988 _ => None,
989 }
990 }
991 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
992}
993
994impl<'a> RefExpr<'a> {}
995
398// ReferenceType 996// ReferenceType
399#[derive(Debug, Clone, Copy)] 997#[derive(Debug, Clone, Copy)]
400pub struct ReferenceType<'a> { 998pub struct ReferenceType<'a> {
@@ -413,6 +1011,24 @@ impl<'a> AstNode<'a> for ReferenceType<'a> {
413 1011
414impl<'a> ReferenceType<'a> {} 1012impl<'a> ReferenceType<'a> {}
415 1013
1014// ReturnExpr
1015#[derive(Debug, Clone, Copy)]
1016pub struct ReturnExpr<'a> {
1017 syntax: SyntaxNodeRef<'a>,
1018}
1019
1020impl<'a> AstNode<'a> for ReturnExpr<'a> {
1021 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
1022 match syntax.kind() {
1023 RETURN_EXPR => Some(ReturnExpr { syntax }),
1024 _ => None,
1025 }
1026 }
1027 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
1028}
1029
1030impl<'a> ReturnExpr<'a> {}
1031
416// Root 1032// Root
417#[derive(Debug, Clone, Copy)] 1033#[derive(Debug, Clone, Copy)]
418pub struct Root<'a> { 1034pub struct Root<'a> {
@@ -503,6 +1119,24 @@ impl<'a> StructDef<'a> {
503 } 1119 }
504} 1120}
505 1121
1122// StructLit
1123#[derive(Debug, Clone, Copy)]
1124pub struct StructLit<'a> {
1125 syntax: SyntaxNodeRef<'a>,
1126}
1127
1128impl<'a> AstNode<'a> for StructLit<'a> {
1129 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
1130 match syntax.kind() {
1131 STRUCT_LIT => Some(StructLit { syntax }),
1132 _ => None,
1133 }
1134 }
1135 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
1136}
1137
1138impl<'a> StructLit<'a> {}
1139
506// TokenTree 1140// TokenTree
507#[derive(Debug, Clone, Copy)] 1141#[derive(Debug, Clone, Copy)]
508pub struct TokenTree<'a> { 1142pub struct TokenTree<'a> {
@@ -541,6 +1175,42 @@ impl<'a> ast::NameOwner<'a> for TraitDef<'a> {}
541impl<'a> ast::AttrsOwner<'a> for TraitDef<'a> {} 1175impl<'a> ast::AttrsOwner<'a> for TraitDef<'a> {}
542impl<'a> TraitDef<'a> {} 1176impl<'a> TraitDef<'a> {}
543 1177
1178// TryExpr
1179#[derive(Debug, Clone, Copy)]
1180pub struct TryExpr<'a> {
1181 syntax: SyntaxNodeRef<'a>,
1182}
1183
1184impl<'a> AstNode<'a> for TryExpr<'a> {
1185 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
1186 match syntax.kind() {
1187 TRY_EXPR => Some(TryExpr { syntax }),
1188 _ => None,
1189 }
1190 }
1191 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
1192}
1193
1194impl<'a> TryExpr<'a> {}
1195
1196// TupleExpr
1197#[derive(Debug, Clone, Copy)]
1198pub struct TupleExpr<'a> {
1199 syntax: SyntaxNodeRef<'a>,
1200}
1201
1202impl<'a> AstNode<'a> for TupleExpr<'a> {
1203 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
1204 match syntax.kind() {
1205 TUPLE_EXPR => Some(TupleExpr { syntax }),
1206 _ => None,
1207 }
1208 }
1209 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
1210}
1211
1212impl<'a> TupleExpr<'a> {}
1213
544// TupleType 1214// TupleType
545#[derive(Debug, Clone, Copy)] 1215#[derive(Debug, Clone, Copy)]
546pub struct TupleType<'a> { 1216pub struct TupleType<'a> {
@@ -697,3 +1367,21 @@ impl<'a> AstNode<'a> for WhereClause<'a> {
697 1367
698impl<'a> WhereClause<'a> {} 1368impl<'a> WhereClause<'a> {}
699 1369
1370// WhileExpr
1371#[derive(Debug, Clone, Copy)]
1372pub struct WhileExpr<'a> {
1373 syntax: SyntaxNodeRef<'a>,
1374}
1375
1376impl<'a> AstNode<'a> for WhileExpr<'a> {
1377 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
1378 match syntax.kind() {
1379 WHILE_EXPR => Some(WhileExpr { syntax }),
1380 _ => None,
1381 }
1382 }
1383 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
1384}
1385
1386impl<'a> WhileExpr<'a> {}
1387
diff --git a/crates/libsyntax2/src/grammar.ron b/crates/libsyntax2/src/grammar.ron
index 0ea7e8ce1..a753d5e48 100644
--- a/crates/libsyntax2/src/grammar.ron
+++ b/crates/libsyntax2/src/grammar.ron
@@ -325,6 +325,74 @@ Grammar(
325 ], 325 ],
326 ), 326 ),
327 327
328 "TupleExpr": (),
329 "ArrayExpr": (),
330 "ParenExpr": (),
331 "PathExpr": (),
332 "LambdaExpr": (),
333 "IfExpr": (),
334 "LoopExpr": (),
335 "ForExpr": (),
336 "WhileExpr": (),
337 "ContinueExpr": (),
338 "BreakExpr": (),
339 "Label": (),
340 "BlockExpr": (),
341 "ReturnExpr": (),
342 "MatchExpr": (),
343 "MatchArmList": (),
344 "MatchArm": (),
345 "MatchGuard": (),
346 "StructLit": (),
347 "NamedFieldList": (),
348 "NamedField": (),
349 "CallExpr": (),
350 "IndexExpr": (),
351 "MethodCallExpr": (),
352 "FieldExpr": (),
353 "TryExpr": (),
354 "CastExpr": (),
355 "RefExpr": (),
356 "PrefixExpr": (),
357 "RangeExpr": (),
358 "BinExpr": (),
359
360 "Expr": (
361 enum: [
362 "TupleExpr",
363 "ArrayExpr",
364 "ParenExpr",
365 "PathExpr",
366 "LambdaExpr",
367 "IfExpr",
368 "LoopExpr",
369 "ForExpr",
370 "WhileExpr",
371 "ContinueExpr",
372 "BreakExpr",
373 "Label",
374 "BlockExpr",
375 "ReturnExpr",
376 "MatchExpr",
377 "MatchArmList",
378 "MatchArm",
379 "MatchGuard",
380 "StructLit",
381 "NamedFieldList",
382 "NamedField",
383 "CallExpr",
384 "IndexExpr",
385 "MethodCallExpr",
386 "FieldExpr",
387 "TryExpr",
388 "CastExpr",
389 "RefExpr",
390 "PrefixExpr",
391 "RangeExpr",
392 "BinExpr",
393 ],
394 ),
395
328 "Name": (), 396 "Name": (),
329 "NameRef": (), 397 "NameRef": (),
330 "Attr": ( options: [ ["value", "TokenTree"] ] ), 398 "Attr": ( options: [ ["value", "TokenTree"] ] ),
@@ -332,5 +400,6 @@ Grammar(
332 "TypeParamList": ( collections: [ ["type_params", "TypeParam" ] ]), 400 "TypeParamList": ( collections: [ ["type_params", "TypeParam" ] ]),
333 "TypeParam": ( traits: ["NameOwner"]), 401 "TypeParam": ( traits: ["NameOwner"]),
334 "WhereClause": (), 402 "WhereClause": (),
403 "Block": (),
335 }, 404 },
336) 405)
diff --git a/crates/libsyntax2/src/lib.rs b/crates/libsyntax2/src/lib.rs
index 3a36a57b1..787dc2b5b 100644
--- a/crates/libsyntax2/src/lib.rs
+++ b/crates/libsyntax2/src/lib.rs
@@ -74,9 +74,7 @@ impl File {
74 File::new(root, errors) 74 File::new(root, errors)
75 } 75 }
76 pub fn reparse(&self, edit: &AtomEdit) -> File { 76 pub fn reparse(&self, edit: &AtomEdit) -> File {
77 self.incremental_reparse(edit).unwrap_or_else(|| { 77 self.incremental_reparse(edit).unwrap_or_else(|| self.full_reparse(edit))
78 self.full_reparse(edit)
79 })
80 } 78 }
81 fn incremental_reparse(&self, edit: &AtomEdit) -> Option<File> { 79 fn incremental_reparse(&self, edit: &AtomEdit) -> Option<File> {
82 let (node, reparser) = find_reparsable_node(self.syntax(), edit.delete)?; 80 let (node, reparser) = find_reparsable_node(self.syntax(), edit.delete)?;