mirror of
				https://github.com/rust-lang/rust-analyzer.git
				synced 2025-10-31 12:04:43 +00:00 
			
		
		
		
	Merge pull request #19397 from gvozdvmozgu/postfix-const-block
add postfix completion for const block
This commit is contained in:
		
						commit
						d305b0b8d0
					
				
					 7 changed files with 86 additions and 20 deletions
				
			
		|  | @ -345,6 +345,7 @@ pub(crate) fn complete_expr_path( | |||
| 
 | ||||
|                     if !in_block_expr { | ||||
|                         add_keyword("unsafe", "unsafe {\n    $0\n}"); | ||||
|                         add_keyword("const", "const {\n    $0\n}"); | ||||
|                     } | ||||
|                     add_keyword("match", "match $1 {\n    $0\n}"); | ||||
|                     add_keyword("while", "while $1 {\n    $0\n}"); | ||||
|  |  | |||
|  | @ -76,6 +76,7 @@ fn foo(a: A) { a.$0 } | |||
|                 kw await                                                           expr.await | ||||
|                 sn box                                                         Box::new(expr) | ||||
|                 sn call                                                        function(expr) | ||||
|                 sn const                                                             const {} | ||||
|                 sn dbg                                                             dbg!(expr) | ||||
|                 sn dbgr                                                           dbg!(&expr) | ||||
|                 sn deref                                                                *expr | ||||
|  | @ -103,6 +104,7 @@ fn foo() { | |||
|                 kw await                                                                          expr.await | ||||
|                 sn box                                                                        Box::new(expr) | ||||
|                 sn call                                                                       function(expr) | ||||
|                 sn const                                                                            const {} | ||||
|                 sn dbg                                                                            dbg!(expr) | ||||
|                 sn dbgr                                                                          dbg!(&expr) | ||||
|                 sn deref                                                                               *expr | ||||
|  | @ -132,6 +134,7 @@ fn foo(a: A) { a.$0 } | |||
|                 kw await                                                           expr.await | ||||
|                 sn box                                                         Box::new(expr) | ||||
|                 sn call                                                        function(expr) | ||||
|                 sn const                                                             const {} | ||||
|                 sn dbg                                                             dbg!(expr) | ||||
|                 sn dbgr                                                           dbg!(&expr) | ||||
|                 sn deref                                                                *expr | ||||
|  |  | |||
|  | @ -155,22 +155,29 @@ pub(crate) fn complete_postfix( | |||
|     postfix_snippet("refm", "&mut expr", &format!("&mut {receiver_text}")).add_to(acc, ctx.db); | ||||
|     postfix_snippet("deref", "*expr", &format!("*{receiver_text}")).add_to(acc, ctx.db); | ||||
| 
 | ||||
|     let mut unsafe_should_be_wrapped = true; | ||||
|     let mut block_should_be_wrapped = true; | ||||
|     if dot_receiver.syntax().kind() == BLOCK_EXPR { | ||||
|         unsafe_should_be_wrapped = false; | ||||
|         block_should_be_wrapped = false; | ||||
|         if let Some(parent) = dot_receiver.syntax().parent() { | ||||
|             if matches!(parent.kind(), IF_EXPR | WHILE_EXPR | LOOP_EXPR | FOR_EXPR) { | ||||
|                 unsafe_should_be_wrapped = true; | ||||
|                 block_should_be_wrapped = true; | ||||
|             } | ||||
|         } | ||||
|     }; | ||||
|     let unsafe_completion_string = if unsafe_should_be_wrapped { | ||||
|     let unsafe_completion_string = if block_should_be_wrapped { | ||||
|         format!("unsafe {{ {receiver_text} }}") | ||||
|     } else { | ||||
|         format!("unsafe {receiver_text}") | ||||
|     }; | ||||
|     postfix_snippet("unsafe", "unsafe {}", &unsafe_completion_string).add_to(acc, ctx.db); | ||||
| 
 | ||||
|     let const_completion_string = if block_should_be_wrapped { | ||||
|         format!("const {{ {receiver_text} }}") | ||||
|     } else { | ||||
|         format!("const {receiver_text}") | ||||
|     }; | ||||
|     postfix_snippet("const", "const {}", &const_completion_string).add_to(acc, ctx.db); | ||||
| 
 | ||||
|     // The rest of the postfix completions create an expression that moves an argument,
 | ||||
|     // so it's better to consider references now to avoid breaking the compilation
 | ||||
| 
 | ||||
|  | @ -430,6 +437,7 @@ fn main() { | |||
|             expect![[r#" | ||||
|                 sn box  Box::new(expr) | ||||
|                 sn call function(expr) | ||||
|                 sn const      const {} | ||||
|                 sn dbg      dbg!(expr) | ||||
|                 sn dbgr    dbg!(&expr) | ||||
|                 sn deref         *expr | ||||
|  | @ -463,6 +471,7 @@ fn main() { | |||
|             expect![[r#" | ||||
|                 sn box  Box::new(expr) | ||||
|                 sn call function(expr) | ||||
|                 sn const      const {} | ||||
|                 sn dbg      dbg!(expr) | ||||
|                 sn dbgr    dbg!(&expr) | ||||
|                 sn deref         *expr | ||||
|  | @ -490,6 +499,7 @@ fn main() { | |||
|             expect![[r#" | ||||
|                 sn box  Box::new(expr) | ||||
|                 sn call function(expr) | ||||
|                 sn const      const {} | ||||
|                 sn dbg      dbg!(expr) | ||||
|                 sn dbgr    dbg!(&expr) | ||||
|                 sn deref         *expr | ||||
|  | @ -516,6 +526,7 @@ fn main() { | |||
|             expect![[r#" | ||||
|                 sn box  Box::new(expr) | ||||
|                 sn call function(expr) | ||||
|                 sn const      const {} | ||||
|                 sn dbg      dbg!(expr) | ||||
|                 sn dbgr    dbg!(&expr) | ||||
|                 sn deref         *expr | ||||
|  | @ -653,44 +664,57 @@ fn main() { | |||
| 
 | ||||
|     #[test] | ||||
|     fn postfix_completion_for_unsafe() { | ||||
|         check_edit("unsafe", r#"fn main() { foo.$0 }"#, r#"fn main() { unsafe { foo } }"#); | ||||
|         check_edit("unsafe", r#"fn main() { { foo }.$0 }"#, r#"fn main() { unsafe { foo } }"#); | ||||
|         postfix_completion_for_block("unsafe"); | ||||
|     } | ||||
| 
 | ||||
|     #[test] | ||||
|     fn postfix_completion_for_const() { | ||||
|         postfix_completion_for_block("const"); | ||||
|     } | ||||
| 
 | ||||
|     fn postfix_completion_for_block(kind: &str) { | ||||
|         check_edit(kind, r#"fn main() { foo.$0 }"#, &format!("fn main() {{ {kind} {{ foo }} }}")); | ||||
|         check_edit( | ||||
|             "unsafe", | ||||
|             kind, | ||||
|             r#"fn main() { { foo }.$0 }"#, | ||||
|             &format!("fn main() {{ {kind} {{ foo }} }}"), | ||||
|         ); | ||||
|         check_edit( | ||||
|             kind, | ||||
|             r#"fn main() { if x { foo }.$0 }"#, | ||||
|             r#"fn main() { unsafe { if x { foo } } }"#, | ||||
|             &format!("fn main() {{ {kind} {{ if x {{ foo }} }} }}"), | ||||
|         ); | ||||
|         check_edit( | ||||
|             "unsafe", | ||||
|             kind, | ||||
|             r#"fn main() { loop { foo }.$0 }"#, | ||||
|             r#"fn main() { unsafe { loop { foo } } }"#, | ||||
|             &format!("fn main() {{ {kind} {{ loop {{ foo }} }} }}"), | ||||
|         ); | ||||
|         check_edit( | ||||
|             "unsafe", | ||||
|             kind, | ||||
|             r#"fn main() { if true {}.$0 }"#, | ||||
|             r#"fn main() { unsafe { if true {} } }"#, | ||||
|             &format!("fn main() {{ {kind} {{ if true {{}} }} }}"), | ||||
|         ); | ||||
|         check_edit( | ||||
|             "unsafe", | ||||
|             kind, | ||||
|             r#"fn main() { while true {}.$0 }"#, | ||||
|             r#"fn main() { unsafe { while true {} } }"#, | ||||
|             &format!("fn main() {{ {kind} {{ while true {{}} }} }}"), | ||||
|         ); | ||||
|         check_edit( | ||||
|             "unsafe", | ||||
|             kind, | ||||
|             r#"fn main() { for i in 0..10 {}.$0 }"#, | ||||
|             r#"fn main() { unsafe { for i in 0..10 {} } }"#, | ||||
|             &format!("fn main() {{ {kind} {{ for i in 0..10 {{}} }} }}"), | ||||
|         ); | ||||
|         check_edit( | ||||
|             "unsafe", | ||||
|             kind, | ||||
|             r#"fn main() { let x = if true {1} else {2}.$0 }"#, | ||||
|             r#"fn main() { let x = unsafe { if true {1} else {2} } }"#, | ||||
|             &format!("fn main() {{ let x = {kind} {{ if true {{1}} else {{2}} }} }}"), | ||||
|         ); | ||||
| 
 | ||||
|         // completion will not be triggered
 | ||||
|         check_edit( | ||||
|             "unsafe", | ||||
|             kind, | ||||
|             r#"fn main() { let x = true else {panic!()}.$0}"#, | ||||
|             r#"fn main() { let x = true else {panic!()}.unsafe $0}"#, | ||||
|             &format!("fn main() {{ let x = true else {{panic!()}}.{kind} $0}}"), | ||||
|         ); | ||||
|     } | ||||
| 
 | ||||
|  |  | |||
|  | @ -3006,6 +3006,7 @@ fn main() { | |||
|                 sn refm &mut expr [] | ||||
|                 sn deref *expr [] | ||||
|                 sn unsafe unsafe {} [] | ||||
|                 sn const const {} [] | ||||
|                 sn match match expr {} [] | ||||
|                 sn box Box::new(expr) [] | ||||
|                 sn dbg dbg!(expr) [] | ||||
|  | @ -3036,6 +3037,7 @@ fn main() { | |||
|                 sn refm &mut expr [] | ||||
|                 sn deref *expr [] | ||||
|                 sn unsafe unsafe {} [] | ||||
|                 sn const const {} [] | ||||
|                 sn match match expr {} [] | ||||
|                 sn box Box::new(expr) [] | ||||
|                 sn dbg dbg!(expr) [] | ||||
|  |  | |||
|  | @ -58,6 +58,7 @@ fn baz() { | |||
|             un Union                  Union | ||||
|             ev TupleV(…)        TupleV(u32) | ||||
|             bt u32                      u32 | ||||
|             kw const | ||||
|             kw crate:: | ||||
|             kw false | ||||
|             kw for | ||||
|  | @ -101,6 +102,7 @@ fn func(param0 @ (param1, param2): (i32, i32)) { | |||
|             lc param1             i32 | ||||
|             lc param2             i32 | ||||
|             bt u32                u32 | ||||
|             kw const | ||||
|             kw crate:: | ||||
|             kw false | ||||
|             kw for | ||||
|  | @ -335,6 +337,7 @@ fn complete_in_match_arm() { | |||
|         expect Lukas Wirth
						Lukas Wirth