File tree Expand file tree Collapse file tree 4 files changed +88
-1
lines changed
crates/ide-completion/src Expand file tree Collapse file tree 4 files changed +88
-1
lines changed Original file line number Diff line number Diff line change @@ -21,6 +21,7 @@ pub(crate) fn complete_expr_path(
2121 ref is_func_update,
2222 ref innermost_ret_ty,
2323 ref impl_,
24+ in_match_guard,
2425 ..
2526 } : & ExprCtx ,
2627) {
@@ -195,7 +196,11 @@ pub(crate) fn complete_expr_path(
195196 add_keyword ( "while" , "while $1 {\n $0\n }" ) ;
196197 add_keyword ( "while let" , "while let $1 = $2 {\n $0\n }" ) ;
197198 add_keyword ( "loop" , "loop {\n $0\n }" ) ;
198- add_keyword ( "if" , "if $1 {\n $0\n }" ) ;
199+ if in_match_guard {
200+ add_keyword ( "if" , "if $0" ) ;
201+ } else {
202+ add_keyword ( "if" , "if $1 {\n $0\n }" ) ;
203+ }
199204 add_keyword ( "if let" , "if let $1 = $2 {\n $0\n }" ) ;
200205 add_keyword ( "for" , "for $1 in $2 {\n $0\n }" ) ;
201206 add_keyword ( "true" , "true" ) ;
Original file line number Diff line number Diff line change @@ -163,4 +163,75 @@ fn main() {
163163"# ,
164164 ) ;
165165 }
166+
167+ #[ test]
168+ fn if_completion_in_match_guard ( ) {
169+ check_edit (
170+ "if" ,
171+ r"
172+ fn main() {
173+ match () {
174+ () $0
175+ }
176+ }
177+ " ,
178+ r"
179+ fn main() {
180+ match () {
181+ () if $0
182+ }
183+ }
184+ " ,
185+ )
186+ }
187+
188+ #[ test]
189+ fn if_completion_in_match_arm_expr ( ) {
190+ check_edit (
191+ "if" ,
192+ r"
193+ fn main() {
194+ match () {
195+ () => $0
196+ }
197+ }
198+ " ,
199+ r"
200+ fn main() {
201+ match () {
202+ () => if $1 {
203+ $0
204+ }
205+ }
206+ }
207+ " ,
208+ )
209+ }
210+
211+ #[ test]
212+ fn if_completion_in_match_arm_expr_block ( ) {
213+ check_edit (
214+ "if" ,
215+ r"
216+ fn main() {
217+ match () {
218+ () => {
219+ $0
220+ }
221+ }
222+ }
223+ " ,
224+ r"
225+ fn main() {
226+ match () {
227+ () => {
228+ if $1 {
229+ $0
230+ }
231+ }
232+ }
233+ }
234+ " ,
235+ )
236+ }
166237}
Original file line number Diff line number Diff line change @@ -138,6 +138,9 @@ pub(crate) struct ExprCtx {
138138 pub ( crate ) self_param : Option < hir:: SelfParam > ,
139139 pub ( crate ) innermost_ret_ty : Option < hir:: Type > ,
140140 pub ( crate ) impl_ : Option < ast:: Impl > ,
141+ /// Whether this expression occurs in match arm guard position: before the
142+ /// fat arrow token
143+ pub ( crate ) in_match_guard : bool ,
141144}
142145
143146/// Original file ast nodes
Original file line number Diff line number Diff line change @@ -763,6 +763,13 @@ impl<'a> CompletionContext<'a> {
763763 . map_or ( false , |it| it. semicolon_token ( ) . is_none ( ) ) ;
764764 let impl_ = fetch_immediate_impl ( sema, original_file, expr. syntax ( ) ) ;
765765
766+ let in_match_guard = match it. parent ( ) . and_then ( ast:: MatchArm :: cast) {
767+ Some ( arm) => arm
768+ . fat_arrow_token ( )
769+ . map_or ( true , |arrow| it. text_range ( ) . start ( ) < arrow. text_range ( ) . start ( ) ) ,
770+ None => false ,
771+ } ;
772+
766773 PathKind :: Expr {
767774 expr_ctx : ExprCtx {
768775 in_block_expr,
@@ -775,6 +782,7 @@ impl<'a> CompletionContext<'a> {
775782 self_param,
776783 incomplete_let,
777784 impl_,
785+ in_match_guard,
778786 } ,
779787 }
780788 } ;
You can’t perform that action at this time.
0 commit comments