mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
[SourceKit/CodeCompletion] Use callback function to run the second pass
To controls the lifetime of CompilerInstance within CompletionIntance. - Prevent from using the same CompilerInstance from multiple completion requests - Separate the stacktrace between "fast" and "normal" completions - Further code consolidation between various completion-like requests
This commit is contained in:
@@ -33,72 +33,18 @@ static bool swiftConformingMethodListImpl(
|
||||
ide::ConformingMethodListConsumer &Consumer,
|
||||
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
|
||||
std::string &Error) {
|
||||
return Lang.performCompletionLikeOperation(
|
||||
UnresolvedInputFile, Offset, Args, FileSystem, Error,
|
||||
[&](CompilerInstance &CI) {
|
||||
// Create a factory for code completion callbacks that will feed the
|
||||
// Consumer.
|
||||
std::unique_ptr<CodeCompletionCallbacksFactory> callbacksFactory(
|
||||
ide::makeConformingMethodListCallbacksFactory(ExpectedTypeNames,
|
||||
Consumer));
|
||||
|
||||
// Resolve symlinks for the input file.
|
||||
llvm::SmallString<128> bufferIdentifier;
|
||||
if (auto err = FileSystem->getRealPath(
|
||||
UnresolvedInputFile->getBufferIdentifier(), bufferIdentifier))
|
||||
bufferIdentifier = UnresolvedInputFile->getBufferIdentifier();
|
||||
|
||||
auto origOffset = Offset;
|
||||
auto newBuffer = ide::makeCodeCompletionMemoryBuffer(
|
||||
UnresolvedInputFile, Offset, bufferIdentifier);
|
||||
|
||||
SourceManager SM;
|
||||
DiagnosticEngine Diags(SM);
|
||||
PrintingDiagnosticConsumer PrintDiags;
|
||||
EditorDiagConsumer TraceDiags;
|
||||
trace::TracedOperation TracedOp{trace::OperationKind::CodeCompletion};
|
||||
|
||||
Diags.addConsumer(PrintDiags);
|
||||
if (TracedOp.enabled()) {
|
||||
Diags.addConsumer(TraceDiags);
|
||||
trace::SwiftInvocation SwiftArgs;
|
||||
trace::initTraceInfo(SwiftArgs, bufferIdentifier, Args);
|
||||
TracedOp.setDiagnosticProvider(
|
||||
[&](SmallVectorImpl<DiagnosticEntryInfo> &diags) {
|
||||
TraceDiags.getAllDiagnostics(diags);
|
||||
});
|
||||
TracedOp.start(
|
||||
SwiftArgs,
|
||||
{std::make_pair("OriginalOffset", std::to_string(origOffset)),
|
||||
std::make_pair("Offset", std::to_string(Offset))});
|
||||
}
|
||||
ForwardingDiagnosticConsumer CIDiags(Diags);
|
||||
|
||||
CompilerInvocation Invocation;
|
||||
bool Failed = Lang.getASTManager()->initCompilerInvocation(
|
||||
Invocation, Args, Diags, newBuffer->getBufferIdentifier(), FileSystem,
|
||||
Error);
|
||||
if (Failed)
|
||||
return false;
|
||||
if (!Invocation.getFrontendOptions().InputsAndOutputs.hasInputs()) {
|
||||
Error = "no input filenames specified";
|
||||
return false;
|
||||
}
|
||||
|
||||
// Pin completion instance.
|
||||
auto CompletionInst = Lang.getCompletionInstance();
|
||||
CompilerInstance *CI = CompletionInst->getCompilerInstance(
|
||||
Invocation, Args, FileSystem, newBuffer.get(), Offset, Error, &CIDiags);
|
||||
if (!CI)
|
||||
return false;
|
||||
SWIFT_DEFER { CI->removeDiagnosticConsumer(&CIDiags); };
|
||||
|
||||
// Perform the parsing and completion.
|
||||
if (!CI->hasPersistentParserState())
|
||||
CI->performParseAndResolveImportsOnly();
|
||||
|
||||
// Create a factory for code completion callbacks that will feed the
|
||||
// Consumer.
|
||||
std::unique_ptr<CodeCompletionCallbacksFactory> callbacksFactory(
|
||||
ide::makeConformingMethodListCallbacksFactory(ExpectedTypeNames,
|
||||
Consumer));
|
||||
|
||||
performCodeCompletionSecondPass(CI->getPersistentParserState(),
|
||||
*callbacksFactory);
|
||||
|
||||
return true;
|
||||
performCodeCompletionSecondPass(CI.getPersistentParserState(),
|
||||
*callbacksFactory);
|
||||
});
|
||||
}
|
||||
|
||||
void SwiftLangSupport::getConformingMethodList(
|
||||
|
||||
Reference in New Issue
Block a user