Skip to content

Commit 558cc63

Browse files
authored
Merge pull request swiftlang#24455 from rintaro/ide-complete-import-swiftmodule
[CodeCompletion] Complete Swift only module name after 'import'
2 parents 4bc22c7 + 0d43e5a commit 558cc63

File tree

15 files changed

+437
-151
lines changed

15 files changed

+437
-151
lines changed

include/swift/AST/ASTContext.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -856,9 +856,9 @@ class ASTContext final {
856856
return getIdentifier(getSwiftName(kind));
857857
}
858858

859-
/// Collect visible clang modules from the ClangModuleLoader. These modules are
860-
/// not necessarily loaded.
861-
void getVisibleTopLevelClangModules(SmallVectorImpl<clang::Module*> &Modules) const;
859+
/// Populate \p names with visible top level module names.
860+
/// This guarantees that resulted \p names doesn't have duplicated names.
861+
void getVisibleTopLevelModuleNames(SmallVectorImpl<Identifier> &names) const;
862862

863863
private:
864864
/// Register the given generic signature builder to be used as the canonical

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 & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,6 @@
4747
#include "swift/Syntax/SyntaxArena.h"
4848
#include "swift/Strings.h"
4949
#include "swift/Subsystems.h"
50-
#include "clang/AST/Attr.h"
51-
#include "clang/AST/DeclObjC.h"
52-
#include "clang/Lex/HeaderSearch.h"
53-
#include "clang/Lex/Preprocessor.h"
5450
#include "llvm/ADT/DenseMap.h"
5551
#include "llvm/ADT/Statistic.h"
5652
#include "llvm/ADT/StringMap.h"
@@ -1423,12 +1419,6 @@ ModuleDecl *ASTContext::getLoadedModule(Identifier ModuleName) const {
14231419
return LoadedModules.lookup(ModuleName);
14241420
}
14251421

1426-
void ASTContext::getVisibleTopLevelClangModules(
1427-
SmallVectorImpl<clang::Module*> &Modules) const {
1428-
getClangModuleLoader()->getClangPreprocessor().getHeaderSearchInfo().
1429-
collectAllModules(Modules);
1430-
}
1431-
14321422
static AllocationArena getArena(GenericSignature *genericSig) {
14331423
if (!genericSig)
14341424
return AllocationArena::Permanent;
@@ -1670,6 +1660,19 @@ void ProtocolDecl::setDefaultAssociatedConformanceWitness(
16701660
(void)pair;
16711661
}
16721662

1663+
void ASTContext::getVisibleTopLevelModuleNames(
1664+
SmallVectorImpl<Identifier> &names) const {
1665+
names.clear();
1666+
for (auto &importer : getImpl().ModuleLoaders)
1667+
importer->collectVisibleTopLevelModuleNames(names);
1668+
1669+
// Sort and unique.
1670+
std::sort(names.begin(), names.end(), [](Identifier LHS, Identifier RHS) {
1671+
return LHS.str().compare_lower(RHS.str()) < 0;
1672+
});
1673+
names.erase(std::unique(names.begin(), names.end()), names.end());
1674+
}
1675+
16731676
bool ASTContext::canImportModule(std::pair<Identifier, SourceLoc> ModulePath) {
16741677
// If this module has already been successfully imported, it is importable.
16751678
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)