File tree 4 files changed +88
-1
lines changed
crates/ide-completion/src
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(
21
21
ref is_func_update,
22
22
ref innermost_ret_ty,
23
23
ref impl_,
24
+ in_match_guard,
24
25
..
25
26
} : & ExprCtx ,
26
27
) {
@@ -195,7 +196,11 @@ pub(crate) fn complete_expr_path(
195
196
add_keyword ( "while" , "while $1 {\n $0\n }" ) ;
196
197
add_keyword ( "while let" , "while let $1 = $2 {\n $0\n }" ) ;
197
198
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
+ }
199
204
add_keyword ( "if let" , "if let $1 = $2 {\n $0\n }" ) ;
200
205
add_keyword ( "for" , "for $1 in $2 {\n $0\n }" ) ;
201
206
add_keyword ( "true" , "true" ) ;
Original file line number Diff line number Diff line change @@ -163,4 +163,75 @@ fn main() {
163
163
"# ,
164
164
) ;
165
165
}
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
+ }
166
237
}
Original file line number Diff line number Diff line change @@ -138,6 +138,9 @@ pub(crate) struct ExprCtx {
138
138
pub ( crate ) self_param : Option < hir:: SelfParam > ,
139
139
pub ( crate ) innermost_ret_ty : Option < hir:: Type > ,
140
140
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 ,
141
144
}
142
145
143
146
/// Original file ast nodes
Original file line number Diff line number Diff line change @@ -763,6 +763,13 @@ impl<'a> CompletionContext<'a> {
763
763
. map_or ( false , |it| it. semicolon_token ( ) . is_none ( ) ) ;
764
764
let impl_ = fetch_immediate_impl ( sema, original_file, expr. syntax ( ) ) ;
765
765
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
+
766
773
PathKind :: Expr {
767
774
expr_ctx : ExprCtx {
768
775
in_block_expr,
@@ -775,6 +782,7 @@ impl<'a> CompletionContext<'a> {
775
782
self_param,
776
783
incomplete_let,
777
784
impl_,
785
+ in_match_guard,
778
786
} ,
779
787
}
780
788
} ;
You can’t perform that action at this time.
0 commit comments