From 2bf720900f94e36969af44ff8ac52470faf9af4b Mon Sep 17 00:00:00 2001 From: Lukas Tobias Wirth Date: Thu, 20 May 2021 10:25:04 +0200 Subject: Check for differing attributes in granularity guessing --- crates/ide_db/src/helpers/insert_use.rs | 14 ++++++++------ crates/ide_db/src/helpers/insert_use/tests.rs | 12 ++++++++++++ crates/ide_db/src/helpers/merge_imports.rs | 2 +- 3 files changed, 21 insertions(+), 7 deletions(-) (limited to 'crates/ide_db') diff --git a/crates/ide_db/src/helpers/insert_use.rs b/crates/ide_db/src/helpers/insert_use.rs index e7ae69302..aa61c5bcb 100644 --- a/crates/ide_db/src/helpers/insert_use.rs +++ b/crates/ide_db/src/helpers/insert_use.rs @@ -4,13 +4,13 @@ use std::cmp::Ordering; use hir::Semantics; use syntax::{ algo, - ast::{self, make, AstNode, ModuleItemOwner, PathSegmentKind, VisibilityOwner}, + ast::{self, make, AstNode, AttrsOwner, ModuleItemOwner, PathSegmentKind, VisibilityOwner}, ted, AstToken, Direction, NodeOrToken, SyntaxNode, SyntaxToken, }; use crate::{ helpers::merge_imports::{ - common_prefix, eq_visibility, try_merge_imports, use_tree_path_cmp, MergeBehavior, + common_prefix, eq_attrs, eq_visibility, try_merge_imports, use_tree_path_cmp, MergeBehavior, }, RootDatabase, }; @@ -88,7 +88,7 @@ impl ImportScope { let use_stmt = |item| match item { ast::Item::Use(use_) => { let use_tree = use_.use_tree()?; - Some((use_tree, use_.visibility())) + Some((use_tree, use_.visibility(), use_.attrs())) } _ => None, }; @@ -98,7 +98,7 @@ impl ImportScope { } .filter_map(use_stmt); let mut res = ImportGranularityGuess::Unknown; - let (mut prev, mut prev_vis) = match use_stmts.next() { + let (mut prev, mut prev_vis, mut prev_attrs) = match use_stmts.next() { Some(it) => it, None => return res, }; @@ -113,11 +113,12 @@ impl ImportScope { } } - let (curr, curr_vis) = match use_stmts.next() { + let (curr, curr_vis, curr_attrs) = match use_stmts.next() { Some(it) => it, None => break res, }; - if eq_visibility(prev_vis, curr_vis.clone()) { + if eq_visibility(prev_vis, curr_vis.clone()) && eq_attrs(prev_attrs, curr_attrs.clone()) + { if let Some((prev_path, curr_path)) = prev.path().zip(curr.path()) { if let Some(_) = common_prefix(&prev_path, &curr_path) { if prev.use_tree_list().is_none() && curr.use_tree_list().is_none() { @@ -133,6 +134,7 @@ impl ImportScope { } prev = curr; prev_vis = curr_vis; + prev_attrs = curr_attrs; } } } diff --git a/crates/ide_db/src/helpers/insert_use/tests.rs b/crates/ide_db/src/helpers/insert_use/tests.rs index f795bbf00..78a2a87b3 100644 --- a/crates/ide_db/src/helpers/insert_use/tests.rs +++ b/crates/ide_db/src/helpers/insert_use/tests.rs @@ -717,6 +717,18 @@ pub use foo::bar::qux; ); } +#[test] +fn guess_skips_differing_attrs() { + check_guess( + r" +pub use foo::bar::baz; +#[doc(hidden)] +pub use foo::bar::qux; +", + ImportGranularityGuess::Unknown, + ); +} + #[test] fn guess_grouping_matters() { check_guess( diff --git a/crates/ide_db/src/helpers/merge_imports.rs b/crates/ide_db/src/helpers/merge_imports.rs index 546f2d0c4..697e8bcff 100644 --- a/crates/ide_db/src/helpers/merge_imports.rs +++ b/crates/ide_db/src/helpers/merge_imports.rs @@ -299,7 +299,7 @@ pub fn eq_visibility(vis0: Option, vis1: Option, attrs1: impl Iterator, ) -> bool { -- cgit v1.2.3