|
1 | 1 | //! Handle syntactic aspects of merging UseTrees.
|
2 | 2 | use std::cmp::Ordering;
|
3 |
| -use std::iter::empty; |
4 | 3 |
|
5 | 4 | use itertools::{EitherOrBoth, Itertools};
|
6 | 5 | use parser::T;
|
@@ -184,59 +183,10 @@ fn recursive_merge(lhs: &ast::UseTree, rhs: &ast::UseTree, merge: MergeBehavior)
|
184 | 183 | }
|
185 | 184 | Err(insert_idx) => {
|
186 | 185 | use_trees.insert(insert_idx, rhs_t.clone());
|
187 |
| - match lhs.use_tree_list() { |
188 |
| - // Creates a new use tree list with the item. |
189 |
| - None => lhs.get_or_create_use_tree_list().add_use_tree(rhs_t), |
190 |
| - // Recreates the use tree list with sorted items (see `use_tree_cmp` doc). |
191 |
| - Some(use_tree_list) => { |
192 |
| - if use_tree_list.l_curly_token().is_none() { |
193 |
| - ted::insert_raw( |
194 |
| - Position::first_child_of(use_tree_list.syntax()), |
195 |
| - make::token(T!['{']), |
196 |
| - ); |
197 |
| - } |
198 |
| - if use_tree_list.r_curly_token().is_none() { |
199 |
| - ted::insert_raw( |
200 |
| - Position::last_child_of(use_tree_list.syntax()), |
201 |
| - make::token(T!['}']), |
202 |
| - ); |
203 |
| - } |
204 |
| - |
205 |
| - let mut elements = Vec::new(); |
206 |
| - for (idx, tree) in use_trees.iter().enumerate() { |
207 |
| - if idx > 0 { |
208 |
| - elements.push(make::token(T![,]).into()); |
209 |
| - elements.push(make::tokens::single_space().into()); |
210 |
| - } |
211 |
| - elements.push(tree.syntax().clone().into()); |
212 |
| - } |
213 |
| - |
214 |
| - let start = use_tree_list |
215 |
| - .l_curly_token() |
216 |
| - .and_then(|l_curly| { |
217 |
| - algo::non_trivia_sibling(l_curly.into(), Direction::Next) |
218 |
| - }) |
219 |
| - .filter(|it| it.kind() != T!['}']); |
220 |
| - let end = use_tree_list |
221 |
| - .r_curly_token() |
222 |
| - .and_then(|r_curly| { |
223 |
| - algo::non_trivia_sibling(r_curly.into(), Direction::Prev) |
224 |
| - }) |
225 |
| - .filter(|it| it.kind() != T!['{']); |
226 |
| - if let Some((start, end)) = start.zip(end) { |
227 |
| - // Attempt to insert elements while preserving preceding and trailing trivia. |
228 |
| - ted::replace_all(start..=end, elements); |
229 |
| - } else { |
230 |
| - let new_use_tree_list = make::use_tree_list(empty()).clone_for_update(); |
231 |
| - let trees_pos = match new_use_tree_list.l_curly_token() { |
232 |
| - Some(l_curly) => Position::after(l_curly), |
233 |
| - None => Position::last_child_of(new_use_tree_list.syntax()), |
234 |
| - }; |
235 |
| - ted::insert_all_raw(trees_pos, elements); |
236 |
| - ted::replace(use_tree_list.syntax(), new_use_tree_list.syntax()); |
237 |
| - } |
238 |
| - } |
239 |
| - } |
| 186 | + // We simply add the use tree to the end of tree list. Ordering of use trees |
| 187 | + // and imports is done by the `try_normalize_*` functions. The sorted `use_trees` |
| 188 | + // vec is only used for binary search. |
| 189 | + lhs.get_or_create_use_tree_list().add_use_tree(rhs_t); |
240 | 190 | }
|
241 | 191 | }
|
242 | 192 | }
|
@@ -594,7 +544,7 @@ fn use_tree_cmp_bin_search(lhs: &ast::UseTree, rhs: &ast::UseTree) -> Ordering {
|
594 | 544 | /// and `crate` first, then identifier imports with lowercase ones first and upper snake case
|
595 | 545 | /// (e.g. UPPER_SNAKE_CASE) ones last, then glob imports, and at last list imports.
|
596 | 546 | ///
|
597 |
| -/// Example foo::{self, foo, baz, Baz, Qux, FOO_BAZ, *, {Bar}} |
| 547 | +/// Example: `foo::{self, baz, foo, Baz, Qux, FOO_BAZ, *, {Bar}}` |
598 | 548 | /// Ref: <https://github.com/rust-lang/rustfmt/blob/6356fca675bd756d71f5c123cd053d17b16c573e/src/imports.rs#L83-L86>.
|
599 | 549 | pub(super) fn use_tree_cmp(a: &ast::UseTree, b: &ast::UseTree) -> Ordering {
|
600 | 550 | let a_is_simple_path = a.is_simple_path() && a.rename().is_none();
|
|
0 commit comments