Skip to content

Commit 858ecb8

Browse files
committed
[CodeCompletion] Don't suggest underscored module name
Underscored modules (e.g. '_Concurrency') are usually not meant be written explicitly in source code. They are usually implicitly imported with other modules. Code completion should not suggest them in type and expression context. rdar://78232229
1 parent b46f7e2 commit 858ecb8

File tree

2 files changed

+63
-5
lines changed

2 files changed

+63
-5
lines changed

lib/IDE/CodeCompletion.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2078,6 +2078,12 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
20782078
IncludeInstanceMembers = true;
20792079
}
20802080

2081+
bool isHiddenModuleName(Identifier Name) {
2082+
return (Name.str().startswith("_") ||
2083+
Name == Ctx.SwiftShimsModuleName ||
2084+
Name.str() == SWIFT_ONONE_SUPPORT);
2085+
}
2086+
20812087
void addSubModuleNames(std::vector<std::pair<std::string, bool>>
20822088
&SubModuleNameVisibilityPairs) {
20832089
for (auto &Pair : SubModuleNameVisibilityPairs) {
@@ -2142,10 +2148,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
21422148

21432149
auto mainModuleName = CurrModule->getName();
21442150
for (auto ModuleName : ModuleNames) {
2145-
if (ModuleName.str().startswith("_") ||
2146-
ModuleName == mainModuleName ||
2147-
ModuleName == Ctx.SwiftShimsModuleName ||
2148-
ModuleName.str() == SWIFT_ONONE_SUPPORT)
2151+
if (ModuleName == mainModuleName || isHiddenModuleName(ModuleName))
21492152
continue;
21502153

21512154
auto MD = ModuleDecl::create(ModuleName, Ctx);
@@ -6257,8 +6260,10 @@ static void deliverCompletionResults(CodeCompletionContext &CompletionContext,
62576260
RequestedModules.push_back({std::move(K), TheModule,
62586261
Request.OnlyTypes, Request.OnlyPrecedenceGroups});
62596262

6263+
auto TheModuleName = TheModule->getName();
62606264
if (Request.IncludeModuleQualifier &&
6261-
seenModuleNames.insert(TheModule->getName()).second)
6265+
!Lookup.isHiddenModuleName(TheModuleName) &&
6266+
seenModuleNames.insert(TheModuleName).second)
62626267
Lookup.addModuleName(TheModule);
62636268
}
62646269
};

test/IDE/complete_modulename.swift

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// BEGIN MyModule.swift
2+
public struct MyModuleTy {}
3+
public func myModuleFunc() {}
4+
5+
6+
// BEGIN _Hidden.swift
7+
public struct HiddenTy {}
8+
public func hiddenFunc() {}
9+
10+
// BEGIN App.swift
11+
12+
import MyModule
13+
import _Hidden
14+
15+
func test() {
16+
let _ = #^EXPR^#
17+
18+
func test() -> #^TYPE^#
19+
}
20+
21+
// EXPR: Begin completion
22+
// EXPR-NOT: _Concurrency[#Module#]
23+
// EXPR-NOT: SwiftShims[#Module#]
24+
// EXPR-NOT: SwiftOnoneSupport[#Module#]
25+
// EXPR-DAG: Decl[Module]/None: swift_ide_test[#Module#]; name=swift_ide_test
26+
// EXPR-DAG: Decl[Module]/None/IsSystem: Swift[#Module#]; name=Swift
27+
// EXPR-DAG: Decl[Module]/None: MyModule[#Module#]; name=MyModule
28+
// EXPR-DAG: Decl[Struct]/OtherModule[MyModule]: MyModuleTy[#MyModuleTy#]; name=MyModuleTy
29+
// EXPR-DAG: Decl[Struct]/OtherModule[_Hidden]: HiddenTy[#HiddenTy#]; name=HiddenTy
30+
// EXPR-DAG: Decl[FreeFunction]/OtherModule[MyModule]: myModuleFunc()[#Void#]; name=myModuleFunc()
31+
// EXPR-DAG: Decl[FreeFunction]/OtherModule[_Hidden]: hiddenFunc()[#Void#]; name=hiddenFunc()
32+
// EXPR: End completions
33+
34+
// TYPE: Begin completion
35+
// TYPE-NOT: _Concurrency[#Module#]
36+
// TYPE-NOT: SwiftShims[#Module#]
37+
// TYPE-NOT: SwiftOnoneSupport[#Module#]
38+
// TYPE-DAG: Decl[Module]/None: swift_ide_test[#Module#]; name=swift_ide_test
39+
// TYPE-DAG: Decl[Module]/None/IsSystem: Swift[#Module#]; name=Swift
40+
// TYPE-DAG: Decl[Module]/None: MyModule[#Module#]; name=MyModule
41+
// TYPE-DAG: Decl[Struct]/OtherModule[MyModule]: MyModuleTy[#MyModuleTy#]; name=MyModuleTy
42+
// TYPE-DAG: Decl[Struct]/OtherModule[_Hidden]: HiddenTy[#HiddenTy#]; name=HiddenTy
43+
// TYPE: End completions
44+
45+
// RUN: %empty-directory(%t)
46+
// RUN: %{python} %utils/split_file.py -o %t %s
47+
48+
// RUN: %empty-directory(%t/Modules)
49+
// RUN: %target-swift-frontend -emit-module -module-name MyModule -o %t/Modules %t/MyModule.swift
50+
// RUN: %target-swift-frontend -emit-module -module-name _Hidden -o %t/Modules %t/_Hidden.swift
51+
52+
// RUN: %empty-directory(%t/Out)
53+
// RUN: %target-swift-ide-test -batch-code-completion -source-filename %t/App.swift -filecheck %raw-FileCheck -completion-output-dir %t/Out -I %t/Modules

0 commit comments

Comments
 (0)