aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_syntax/src')
-rw-r--r--crates/ra_syntax/src/algo.rs4
-rw-r--r--crates/ra_syntax/src/ast/generated.rs6
-rw-r--r--crates/ra_syntax/src/grammar.ron2
-rw-r--r--crates/ra_syntax/src/grammar/expressions/atom.rs18
4 files changed, 24 insertions, 6 deletions
diff --git a/crates/ra_syntax/src/algo.rs b/crates/ra_syntax/src/algo.rs
index 45747e21d..99b0983b0 100644
--- a/crates/ra_syntax/src/algo.rs
+++ b/crates/ra_syntax/src/algo.rs
@@ -17,14 +17,14 @@ pub fn find_leaf_at_offset(node: &SyntaxNode, offset: TextUnit) -> LeafAtOffset<
17} 17}
18 18
19/// Finds a node of specific Ast type at offset. Note that this is slightly 19/// Finds a node of specific Ast type at offset. Note that this is slightly
20/// impercise: if the cursor is strictly betwen two nodes of the desired type, 20/// imprecise: if the cursor is strictly between two nodes of the desired type,
21/// as in 21/// as in
22/// 22///
23/// ```no-run 23/// ```no-run
24/// struct Foo {}|struct Bar; 24/// struct Foo {}|struct Bar;
25/// ``` 25/// ```
26/// 26///
27/// then the left node will be silently prefered. 27/// then the left node will be silently preferred.
28pub fn find_node_at_offset<N: AstNode>(syntax: &SyntaxNode, offset: TextUnit) -> Option<&N> { 28pub fn find_node_at_offset<N: AstNode>(syntax: &SyntaxNode, offset: TextUnit) -> Option<&N> {
29 find_leaf_at_offset(syntax, offset).find_map(|leaf| leaf.ancestors().find_map(N::cast)) 29 find_leaf_at_offset(syntax, offset).find_map(|leaf| leaf.ancestors().find_map(N::cast))
30} 30}
diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs
index ce559882b..d0561c495 100644
--- a/crates/ra_syntax/src/ast/generated.rs
+++ b/crates/ra_syntax/src/ast/generated.rs
@@ -1982,7 +1982,11 @@ impl ToOwned for MatchGuard {
1982} 1982}
1983 1983
1984 1984
1985impl MatchGuard {} 1985impl MatchGuard {
1986 pub fn expr(&self) -> Option<&Expr> {
1987 super::child_opt(self)
1988 }
1989}
1986 1990
1987// MethodCallExpr 1991// MethodCallExpr
1988#[derive(Debug, PartialEq, Eq, Hash)] 1992#[derive(Debug, PartialEq, Eq, Hash)]
diff --git a/crates/ra_syntax/src/grammar.ron b/crates/ra_syntax/src/grammar.ron
index f4841241f..d4c863705 100644
--- a/crates/ra_syntax/src/grammar.ron
+++ b/crates/ra_syntax/src/grammar.ron
@@ -418,7 +418,7 @@ Grammar(
418 ], 418 ],
419 collections: [ [ "pats", "Pat" ] ] 419 collections: [ [ "pats", "Pat" ] ]
420 ), 420 ),
421 "MatchGuard": (), 421 "MatchGuard": (options: ["Expr"]),
422 "StructLit": (options: ["Path", "NamedFieldList", ["spread", "Expr"]]), 422 "StructLit": (options: ["Path", "NamedFieldList", ["spread", "Expr"]]),
423 "NamedFieldList": (collections: [ ["fields", "NamedField"] ]), 423 "NamedFieldList": (collections: [ ["fields", "NamedField"] ]),
424 "NamedField": (options: ["NameRef", "Expr"]), 424 "NamedField": (options: ["NameRef", "Expr"]),
diff --git a/crates/ra_syntax/src/grammar/expressions/atom.rs b/crates/ra_syntax/src/grammar/expressions/atom.rs
index 6d6d89f70..600774afd 100644
--- a/crates/ra_syntax/src/grammar/expressions/atom.rs
+++ b/crates/ra_syntax/src/grammar/expressions/atom.rs
@@ -360,8 +360,8 @@ fn match_arm(p: &mut Parser) -> BlockLike {
360 while p.eat(PIPE) { 360 while p.eat(PIPE) {
361 patterns::pattern(p); 361 patterns::pattern(p);
362 } 362 }
363 if p.eat(IF_KW) { 363 if p.at(IF_KW) {
364 expr(p); 364 match_guard(p);
365 } 365 }
366 p.expect(FAT_ARROW); 366 p.expect(FAT_ARROW);
367 let ret = expr_stmt(p); 367 let ret = expr_stmt(p);
@@ -369,6 +369,20 @@ fn match_arm(p: &mut Parser) -> BlockLike {
369 ret 369 ret
370} 370}
371 371
372// test match_guard
373// fn foo() {
374// match () {
375// _ if foo => (),
376// }
377// }
378fn match_guard(p: &mut Parser) -> CompletedMarker {
379 assert!(p.at(IF_KW));
380 let m = p.start();
381 p.bump();
382 expr(p);
383 m.complete(p, MATCH_GUARD)
384}
385
372// test block_expr 386// test block_expr
373// fn foo() { 387// fn foo() {
374// {}; 388// {};