]> git.lizzy.rs Git - rust.git/blobdiff - crates/ide_completion/src/completions/mod_.rs
Merge #11393
[rust.git] / crates / ide_completion / src / completions / mod_.rs
index 4f941573664593b77987e2ba80f86f91185a13ab..64e992c2e6be6c651b104da4d5377029b9a43b11 100644 (file)
@@ -9,20 +9,20 @@
 };
 use rustc_hash::FxHashSet;
 
-use crate::CompletionItem;
+use crate::{patterns::ImmediateLocation, CompletionItem};
 
-use crate::{context::CompletionContext, item::CompletionKind, Completions};
+use crate::{context::CompletionContext, Completions};
 
 /// Complete mod declaration, i.e. `mod $0 ;`
 pub(crate) fn complete_mod(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
-    let mod_under_caret = match &ctx.mod_declaration_under_caret {
-        Some(mod_under_caret) if mod_under_caret.item_list().is_none() => mod_under_caret,
+    let mod_under_caret = match &ctx.completion_location {
+        Some(ImmediateLocation::ModDeclaration(mod_under_caret)) => mod_under_caret,
         _ => return None,
     };
 
     let _p = profile::span("completion::complete_mod");
 
-    let current_module = ctx.scope.module()?;
+    let current_module = ctx.module?;
 
     let module_definition_file =
         current_module.definition_source(ctx.db).file_id.original_file(ctx.db);
@@ -80,8 +80,7 @@ pub(crate) fn complete_mod(acc: &mut Completions, ctx: &CompletionContext) -> Op
             if mod_under_caret.semicolon_token().is_none() {
                 label.push(';');
             }
-            let mut item = CompletionItem::new(CompletionKind::Magic, ctx.source_range(), &label);
-            item.kind(SymbolKind::Module);
+            let item = CompletionItem::new(SymbolKind::Module, ctx.source_range(), &label);
             item.add_to(acc)
         });
 
