11use ide_db:: syntax_helpers:: suggest_name;
22use syntax:: ast:: { self , AstNode , syntax_factory:: SyntaxFactory } ;
33
4- use crate :: { AssistContext , AssistId , Assists } ;
4+ use crate :: { AssistContext , AssistId , Assists , utils :: cover_let_chain } ;
55
66// Assist: replace_is_some_with_if_let_some
77//
@@ -27,13 +27,11 @@ pub(crate) fn replace_is_method_with_if_let_method(
2727 let if_expr = ctx. find_node_at_offset :: < ast:: IfExpr > ( ) ?;
2828
2929 let cond = if_expr. condition ( ) ?;
30+ let cond = cover_let_chain ( cond, ctx. selection_trimmed ( ) ) ?;
3031 let call_expr = match cond {
3132 ast:: Expr :: MethodCallExpr ( call) => call,
3233 _ => return None ,
3334 } ;
34- if ctx. offset ( ) > if_expr. then_branch ( ) ?. stmt_list ( ) ?. l_curly_token ( ) ?. text_range ( ) . end ( ) {
35- return None ;
36- }
3735
3836 let name_ref = call_expr. name_ref ( ) ?;
3937 match name_ref. text ( ) . as_str ( ) {
@@ -195,6 +193,63 @@ fn main() {
195193 ) ;
196194 }
197195
196+ #[ test]
197+ fn replace_is_some_with_if_let_some_in_let_chain ( ) {
198+ check_assist (
199+ replace_is_method_with_if_let_method,
200+ r#"
201+ fn main() {
202+ let x = Some(1);
203+ let cond = true;
204+ if cond && x.is_som$0e() {}
205+ }
206+ "# ,
207+ r#"
208+ fn main() {
209+ let x = Some(1);
210+ let cond = true;
211+ if cond && let Some(${0:x1}) = x {}
212+ }
213+ "# ,
214+ ) ;
215+
216+ check_assist (
217+ replace_is_method_with_if_let_method,
218+ r#"
219+ fn main() {
220+ let x = Some(1);
221+ let cond = true;
222+ if x.is_som$0e() && cond {}
223+ }
224+ "# ,
225+ r#"
226+ fn main() {
227+ let x = Some(1);
228+ let cond = true;
229+ if let Some(${0:x1}) = x && cond {}
230+ }
231+ "# ,
232+ ) ;
233+
234+ check_assist (
235+ replace_is_method_with_if_let_method,
236+ r#"
237+ fn main() {
238+ let x = Some(1);
239+ let cond = true;
240+ if cond && x.is_som$0e() && cond {}
241+ }
242+ "# ,
243+ r#"
244+ fn main() {
245+ let x = Some(1);
246+ let cond = true;
247+ if cond && let Some(${0:x1}) = x && cond {}
248+ }
249+ "# ,
250+ ) ;
251+ }
252+
198253 #[ test]
199254 fn replace_is_some_with_if_let_some_not_applicable_after_l_curly ( ) {
200255 check_assist_not_applicable (
0 commit comments