Skip to content

Commit 9ff84e1

Browse files
committed
[CodeCompletion] For submodules, also use ASTContext to collect their visibility.
1 parent 9e28192 commit 9ff84e1

File tree

6 files changed

+30
-16
lines changed

6 files changed

+30
-16
lines changed

include/swift/ClangImporter/ClangImporter.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -255,12 +255,11 @@ class ClangImporter final : public ClangModuleLoader {
255255
/// Dump Swift lookup tables.
256256
void dumpSwiftLookupTables();
257257

258-
/// Given the path of a Clang module, collect the names of all its submodules
259-
/// and their corresponding visibility. Calling this function does not load the
260-
/// module.
261-
void collectSubModuleNamesAndVisibility(
258+
/// Given the path of a Clang module, collect the names of all its submodules.
259+
/// Calling this function does not load the module.
260+
void collectSubModuleNames(
262261
ArrayRef<std::pair<Identifier, SourceLoc>> path,
263-
std::vector<std::pair<std::string, bool>> &namesVisiblePairs);
262+
std::vector<std::string> &names);
264263

265264
/// Given a Clang module, decide whether this module is imported already.
266265
static bool isModuleImported(const clang::Module *M);

include/swift/Parse/CodeCompletionCallbacks.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ class CodeCompletionCallbacks {
176176
virtual void completePoundAvailablePlatform() = 0;
177177

178178
/// Complete the import decl with importable modules.
179-
virtual void completeImportDecl(ArrayRef<std::pair<Identifier, SourceLoc>> Path) = 0;
179+
virtual void completeImportDecl(std::vector<std::pair<Identifier, SourceLoc>> &Path) = 0;
180180

181181
/// Complete unresolved members after dot.
182182
virtual void completeUnresolvedMember(UnresolvedMemberExpr *E,

lib/ClangImporter/ClangImporter.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -943,9 +943,9 @@ std::string ClangImporter::getBridgingHeaderContents(StringRef headerPath,
943943
return result;
944944
}
945945

946-
void ClangImporter::collectSubModuleNamesAndVisibility(
946+
void ClangImporter::collectSubModuleNames(
947947
ArrayRef<std::pair<Identifier, SourceLoc>> path,
948-
std::vector<std::pair<std::string, bool>> &namesVisiblePairs) {
948+
std::vector<std::string> &names) {
949949
auto &clangHeaderSearch = Impl.getClangPreprocessor().getHeaderSearchInfo();
950950

951951
// Look up the top-level module first.
@@ -962,9 +962,7 @@ void ClangImporter::collectSubModuleNamesAndVisibility(
962962
auto submoduleNameLength = submodule->getFullModuleName().length();
963963
for (auto sub : submodule->submodules()) {
964964
StringRef full = sub->getFullModuleName();
965-
namesVisiblePairs.push_back(
966-
std::make_pair(full.substr(submoduleNameLength + 1).str(),
967-
isModuleImported(sub)));
965+
names.push_back(full.substr(submoduleNameLength + 1).str());
968966
}
969967
}
970968

lib/IDE/CodeCompletion.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,7 +1233,7 @@ class CodeCompletionCallbacksImpl : public CodeCompletionCallbacks {
12331233
SmallVectorImpl<StringRef> &Keywords) override;
12341234

12351235
void completePoundAvailablePlatform() override;
1236-
void completeImportDecl(ArrayRef<std::pair<Identifier, SourceLoc>> Path) override;
1236+
void completeImportDecl(std::vector<std::pair<Identifier, SourceLoc>> &Path) override;
12371237
void completeUnresolvedMember(UnresolvedMemberExpr *E,
12381238
ArrayRef<StringRef> Identifiers,
12391239
bool HasReturn) override;
@@ -4010,15 +4010,23 @@ void CodeCompletionCallbacksImpl::completeCaseStmtDotPrefix() {
40104010
}
40114011

40124012
void CodeCompletionCallbacksImpl::completeImportDecl(
4013-
ArrayRef<std::pair<Identifier, SourceLoc>> Path) {
4013+
std::vector<std::pair<Identifier, SourceLoc>> &Path) {
40144014
Kind = CompletionKind::Import;
40154015
CurDeclContext = P.CurDeclContext;
40164016
DotLoc = Path.empty() ? SourceLoc() : Path.back().second;
40174017
if (DotLoc.isInvalid())
40184018
return;
40194019
auto Importer = static_cast<ClangImporter *>(CurDeclContext->getASTContext().
40204020
getClangModuleLoader());
4021-
Importer->collectSubModuleNamesAndVisibility(Path, SubModuleNameVisibilityPairs);
4021+
std::vector<std::string> SubNames;
4022+
Importer->collectSubModuleNames(Path, SubNames);
4023+
ASTContext &Ctx = CurDeclContext->getASTContext();
4024+
for (StringRef Sub : SubNames) {
4025+
Path.push_back(std::make_pair(Ctx.getIdentifier(Sub), SourceLoc()));
4026+
SubModuleNameVisibilityPairs.push_back(
4027+
std::make_pair(Sub.str(), Ctx.getLoadedModule(Path)));
4028+
Path.pop_back();
4029+
}
40224030
}
40234031

40244032
void CodeCompletionCallbacksImpl::completeUnresolvedMember(UnresolvedMemberExpr *E,

lib/Parse/ParseDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2258,7 +2258,7 @@ ParserResult<ImportDecl> Parser::parseDeclImport(ParseDeclOptions Flags,
22582258
KindLoc = consumeToken();
22592259
}
22602260

2261-
SmallVector<std::pair<Identifier, SourceLoc>, 8> ImportPath;
2261+
std::vector<std::pair<Identifier, SourceLoc>> ImportPath;
22622262
do {
22632263
if (Tok.is(tok::code_complete)) {
22642264
consumeToken();

test/IDE/complete_import.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// RUN: %target-swift-ide-test -code-completion -source-filename %s -F %S/Inputs/mock-sdk -code-completion-token=CLANG_IMPORT5 | FileCheck %s -check-prefix=CLANG_IMPORT5
66
// RUN: %target-swift-ide-test -code-completion -source-filename %s -F %S/Inputs/mock-sdk -code-completion-token=CLANG_IMPORT6 | FileCheck %s -check-prefix=CLANG_IMPORT6
77
// RUN: %target-swift-ide-test -code-completion -source-filename %s -F %S/Inputs/mock-sdk -code-completion-token=CLANG_IMPORT7 | FileCheck %s -check-prefix=CLANG_IMPORT7
8+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -F %S/Inputs/mock-sdk -code-completion-token=CLANG_IMPORT8 | FileCheck %s -check-prefix=CLANG_IMPORT8
89

910
// REQUIRES: objc_interop
1011

@@ -29,7 +30,15 @@ import #^CLANG_IMPORT2^#
2930
import Foo.#^CLANG_IMPORT3^#
3031

3132
// CLANG_IMPORT3: Begin completions
32-
// CLANG_IMPORT3-NEXT: Decl[Module]/OtherModule[FooSub]/NotRecommended: FooSub[#Module#]; name=FooSub
33+
// CLANG_IMPORT3-NEXT: Decl[Module]/OtherModule[FooSub]: FooSub[#Module#]; name=FooSub
34+
35+
import Foo.FooSub
36+
37+
import Foo.#^CLANG_IMPORT8^#
38+
39+
// FIXME: This should be marked as not recommended, holding for Swift's submodules support.
40+
// CLANG_IMPORT8: Begin completions
41+
// CLANG_IMPORT8-NEXT: Decl[Module]/OtherModule[FooSub]: FooSub[#Module#]; name=FooSub
3342

3443
import Foo#^CLANG_IMPORT4^#
3544
// CLANG_IMPORT4-NOT: Begin completions

0 commit comments

Comments
 (0)