From 57934ac47a4e0a14237103a1745c49187af7f048 Mon Sep 17 00:00:00 2001 From: Alain Siegrist Date: Sun, 8 Oct 2023 23:15:04 +0200 Subject: [PATCH 1/3] Add postfix completion for let else --- .../ide-completion/src/completions/postfix.rs | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/crates/ide-completion/src/completions/postfix.rs b/crates/ide-completion/src/completions/postfix.rs index a846ffe10e..e071547141 100644 --- a/crates/ide-completion/src/completions/postfix.rs +++ b/crates/ide-completion/src/completions/postfix.rs @@ -75,6 +75,11 @@ pub(crate) fn complete_postfix( let try_enum = TryEnum::from_ty(&ctx.sema, &receiver_ty.strip_references()); if let Some(try_enum) = &try_enum { + let in_loop = dot_receiver + .syntax() + .ancestors() + .any(|n| matches!(n.kind(), WHILE_EXPR | LOOP_EXPR | FOR_EXPR)); + match try_enum { TryEnum::Result => { postfix_snippet( @@ -84,6 +89,17 @@ pub(crate) fn complete_postfix( ) .add_to(acc, ctx.db); + postfix_snippet( + "lete", + "let Ok else {}", + &if in_loop { + format!("let Ok($1) = {receiver_text} else {{\n ${{2|continue,break,return|}};\n}};\n$0") + } else { + format!("let Ok($1) = {receiver_text} else {{\n return;\n}};\n$0") + }, + ) + .add_to(acc, ctx.db); + postfix_snippet( "while", "while let Ok {}", @@ -99,6 +115,17 @@ pub(crate) fn complete_postfix( ) .add_to(acc, ctx.db); + postfix_snippet( + "lete", + "let Some else {}", + &if in_loop { + format!("let Some($1) = {receiver_text} else {{\n ${{2|continue,break,return|}};\n}};\n$0") + } else { + format!("let Some($1) = {receiver_text} else {{\n return;\n}};\n$0") + }, + ) + .add_to(acc, ctx.db); + postfix_snippet( "while", "while let Some {}", @@ -469,6 +496,56 @@ fn main() { ); } + #[test] + fn option_letelse() { + check_edit( + "lete", + r#" +//- minicore: option +fn main() { + let bar = Some(true); + bar.$0 +} +"#, + r#" +fn main() { + let bar = Some(true); + let Some($1) = bar else { + return; +}; +$0 +} +"#, + ); + } + + #[test] + fn option_letelse_loop() { + check_edit( + "lete", + r#" +//- minicore: option +fn main() { + let bar = Some(true); + loop { + bar.$0 + } +} +"#, + r#" +fn main() { + let bar = Some(true); + loop { + let Some($1) = bar else { + ${2|continue,break,return|}; +}; +$0 + } +} +"#, + ); + } + #[test] fn result_match() { check_edit( From 51d57b9bb62df7caee53e1a378191434590e765a Mon Sep 17 00:00:00 2001 From: Alain Siegrist Date: Mon, 9 Oct 2023 18:21:33 +0200 Subject: [PATCH 2/3] Make return a snippet --- crates/ide-completion/src/completions/postfix.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/ide-completion/src/completions/postfix.rs b/crates/ide-completion/src/completions/postfix.rs index e071547141..e062578b2b 100644 --- a/crates/ide-completion/src/completions/postfix.rs +++ b/crates/ide-completion/src/completions/postfix.rs @@ -95,7 +95,7 @@ pub(crate) fn complete_postfix( &if in_loop { format!("let Ok($1) = {receiver_text} else {{\n ${{2|continue,break,return|}};\n}};\n$0") } else { - format!("let Ok($1) = {receiver_text} else {{\n return;\n}};\n$0") + format!("let Ok($1) = {receiver_text} else {{\n ${{2:return}};\n}};\n$0") }, ) .add_to(acc, ctx.db); @@ -121,7 +121,7 @@ pub(crate) fn complete_postfix( &if in_loop { format!("let Some($1) = {receiver_text} else {{\n ${{2|continue,break,return|}};\n}};\n$0") } else { - format!("let Some($1) = {receiver_text} else {{\n return;\n}};\n$0") + format!("let Some($1) = {receiver_text} else {{\n ${{2:return}};\n}};\n$0") }, ) .add_to(acc, ctx.db); @@ -511,7 +511,7 @@ fn main() { fn main() { let bar = Some(true); let Some($1) = bar else { - return; + ${2:return}; }; $0 } From 7ec32d091a05b9cfc3a9a8fb0f14be423e455f41 Mon Sep 17 00:00:00 2001 From: Alain Siegrist Date: Sat, 27 Jan 2024 14:28:50 +0100 Subject: [PATCH 3/3] Remove letelse control flow snippets --- .../ide-completion/src/completions/postfix.rs | 46 ++----------------- 1 file changed, 3 insertions(+), 43 deletions(-) diff --git a/crates/ide-completion/src/completions/postfix.rs b/crates/ide-completion/src/completions/postfix.rs index e062578b2b..af83d4104f 100644 --- a/crates/ide-completion/src/completions/postfix.rs +++ b/crates/ide-completion/src/completions/postfix.rs @@ -75,11 +75,6 @@ pub(crate) fn complete_postfix( let try_enum = TryEnum::from_ty(&ctx.sema, &receiver_ty.strip_references()); if let Some(try_enum) = &try_enum { - let in_loop = dot_receiver - .syntax() - .ancestors() - .any(|n| matches!(n.kind(), WHILE_EXPR | LOOP_EXPR | FOR_EXPR)); - match try_enum { TryEnum::Result => { postfix_snippet( @@ -92,11 +87,7 @@ pub(crate) fn complete_postfix( postfix_snippet( "lete", "let Ok else {}", - &if in_loop { - format!("let Ok($1) = {receiver_text} else {{\n ${{2|continue,break,return|}};\n}};\n$0") - } else { - format!("let Ok($1) = {receiver_text} else {{\n ${{2:return}};\n}};\n$0") - }, + &format!("let Ok($1) = {receiver_text} else {{\n $2\n}};\n$0"), ) .add_to(acc, ctx.db); @@ -118,11 +109,7 @@ pub(crate) fn complete_postfix( postfix_snippet( "lete", "let Some else {}", - &if in_loop { - format!("let Some($1) = {receiver_text} else {{\n ${{2|continue,break,return|}};\n}};\n$0") - } else { - format!("let Some($1) = {receiver_text} else {{\n ${{2:return}};\n}};\n$0") - }, + &format!("let Some($1) = {receiver_text} else {{\n $2\n}};\n$0"), ) .add_to(acc, ctx.db); @@ -511,7 +498,7 @@ fn main() { fn main() { let bar = Some(true); let Some($1) = bar else { - ${2:return}; + $2 }; $0 } @@ -519,33 +506,6 @@ $0 ); } - #[test] - fn option_letelse_loop() { - check_edit( - "lete", - r#" -//- minicore: option -fn main() { - let bar = Some(true); - loop { - bar.$0 - } -} -"#, - r#" -fn main() { - let bar = Some(true); - loop { - let Some($1) = bar else { - ${2|continue,break,return|}; -}; -$0 - } -} -"#, - ); - } - #[test] fn result_match() { check_edit(