Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit b241593

Browse files
committed
merge imports assist always applies to covering use item except for nested use tree selections
1 parent a8a18f3 commit b241593

File tree

1 file changed

+23
-76
lines changed

1 file changed

+23
-76
lines changed

crates/ide-assists/src/handlers/merge_imports.rs

Lines changed: 23 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,13 @@ use Edit::*;
3232
pub(crate) fn merge_imports(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
3333
let (target, edits) = if ctx.has_empty_selection() {
3434
// Merge a neighbor
35-
let mut tree: ast::UseTree = ctx.find_node_at_offset()?;
36-
if ctx.config.insert_use.granularity == ImportGranularity::One
37-
&& tree.parent_use_tree_list().is_some()
38-
{
39-
cov_mark::hit!(resolve_top_use_tree_for_import_one);
40-
tree = tree.top_use_tree();
41-
}
35+
cov_mark::hit!(merge_with_use_item_neighbors);
36+
let tree = ctx.find_node_at_offset::<ast::UseTree>()?.top_use_tree();
4237
let target = tree.syntax().text_range();
4338

44-
let edits = if let Some(use_item) = tree.syntax().parent().and_then(ast::Use::cast) {
45-
cov_mark::hit!(merge_with_use_item_neighbors);
46-
let mut neighbor = next_prev().find_map(|dir| neighbor(&use_item, dir)).into_iter();
47-
use_item.try_merge_from(&mut neighbor, &ctx.config.insert_use)
48-
} else {
49-
cov_mark::hit!(merge_with_use_tree_neighbors);
50-
let mut neighbor = next_prev().find_map(|dir| neighbor(&tree, dir)).into_iter();
51-
tree.clone().try_merge_from(&mut neighbor, &ctx.config.insert_use)
52-
};
39+
let use_item = tree.syntax().parent().and_then(ast::Use::cast)?;
40+
let mut neighbor = next_prev().find_map(|dir| neighbor(&use_item, dir)).into_iter();
41+
let edits = use_item.try_merge_from(&mut neighbor, &ctx.config.insert_use);
5342
(target, edits?)
5443
} else {
5544
// Merge selected
@@ -159,10 +148,7 @@ impl Edit {
159148

160149
#[cfg(test)]
161150
mod tests {
162-
use crate::tests::{
163-
check_assist, check_assist_import_one, check_assist_not_applicable,
164-
check_assist_not_applicable_for_import_one,
165-
};
151+
use crate::tests::{check_assist, check_assist_import_one, check_assist_not_applicable};
166152

167153
use super::*;
168154

@@ -206,10 +192,7 @@ use std::fmt::{Debug, Display};
206192
);
207193

208194
// The assist macro below calls `check_assist_import_one` 4 times with different input
209-
// use item variations based on the first 2 input parameters, but only 2 calls
210-
// contain `use {std::fmt$0::{Display, Debug}};` for which the top use tree will need
211-
// to be resolved.
212-
cov_mark::check_count!(resolve_top_use_tree_for_import_one, 2);
195+
// use item variations based on the first 2 input parameters.
213196
cov_mark::check_count!(merge_with_use_item_neighbors, 4);
214197
check_assist_import_one_variations!(
215198
"std::fmt$0::{Display, Debug}",
@@ -257,7 +240,7 @@ use std::fmt::{Debug, Display};
257240
}
258241

259242
#[test]
260-
fn merge_self1() {
243+
fn merge_self() {
261244
check_assist(
262245
merge_imports,
263246
r"
@@ -275,28 +258,6 @@ use std::fmt::{self, Display};
275258
);
276259
}
277260

278-
#[test]
279-
fn merge_self2() {
280-
check_assist(
281-
merge_imports,
282-
r"
283-
use std::{fmt, $0fmt::Display};
284-
",
285-
r"
286-
use std::{fmt::{self, Display}};
287-
",
288-
);
289-
}
290-
291-
#[test]
292-
fn not_applicable_to_single_one_style_import() {
293-
cov_mark::check!(resolve_top_use_tree_for_import_one);
294-
check_assist_not_applicable_for_import_one(
295-
merge_imports,
296-
"use {std::{fmt, $0fmt::Display}};",
297-
);
298-
}
299-
300261
#[test]
301262
fn skip_pub1() {
302263
check_assist_not_applicable(
@@ -385,14 +346,14 @@ pub(in this::path) use std::fmt::{Debug, Display};
385346

386347
#[test]
387348
fn test_merge_nested() {
388-
cov_mark::check!(merge_with_use_tree_neighbors);
389349
check_assist(
390350
merge_imports,
391351
r"
392-
use std::{fmt$0::Debug, fmt::Display};
352+
use std::{fmt$0::Debug, fmt::Error};
353+
use std::{fmt::Write, fmt::Display};
393354
",
394355
r"
395-
use std::{fmt::{Debug, Display}};
356+
use std::fmt::{Debug, Display, Error, Write};
396357
",
397358
);
398359
}
@@ -402,10 +363,11 @@ use std::{fmt::{Debug, Display}};
402363
check_assist(
403364
merge_imports,
404365
r"
405-
use std::{fmt::Debug, fmt$0::Display};
366+
use std::{fmt::Debug, fmt$0::Error};
367+
use std::{fmt::Write, fmt::Display};
406368
",
407369
r"
408-
use std::{fmt::{Debug, Display}};
370+
use std::fmt::{Debug, Display, Error, Write};
409371
",
410372
);
411373
}
@@ -448,19 +410,6 @@ use std::fmt::{self, Debug, Display, Write};
448410
);
449411
}
450412

451-
#[test]
452-
fn test_merge_self_with_nested_self_item() {
453-
check_assist(
454-
merge_imports,
455-
r"
456-
use std::{fmt$0::{self, Debug}, fmt::{Write, Display}};
457-
",
458-
r"
459-
use std::{fmt::{self, Debug, Display, Write}};
460-
",
461-
);
462-
}
463-
464413
#[test]
465414
fn test_merge_nested_self_and_empty() {
466415
check_assist(
@@ -579,29 +528,27 @@ use foo::{bar, baz};
579528
check_assist(
580529
merge_imports,
581530
r"
582-
use {
583-
foo$0::bar,
584-
foo::baz,
531+
use foo$0::{
532+
bar, baz,
585533
};
534+
use foo::qux;
586535
",
587536
r"
588-
use {
589-
foo::{bar, baz},
537+
use foo::{
538+
bar, baz, qux,
590539
};
591540
",
592541
);
593542
check_assist(
594543
merge_imports,
595544
r"
596-
use {
597-
foo::baz,
598-
foo$0::bar,
545+
use foo::{
546+
baz, bar,
599547
};
548+
use foo$0::qux;
600549
",
601550
r"
602-
use {
603-
foo::{bar, baz},
604-
};
551+
use foo::{bar, baz, qux};
605552
",
606553
);
607554
}

0 commit comments

Comments
 (0)