|
1 | 1 | use either::Either; |
2 | 2 | use ide_db::imports::{ |
3 | 3 | insert_use::{ImportGranularity, InsertUseConfig}, |
4 | | - merge_imports::{MergeBehavior, try_merge_imports, try_merge_trees, try_normalize_use_tree}, |
| 4 | + merge_imports::{MergeBehavior, try_merge_imports, try_merge_trees}, |
5 | 5 | }; |
6 | | -use itertools::Itertools; |
7 | 6 | use syntax::{ |
8 | 7 | AstNode, SyntaxElement, SyntaxNode, |
9 | 8 | algo::neighbor, |
10 | | - ast::{self, edit_in_place::Removable}, |
11 | | - match_ast, ted, |
| 9 | + ast::{self, syntax_factory::SyntaxFactory}, |
| 10 | + match_ast, |
| 11 | + syntax_editor::Removable, |
12 | 12 | }; |
13 | 13 |
|
14 | 14 | use crate::{ |
@@ -69,49 +69,32 @@ pub(crate) fn merge_imports(acc: &mut Assists, ctx: &AssistContext<'_>) -> Optio |
69 | 69 | (selection_range, edits?) |
70 | 70 | }; |
71 | 71 |
|
| 72 | + let parent_node = match ctx.covering_element() { |
| 73 | + SyntaxElement::Node(n) => n, |
| 74 | + SyntaxElement::Token(t) => t.parent()?, |
| 75 | + }; |
| 76 | + |
72 | 77 | acc.add(AssistId::refactor_rewrite("merge_imports"), "Merge imports", target, |builder| { |
73 | | - let edits_mut: Vec<Edit> = edits |
74 | | - .into_iter() |
75 | | - .map(|it| match it { |
76 | | - Remove(Either::Left(it)) => Remove(Either::Left(builder.make_mut(it))), |
77 | | - Remove(Either::Right(it)) => Remove(Either::Right(builder.make_mut(it))), |
78 | | - Replace(old, new) => Replace(builder.make_syntax_mut(old), new), |
79 | | - }) |
80 | | - .collect(); |
81 | | - for edit in edits_mut { |
| 78 | + let make = SyntaxFactory::with_mappings(); |
| 79 | + let mut editor = builder.make_editor(&parent_node); |
| 80 | + |
| 81 | + for edit in edits { |
82 | 82 | match edit { |
83 | | - Remove(it) => it.as_ref().either(Removable::remove, Removable::remove), |
84 | | - Replace(old, new) => { |
85 | | - ted::replace(old, &new); |
86 | | - |
87 | | - // If there's a selection and we're replacing a use tree in a tree list, |
88 | | - // normalize the parent use tree if it only contains the merged subtree. |
89 | | - if !ctx.has_empty_selection() { |
90 | | - let normalized_use_tree = ast::UseTree::cast(new) |
91 | | - .as_ref() |
92 | | - .and_then(ast::UseTree::parent_use_tree_list) |
93 | | - .and_then(|use_tree_list| { |
94 | | - if use_tree_list.use_trees().collect_tuple::<(_,)>().is_some() { |
95 | | - Some(use_tree_list.parent_use_tree()) |
96 | | - } else { |
97 | | - None |
98 | | - } |
99 | | - }) |
100 | | - .and_then(|target_tree| { |
101 | | - try_normalize_use_tree( |
102 | | - &target_tree, |
103 | | - ctx.config.insert_use.granularity.into(), |
104 | | - ) |
105 | | - .map(|top_use_tree_flat| (target_tree, top_use_tree_flat)) |
106 | | - }); |
107 | | - if let Some((old_tree, new_tree)) = normalized_use_tree { |
108 | | - cov_mark::hit!(replace_parent_with_normalized_use_tree); |
109 | | - ted::replace(old_tree.syntax(), new_tree.syntax()); |
110 | | - } |
| 83 | + Remove(it) => { |
| 84 | + let node = it.as_ref(); |
| 85 | + if let Some(left) = node.left() { |
| 86 | + left.remove(&mut editor); |
| 87 | + } else if let Some(right) = node.right() { |
| 88 | + right.remove(&mut editor); |
111 | 89 | } |
112 | 90 | } |
| 91 | + Replace(old, new) => { |
| 92 | + editor.replace(old, &new); |
| 93 | + } |
113 | 94 | } |
114 | 95 | } |
| 96 | + editor.add_mappings(make.finish_with_mappings()); |
| 97 | + builder.add_file_edits(ctx.vfs_file_id(), editor); |
115 | 98 | }) |
116 | 99 | } |
117 | 100 |
|
@@ -723,11 +706,10 @@ use std::{ |
723 | 706 | ); |
724 | 707 |
|
725 | 708 | cov_mark::check!(merge_with_selected_use_tree_neighbors); |
726 | | - cov_mark::check!(replace_parent_with_normalized_use_tree); |
727 | 709 | check_assist( |
728 | 710 | merge_imports, |
729 | 711 | r"use std::$0{fmt::Display, fmt::Debug}$0;", |
730 | | - r"use std::fmt::{Debug, Display};", |
| 712 | + r"use std::{fmt::{Debug, Display}};", |
731 | 713 | ); |
732 | 714 | } |
733 | 715 |
|
|
0 commit comments