aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_db
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2021-05-10 17:04:41 +0100
committerAleksey Kladov <[email protected]>2021-05-14 14:19:27 +0100
commit0650f77dd9defaf352f81c5ee4ee73a1eae942b7 (patch)
tree4758b6ea999292cf18a078dd942f51621a2d68ea /crates/ide_db
parentab528e85f71da188722ae031b31a3a70bac1cadd (diff)
internal: remove one more immutable tree
Diffstat (limited to 'crates/ide_db')
-rw-r--r--crates/ide_db/src/helpers/merge_imports.rs34
1 files changed, 20 insertions, 14 deletions
diff --git a/crates/ide_db/src/helpers/merge_imports.rs b/crates/ide_db/src/helpers/merge_imports.rs
index af2a51a4d..475ef99b5 100644
--- a/crates/ide_db/src/helpers/merge_imports.rs
+++ b/crates/ide_db/src/helpers/merge_imports.rs
@@ -2,8 +2,9 @@
2use std::cmp::Ordering; 2use std::cmp::Ordering;
3 3
4use itertools::{EitherOrBoth, Itertools}; 4use itertools::{EitherOrBoth, Itertools};
5use syntax::ast::{ 5use syntax::{
6 self, edit::AstNodeEdit, make, AstNode, AttrsOwner, PathSegmentKind, VisibilityOwner, 6 ast::{self, make, AstNode, AttrsOwner, PathSegmentKind, VisibilityOwner},
7 ted,
7}; 8};
8 9
9/// What type of merges are allowed. 10/// What type of merges are allowed.
@@ -65,7 +66,7 @@ pub fn try_merge_trees(
65 } else { 66 } else {
66 (lhs.split_prefix(&lhs_prefix), rhs.split_prefix(&rhs_prefix)) 67 (lhs.split_prefix(&lhs_prefix), rhs.split_prefix(&rhs_prefix))
67 }; 68 };
68 recursive_merge(&lhs, &rhs, merge) 69 recursive_merge(&lhs, &rhs, merge).map(|it| it.clone_for_update())
69} 70}
70 71
71/// Recursively "zips" together lhs and rhs. 72/// Recursively "zips" together lhs and rhs.
@@ -78,7 +79,8 @@ fn recursive_merge(
78 .use_tree_list() 79 .use_tree_list()
79 .into_iter() 80 .into_iter()
80 .flat_map(|list| list.use_trees()) 81 .flat_map(|list| list.use_trees())
81 // we use Option here to early return from this function(this is not the same as a `filter` op) 82 // We use Option here to early return from this function(this is not the
83 // same as a `filter` op).
82 .map(|tree| match merge.is_tree_allowed(&tree) { 84 .map(|tree| match merge.is_tree_allowed(&tree) {
83 true => Some(tree), 85 true => Some(tree),
84 false => None, 86 false => None,
@@ -111,8 +113,10 @@ fn recursive_merge(
111 let tree_is_self = |tree: ast::UseTree| { 113 let tree_is_self = |tree: ast::UseTree| {
112 tree.path().as_ref().map(path_is_self).unwrap_or(false) 114 tree.path().as_ref().map(path_is_self).unwrap_or(false)
113 }; 115 };
114 // check if only one of the two trees has a tree list, and whether that then contains `self` or not. 116 // Check if only one of the two trees has a tree list, and
115 // If this is the case we can skip this iteration since the path without the list is already included in the other one via `self` 117 // whether that then contains `self` or not. If this is the
118 // case we can skip this iteration since the path without
119 // the list is already included in the other one via `self`.
116 let tree_contains_self = |tree: &ast::UseTree| { 120 let tree_contains_self = |tree: &ast::UseTree| {
117 tree.use_tree_list() 121 tree.use_tree_list()
118 .map(|tree_list| tree_list.use_trees().any(tree_is_self)) 122 .map(|tree_list| tree_list.use_trees().any(tree_is_self))
@@ -127,9 +131,11 @@ fn recursive_merge(
127 _ => (), 131 _ => (),
128 } 132 }
129 133
130 // glob imports arent part of the use-tree lists so we need to special handle them here as well 134 // Glob imports aren't part of the use-tree lists so we need
131 // this special handling is only required for when we merge a module import into a glob import of said module 135 // to special handle them here as well this special handling
132 // see the `merge_self_glob` or `merge_mod_into_glob` tests 136 // is only required for when we merge a module import into a
137 // glob import of said module see the `merge_self_glob` or
138 // `merge_mod_into_glob` tests.
133 if lhs_t.star_token().is_some() || rhs_t.star_token().is_some() { 139 if lhs_t.star_token().is_some() || rhs_t.star_token().is_some() {
134 *lhs_t = make::use_tree( 140 *lhs_t = make::use_tree(
135 make::path_unqualified(make::path_segment_self()), 141 make::path_unqualified(make::path_segment_self()),
@@ -165,11 +171,11 @@ fn recursive_merge(
165 } 171 }
166 } 172 }
167 173
168 Some(if let Some(old) = lhs.use_tree_list() { 174 let lhs = lhs.clone_subtree().clone_for_update();
169 lhs.replace_descendant(old, make::use_tree_list(use_trees)).clone_for_update() 175 if let Some(old) = lhs.use_tree_list() {
170 } else { 176 ted::replace(old.syntax(), make::use_tree_list(use_trees).syntax().clone_for_update());
171 lhs.clone() 177 }
172 }) 178 ast::UseTree::cast(lhs.syntax().clone_subtree())
173} 179}
174 180
175/// Traverses both paths until they differ, returning the common prefix of both. 181/// Traverses both paths until they differ, returning the common prefix of both.