Skip to content

Commit 7350917

Browse files
author
Nathan Hawes
committed
[code-completion] Complete after qualifying module names in type position
Previously we would just return if the parsed type loc didn't type check as a type.
1 parent d42d2e1 commit 7350917

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

lib/IDE/CodeCompletion.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5116,8 +5116,20 @@ void CodeCompletionCallbacksImpl::doneParsing() {
51165116
return;
51175117
}
51185118

5119-
if (!ParsedTypeLoc.isNull() && !typecheckParsedType())
5120-
return;
5119+
if (!ParsedTypeLoc.isNull() && !typecheckParsedType()) {
5120+
// If we think we parsed a type but it doesn't type check as a type, see if
5121+
// it's a qualifying module name and recover if so.
5122+
if (auto *ITR = dyn_cast<IdentTypeRepr>(ParsedTypeLoc.getTypeRepr())) {
5123+
SmallVector<ImportDecl::AccessPathElement, 4> AccessPath;
5124+
for (auto Component : ITR->getComponentRange())
5125+
AccessPath.push_back({ Component->getIdentifier(),
5126+
Component->getIdLoc() });
5127+
if (auto Module = Context.getLoadedModule(AccessPath))
5128+
ParsedTypeLoc.setType(ModuleType::get(Module));
5129+
}
5130+
if (ParsedTypeLoc.isNull())
5131+
return;
5132+
}
51215133

51225134
CompletionLookup Lookup(CompletionContext.getResultSink(), P.Context,
51235135
CurDeclContext, &CompletionContext);

test/IDE/complete_from_swift_module.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@
2121

2222
// RUN: %target-swift-ide-test -code-completion -source-filename %s -I %t -code-completion-token=MODULE_QUALIFIED_5 | %FileCheck %s -check-prefix=ERROR_COMMON
2323

24+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -I %t -code-completion-token=STDLIB_TYPE_QUALIFIED > %t.compl.txt
25+
// RUN: %FileCheck %s -check-prefix=STDLIB_TYPE_QUALIFIED < %t.compl.txt
26+
27+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -I %t -code-completion-token=MODULE_TYPE_QUALIFIED > %t.compl.txt
28+
// RUN: %FileCheck %s -check-prefix=MODULE_TYPE_QUALIFIED < %t.compl.txt
29+
2430
// RUN: %target-swift-ide-test -code-completion -source-filename %s -I %t -code-completion-token=POSTFIX_OPERATOR_1 > %t.compl.txt
2531
// RUN: %FileCheck %s -check-prefix=POSTFIX_OPERATOR_1 < %t.compl.txt
2632
// RUN: %FileCheck %s -check-prefix=NEGATIVE_POSTFIX_OPERATOR_1 < %t.compl.txt
@@ -99,3 +105,20 @@ func testPostfixOperator1(x: Int) {
99105
// TOP_LEVEL_1-DAG: Decl[GlobalVar]/Local: hiddenImport[#Int#]{{; name=.+$}}
100106
// TOP_LEVEL_1-DAG: Decl[GlobalVar]/OtherModule[foo_swift_module]: globalVar[#Int#]{{; name=.+$}}
101107
// TOP_LEVEL_1: End completions
108+
109+
struct Foo: Swift.Array.#^STDLIB_TYPE_QUALIFIED^# {}
110+
// STDLIB_TYPE_QUALIFIED: Begin completions
111+
// STDLIB_TYPE_QUALIFIED: Decl[TypeAlias]/CurrNominal: Index[#Int#]; name=Index
112+
// STDLIB_TYPE_QUALIFIED: Decl[TypeAlias]/CurrNominal: Element[#Element#]; name=Element
113+
// STDLIB_TYPE_QUALIFIED: Keyword/None: Type[#Array.Type#]; name=Type
114+
// STDLIB_TYPE_QUALIFIED: End completions
115+
116+
func foo() -> foo_swift_module.#^MODULE_TYPE_QUALIFIED^# {}
117+
// MODULE_TYPE_QUALIFIED: Begin completions
118+
// MODULE_TYPE_QUALIFIED: Decl[Protocol]/OtherModule[foo_swift_module]: BarProtocol[#BarProtocol#]; name=BarProtocol
119+
// MODULE_TYPE_QUALIFIED: Decl[Enum]/OtherModule[foo_swift_module]: MyQuickLookObject[#MyQuickLookObject#]; name=MyQuickLookObject
120+
// MODULE_TYPE_QUALIFIED: Decl[Struct]/OtherModule[foo_swift_module]: BarGenericSwiftStruct1[#BarGenericSwiftStruct1#]; name=BarGenericSwiftStruct1
121+
// MODULE_TYPE_QUALIFIED: Decl[Struct]/OtherModule[foo_swift_module]: FooSwiftStruct[#FooSwiftStruct#]; name=FooSwiftStruct
122+
// MODULE_TYPE_QUALIFIED: Decl[Struct]/OtherModule[foo_swift_module]: BarGenericSwiftStruct2[#BarGenericSwiftStruct2#]; name=BarGenericSwiftStruct2
123+
// MODULE_TYPE_QUALIFIED: Keyword/None: Type[#module<foo_swift_module>.Type#]; name=Type
124+
// MODULE_TYPE_QUALIFIED: End completions

0 commit comments

Comments
 (0)