]> git.lizzy.rs Git - rust.git/commitdiff
Add ModuleOrItem guess to import granularity guessing
authorLukas Wirth <lukastw97@gmail.com>
Thu, 1 Jul 2021 19:32:35 +0000 (21:32 +0200)
committerLukas Wirth <lukastw97@gmail.com>
Thu, 1 Jul 2021 19:32:35 +0000 (21:32 +0200)
crates/ide_db/src/helpers/insert_use.rs
crates/ide_db/src/helpers/insert_use/tests.rs

index e6b4832e7db59eee41bbfdf75c4b3e742933e803..226b9f8e9cbe2c0e233b137f0c68d3a9091486f2 100644 (file)
@@ -145,14 +145,18 @@ fn guess_granularity_from_scope(&self) -> ImportGranularityGuess {
                             let prefix_c = prev_prefix.qualifiers().count();
                             let curr_c = curr_path.qualifiers().count() - prefix_c;
                             let prev_c = prev_path.qualifiers().count() - prefix_c;
-                            if curr_c <= 1 || prev_c <= 1 {
-                                // Same prefix but no use tree lists so this has to be of item style.
-                                break ImportGranularityGuess::Item; // this overwrites CrateOrModule, technically the file doesn't adhere to anything here.
+                            if curr_c == 1 && prev_c == 1 {
+                                // Same prefix, only differing in the last segment and no use tree lists so this has to be of item style.
+                                break ImportGranularityGuess::Item;
+                            } else {
+                                // Same prefix and no use tree list but differs in more than one segment at the end. This might be module style still.
+                                res = ImportGranularityGuess::ModuleOrItem;
                             }
+                        } else {
+                            // Same prefix with item tree lists, has to be module style as it
+                            // can't be crate style since the trees wouldn't share a prefix then.
+                            break ImportGranularityGuess::Module;
                         }
-                        // Same prefix with item tree lists, has to be module style as it
-                        // can't be crate style since the trees wouldn't share a prefix then.
-                        break ImportGranularityGuess::Module;
                     }
                 }
             }
@@ -168,6 +172,7 @@ enum ImportGranularityGuess {
     Unknown,
     Item,
     Module,
+    ModuleOrItem,
     Crate,
     CrateOrModule,
 }
@@ -186,6 +191,7 @@ pub fn insert_use<'a>(scope: &ImportScope, path: ast::Path, cfg: &InsertUseConfi
             ImportGranularityGuess::Unknown => mb,
             ImportGranularityGuess::Item => None,
             ImportGranularityGuess::Module => Some(MergeBehavior::Module),
+            ImportGranularityGuess::ModuleOrItem => mb.and(Some(MergeBehavior::Module)),
             ImportGranularityGuess::Crate => Some(MergeBehavior::Crate),
             ImportGranularityGuess::CrateOrModule => mb.or(Some(MergeBehavior::Crate)),
         };
index 01894630a8f321d337b750a6ec3f19188ce61b8d..2e0153a180d5c4688c253910fddc54a90a0418e6 100644 (file)
@@ -743,12 +743,23 @@ fn guess_item() {
 ",
         ImportGranularityGuess::Item,
     );
+}
+
+#[test]
+fn guess_module_or_item() {
     check_guess(
         r"
 use foo::bar::Bar;
-use foo::baz;
+use foo::qux;
 ",
-        ImportGranularityGuess::Item,
+        ImportGranularityGuess::ModuleOrItem,
+    );
+    check_guess(
+        r"
+use foo::bar::Bar;
+use foo::bar;
+",
+        ImportGranularityGuess::ModuleOrItem,
     );
 }