Skip to content

Commit c1530ee

Browse files
committed
[SourceKit] Add option to enable ASTContext reusing for code completion
1 parent 62c4412 commit c1530ee

File tree

7 files changed

+45
-2
lines changed

7 files changed

+45
-2
lines changed

include/swift/IDE/CompletionInstance.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ makeCodeCompletionMemoryBuffer(const llvm::MemoryBuffer *origBuf,
3636

3737
class CompletionInstance {
3838
std::unique_ptr<CompilerInstance> CachedCI = nullptr;
39+
bool EnableASTCaching = false;
3940

4041
swift::CompilerInstance *
4142
getReusingCompilerInstance(const swift::CompilerInvocation &Invocation,
@@ -49,6 +50,10 @@ class CompletionInstance {
4950
std::string &Error, DiagnosticConsumer *DiagC);
5051

5152
public:
53+
void setEnableASTCaching(bool Flag) {
54+
EnableASTCaching = Flag;
55+
}
56+
5257
swift::CompilerInstance *getCompilerInstance(
5358
swift::CompilerInvocation &Invocation,
5459
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,

lib/IDE/CompletionInstance.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ CompilerInstance *CompletionInstance::getReusingCompilerInstance(
130130
const swift::CompilerInvocation &Invocation,
131131
llvm::MemoryBuffer *completionBuffer, unsigned int Offset,
132132
DiagnosticConsumer *DiagC) {
133+
if (!EnableASTCaching)
134+
return nullptr;
135+
133136
if (!CachedCI)
134137
return nullptr;
135138

tools/SourceKit/include/SourceKit/Core/LangSupport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,7 @@ class LangSupport {
653653

654654
virtual void
655655
codeComplete(llvm::MemoryBuffer *InputBuf, unsigned Offset,
656+
OptionsDictionary *options,
656657
CodeCompletionConsumer &Consumer, ArrayRef<const char *> Args,
657658
Optional<VFSOptions> vfsOptions) = 0;
658659

tools/SourceKit/lib/SwiftLang/CodeCompletionOrganizer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ struct Options {
4242
bool hideLowPriority = true;
4343
bool hideByNameStyle = true;
4444
bool fuzzyMatching = true;
45+
bool reuseASTContextIfPossible = false;
4546
unsigned minFuzzyLength = 2;
4647
unsigned showTopNonLiteralResults = 3;
4748

tools/SourceKit/lib/SwiftLang/SwiftCompletion.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,29 @@ static bool swiftCodeCompleteImpl(
199199
return true;
200200
}
201201

202+
static void translateCodeCompletionOptions(OptionsDictionary &from,
203+
CodeCompletion::Options &to,
204+
StringRef &filterText,
205+
unsigned &resultOffset,
206+
unsigned &maxResults);
207+
202208
void SwiftLangSupport::codeComplete(
203209
llvm::MemoryBuffer *UnresolvedInputFile, unsigned Offset,
210+
OptionsDictionary *options,
204211
SourceKit::CodeCompletionConsumer &SKConsumer, ArrayRef<const char *> Args,
205212
Optional<VFSOptions> vfsOptions) {
206213

214+
215+
CodeCompletion::Options CCOpts;
216+
if (options) {
217+
StringRef filterText;
218+
unsigned resultOffset = 0;
219+
unsigned maxResults = 0;
220+
translateCodeCompletionOptions(*options, CCOpts, filterText, resultOffset,
221+
maxResults);
222+
}
223+
CompletionInst->setEnableASTCaching(CCOpts.reuseASTContextIfPossible);
224+
207225
std::string error;
208226
// FIXME: the use of None as primary file is to match the fact we do not read
209227
// the document contents using the editor documents infrastructure.
@@ -823,6 +841,7 @@ static void translateCodeCompletionOptions(OptionsDictionary &from,
823841
static UIdent KeyContextWeight("key.codecomplete.sort.contextweight");
824842
static UIdent KeyFuzzyWeight("key.codecomplete.sort.fuzzyweight");
825843
static UIdent KeyPopularityBonus("key.codecomplete.sort.popularitybonus");
844+
static UIdent KeyReuseASTContext("key.codecomplete.reuseastcontext");
826845
from.valueForOption(KeySortByName, to.sortByName);
827846
from.valueForOption(KeyUseImportDepth, to.useImportDepth);
828847
from.valueForOption(KeyGroupOverloads, to.groupOverloads);
@@ -846,6 +865,7 @@ static void translateCodeCompletionOptions(OptionsDictionary &from,
846865
from.valueForOption(KeyPopularityBonus, to.popularityBonus);
847866
from.valueForOption(KeyHideByName, to.hideByNameStyle);
848867
from.valueForOption(KeyTopNonLiteral, to.showTopNonLiteralResults);
868+
from.valueForOption(KeyReuseASTContext, to.reuseASTContextIfPossible);
849869
}
850870

851871
/// Canonicalize a name that is in the format of a reference to a function into
@@ -1164,6 +1184,7 @@ void SwiftLangSupport::codeCompleteOpen(
11641184
if (options)
11651185
translateCodeCompletionOptions(*options, CCOpts, filterText, resultOffset,
11661186
maxResults);
1187+
CompletionInst->setEnableASTCaching(CCOpts.reuseASTContextIfPossible);
11671188

11681189
std::string error;
11691190
// FIXME: the use of None as primary file is to match the fact we do not read
@@ -1284,6 +1305,7 @@ void SwiftLangSupport::codeCompleteUpdate(
12841305
if (options)
12851306
translateCodeCompletionOptions(*options, CCOpts, filterText, resultOffset,
12861307
maxResults);
1308+
CompletionInst->setEnableASTCaching(CCOpts.reuseASTContextIfPossible);
12871309

12881310
NameToPopularityMap *nameToPopularity = nullptr;
12891311
// This reference must outlive the uses of nameToPopularity.

tools/SourceKit/lib/SwiftLang/SwiftLangSupport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,7 @@ class SwiftLangSupport : public LangSupport {
445445

446446
void codeComplete(
447447
llvm::MemoryBuffer *InputBuf, unsigned Offset,
448+
OptionsDictionary *options,
448449
SourceKit::CodeCompletionConsumer &Consumer, ArrayRef<const char *> Args,
449450
Optional<VFSOptions> vfsOptions) override;
450451

tools/SourceKit/tools/sourcekitd/lib/API/Requests.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ static void findRelatedIdents(StringRef Filename,
158158

159159
static sourcekitd_response_t
160160
codeComplete(llvm::MemoryBuffer *InputBuf, int64_t Offset,
161+
Optional<RequestDict> optionsDict,
161162
ArrayRef<const char *> Args, Optional<VFSOptions> vfsOptions);
162163

163164
static sourcekitd_response_t codeCompleteOpen(StringRef name,
@@ -940,7 +941,9 @@ static void handleSemanticRequest(
940941
int64_t Offset;
941942
if (Req.getInt64(KeyOffset, Offset, /*isOptional=*/false))
942943
return Rec(createErrorRequestInvalid("missing 'key.offset'"));
943-
return Rec(codeComplete(InputBuf.get(), Offset, Args, std::move(vfsOptions)));
944+
Optional<RequestDict> options = Req.getDictionary(KeyCodeCompleteOptions);
945+
return Rec(codeComplete(InputBuf.get(), Offset, options, Args,
946+
std::move(vfsOptions)));
944947
}
945948

946949
if (ReqUID == RequestCodeCompleteOpen) {
@@ -1932,12 +1935,19 @@ class SKCodeCompletionConsumer : public CodeCompletionConsumer {
19321935

19331936
static sourcekitd_response_t
19341937
codeComplete(llvm::MemoryBuffer *InputBuf, int64_t Offset,
1938+
Optional<RequestDict> optionsDict,
19351939
ArrayRef<const char *> Args,
19361940
Optional<VFSOptions> vfsOptions) {
19371941
ResponseBuilder RespBuilder;
19381942
SKCodeCompletionConsumer CCC(RespBuilder);
1943+
1944+
std::unique_ptr<SKOptionsDictionary> options;
1945+
if (optionsDict)
1946+
options = std::make_unique<SKOptionsDictionary>(*optionsDict);
1947+
19391948
LangSupport &Lang = getGlobalContext().getSwiftLangSupport();
1940-
Lang.codeComplete(InputBuf, Offset, CCC, Args, std::move(vfsOptions));
1949+
Lang.codeComplete(InputBuf, Offset, options.get(), CCC, Args,
1950+
std::move(vfsOptions));
19411951
return CCC.createResponse();
19421952
}
19431953

0 commit comments

Comments
 (0)