Skip to content

Commit 6956089

Browse files
committed
[CodeCompletion] Complete Swift only module name after 'import'
rdar://problem/39392446
1 parent c7052b0 commit 6956089

File tree

14 files changed

+237
-26
lines changed

14 files changed

+237
-26
lines changed

include/swift/AST/ASTContext.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,10 @@ class ASTContext final {
860860
/// not necessarily loaded.
861861
void getVisibleTopLevelClangModules(SmallVectorImpl<clang::Module*> &Modules) const;
862862

863+
/// Populate \p names with visible top level module names.
864+
/// This guarantees that resulted \p names doesn't have duplicated names.
865+
void getVisibleTopLevelModuleNames(SmallVectorImpl<Identifier> &names) const;
866+
863867
private:
864868
/// Register the given generic signature builder to be used as the canonical
865869
/// generic signature builder for the given signature, if we don't already

include/swift/AST/ModuleLoader.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ class ModuleLoader {
8282
public:
8383
virtual ~ModuleLoader() = default;
8484

85+
/// Collect visible module names.
86+
///
87+
/// Append visible module names to \p names. Note that names are possibly
88+
/// duplicated, and not guaranteed to be ordered in any way.
89+
virtual void collectVisibleTopLevelModuleNames(
90+
SmallVectorImpl<Identifier> &names) const = 0;
91+
8592
/// Check whether the module with a given name can be imported without
8693
/// importing it.
8794
///

include/swift/ClangImporter/ClangImporter.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ class ClangImporter final : public ClangModuleLoader {
119119
static std::shared_ptr<clang::DependencyCollector>
120120
createDependencyCollector(bool TrackSystemDeps);
121121

122+
/// Append visible module names to \p names. Note that names are possibly
123+
/// duplicated, and not guaranteed to be ordered in any way.
124+
void collectVisibleTopLevelModuleNames(
125+
SmallVectorImpl<Identifier> &names) const override;
126+
122127
/// Check whether the module with a given name can be imported without
123128
/// importing it.
124129
///
@@ -336,7 +341,7 @@ class ClangImporter final : public ClangModuleLoader {
336341
/// Calling this function does not load the module.
337342
void collectSubModuleNames(
338343
ArrayRef<std::pair<Identifier, SourceLoc>> path,
339-
std::vector<std::string> &names);
344+
std::vector<std::string> &names) const;
340345

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

include/swift/DWARFImporter/DWARFImporter.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ class DWARFImporter final : public ClangModuleLoader {
6666

6767
~DWARFImporter();
6868

69+
void collectVisibleTopLevelModuleNames(
70+
SmallVectorImpl<Identifier> &names) const override;
71+
6972
/// Check whether the module with a given name can be imported without
7073
/// importing it.
7174
///

include/swift/Frontend/ParseableInterfaceModuleLoader.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ class ParseableInterfaceModuleLoader : public SerializedModuleLoaderBase {
154154
RemarkOnRebuildFromInterface));
155155
}
156156

157+
/// Append visible module names to \p names. Note that names are possibly
158+
/// duplicated, and not guaranteed to be ordered in any way.
159+
void collectVisibleTopLevelModuleNames(
160+
SmallVectorImpl<Identifier> &names) const override;
161+
157162
/// Unconditionally build \p InPath (a swiftinterface file) to \p OutPath (as
158163
/// a swiftmodule file).
159164
///

include/swift/Sema/SourceLoader.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ class SourceLoader : public ModuleLoader {
4848
SourceLoader &operator=(const SourceLoader &) = delete;
4949
SourceLoader &operator=(SourceLoader &&) = delete;
5050

51+
/// Append visible module names to \p names. Note that names are possibly
52+
/// duplicated, and not guaranteed to be ordered in any way.
53+
void collectVisibleTopLevelModuleNames(
54+
SmallVectorImpl<Identifier> &names) const override;
55+
5156
/// Check whether the module with a given name can be imported without
5257
/// importing it.
5358
///

include/swift/Serialization/SerializedModuleLoader.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ class SerializedModuleLoaderBase : public ModuleLoader {
4444
SerializedModuleLoaderBase(ASTContext &ctx, DependencyTracker *tracker,
4545
ModuleLoadingMode LoadMode);
4646

47+
void collectVisibleTopLevelModuleNamesImpl(SmallVectorImpl<Identifier> &names,
48+
StringRef extension) const;
49+
4750
using AccessPathElem = std::pair<Identifier, SourceLoc>;
4851
bool findModule(AccessPathElem moduleID,
4952
std::unique_ptr<llvm::MemoryBuffer> *moduleBuffer,
@@ -169,6 +172,11 @@ class SerializedModuleLoader : public SerializedModuleLoaderBase {
169172
public:
170173
virtual ~SerializedModuleLoader();
171174

175+
/// Append visible module names to \p names. Note that names are possibly
176+
/// duplicated, and not guaranteed to be ordered in any way.
177+
void collectVisibleTopLevelModuleNames(
178+
SmallVectorImpl<Identifier> &names) const override;
179+
172180
/// Create a new importer that can load serialized Swift modules
173181
/// into the given ASTContext.
174182
static std::unique_ptr<SerializedModuleLoader>
@@ -220,6 +228,9 @@ class MemoryBufferSerializedModuleLoader : public SerializedModuleLoaderBase {
220228
MemoryBuffers[AccessPath] = std::move(input);
221229
}
222230

231+
void collectVisibleTopLevelModuleNames(
232+
SmallVectorImpl<Identifier> &names) const override {}
233+
223234
/// Create a new importer that can load serialized Swift modules
224235
/// into the given ASTContext.
225236
static std::unique_ptr<MemoryBufferSerializedModuleLoader>

lib/AST/ASTContext.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1670,6 +1670,19 @@ void ProtocolDecl::setDefaultAssociatedConformanceWitness(
16701670
(void)pair;
16711671
}
16721672

1673+
void ASTContext::getVisibleTopLevelModuleNames(
1674+
SmallVectorImpl<Identifier> &names) const {
1675+
names.clear();
1676+
for (auto &importer : getImpl().ModuleLoaders)
1677+
importer->collectVisibleTopLevelModuleNames(names);
1678+
1679+
// Sort and unique.
1680+
std::sort(names.begin(), names.end(), [](Identifier LHS, Identifier RHS) {
1681+
return LHS.str().compare_lower(RHS.str()) < 0;
1682+
});
1683+
names.erase(std::unique(names.begin(), names.end()), names.end());
1684+
}
1685+
16731686
bool ASTContext::canImportModule(std::pair<Identifier, SourceLoc> ModulePath) {
16741687
// If this module has already been successfully imported, it is importable.
16751688
if (getLoadedModule(ModulePath) != nullptr)

lib/ClangImporter/ClangImporter.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1494,9 +1494,22 @@ ClangImporter::emitBridgingPCH(StringRef headerPath,
14941494
return false;
14951495
}
14961496

1497+
void ClangImporter::collectVisibleTopLevelModuleNames(
1498+
SmallVectorImpl<Identifier> &names) const {
1499+
SmallVector<clang::Module *, 32> Modules;
1500+
Impl.getClangPreprocessor().getHeaderSearchInfo().collectAllModules(Modules);
1501+
for (auto &M : Modules) {
1502+
if (!M->isAvailable())
1503+
continue;
1504+
1505+
names.push_back(
1506+
Impl.SwiftContext.getIdentifier(M->getTopLevelModuleName()));
1507+
}
1508+
}
1509+
14971510
void ClangImporter::collectSubModuleNames(
14981511
ArrayRef<std::pair<Identifier, SourceLoc>> path,
1499-
std::vector<std::string> &names) {
1512+
std::vector<std::string> &names) const {
15001513
auto &clangHeaderSearch = Impl.getClangPreprocessor().getHeaderSearchInfo();
15011514

15021515
// Look up the top-level module first.

lib/DWARFImporter/DWARFImporter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ DWARFImporter::DWARFImporter(ASTContext &ctx,
142142

143143
DWARFImporter::~DWARFImporter() { delete &Impl; }
144144

145+
void DWARFImporter::collectVisibleTopLevelModuleNames(
146+
SmallVectorImpl<Identifier> &names) const {}
147+
145148
bool DWARFImporter::canImportModule(std::pair<Identifier, SourceLoc> named) {
146149
return false;
147150
}

0 commit comments

Comments
 (0)