aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/libeditor/src/completion.rs29
-rw-r--r--crates/libsyntax2/src/ast/generated.rs31
-rw-r--r--crates/libsyntax2/src/grammar.ron6
3 files changed, 49 insertions, 17 deletions
diff --git a/crates/libeditor/src/completion.rs b/crates/libeditor/src/completion.rs
index 6335dba17..ec9388ee3 100644
--- a/crates/libeditor/src/completion.rs
+++ b/crates/libeditor/src/completion.rs
@@ -5,7 +5,6 @@ use libsyntax2::{
5 ast::{self, NameOwner}, 5 ast::{self, NameOwner},
6 algo::{ 6 algo::{
7 ancestors, 7 ancestors,
8 visit::{visitor_ctx, VisitorCtx},
9 walk::preorder, 8 walk::preorder,
10 generate, 9 generate,
11 }, 10 },
@@ -52,24 +51,24 @@ fn compute_scopes(fn_def: ast::FnDef) -> FnScopes {
52 51
53 let mut scope = root; 52 let mut scope = root;
54 if let Some(body) = fn_def.body() { 53 if let Some(body) = fn_def.body() {
55 for child in body.syntax().children() { 54 for stmt in body.statements() {
56 let _ = visitor_ctx((&mut scopes, &mut scope)) 55 match stmt {
57 .visit::<ast::LetStmt, _>(|stmt, (scopes, scope)| { 56 ast::Stmt::LetStmt(stmt) => {
58 *scope = scopes.new_scope(*scope); 57 scope = scopes.new_scope(scope);
59 if let Some(pat) = stmt.pat() { 58 if let Some(pat) = stmt.pat() {
60 scopes.add_bindings(*scope, pat); 59 scopes.add_bindings(scope, pat);
61 } 60 }
62 if let Some(expr) = stmt.initializer() { 61 if let Some(expr) = stmt.initializer() {
63 scopes.set_scope(expr.syntax(), *scope) 62 scopes.set_scope(expr.syntax(), scope)
64 } 63 }
65 }) 64 }
66 .visit::<ast::ExprStmt, _>(|expr, (scopes, scope)| { 65 ast::Stmt::ExprStmt(expr) => {
67 scopes.set_scope(expr.syntax(), *scope) 66 scopes.set_scope(expr.syntax(), scope)
68 }) 67 }
69 .visit::<ast::Expr, _>(|expr, (scopes, scope)| { 68 }
70 scopes.set_scope(expr.syntax(), *scope) 69 }
71 }) 70 if let Some(expr) = body.expr() {
72 .accept(child); 71 scopes.set_scope(expr.syntax(), scope)
73 } 72 }
74 } 73 }
75 scopes 74 scopes
diff --git a/crates/libsyntax2/src/ast/generated.rs b/crates/libsyntax2/src/ast/generated.rs
index b937fe5a2..1dd161f52 100644
--- a/crates/libsyntax2/src/ast/generated.rs
+++ b/crates/libsyntax2/src/ast/generated.rs
@@ -116,9 +116,13 @@ impl<'a> AstNode<'a> for Block<'a> {
116} 116}
117 117
118impl<'a> Block<'a> { 118impl<'a> Block<'a> {
119 pub fn let_stmts(self) -> impl Iterator<Item = LetStmt<'a>> + 'a { 119 pub fn statements(self) -> impl Iterator<Item = Stmt<'a>> + 'a {
120 super::children(self) 120 super::children(self)
121 } 121 }
122
123 pub fn expr(self) -> Option<Expr<'a>> {
124 super::child_opt(self)
125 }
122} 126}
123 127
124// BlockExpr 128// BlockExpr
@@ -1370,6 +1374,31 @@ impl<'a> ast::TypeParamsOwner<'a> for StaticDef<'a> {}
1370impl<'a> ast::AttrsOwner<'a> for StaticDef<'a> {} 1374impl<'a> ast::AttrsOwner<'a> for StaticDef<'a> {}
1371impl<'a> StaticDef<'a> {} 1375impl<'a> StaticDef<'a> {}
1372 1376
1377// Stmt
1378#[derive(Debug, Clone, Copy)]
1379pub enum Stmt<'a> {
1380 ExprStmt(ExprStmt<'a>),
1381 LetStmt(LetStmt<'a>),
1382}
1383
1384impl<'a> AstNode<'a> for Stmt<'a> {
1385 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
1386 match syntax.kind() {
1387 EXPR_STMT => Some(Stmt::ExprStmt(ExprStmt { syntax })),
1388 LET_STMT => Some(Stmt::LetStmt(LetStmt { syntax })),
1389 _ => None,
1390 }
1391 }
1392 fn syntax(self) -> SyntaxNodeRef<'a> {
1393 match self {
1394 Stmt::ExprStmt(inner) => inner.syntax(),
1395 Stmt::LetStmt(inner) => inner.syntax(),
1396 }
1397 }
1398}
1399
1400impl<'a> Stmt<'a> {}
1401
1373// StructDef 1402// StructDef
1374#[derive(Debug, Clone, Copy)] 1403#[derive(Debug, Clone, Copy)]
1375pub struct StructDef<'a> { 1404pub struct StructDef<'a> {
diff --git a/crates/libsyntax2/src/grammar.ron b/crates/libsyntax2/src/grammar.ron
index aa2742b3e..f3c3d3036 100644
--- a/crates/libsyntax2/src/grammar.ron
+++ b/crates/libsyntax2/src/grammar.ron
@@ -437,9 +437,13 @@ Grammar(
437 ["pat", "Pat"], 437 ["pat", "Pat"],
438 ["initializer", "Expr"], 438 ["initializer", "Expr"],
439 ]), 439 ]),
440 "Stmt": (
441 enum: ["ExprStmt", "LetStmt"],
442 ),
440 "Block": ( 443 "Block": (
444 options: [["expr", "Expr"]],
441 collections: [ 445 collections: [
442 ["let_stmts", "LetStmt"], 446 ["statements", "Stmt"],
443 ] 447 ]
444 ), 448 ),
445 "ParamList": ( 449 "ParamList": (