]> git.lizzy.rs Git - rust.git/blobdiff - src/tools/rust-analyzer/crates/syntax/src/ast/node_ext.rs
:arrow_up: rust-analyzer
[rust.git] / src / tools / rust-analyzer / crates / syntax / src / ast / node_ext.rs
index b143df1f83f2ae3a2aa26dad048cd1fedc5c0492..bb92c51e9a90ed766bce24344f8ddeeb2337ed8c 100644 (file)
@@ -806,6 +806,19 @@ pub fn type_or_const_params(&self) -> impl Iterator<Item = ast::TypeOrConstParam
     }
 }
 
+impl ast::ForExpr {
+    pub fn iterable(&self) -> Option<ast::Expr> {
+        // If the iterable is a BlockExpr, check if the body is missing.
+        // If it is assume the iterable is the expression that is missing instead.
+        let mut exprs = support::children(self.syntax());
+        let first = exprs.next();
+        match first {
+            Some(ast::Expr::BlockExpr(_)) => exprs.next().and(first),
+            first => first,
+        }
+    }
+}
+
 impl ast::HasLoopBody for ast::ForExpr {
     fn loop_body(&self) -> Option<ast::BlockExpr> {
         let mut exprs = support::children(self.syntax());
@@ -815,6 +828,19 @@ fn loop_body(&self) -> Option<ast::BlockExpr> {
     }
 }
 
+impl ast::WhileExpr {
+    pub fn condition(&self) -> Option<ast::Expr> {
+        // If the condition is a BlockExpr, check if the body is missing.
+        // If it is assume the condition is the expression that is missing instead.
+        let mut exprs = support::children(self.syntax());
+        let first = exprs.next();
+        match first {
+            Some(ast::Expr::BlockExpr(_)) => exprs.next().and(first),
+            first => first,
+        }
+    }
+}
+
 impl ast::HasLoopBody for ast::WhileExpr {
     fn loop_body(&self) -> Option<ast::BlockExpr> {
         let mut exprs = support::children(self.syntax());
@@ -835,3 +861,15 @@ fn from(it: ast::Adt) -> Self {
         }
     }
 }
+
+impl ast::IfExpr {
+    pub fn condition(&self) -> Option<ast::Expr> {
+        support::child(&self.syntax)
+    }
+}
+
+impl ast::MatchGuard {
+    pub fn condition(&self) -> Option<ast::Expr> {
+        support::child(&self.syntax)
+    }
+}