Skip to content

Commit f1a40f9

Browse files
bors[bot]Veykril
andauthored
Merge #9187
9187: fix: Fix edge case for ImportGranularity guessing r=Veykril a=Veykril bors r+ Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2 parents b6199de + 31aad25 commit f1a40f9

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

crates/ide_db/src/helpers/insert_use.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,19 @@ impl ImportScope {
120120
if eq_visibility(prev_vis, curr_vis.clone()) && eq_attrs(prev_attrs, curr_attrs.clone())
121121
{
122122
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) {
124124
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.
126-
break ImportGranularityGuess::Item; // this overwrites CrateOrModule, technically the file doesn't adhere to anything here.
127-
} else {
128-
// Same prefix with item tree lists, has to be module style as it
129-
// can't be crate style since the trees wouldn't share a prefix then.
130-
break ImportGranularityGuess::Module;
125+
let prefix_c = prev_prefix.qualifiers().count();
126+
let curr_c = curr_path.qualifiers().count() - prefix_c;
127+
let prev_c = prev_path.qualifiers().count() - prefix_c;
128+
if curr_c <= 1 || prev_c <= 1 {
129+
// Same prefix but no use tree lists so this has to be of item style.
130+
break ImportGranularityGuess::Item; // this overwrites CrateOrModule, technically the file doesn't adhere to anything here.
131+
}
131132
}
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;
132136
}
133137
}
134138
}

crates/ide_db/src/helpers/insert_use/tests.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,13 @@ fn guess_item() {
660660
r"
661661
use foo::bar::baz;
662662
use foo::bar::qux;
663+
",
664+
ImportGranularityGuess::Item,
665+
);
666+
check_guess(
667+
r"
668+
use foo::bar::Bar;
669+
use foo::baz;
663670
",
664671
ImportGranularityGuess::Item,
665672
);
@@ -679,6 +686,14 @@ use foo::bar::{qux, quux};
679686
r"
680687
use foo::bar::baz;
681688
use foo::{baz::{qux, quux}, bar};
689+
",
690+
ImportGranularityGuess::Module,
691+
);
692+
check_guess(
693+
r"
694+
use foo::bar::Bar;
695+
use foo::baz::Baz;
696+
use foo::{Foo, Qux};
682697
",
683698
ImportGranularityGuess::Module,
684699
);

crates/syntax/src/ast/node_ext.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,11 +259,14 @@ impl ast::Path {
259259
}
260260

261261
pub fn segments(&self) -> impl Iterator<Item = ast::PathSegment> + Clone {
262-
// cant make use of SyntaxNode::siblings, because the returned Iterator is not clone
263262
successors(self.first_segment(), |p| {
264263
p.parent_path().parent_path().and_then(|p| p.segment())
265264
})
266265
}
266+
267+
pub fn qualifiers(&self) -> impl Iterator<Item = ast::Path> + Clone {
268+
successors(self.qualifier(), |p| p.qualifier())
269+
}
267270
}
268271
impl ast::UseTree {
269272
pub fn is_simple_path(&self) -> bool {

0 commit comments

Comments
 (0)