Skip to content

Commit c4586bd

Browse files
committed
Add applicable on else for invert_if
Example --- ```rust fn f() { if cond { 3 * 2 } e$0lse { 1 } } ``` -> ```rust fn f() { if !cond { 1 } else { 3 * 2 } } ```
1 parent a56e577 commit c4586bd

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

crates/ide-assists/src/handlers/invert_if.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ use crate::{
2727
// }
2828
// ```
2929
pub(crate) fn invert_if(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
30-
let if_keyword = ctx.find_token_syntax_at_offset(T![if])?;
30+
let if_keyword = ctx
31+
.find_token_syntax_at_offset(T![if])
32+
.or_else(|| ctx.find_token_syntax_at_offset(T![else]))?;
3133
let expr = ast::IfExpr::cast(if_keyword.parent()?)?;
3234
let if_range = if_keyword.text_range();
3335
let cursor_in_range = if_range.contains_range(ctx.selection_trimmed());
@@ -111,6 +113,15 @@ mod tests {
111113
)
112114
}
113115

116+
#[test]
117+
fn invert_if_on_else_keyword() {
118+
check_assist(
119+
invert_if,
120+
"fn f() { if cond { 3 * 2 } e$0lse { 1 } }",
121+
"fn f() { if !cond { 1 } else { 3 * 2 } }",
122+
)
123+
}
124+
114125
#[test]
115126
fn invert_if_doesnt_apply_with_cursor_not_on_if() {
116127
check_assist_not_applicable(invert_if, "fn f() { if !$0cond { 3 * 2 } else { 1 } }")

0 commit comments

Comments
 (0)