about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2022-04-28 18:23:26 +0000
committerbors <bors@rust-lang.org>2022-04-28 18:23:26 +0000
commit7abf8cc21cd23ec6f080958be57ede911df16ec9 (patch)
treec6e0a6f000cf9653c0e308166aa81153fed6a9c5
parent339a1867f45b6bbfa0f5f5f274706b1b81b71663 (diff)
parent52010d7dc7ff2753e96f8cac3da0fd1c6098e4be (diff)
downloadrust-7abf8cc21cd23ec6f080958be57ede911df16ec9.tar.gz
rust-7abf8cc21cd23ec6f080958be57ede911df16ec9.zip
Auto merge of #12111 - jonas-schievink:fix-outline-mod-completion, r=jonas-schievink
fix: fix outline mod completion with partial module name

Fixes https://github.com/rust-lang/rust-analyzer/issues/12104

![screenshot-2022-04-28-20:22:42](https://user-images.githubusercontent.com/1786438/165821068-a673a154-ce53-4489-af60-56d09dc9061c.png)
-rw-r--r--crates/ide_completion/src/completions/mod_.rs39
1 files changed, 38 insertions, 1 deletions
diff --git a/crates/ide_completion/src/completions/mod_.rs b/crates/ide_completion/src/completions/mod_.rs
index 5551934da93..cf7566bcc3c 100644
--- a/crates/ide_completion/src/completions/mod_.rs
+++ b/crates/ide_completion/src/completions/mod_.rs
@@ -8,6 +8,7 @@ use ide_db::{
     base_db::{SourceDatabaseExt, VfsPath},
     RootDatabase, SymbolKind,
 };
+use syntax::{ast, AstNode, SyntaxKind};
 
 use crate::{context::NameContext, CompletionItem};
 
@@ -24,7 +25,21 @@ pub(crate) fn complete_mod(acc: &mut Completions, ctx: &CompletionContext) -> Op
 
     let _p = profile::span("completion::complete_mod");
 
-    let current_module = ctx.module;
+    let mut current_module = ctx.module;
+    // For `mod $0`, `ctx.module` is its parent, but for `mod f$0`, it's `mod f` itself, but we're
+    // interested in its parent.
+    if ctx.original_token.kind() == SyntaxKind::IDENT {
+        if let Some(module) = ctx.original_token.ancestors().nth(1).and_then(ast::Module::cast) {
+            match current_module.definition_source(ctx.db).value {
+                ModuleSource::Module(src) if src == module => {
+                    if let Some(parent) = current_module.parent(ctx.db) {
+                        current_module = parent;
+                    }
+                }
+                _ => {}
+            }
+        }
+    }
 
     let module_definition_file =
         current_module.definition_source(ctx.db).file_id.original_file(ctx.db);
@@ -314,4 +329,26 @@ fn bar_ignored() {}
             expect![[r#""#]],
         );
     }
+
+    #[test]
+    fn name_partially_typed() {
+        check(
+            r#"
+//- /lib.rs
+mod f$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;
+            "#]],
+        );
+    }
 }