@@ -82,6 +82,11 @@ use crate::{utils::get_methods, AssistContext, AssistId, AssistKind, Assists};
82
82
// }
83
83
// ```
84
84
pub ( crate ) fn sort_items ( acc : & mut Assists , ctx : & AssistContext ) -> Option < ( ) > {
85
+ if ctx. frange . range . is_empty ( ) {
86
+ cov_mark:: hit!( not_applicable_if_no_selection) ;
87
+ return None ;
88
+ }
89
+
85
90
if let Some ( trait_ast) = ctx. find_node_at_offset :: < ast:: Trait > ( ) {
86
91
add_sort_methods_assist ( acc, trait_ast. assoc_item_list ( ) ?)
87
92
} else if let Some ( impl_ast) = ctx. find_node_at_offset :: < ast:: Impl > ( ) {
@@ -205,14 +210,29 @@ mod tests {
205
210
206
211
use super :: * ;
207
212
213
+ #[ test]
214
+ fn not_applicable_if_no_selection ( ) {
215
+ cov_mark:: check!( not_applicable_if_no_selection) ;
216
+
217
+ check_assist_not_applicable (
218
+ sort_items,
219
+ r#"
220
+ t$0rait Bar {
221
+ fn b();
222
+ fn a();
223
+ }
224
+ "# ,
225
+ )
226
+ }
227
+
208
228
#[ test]
209
229
fn not_applicable_if_trait_empty ( ) {
210
230
cov_mark:: check!( not_applicable_if_sorted_or_empty_or_single) ;
211
231
212
232
check_assist_not_applicable (
213
233
sort_items,
214
234
r#"
215
- t$0rait Bar {
235
+ t$0rait Bar$0 {
216
236
}
217
237
"# ,
218
238
)
@@ -226,7 +246,7 @@ t$0rait Bar {
226
246
sort_items,
227
247
r#"
228
248
struct Bar;
229
- $0impl Bar {
249
+ $0impl Bar$0 {
230
250
}
231
251
"# ,
232
252
)
@@ -239,7 +259,7 @@ $0impl Bar {
239
259
check_assist_not_applicable (
240
260
sort_items,
241
261
r#"
242
- $0struct Bar;
262
+ $0struct Bar$0 ;
243
263
"# ,
244
264
)
245
265
}
@@ -251,7 +271,7 @@ $0struct Bar;
251
271
check_assist_not_applicable (
252
272
sort_items,
253
273
r#"
254
- $0struct Bar { };
274
+ $0struct Bar$0 { };
255
275
"# ,
256
276
)
257
277
}
@@ -263,7 +283,7 @@ $0struct Bar { };
263
283
check_assist_not_applicable (
264
284
sort_items,
265
285
r#"
266
- $0enum ZeroVariants {};
286
+ $0enum ZeroVariants$0 {};
267
287
"# ,
268
288
)
269
289
}
@@ -275,7 +295,7 @@ $0enum ZeroVariants {};
275
295
check_assist_not_applicable (
276
296
sort_items,
277
297
r#"
278
- t$0rait Bar {
298
+ t$0rait Bar$0 {
279
299
fn a() {}
280
300
fn b() {}
281
301
fn c() {}
@@ -292,7 +312,7 @@ t$0rait Bar {
292
312
sort_items,
293
313
r#"
294
314
struct Bar;
295
- $0impl Bar {
315
+ $0impl Bar$0 {
296
316
fn a() {}
297
317
fn b() {}
298
318
fn c() {}
@@ -308,7 +328,7 @@ $0impl Bar {
308
328
check_assist_not_applicable (
309
329
sort_items,
310
330
r#"
311
- $0struct Bar {
331
+ $0struct Bar$0 {
312
332
a: u32,
313
333
b: u8,
314
334
c: u64,
@@ -324,7 +344,7 @@ $0struct Bar {
324
344
check_assist_not_applicable (
325
345
sort_items,
326
346
r#"
327
- $0union Bar {
347
+ $0union Bar$0 {
328
348
a: u32,
329
349
b: u8,
330
350
c: u64,
@@ -340,7 +360,7 @@ $0union Bar {
340
360
check_assist_not_applicable (
341
361
sort_items,
342
362
r#"
343
- $0enum Bar {
363
+ $0enum Bar$0 {
344
364
a,
345
365
b,
346
366
c,
@@ -354,7 +374,7 @@ $0enum Bar {
354
374
check_assist (
355
375
sort_items,
356
376
r#"
357
- $0trait Bar {
377
+ $0trait Bar$0 {
358
378
fn a() {
359
379
360
380
}
@@ -386,7 +406,7 @@ trait Bar {
386
406
sort_items,
387
407
r#"
388
408
struct Bar;
389
- $0impl Bar {
409
+ $0impl Bar$0 {
390
410
fn c() {}
391
411
fn a() {}
392
412
/// long
@@ -416,7 +436,7 @@ impl Bar {
416
436
check_assist (
417
437
sort_items,
418
438
r#"
419
- $0struct Bar {
439
+ $0struct Bar$0 {
420
440
b: u8,
421
441
a: u32,
422
442
c: u64,
@@ -437,7 +457,7 @@ struct Bar {
437
457
check_assist (
438
458
sort_items,
439
459
r#"
440
- $0struct Bar<'a, T> {
460
+ $0struct Bar<'a,$0 T> {
441
461
d: &'a str,
442
462
b: u8,
443
463
a: T,
@@ -460,7 +480,7 @@ struct Bar<'a, T> {
460
480
check_assist (
461
481
sort_items,
462
482
r#"
463
- $0struct Bar {
483
+ $0struct Bar $0 {
464
484
aaa: u8,
465
485
a: usize,
466
486
b: u8,
@@ -481,7 +501,7 @@ struct Bar {
481
501
check_assist (
482
502
sort_items,
483
503
r#"
484
- $0union Bar {
504
+ $0union Bar$0 {
485
505
b: u8,
486
506
a: u32,
487
507
c: u64,
@@ -502,7 +522,7 @@ union Bar {
502
522
check_assist (
503
523
sort_items,
504
524
r#"
505
- $0enum Bar {
525
+ $0enum Bar $0 {
506
526
d{ first: u32, second: usize},
507
527
b = 14,
508
528
a,
@@ -526,7 +546,7 @@ enum Bar {
526
546
sort_items,
527
547
r#"
528
548
enum Bar {
529
- d$0{ second: usize, first: u32 },
549
+ d$0{ second: usize, first: u32 }$0 ,
530
550
b = 14,
531
551
a,
532
552
c(u32, usize),
0 commit comments