mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-15 06:45:27 +00:00
⬆️ rust-analyzer
This commit is contained in:
parent
544b4cfe4d
commit
dbf04a5ee2
106 changed files with 2219 additions and 609 deletions
|
@ -48,23 +48,30 @@ impl From<ast::IfExpr> for ElseBranch {
|
|||
}
|
||||
|
||||
impl ast::IfExpr {
|
||||
pub fn condition(&self) -> Option<ast::Expr> {
|
||||
// If the condition is a BlockExpr, check if the then 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,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn then_branch(&self) -> Option<ast::BlockExpr> {
|
||||
self.children_after_condition().next()
|
||||
match support::children(self.syntax()).nth(1)? {
|
||||
ast::Expr::BlockExpr(block) => Some(block),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn else_branch(&self) -> Option<ElseBranch> {
|
||||
let res = match self.children_after_condition().nth(1) {
|
||||
Some(block) => ElseBranch::Block(block),
|
||||
None => {
|
||||
let elif = self.children_after_condition().next()?;
|
||||
ElseBranch::IfExpr(elif)
|
||||
}
|
||||
};
|
||||
Some(res)
|
||||
}
|
||||
|
||||
fn children_after_condition<N: AstNode>(&self) -> impl Iterator<Item = N> {
|
||||
self.syntax().children().skip(1).filter_map(N::cast)
|
||||
match support::children(self.syntax()).nth(2)? {
|
||||
ast::Expr::BlockExpr(block) => Some(ElseBranch::Block(block)),
|
||||
ast::Expr::IfExpr(elif) => Some(ElseBranch::IfExpr(elif)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -356,7 +363,15 @@ impl ast::BlockExpr {
|
|||
Some(it) => it,
|
||||
None => return true,
|
||||
};
|
||||
!matches!(parent.kind(), FN | IF_EXPR | WHILE_EXPR | LOOP_EXPR)
|
||||
match parent.kind() {
|
||||
FOR_EXPR | IF_EXPR => parent
|
||||
.children()
|
||||
.filter(|it| ast::Expr::can_cast(it.kind()))
|
||||
.next()
|
||||
.map_or(true, |it| it == *self.syntax()),
|
||||
LET_ELSE | FN | WHILE_EXPR | LOOP_EXPR | CONST_BLOCK_PAT => false,
|
||||
_ => true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -937,12 +937,6 @@ impl From<ast::Adt> for ast::Item {
|
|||
}
|
||||
}
|
||||
|
||||
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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue