diff options
author | Lukas Wirth <[email protected]> | 2021-06-08 21:14:30 +0100 |
---|---|---|
committer | Lukas Wirth <[email protected]> | 2021-06-08 21:14:30 +0100 |
commit | 31aad2528f53349e9a0601c756d3c9f86ca9154c (patch) | |
tree | d2250c8e6ccb80eb236e1731db9f4e5c80e48e54 | |
parent | 590472607c9629fdd37e3f6f33dacfdc2a3f56cc (diff) |
Fix edge case for ImportGranularity guessing
-rw-r--r-- | crates/ide_db/src/helpers/insert_use.rs | 18 | ||||
-rw-r--r-- | crates/ide_db/src/helpers/insert_use/tests.rs | 15 | ||||
-rw-r--r-- | crates/syntax/src/ast/node_ext.rs | 5 |
3 files changed, 30 insertions, 8 deletions
diff --git a/crates/ide_db/src/helpers/insert_use.rs b/crates/ide_db/src/helpers/insert_use.rs index aa61c5bcb..10bbafe77 100644 --- a/crates/ide_db/src/helpers/insert_use.rs +++ b/crates/ide_db/src/helpers/insert_use.rs | |||
@@ -120,15 +120,19 @@ impl ImportScope { | |||
120 | if eq_visibility(prev_vis, curr_vis.clone()) && eq_attrs(prev_attrs, curr_attrs.clone()) | 120 | if eq_visibility(prev_vis, curr_vis.clone()) && eq_attrs(prev_attrs, curr_attrs.clone()) |
121 | { | 121 | { |
122 | if let Some((prev_path, curr_path)) = prev.path().zip(curr.path()) { | 122 | if let Some((prev_path, curr_path)) = prev.path().zip(curr.path()) { |
123 | if let Some(_) = common_prefix(&prev_path, &curr_path) { | 123 | if let Some((prev_prefix, _)) = common_prefix(&prev_path, &curr_path) { |
124 | if prev.use_tree_list().is_none() && curr.use_tree_list().is_none() { | 124 | if prev.use_tree_list().is_none() && curr.use_tree_list().is_none() { |
125 | // Same prefix but no use tree lists so this has to be of item style. | 125 | let prefix_c = prev_prefix.qualifiers().count(); |
126 | break ImportGranularityGuess::Item; // this overwrites CrateOrModule, technically the file doesn't adhere to anything here. | 126 | let curr_c = curr_path.qualifiers().count() - prefix_c; |
127 | } else { | 127 | let prev_c = prev_path.qualifiers().count() - prefix_c; |
128 | // Same prefix with item tree lists, has to be module style as it | 128 | if curr_c <= 1 || prev_c <= 1 { |
129 | // can't be crate style since the trees wouldn't share a prefix then. | 129 | // Same prefix but no use tree lists so this has to be of item style. |
130 | break ImportGranularityGuess::Module; | 130 | break ImportGranularityGuess::Item; // this overwrites CrateOrModule, technically the file doesn't adhere to anything here. |
131 | } | ||
131 | } | 132 | } |
133 | // Same prefix with item tree lists, has to be module style as it | ||
134 | // can't be crate style since the trees wouldn't share a prefix then. | ||
135 | break ImportGranularityGuess::Module; | ||
132 | } | 136 | } |
133 | } | 137 | } |
134 | } | 138 | } |
diff --git a/crates/ide_db/src/helpers/insert_use/tests.rs b/crates/ide_db/src/helpers/insert_use/tests.rs index 78a2a87b3..70b11bf81 100644 --- a/crates/ide_db/src/helpers/insert_use/tests.rs +++ b/crates/ide_db/src/helpers/insert_use/tests.rs | |||
@@ -663,6 +663,13 @@ use foo::bar::qux; | |||
663 | ", | 663 | ", |
664 | ImportGranularityGuess::Item, | 664 | ImportGranularityGuess::Item, |
665 | ); | 665 | ); |
666 | check_guess( | ||
667 | r" | ||
668 | use foo::bar::Bar; | ||
669 | use foo::baz; | ||
670 | ", | ||
671 | ImportGranularityGuess::Item, | ||
672 | ); | ||
666 | } | 673 | } |
667 | 674 | ||
668 | #[test] | 675 | #[test] |
@@ -682,6 +689,14 @@ use foo::{baz::{qux, quux}, bar}; | |||
682 | ", | 689 | ", |
683 | ImportGranularityGuess::Module, | 690 | ImportGranularityGuess::Module, |
684 | ); | 691 | ); |
692 | check_guess( | ||
693 | r" | ||
694 | use foo::bar::Bar; | ||
695 | use foo::baz::Baz; | ||
696 | use foo::{Foo, Qux}; | ||
697 | ", | ||
698 | ImportGranularityGuess::Module, | ||
699 | ); | ||
685 | } | 700 | } |
686 | 701 | ||
687 | #[test] | 702 | #[test] |
diff --git a/crates/syntax/src/ast/node_ext.rs b/crates/syntax/src/ast/node_ext.rs index 884fe0739..a60bc5ad9 100644 --- a/crates/syntax/src/ast/node_ext.rs +++ b/crates/syntax/src/ast/node_ext.rs | |||
@@ -259,11 +259,14 @@ impl ast::Path { | |||
259 | } | 259 | } |
260 | 260 | ||
261 | pub fn segments(&self) -> impl Iterator<Item = ast::PathSegment> + Clone { | 261 | pub fn segments(&self) -> impl Iterator<Item = ast::PathSegment> + Clone { |
262 | // cant make use of SyntaxNode::siblings, because the returned Iterator is not clone | ||
263 | successors(self.first_segment(), |p| { | 262 | successors(self.first_segment(), |p| { |
264 | p.parent_path().parent_path().and_then(|p| p.segment()) | 263 | p.parent_path().parent_path().and_then(|p| p.segment()) |
265 | }) | 264 | }) |
266 | } | 265 | } |
266 | |||
267 | pub fn qualifiers(&self) -> impl Iterator<Item = ast::Path> + Clone { | ||
268 | successors(self.qualifier(), |p| p.qualifier()) | ||
269 | } | ||
267 | } | 270 | } |
268 | impl ast::UseTree { | 271 | impl ast::UseTree { |
269 | pub fn is_simple_path(&self) -> bool { | 272 | pub fn is_simple_path(&self) -> bool { |