Skip to content

Commit 367c981

Browse files
committed
[SourceKit] Move invocation of code completion second pass for ConformingMethodList from SoruceKit to CompletionInstance
1 parent ab257bb commit 367c981

File tree

3 files changed

+203
-212
lines changed

3 files changed

+203
-212
lines changed

include/swift/IDE/CompletionInstance.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "swift/Frontend/Frontend.h"
1717
#include "swift/IDE/CancellableResult.h"
18+
#include "swift/IDE/ConformingMethodList.h"
1819
#include "swift/IDE/TypeContextInfo.h"
1920
#include "llvm/ADT/Hashing.h"
2021
#include "llvm/ADT/IntrusiveRefCntPtr.h"
@@ -52,11 +53,19 @@ struct CompletionInstanceResult {
5253
/// The results returned from \c CompletionInstance::typeContextInfo.
5354
struct TypeContextInfoResult {
5455
/// The actual results. If empty, no results were found.
55-
ArrayRef<ide::TypeContextInfoItem> Results;
56+
ArrayRef<TypeContextInfoItem> Results;
5657
/// Whether an AST was reused to produce the results.
5758
bool DidReuseAST;
5859
};
5960

61+
/// The results returned from \c CompletionInstance::conformingMethodList.
62+
struct ConformingMethodListResults {
63+
/// The actual results. If \c nullptr, no results were found.
64+
const ConformingMethodListResult *Result;
65+
/// Whether an AST was reused for the completion.
66+
bool DidReuseAST;
67+
};
68+
6069
/// Manages \c CompilerInstance for completion like operations.
6170
class CompletionInstance {
6271
struct Options {
@@ -141,6 +150,14 @@ class CompletionInstance {
141150
DiagnosticConsumer *DiagC,
142151
llvm::function_ref<void(CancellableResult<TypeContextInfoResult>)>
143152
Callback);
153+
154+
void conformingMethodList(
155+
swift::CompilerInvocation &Invocation, llvm::ArrayRef<const char *> Args,
156+
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
157+
llvm::MemoryBuffer *completionBuffer, unsigned int Offset,
158+
DiagnosticConsumer *DiagC, ArrayRef<const char *> ExpectedTypeNames,
159+
llvm::function_ref<void(CancellableResult<ConformingMethodListResults>)>
160+
Callback);
144161
};
145162

146163
} // namespace ide

lib/IDE/CompletionInstance.cpp

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -701,9 +701,65 @@ void swift::ide::CompletionInstance::typeContextInfo(
701701
*Result.CI.getCodeCompletionFile(), *callbacksFactory);
702702
if (!Consumer.HandleResultsCalled) {
703703
// If we didn't receive a handleResult call from the second
704-
// pass, we didn't receive any results. To make sure
705-
// DeliverTransformed gets called exactly once, call it with
706-
// no results here.
704+
// pass, we didn't receive any results. To make sure Callback
705+
// gets called exactly once, call it manually with no results
706+
// here.
707+
DeliverTransformed(
708+
ResultType::success({/*Results=*/{}, Result.DidReuseAST}));
709+
}
710+
},
711+
Callback);
712+
});
713+
}
714+
715+
void swift::ide::CompletionInstance::conformingMethodList(
716+
swift::CompilerInvocation &Invocation, llvm::ArrayRef<const char *> Args,
717+
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
718+
llvm::MemoryBuffer *completionBuffer, unsigned int Offset,
719+
DiagnosticConsumer *DiagC, ArrayRef<const char *> ExpectedTypeNames,
720+
llvm::function_ref<void(CancellableResult<ConformingMethodListResults>)>
721+
Callback) {
722+
using ResultType = CancellableResult<ConformingMethodListResults>;
723+
724+
struct ConsumerToCallbackAdapter
725+
: public swift::ide::ConformingMethodListConsumer {
726+
bool ReusingASTContext;
727+
llvm::function_ref<void(ResultType)> Callback;
728+
bool HandleResultsCalled = false;
729+
730+
ConsumerToCallbackAdapter(bool ReusingASTContext,
731+
llvm::function_ref<void(ResultType)> Callback)
732+
: ReusingASTContext(ReusingASTContext), Callback(Callback) {}
733+
734+
void handleResult(const ide::ConformingMethodListResult &result) override {
735+
HandleResultsCalled = true;
736+
Callback(ResultType::success({&result, ReusingASTContext}));
737+
}
738+
};
739+
740+
performOperation(
741+
Invocation, Args, FileSystem, completionBuffer, Offset, DiagC,
742+
[&](CancellableResult<CompletionInstanceResult> CIResult) {
743+
CIResult.mapAsync<ConformingMethodListResults>(
744+
[&ExpectedTypeNames](auto &Result, auto DeliverTransformed) {
745+
ConsumerToCallbackAdapter Consumer(Result.DidReuseAST,
746+
DeliverTransformed);
747+
std::unique_ptr<CodeCompletionCallbacksFactory> callbacksFactory(
748+
ide::makeConformingMethodListCallbacksFactory(
749+
ExpectedTypeNames, Consumer));
750+
751+
if (!Result.DidFindCodeCompletionToken) {
752+
DeliverTransformed(
753+
ResultType::success({/*Results=*/{}, Result.DidReuseAST}));
754+
}
755+
756+
performCodeCompletionSecondPass(
757+
*Result.CI.getCodeCompletionFile(), *callbacksFactory);
758+
if (!Consumer.HandleResultsCalled) {
759+
// If we didn't receive a handleResult call from the second
760+
// pass, we didn't receive any results. To make sure Callback
761+
// gets called exactly once, call it manually with no results
762+
// here.
707763
DeliverTransformed(
708764
ResultType::success({/*Results=*/{}, Result.DidReuseAST}));
709765
}

0 commit comments

Comments
 (0)