Skip to content

Commit

Permalink
Auto merge of rust-lang#14123 - dqkqd:discard-postfix-completion-for-…
Browse files Browse the repository at this point in the history
…indivisble-expr, r=Veykril

fix: Don't trigger postfix completion in `if` block which has an `else` block

Fix rust-lang#14096
  • Loading branch information
bors committed Feb 14, 2023
2 parents a33e9d9 + 0285acc commit 3812951
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
12 changes: 12 additions & 0 deletions crates/ide-completion/src/completions/postfix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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#""#]],
);
}
}
28 changes: 28 additions & 0 deletions crates/ide-completion/src/context/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,18 @@ fn classify_name_ref(
},
_ => false,
};

let reciever_is_part_of_indivisible_expression = match &receiver {
Some(ast::Expr::IfExpr(_)) => {
let next_token_kind = next_non_trivia_token(name_ref.syntax().clone()).map(|t| t.kind());
next_token_kind == Some(SyntaxKind::ELSE_KW)
},
_ => 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 },
Expand Down Expand Up @@ -1317,6 +1329,22 @@ fn previous_non_trivia_token(e: impl Into<SyntaxElement>) -> Option<SyntaxToken>
None
}

fn next_non_trivia_token(e: impl Into<SyntaxElement>) -> Option<SyntaxToken> {
let mut token = match e.into() {
SyntaxElement::Node(n) => n.last_token()?,
SyntaxElement::Token(t) => t,
}
.next_token();
while let Some(inner) = token {
if !inner.kind().is_trivia() {
return Some(inner);
} else {
token = inner.next_token();
}
}
None
}

fn next_non_trivia_sibling(ele: SyntaxElement) -> Option<SyntaxElement> {
let mut e = ele.next_sibling_or_token();
while let Some(inner) = e {
Expand Down

0 comments on commit 3812951

Please sign in to comment.