From d201317c67eb1310ff775cf2164c9f4731c72c83 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sat, 5 Sep 2020 15:00:06 +0200 Subject: Fix segment_iter not iterating segments properly --- crates/assists/src/utils/insert_use.rs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'crates/assists/src/utils') diff --git a/crates/assists/src/utils/insert_use.rs b/crates/assists/src/utils/insert_use.rs index 8a4c8520d..1cb52318a 100644 --- a/crates/assists/src/utils/insert_use.rs +++ b/crates/assists/src/utils/insert_use.rs @@ -138,7 +138,7 @@ pub(crate) fn insert_use( algo::insert_children(scope.as_syntax_node(), insert_position, to_insert) } -fn try_merge_imports( +pub(crate) fn try_merge_imports( old: &ast::Use, new: &ast::Use, merge_behaviour: MergeBehaviour, @@ -161,7 +161,7 @@ fn use_tree_list_is_nested(tl: &ast::UseTreeList) -> bool { } // FIXME: currently this merely prepends the new tree into old, ideally it would insert the items in a sorted fashion -pub fn try_merge_trees( +pub(crate) fn try_merge_trees( old: &ast::UseTree, new: &ast::UseTree, merge_behaviour: MergeBehaviour, @@ -278,7 +278,8 @@ fn first_path(path: &ast::Path) -> ast::Path { } fn segment_iter(path: &ast::Path) -> impl Iterator + Clone { - path.syntax().children().flat_map(ast::PathSegment::cast) + // cant make use of SyntaxNode::siblings, because the returned Iterator is not clone + successors(first_segment(path), |p| p.parent_path().parent_path().and_then(|p| p.segment())) } #[derive(PartialEq, Eq)] @@ -684,8 +685,18 @@ use std::io;", check_last( "foo::bar", r"use foo::bar::baz::Qux;", - r"use foo::bar::baz::Qux; -use foo::bar;", + r"use foo::bar; +use foo::bar::baz::Qux;", + ); + } + + #[test] + fn insert_short_before_long() { + check_none( + "foo::bar", + r"use foo::bar::baz::Qux;", + r"use foo::bar; +use foo::bar::baz::Qux;", ); } -- cgit v1.2.3 From 7ccb198af81d8f33ccad66a417ae6529f91df625 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sat, 5 Sep 2020 15:51:26 +0200 Subject: Remove duplicated import merge logic --- crates/assists/src/utils/insert_use.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'crates/assists/src/utils') diff --git a/crates/assists/src/utils/insert_use.rs b/crates/assists/src/utils/insert_use.rs index 1cb52318a..a920e12c5 100644 --- a/crates/assists/src/utils/insert_use.rs +++ b/crates/assists/src/utils/insert_use.rs @@ -143,8 +143,13 @@ pub(crate) fn try_merge_imports( new: &ast::Use, merge_behaviour: MergeBehaviour, ) -> Option { - // don't merge into re-exports - if old.visibility().and_then(|vis| vis.pub_token()).is_some() { + // don't merge imports with different visibilities + if old + .visibility() + .and_then(|vis| vis.pub_token()) + .or_else(|| new.visibility().and_then(|vis| vis.pub_token())) + .is_some() + { return None; } let old_tree = old.use_tree()?; -- cgit v1.2.3 From 74b755d23366bcfa5437df25b023f5a2451e1ea6 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sat, 5 Sep 2020 16:15:16 +0200 Subject: Allow merge_imports assists to merge imports of equal visibility --- crates/assists/src/utils/insert_use.rs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'crates/assists/src/utils') 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( algo::insert_children(scope.as_syntax_node(), insert_position, to_insert) } +fn eq_visibility(vis0: Option, vis1: Option) -> bool { + match (vis0, vis1) { + (None, None) => true, + // FIXME: Don't use the string representation to check for equality + // spaces inside of the node would break this comparison + (Some(vis0), Some(vis1)) => vis0.to_string() == vis1.to_string(), + _ => false, + } +} + pub(crate) fn try_merge_imports( old: &ast::Use, new: &ast::Use, merge_behaviour: MergeBehaviour, ) -> Option { // don't merge imports with different visibilities - if old - .visibility() - .and_then(|vis| vis.pub_token()) - .or_else(|| new.visibility().and_then(|vis| vis.pub_token())) - .is_some() - { + if !eq_visibility(old.visibility(), new.visibility()) { return None; } let old_tree = old.use_tree()?; -- cgit v1.2.3