aboutsummaryrefslogtreecommitdiff
path: root/crates/assists
diff options
context:
space:
mode:
authorLukas Wirth <[email protected]>2020-09-05 15:15:16 +0100
committerLukas Wirth <[email protected]>2020-09-05 15:15:16 +0100
commit74b755d23366bcfa5437df25b023f5a2451e1ea6 (patch)
tree064d14350b07990caabce384e4529ccbbe7f50b7 /crates/assists
parent7ccb198af81d8f33ccad66a417ae6529f91df625 (diff)
Allow merge_imports assists to merge imports of equal visibility
Diffstat (limited to 'crates/assists')
-rw-r--r--crates/assists/src/handlers/merge_imports.rs50
-rw-r--r--crates/assists/src/utils/insert_use.rs17
2 files changed, 61 insertions, 6 deletions
diff --git a/crates/assists/src/handlers/merge_imports.rs b/crates/assists/src/handlers/merge_imports.rs
index da084d5fb..0bd679260 100644
--- a/crates/assists/src/handlers/merge_imports.rs
+++ b/crates/assists/src/handlers/merge_imports.rs
@@ -150,6 +150,56 @@ pub use std::fmt::Display;
150 } 150 }
151 151
152 #[test] 152 #[test]
153 fn skip_pub_crate_pub() {
154 check_assist_not_applicable(
155 merge_imports,
156 r"
157pub(crate) use std::fmt<|>::Debug;
158pub use std::fmt::Display;
159",
160 );
161 }
162
163 #[test]
164 fn skip_pub_pub_crate() {
165 check_assist_not_applicable(
166 merge_imports,
167 r"
168pub use std::fmt<|>::Debug;
169pub(crate) use std::fmt::Display;
170",
171 );
172 }
173
174 #[test]
175 fn merge_pub() {
176 check_assist(
177 merge_imports,
178 r"
179pub use std::fmt<|>::Debug;
180pub use std::fmt::Display;
181",
182 r"
183pub use std::fmt::{Debug, Display};
184",
185 )
186 }
187
188 #[test]
189 fn merge_pub_crate() {
190 check_assist(
191 merge_imports,
192 r"
193pub(crate) use std::fmt<|>::Debug;
194pub(crate) use std::fmt::Display;
195",
196 r"
197pub(crate) use std::fmt::{Debug, Display};
198",
199 )
200 }
201
202 #[test]
153 fn test_merge_nested() { 203 fn test_merge_nested() {
154 check_assist( 204 check_assist(
155 merge_imports, 205 merge_imports,
diff --git a/crates/assists/src/utils/insert_use.rs b/crates/assists/src/utils/insert_use.rs
index a920e12c5..98553b2e0 100644
--- a/crates/assists/src/utils/insert_use.rs
+++ b/crates/assists/src/utils/insert_use.rs
@@ -138,18 +138,23 @@ pub(crate) fn insert_use(
138 algo::insert_children(scope.as_syntax_node(), insert_position, to_insert) 138 algo::insert_children(scope.as_syntax_node(), insert_position, to_insert)
139} 139}
140 140
141fn eq_visibility(vis0: Option<ast::Visibility>, vis1: Option<ast::Visibility>) -> bool {
142 match (vis0, vis1) {
143 (None, None) => true,
144 // FIXME: Don't use the string representation to check for equality
145 // spaces inside of the node would break this comparison
146 (Some(vis0), Some(vis1)) => vis0.to_string() == vis1.to_string(),
147 _ => false,
148 }
149}
150
141pub(crate) fn try_merge_imports( 151pub(crate) fn try_merge_imports(
142 old: &ast::Use, 152 old: &ast::Use,
143 new: &ast::Use, 153 new: &ast::Use,
144 merge_behaviour: MergeBehaviour, 154 merge_behaviour: MergeBehaviour,
145) -> Option<ast::Use> { 155) -> Option<ast::Use> {
146 // don't merge imports with different visibilities 156 // don't merge imports with different visibilities
147 if old 157 if !eq_visibility(old.visibility(), new.visibility()) {
148 .visibility()
149 .and_then(|vis| vis.pub_token())
150 .or_else(|| new.visibility().and_then(|vis| vis.pub_token()))
151 .is_some()
152 {
153 return None; 158 return None;
154 } 159 }
155 let old_tree = old.use_tree()?; 160 let old_tree = old.use_tree()?;