@@ -122,7 +121,7 @@ fn directory_to_look_for_submodules(
     module_chain_to_containing_module_file(module, db)
         .into_iter()
         .filter_map(|module| module.name(db))
-        .try_fold(base_directory, |path, name| path.join(&name.to_string()))
+        .try_fold(base_directory, |path, name| path.join(&name.to_smol_str()))
 }
 
 fn module_chain_to_containing_module_file(
@@ -141,11 +140,12 @@ fn module_chain_to_containing_module_file(
 
 #[cfg(test)]
 mod tests {
-    use crate::{test_utils::completion_list, CompletionKind};
     use expect_test::{expect, Expect};
 
+    use crate::tests::completion_list;
+
     fn check(ra_fixture: &str, expect: Expect) {
-        let actual = completion_list(ra_fixture, CompletionKind::Magic);
+        let actual = completion_list(ra_fixture);
         expect.assert_eq(&actual);
     }
 
@@ -153,17 +153,17 @@ fn check(ra_fixture: &str, expect: Expect) {
     fn lib_module_completion() {
         check(
             r#"
-            //- /lib.rs
-            mod $0
-            //- /foo.rs
-            fn foo() {}
-            //- /foo/ignored_foo.rs
-            fn ignored_foo() {}
-            //- /bar/mod.rs
-            fn bar() {}
-            //- /bar/ignored_bar.rs
-            fn ignored_bar() {}
-        "#,
+//- /lib.rs
+mod $0
+//- /foo.rs
+fn foo() {}
+//- /foo/ignored_foo.rs
+fn ignored_foo() {}
+//- /bar/mod.rs
+fn bar() {}
+//- /bar/ignored_bar.rs
+fn ignored_bar() {}
+"#,
             expect![[r#"
                 md foo;
                 md bar;
@@ -175,13 +175,13 @@ fn ignored_bar() {}
     fn no_module_completion_with_module_body() {
         check(
             r#"
-            //- /lib.rs
-            mod $0 {
+//- /lib.rs
+mod $0 {
 
-            }
-            //- /foo.rs
-            fn foo() {}
-        "#,
+}
+//- /foo.rs
+fn foo() {}
+"#,
             expect![[r#""#]],
         );
     }
@@ -190,17 +190,17 @@ fn foo() {}
     fn main_module_completion() {
         check(
             r#"
-            //- /main.rs
-            mod $0
-            //- /foo.rs
-            fn foo() {}
-            //- /foo/ignored_foo.rs
-            fn ignored_foo() {}
-            //- /bar/mod.rs
-            fn bar() {}
-            //- /bar/ignored_bar.rs
-            fn ignored_bar() {}
-        "#,
+//- /main.rs
+mod $0
+//- /foo.rs
+fn foo() {}
+//- /foo/ignored_foo.rs
+fn ignored_foo() {}
+//- /bar/mod.rs
+fn bar() {}
+//- /bar/ignored_bar.rs
+fn ignored_bar() {}
+"#,
             expect![[r#"
                 md foo;
                 md bar;
@@ -212,13 +212,13 @@ fn ignored_bar() {}
     fn main_test_module_completion() {
         check(
             r#"
-            //- /main.rs
-            mod tests {
-                mod $0;
-            }
-            //- /tests/foo.rs
-            fn foo() {}
-        "#,
+//- /main.rs
+mod tests {
+    mod $0;
+}
+//- /tests/foo.rs
+fn foo() {}
+"#,
             expect![[r#"
                 md foo
             "#]],
@@ -229,19 +229,19 @@ fn foo() {}
     fn directly_nested_module_completion() {
         check(
             r#"
-            //- /lib.rs
-            mod foo;
-            //- /foo.rs
-            mod $0;
-            //- /foo/bar.rs
-            fn bar() {}
-            //- /foo/bar/ignored_bar.rs
-            fn ignored_bar() {}
-            //- /foo/baz/mod.rs
-            fn baz() {}
-            //- /foo/moar/ignored_moar.rs
-            fn ignored_moar() {}
-        "#,
+//- /lib.rs
+mod foo;
+//- /foo.rs
+mod $0;
+//- /foo/bar.rs
+fn bar() {}
+//- /foo/bar/ignored_bar.rs
+fn ignored_bar() {}
+//- /foo/baz/mod.rs
+fn baz() {}
+//- /foo/moar/ignored_moar.rs
+fn ignored_moar() {}
+"#,
             expect![[r#"
                 md bar
                 md baz
@@ -253,15 +253,15 @@ fn ignored_moar() {}
     fn nested_in_source_module_completion() {
         check(
             r#"
-            //- /lib.rs
-            mod foo;
-            //- /foo.rs
-            mod bar {
-                mod $0
-            }
-            //- /foo/bar/baz.rs
-            fn baz() {}
-        "#,
+//- /lib.rs
+mod foo;
+//- /foo.rs
+mod bar {
+    mod $0
+}
+//- /foo/bar/baz.rs
+fn baz() {}
+"#,
             expect![[r#"
                 md baz;
             "#]],
@@ -299,16 +299,16 @@ fn baz() {}
     fn already_declared_bin_module_completion_omitted() {
         check(
             r#"
-            //- /src/bin.rs crate:main
-            fn main() {}
-            //- /src/bin/foo.rs
-            mod $0
-            //- /src/bin/bar.rs
-            mod foo;
-            fn bar() {}
-            //- /src/bin/bar/bar_ignored.rs
-            fn bar_ignored() {}
-        "#,
+//- /src/bin.rs crate:main
+fn main() {}
+//- /src/bin/foo.rs
+mod $0
+//- /src/bin/bar.rs
+mod foo;
+fn bar() {}
+//- /src/bin/bar/bar_ignored.rs
+fn bar_ignored() {}
+"#,
             expect![[r#""#]],
         );
     }