Don't trigger postfix completion if block which has an else block

Discard postfix completion if the next_non_trivia_sibling of dot_token
is an ELSE_KW.
This commit is contained in:
Duong Quoc Khanh 2023-02-11 21:26:00 +09:00
parent 8011029d3a
commit e1396bde73
No known key found for this signature in database
GPG key ID: DA52A74E804A00E7
2 changed files with 32 additions and 0 deletions

View file

@ -747,4 +747,16 @@ fn main() {
"#,
);
}
#[test]
fn no_postfix_completions_in_if_block_that_has_an_else() {
check(
r#"
fn test() {
if true {}.$0 else {}
}
"#,
expect![[r#""#]],
);
}
}

View file

@ -605,6 +605,26 @@ fn classify_name_ref(
},
_ => false,
};
let reciever_is_part_of_indivisible_expression = match &receiver {
Some(ast::Expr::IfExpr(_)) => {
let next_sibling = field.dot_token().and_then(|token| {
let dot_token = original_file.covering_element(token.text_range());
let next_sibling = dot_token.as_token().and_then(|t| t.next_token()).and_then(|t| next_non_trivia_sibling(t.into()));
next_sibling
});
match next_sibling {
Some(syntax::NodeOrToken::Node(n)) => n.first_child_or_token().map(|t| t.kind()) == Some(SyntaxKind::ELSE_KW),
Some(syntax::NodeOrToken::Token(t)) => t.kind() == SyntaxKind::ELSE_KW,
None => false
}
},
_ => false
};
if reciever_is_part_of_indivisible_expression {
return None;
}
let kind = NameRefKind::DotAccess(DotAccess {
receiver_ty: receiver.as_ref().and_then(|it| sema.type_of_expr(it)),
kind: DotAccessKind::Field { receiver_is_ambiguous_float_literal },