Skip to content

Commit 04ff291

Browse files
committed
[SourceKit] Allow executing a completion-like operation without inserting a code completion token into the buffer
We will use this for solver-based cursor info, which doesn’t use code completion tokens.
1 parent 1485fe5 commit 04ff291

File tree

5 files changed

+25
-9
lines changed

5 files changed

+25
-9
lines changed

tools/SourceKit/lib/SwiftLang/SwiftCompletion.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ static void swiftCodeCompleteImpl(
105105
CompletionContext.setAddCallWithNoDefaultArgs(opts.addCallWithNoDefaultArgs);
106106

107107
Lang.performWithParamsToCompletionLikeOperation(
108-
UnresolvedInputFile, Offset, Args, FileSystem, CancellationToken,
108+
UnresolvedInputFile, Offset, /*InsertCodeCompletionToken=*/true, Args,
109+
FileSystem, CancellationToken,
109110
[&](CancellableResult<SwiftLangSupport::CompletionLikeOperationParams>
110111
ParamsResult) {
111112
ParamsResult.mapAsync<CodeCompleteResult>(

tools/SourceKit/lib/SwiftLang/SwiftConformingMethodList.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ void SwiftLangSupport::getConformingMethodList(
176176
}
177177

178178
performWithParamsToCompletionLikeOperation(
179-
UnresolvedInputFile, Offset, Args, fileSystem, CancellationToken,
179+
UnresolvedInputFile, Offset, /*InsertCodeCompletionToken=*/true, Args,
180+
fileSystem, CancellationToken,
180181
[&](CancellableResult<CompletionLikeOperationParams> ParmsResult) {
181182
ParmsResult.mapAsync<ConformingMethodListResults>(
182183
[&](auto &Params, auto DeliverTransformed) {

tools/SourceKit/lib/SwiftLang/SwiftLangSupport.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,7 +1008,7 @@ SwiftLangSupport::getFileSystem(const Optional<VFSOptions> &vfsOptions,
10081008

10091009
void SwiftLangSupport::performWithParamsToCompletionLikeOperation(
10101010
llvm::MemoryBuffer *UnresolvedInputFile, unsigned Offset,
1011-
ArrayRef<const char *> Args,
1011+
bool InsertCodeCompletionToken, ArrayRef<const char *> Args,
10121012
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
10131013
SourceKitCancellationToken CancellationToken,
10141014
llvm::function_ref<void(CancellableResult<CompletionLikeOperationParams>)>
@@ -1026,8 +1026,18 @@ void SwiftLangSupport::performWithParamsToCompletionLikeOperation(
10261026
// Create a buffer for code completion. This contains '\0' at 'Offset'
10271027
// position of 'UnresolvedInputFile' buffer.
10281028
auto origOffset = Offset;
1029-
auto newBuffer = ide::makeCodeCompletionMemoryBuffer(
1030-
UnresolvedInputFile, Offset, bufferIdentifier);
1029+
1030+
// If we inserted a code completion token into the buffer, this keeps the
1031+
// buffer alive. Should never be accessed, `buffer` should be used instead.
1032+
std::unique_ptr<llvm::MemoryBuffer> codeCompletionBuffer;
1033+
llvm::MemoryBuffer *buffer;
1034+
if (InsertCodeCompletionToken) {
1035+
codeCompletionBuffer = ide::makeCodeCompletionMemoryBuffer(
1036+
UnresolvedInputFile, Offset, bufferIdentifier);
1037+
buffer = codeCompletionBuffer.get();
1038+
} else {
1039+
buffer = UnresolvedInputFile;
1040+
}
10311041

10321042
SourceManager SM;
10331043
DiagnosticEngine Diags(SM);
@@ -1055,7 +1065,7 @@ void SwiftLangSupport::performWithParamsToCompletionLikeOperation(
10551065
std::string CompilerInvocationError;
10561066
bool CreatingInvocationFailed = getASTManager()->initCompilerInvocation(
10571067
Invocation, Args, FrontendOptions::ActionType::Typecheck, Diags,
1058-
newBuffer->getBufferIdentifier(), FileSystem, CompilerInvocationError);
1068+
buffer->getBufferIdentifier(), FileSystem, CompilerInvocationError);
10591069
if (CreatingInvocationFailed) {
10601070
PerformOperation(CancellableResult<CompletionLikeOperationParams>::failure(
10611071
CompilerInvocationError));
@@ -1075,7 +1085,7 @@ void SwiftLangSupport::performWithParamsToCompletionLikeOperation(
10751085
CancellationFlag->store(true, std::memory_order_relaxed);
10761086
});
10771087

1078-
CompletionLikeOperationParams Params = {Invocation, newBuffer.get(), &CIDiags,
1088+
CompletionLikeOperationParams Params = {Invocation, buffer, &CIDiags,
10791089
CancellationFlag};
10801090
PerformOperation(
10811091
CancellableResult<CompletionLikeOperationParams>::success(Params));

tools/SourceKit/lib/SwiftLang/SwiftLangSupport.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,9 +516,12 @@ class SwiftLangSupport : public LangSupport {
516516

517517
/// Execute \p PerformOperation synchronously with the parameters necessary to
518518
/// invoke a completion-like operation on \c CompletionInstance.
519+
/// If \p InsertCodeCompletionToken is \c true, a code completion token will
520+
/// be inserted into the source buffer, if \p InsertCodeCompletionToken is \c
521+
/// false, the buffer is left as-is.
519522
void performWithParamsToCompletionLikeOperation(
520523
llvm::MemoryBuffer *UnresolvedInputFile, unsigned Offset,
521-
ArrayRef<const char *> Args,
524+
bool InsertCodeCompletionToken, ArrayRef<const char *> Args,
522525
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
523526
SourceKitCancellationToken CancellationToken,
524527
llvm::function_ref<

tools/SourceKit/lib/SwiftLang/SwiftTypeContextInfo.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ void SwiftLangSupport::getExpressionContextInfo(
146146
}
147147

148148
performWithParamsToCompletionLikeOperation(
149-
UnresolvedInputFile, Offset, Args, fileSystem, CancellationToken,
149+
UnresolvedInputFile, Offset, /*InsertCodeCompletionToken=*/true, Args,
150+
fileSystem, CancellationToken,
150151
[&](CancellableResult<CompletionLikeOperationParams> ParamsResult) {
151152
ParamsResult.mapAsync<TypeContextInfoResult>(
152153
[&](auto &CIParams, auto DeliverTransformed) {

0 commit comments

Comments
 (0)