From 700034bd5a46dd6e3b5aa6322522e42c2a665fe9 Mon Sep 17 00:00:00 2001 From: Jesse Bakker Date: Fri, 18 Dec 2020 14:30:56 +0100 Subject: Do not merge imports with different attributes --- crates/ide_db/src/helpers/insert_use.rs | 15 ++++++++++++++- crates/ide_db/src/helpers/insert_use/tests.rs | 14 ++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) (limited to 'crates/ide_db/src/helpers') diff --git a/crates/ide_db/src/helpers/insert_use.rs b/crates/ide_db/src/helpers/insert_use.rs index 9be36d59b..d6b498be3 100644 --- a/crates/ide_db/src/helpers/insert_use.rs +++ b/crates/ide_db/src/helpers/insert_use.rs @@ -9,7 +9,7 @@ use syntax::{ ast::{ self, edit::{AstNodeEdit, IndentLevel}, - make, AstNode, PathSegmentKind, VisibilityOwner, + make, AstNode, AttrsOwner, PathSegmentKind, VisibilityOwner, }, AstToken, InsertPosition, NodeOrToken, SyntaxElement, SyntaxNode, SyntaxToken, }; @@ -180,6 +180,15 @@ fn eq_visibility(vis0: Option, vis1: Option) - } } +fn eq_attrs( + attrs0: impl Iterator, + attrs1: impl Iterator, +) -> bool { + let attrs0 = attrs0.map(|attr| attr.to_string()); + let attrs1 = attrs1.map(|attr| attr.to_string()); + attrs0.eq(attrs1) +} + pub fn try_merge_imports( lhs: &ast::Use, rhs: &ast::Use, @@ -189,6 +198,10 @@ pub fn try_merge_imports( if !eq_visibility(lhs.visibility(), rhs.visibility()) { return None; } + if !eq_attrs(lhs.attrs(), rhs.attrs()) { + return None; + } + let lhs_tree = lhs.use_tree()?; let rhs_tree = rhs.use_tree()?; let merged = try_merge_trees(&lhs_tree, &rhs_tree, merge_behavior)?; diff --git a/crates/ide_db/src/helpers/insert_use/tests.rs b/crates/ide_db/src/helpers/insert_use/tests.rs index 9e194354e..a603fe87f 100644 --- a/crates/ide_db/src/helpers/insert_use/tests.rs +++ b/crates/ide_db/src/helpers/insert_use/tests.rs @@ -447,6 +447,20 @@ use std::io;", ) } +#[test] +fn merge_groups_skip_attributed() { + check_full( + "std::io", + r#" +#[cfg(feature = "gated")] use std::fmt::{Result, Display}; +"#, + r#" +#[cfg(feature = "gated")] use std::fmt::{Result, Display}; +use std::io; +"#, + ) +} + #[test] #[ignore] // FIXME: Support this fn split_out_merge() { -- cgit v1.2.3