Skip to content

Commit 6031dee

Browse files
committed
Enable assist only if an item is selected
1 parent e71b239 commit 6031dee

File tree

1 file changed

+38
-18
lines changed

1 file changed

+38
-18
lines changed

crates/ide_assists/src/handlers/sort_items.rs

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ use crate::{utils::get_methods, AssistContext, AssistId, AssistKind, Assists};
8282
// }
8383
// ```
8484
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+
8590
if let Some(trait_ast) = ctx.find_node_at_offset::<ast::Trait>() {
8691
add_sort_methods_assist(acc, trait_ast.assoc_item_list()?)
8792
} else if let Some(impl_ast) = ctx.find_node_at_offset::<ast::Impl>() {
@@ -205,14 +210,29 @@ mod tests {
205210

206211
use super::*;
207212

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+
208228
#[test]
209229
fn not_applicable_if_trait_empty() {
210230
cov_mark::check!(not_applicable_if_sorted_or_empty_or_single);
211231

212232
check_assist_not_applicable(
213233
sort_items,
214234
r#"
215-
t$0rait Bar {
235+
t$0rait Bar$0 {
216236
}
217237
"#,
218238
)
@@ -226,7 +246,7 @@ t$0rait Bar {
226246
sort_items,
227247
r#"
228248
struct Bar;
229-
$0impl Bar {
249+
$0impl Bar$0 {
230250
}
231251
"#,
232252
)
@@ -239,7 +259,7 @@ $0impl Bar {
239259
check_assist_not_applicable(
240260
sort_items,
241261
r#"
242-
$0struct Bar;
262+
$0struct Bar$0 ;
243263
"#,
244264
)
245265
}
@@ -251,7 +271,7 @@ $0struct Bar;
251271
check_assist_not_applicable(
252272
sort_items,
253273
r#"
254-
$0struct Bar { };
274+
$0struct Bar$0 { };
255275
"#,
256276
)
257277
}
@@ -263,7 +283,7 @@ $0struct Bar { };
263283
check_assist_not_applicable(
264284
sort_items,
265285
r#"
266-
$0enum ZeroVariants {};
286+
$0enum ZeroVariants$0 {};
267287
"#,
268288
)
269289
}
@@ -275,7 +295,7 @@ $0enum ZeroVariants {};
275295
check_assist_not_applicable(
276296
sort_items,
277297
r#"
278-
t$0rait Bar {
298+
t$0rait Bar$0 {
279299
fn a() {}
280300
fn b() {}
281301
fn c() {}
@@ -292,7 +312,7 @@ t$0rait Bar {
292312
sort_items,
293313
r#"
294314
struct Bar;
295-
$0impl Bar {
315+
$0impl Bar$0 {
296316
fn a() {}
297317
fn b() {}
298318
fn c() {}
@@ -308,7 +328,7 @@ $0impl Bar {
308328
check_assist_not_applicable(
309329
sort_items,
310330
r#"
311-
$0struct Bar {
331+
$0struct Bar$0 {
312332
a: u32,
313333
b: u8,
314334
c: u64,
@@ -324,7 +344,7 @@ $0struct Bar {
324344
check_assist_not_applicable(
325345
sort_items,
326346
r#"
327-
$0union Bar {
347+
$0union Bar$0 {
328348
a: u32,
329349
b: u8,
330350
c: u64,
@@ -340,7 +360,7 @@ $0union Bar {
340360
check_assist_not_applicable(
341361
sort_items,
342362
r#"
343-
$0enum Bar {
363+
$0enum Bar$0 {
344364
a,
345365
b,
346366
c,
@@ -354,7 +374,7 @@ $0enum Bar {
354374
check_assist(
355375
sort_items,
356376
r#"
357-
$0trait Bar {
377+
$0trait Bar$0 {
358378
fn a() {
359379
360380
}
@@ -386,7 +406,7 @@ trait Bar {
386406
sort_items,
387407
r#"
388408
struct Bar;
389-
$0impl Bar {
409+
$0impl Bar$0 {
390410
fn c() {}
391411
fn a() {}
392412
/// long
@@ -416,7 +436,7 @@ impl Bar {
416436
check_assist(
417437
sort_items,
418438
r#"
419-
$0struct Bar {
439+
$0struct Bar$0 {
420440
b: u8,
421441
a: u32,
422442
c: u64,
@@ -437,7 +457,7 @@ struct Bar {
437457
check_assist(
438458
sort_items,
439459
r#"
440-
$0struct Bar<'a, T> {
460+
$0struct Bar<'a,$0 T> {
441461
d: &'a str,
442462
b: u8,
443463
a: T,
@@ -460,7 +480,7 @@ struct Bar<'a, T> {
460480
check_assist(
461481
sort_items,
462482
r#"
463-
$0struct Bar {
483+
$0struct Bar $0{
464484
aaa: u8,
465485
a: usize,
466486
b: u8,
@@ -481,7 +501,7 @@ struct Bar {
481501
check_assist(
482502
sort_items,
483503
r#"
484-
$0union Bar {
504+
$0union Bar$0 {
485505
b: u8,
486506
a: u32,
487507
c: u64,
@@ -502,7 +522,7 @@ union Bar {
502522
check_assist(
503523
sort_items,
504524
r#"
505-
$0enum Bar {
525+
$0enum Bar $0{
506526
d{ first: u32, second: usize},
507527
b = 14,
508528
a,
@@ -526,7 +546,7 @@ enum Bar {
526546
sort_items,
527547
r#"
528548
enum Bar {
529-
d$0{ second: usize, first: u32 },
549+
d$0{ second: usize, first: u32 }$0,
530550
b = 14,
531551
a,
532552
c(u32, usize),

0 commit comments

Comments
 (0)