Skip to content

Commit fa04717

Browse files
authored
Merge pull request #39395 from rintaro/sourcekit-completion-inits
[CodeCompletion] Remove two completion options from LangOptions
2 parents c52f37c + afc24df commit fa04717

File tree

9 files changed

+63
-47
lines changed

9 files changed

+63
-47
lines changed

include/swift/Basic/LangOptions.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -230,14 +230,6 @@ namespace swift {
230230
/// Keep comments during lexing and attach them to declarations.
231231
bool AttachCommentsToDecls = false;
232232

233-
/// Whether to include initializers when code-completing a postfix
234-
/// expression.
235-
bool CodeCompleteInitsInPostfixExpr = false;
236-
237-
/// Whether to use heuristics to decide whether to show call-pattern
238-
/// completions.
239-
bool CodeCompleteCallPatternHeuristics = false;
240-
241233
///
242234
/// Flags for use by tests
243235
///

include/swift/IDE/CodeCompletion.h

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,13 @@ struct CodeCompletionResultSink {
947947
/// Whether to emit object literals if desired.
948948
bool includeObjectLiterals = true;
949949

950+
/// Whether to emit type initializers in addition to type names in expression
951+
/// position.
952+
bool addInitsToTopLevel = false;
953+
954+
/// Whether to perform "call pettern heuristics".
955+
bool enableCallPatternHeuristics = false;
956+
950957
std::vector<CodeCompletionResult *> Results;
951958

952959
/// A single-element cache for module names stored in Allocator, keyed by a
@@ -1021,7 +1028,23 @@ class CodeCompletionContext {
10211028
void setIncludeObjectLiterals(bool flag) {
10221029
CurrentResults.includeObjectLiterals = flag;
10231030
}
1024-
bool includeObjectLiterals() { return CurrentResults.includeObjectLiterals; }
1031+
bool includeObjectLiterals() const {
1032+
return CurrentResults.includeObjectLiterals;
1033+
}
1034+
1035+
void setAddInitsToTopLevel(bool flag) {
1036+
CurrentResults.addInitsToTopLevel = flag;
1037+
}
1038+
bool getAddInitsToTopLevel() const {
1039+
return CurrentResults.addInitsToTopLevel;
1040+
}
1041+
1042+
void setCallPatternHeuristics(bool flag) {
1043+
CurrentResults.enableCallPatternHeuristics = flag;
1044+
}
1045+
bool getCallPatternHeuristics() const {
1046+
return CurrentResults.enableCallPatternHeuristics;
1047+
}
10251048

10261049
/// Allocate a string owned by the code completion context.
10271050
StringRef copyString(StringRef Str);

include/swift/IDE/CodeCompletionCache.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class CodeCompletionCache {
4242
bool ResultsHaveLeadingDot;
4343
bool ForTestableLookup;
4444
bool ForPrivateImportLookup;
45-
bool CodeCompleteInitsInPostfixExpr;
45+
bool AddInitsInToplevel;
4646
bool Annotated;
4747

4848
friend bool operator==(const Key &LHS, const Key &RHS) {
@@ -52,7 +52,8 @@ class CodeCompletionCache {
5252
LHS.ResultsHaveLeadingDot == RHS.ResultsHaveLeadingDot &&
5353
LHS.ForTestableLookup == RHS.ForTestableLookup &&
5454
LHS.ForPrivateImportLookup == RHS.ForPrivateImportLookup &&
55-
LHS.CodeCompleteInitsInPostfixExpr == RHS.CodeCompleteInitsInPostfixExpr;
55+
LHS.AddInitsInToplevel == RHS.AddInitsInToplevel &&
56+
LHS.Annotated == RHS.Annotated;
5657
}
5758
};
5859

@@ -123,6 +124,7 @@ struct DenseMapInfo<swift::ide::CodeCompletionCache::Key> {
123124
H ^= std::hash<bool>()(Val.ResultsHaveLeadingDot);
124125
H ^= std::hash<bool>()(Val.ForTestableLookup);
125126
H ^= std::hash<bool>()(Val.ForPrivateImportLookup);
127+
H ^= std::hash<bool>()(Val.AddInitsInToplevel);
126128
H ^= std::hash<bool>()(Val.Annotated);
127129
return static_cast<unsigned>(H);
128130
}

lib/Frontend/CompilerInvocation.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -506,12 +506,6 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
506506
}
507507
}
508508

509-
Opts.CodeCompleteInitsInPostfixExpr |=
510-
Args.hasArg(OPT_code_complete_inits_in_postfix_expr);
511-
512-
Opts.CodeCompleteCallPatternHeuristics |=
513-
Args.hasArg(OPT_code_complete_call_pattern_heuristics);
514-
515509
if (auto A = Args.getLastArg(OPT_enable_target_os_checking,
516510
OPT_disable_target_os_checking)) {
517511
Opts.EnableTargetOSChecking

lib/IDE/CodeCompletion.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3366,7 +3366,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
33663366
void addConstructorCallsForType(Type type, Identifier name,
33673367
DeclVisibilityKind Reason,
33683368
DynamicLookupInfo dynamicLookupInfo) {
3369-
if (!Ctx.LangOpts.CodeCompleteInitsInPostfixExpr)
3369+
if (!Sink.addInitsToTopLevel)
33703370
return;
33713371

33723372
assert(CurrDeclContext);
@@ -4459,7 +4459,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
44594459
});
44604460

44614461
// Optionally add object literals.
4462-
if (CompletionContext->includeObjectLiterals()) {
4462+
if (Sink.includeObjectLiterals) {
44634463
auto floatType = context.getFloatType();
44644464
addFromProto(LK::ColorLiteral, [&](Builder &builder) {
44654465
builder.addBaseName("#colorLiteral");
@@ -5766,7 +5766,7 @@ void CodeCompletionCallbacksImpl::completePostfixExprParen(Expr *E,
57665766
CodeCompleteTokenExpr = static_cast<CodeCompletionExpr*>(CodeCompletionE);
57675767

57685768
ShouldCompleteCallPatternAfterParen = true;
5769-
if (Context.LangOpts.CodeCompleteCallPatternHeuristics) {
5769+
if (CompletionContext.getCallPatternHeuristics()) {
57705770
// Lookahead one token to decide what kind of call completions to provide.
57715771
// When it appears that there is already code for the call present, just
57725772
// complete values and/or argument labels. Otherwise give the entire call
@@ -5908,7 +5908,7 @@ void CodeCompletionCallbacksImpl::completeCallArg(CodeCompletionExpr *E,
59085908
ShouldCompleteCallPatternAfterParen = false;
59095909
if (isFirst) {
59105910
ShouldCompleteCallPatternAfterParen = true;
5911-
if (Context.LangOpts.CodeCompleteCallPatternHeuristics) {
5911+
if (CompletionContext.getCallPatternHeuristics()) {
59125912
// Lookahead one token to decide what kind of call completions to provide.
59135913
// When it appears that there is already code for the call present, just
59145914
// complete values and/or argument labels. Otherwise give the entire call
@@ -6635,7 +6635,7 @@ static void deliverCompletionResults(CodeCompletionContext &CompletionContext,
66356635
SF.hasTestableOrPrivateImport(
66366636
AccessLevel::Internal, TheModule,
66376637
SourceFile::ImportQueryKind::PrivateOnly),
6638-
Ctx.LangOpts.CodeCompleteInitsInPostfixExpr,
6638+
CompletionContext.getAddInitsToTopLevel(),
66396639
CompletionContext.getAnnotateResult(),
66406640
};
66416641

@@ -7600,7 +7600,11 @@ void SimpleCachingCodeCompletionConsumer::handleResultsAndModules(
76007600
if (!V.hasValue()) {
76017601
// No cached results found. Fill the cache.
76027602
V = context.Cache.createValue();
7603-
(*V)->Sink.annotateResult = context.getAnnotateResult();
7603+
CodeCompletionResultSink &Sink = (*V)->Sink;
7604+
Sink.annotateResult = context.getAnnotateResult();
7605+
Sink.addInitsToTopLevel = context.getAddInitsToTopLevel();
7606+
Sink.enableCallPatternHeuristics = context.getCallPatternHeuristics();
7607+
Sink.includeObjectLiterals = context.includeObjectLiterals();
76047608
lookupCodeCompletionResultsFromModule(
76057609
(*V)->Sink, R.TheModule, R.Key.AccessPath,
76067610
R.Key.ResultsHaveLeadingDot, SF);

lib/IDE/CodeCompletionCache.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ static void writeCachedModule(llvm::raw_ostream &out,
293293
OSSLE.write(K.ResultsHaveLeadingDot);
294294
OSSLE.write(K.ForTestableLookup);
295295
OSSLE.write(K.ForPrivateImportLookup);
296-
OSSLE.write(K.CodeCompleteInitsInPostfixExpr);
296+
OSSLE.write(K.AddInitsInToplevel);
297297
OSSLE.write(K.Annotated);
298298
LE.write(static_cast<uint32_t>(OSS.tell())); // Size of debug info
299299
out.write(OSS.str().data(), OSS.str().size()); // Debug info blob
@@ -404,7 +404,7 @@ static std::string getName(StringRef cacheDirectory,
404404
OSS << (K.ResultsHaveLeadingDot ? "-dot" : "")
405405
<< (K.ForTestableLookup ? "-testable" : "")
406406
<< (K.ForPrivateImportLookup ? "-private" : "")
407-
<< (K.CodeCompleteInitsInPostfixExpr ? "-inits" : "")
407+
<< (K.AddInitsInToplevel ? "-inits" : "")
408408
<< (K.Annotated ? "-annotated" : "");
409409

410410
// name[-access-path-components]

tools/SourceKit/lib/SwiftLang/CodeCompletionOrganizer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ struct Options {
3636
bool addInnerResults = false;
3737
bool addInnerOperators = true;
3838
bool addInitsToTopLevel = false;
39-
bool callPatternHeuristics = true;
39+
bool callPatternHeuristics = false;
4040
bool hideUnderscores = true;
4141
bool reallyHideAllUnderscores = false;
4242
bool hideLowPriority = true;

tools/SourceKit/lib/SwiftLang/SwiftCompletion.cpp

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ static bool swiftCodeCompleteImpl(
133133
CompletionContext.ReusingASTContext = reusingASTContext;
134134
CompletionContext.setAnnotateResult(opts.annotatedDescription);
135135
CompletionContext.setIncludeObjectLiterals(opts.includeObjectLiterals);
136+
CompletionContext.setAddInitsToTopLevel(opts.addInitsToTopLevel);
137+
CompletionContext.setCallPatternHeuristics(opts.callPatternHeuristics);
136138
std::unique_ptr<CodeCompletionCallbacksFactory> callbacksFactory(
137139
ide::makeCodeCompletionCallbacksFactory(CompletionContext,
138140
SwiftConsumer));
@@ -1077,6 +1079,8 @@ void SwiftLangSupport::codeCompleteOpen(
10771079
unsigned resultOffset = 0;
10781080
unsigned maxResults = 0;
10791081
CodeCompletion::Options CCOpts;
1082+
// Enable "call pattern heuristics" by default for this API.
1083+
CCOpts.callPatternHeuristics = true;
10801084
if (options)
10811085
translateCodeCompletionOptions(*options, CCOpts, filterText, resultOffset,
10821086
maxResults);
@@ -1119,20 +1123,9 @@ void SwiftLangSupport::codeCompleteOpen(
11191123
extendCompletions(results, sink, info, nameToPopularity, CCOpts);
11201124
});
11211125

1122-
// Add any codecomplete.open specific flags.
1123-
std::vector<const char *> extendedArgs(args.begin(), args.end());
1124-
if (CCOpts.addInitsToTopLevel) {
1125-
extendedArgs.push_back("-Xfrontend");
1126-
extendedArgs.push_back("-code-complete-inits-in-postfix-expr");
1127-
}
1128-
if (CCOpts.callPatternHeuristics) {
1129-
extendedArgs.push_back("-Xfrontend");
1130-
extendedArgs.push_back("-code-complete-call-pattern-heuristics");
1131-
}
1132-
11331126
// Invoke completion.
11341127
if (!swiftCodeCompleteImpl(*this, inputBuf, offset, swiftConsumer,
1135-
extendedArgs, fileSystem, CCOpts, error)) {
1128+
args, fileSystem, CCOpts, error)) {
11361129
consumer.failed(error);
11371130
return;
11381131
}
@@ -1150,7 +1143,7 @@ void SwiftLangSupport::codeCompleteOpen(
11501143
using CodeCompletion::SessionCacheRef;
11511144
auto bufferCopy = llvm::MemoryBuffer::getMemBufferCopy(
11521145
inputBuf->getBuffer(), inputBuf->getBufferIdentifier());
1153-
std::vector<std::string> argsCopy(extendedArgs.begin(), extendedArgs.end());
1146+
std::vector<std::string> argsCopy(args.begin(), args.end());
11541147
SessionCacheRef session{new SessionCache(
11551148
std::move(sink), std::move(bufferCopy), std::move(argsCopy), fileSystem,
11561149
completionKind, typeContextKind, mayUseImplicitMemberExpr,

tools/swift-ide-test/swift-ide-test.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ CodeCompletionSourceText("code-completion-sourcetext",
471471
llvm::cl::init(false));
472472

473473
static llvm::cl::opt<bool>
474-
CodeCOmpletionAnnotateResults("code-completion-annotate-results",
474+
CodeCompletionAnnotateResults("code-completion-annotate-results",
475475
llvm::cl::desc("annotate completion results with XML"),
476476
llvm::cl::cat(Category),
477477
llvm::cl::init(false));
@@ -925,6 +925,8 @@ static int doCodeCompletion(const CompilerInvocation &InitInvok,
925925
bool CodeCompletionKeywords,
926926
bool CodeCompletionComments,
927927
bool CodeCompletionAnnotateResults,
928+
bool CodeCompletionAddInitsToTopLevel,
929+
bool CodeCompletionCallPatternHeuristics,
928930
bool CodeCompletionSourceText) {
929931
std::unique_ptr<ide::OnDiskCodeCompletionCache> OnDiskCache;
930932
if (!options::CompletionCachePath.empty()) {
@@ -934,6 +936,8 @@ static int doCodeCompletion(const CompilerInvocation &InitInvok,
934936
ide::CodeCompletionCache CompletionCache(OnDiskCache.get());
935937
ide::CodeCompletionContext CompletionContext(CompletionCache);
936938
CompletionContext.setAnnotateResult(CodeCompletionAnnotateResults);
939+
CompletionContext.setAddInitsToTopLevel(CodeCompletionAddInitsToTopLevel);
940+
CompletionContext.setCallPatternHeuristics(CodeCompletionCallPatternHeuristics);
937941

938942
// Create a CodeCompletionConsumer.
939943
std::unique_ptr<ide::CodeCompletionConsumer> Consumer(
@@ -1130,6 +1134,8 @@ static int doBatchCodeCompletion(const CompilerInvocation &InitInvok,
11301134
bool CodeCompletionKeywords,
11311135
bool CodeCompletionComments,
11321136
bool CodeCompletionAnnotateResults,
1137+
bool CodeCompletionAddInitsToTopLevel,
1138+
bool CodeCompletionCallPatternHeuristics,
11331139
bool CodeCompletionSourceText) {
11341140
auto FileBufOrErr = llvm::MemoryBuffer::getFile(SourceFilename);
11351141
if (!FileBufOrErr) {
@@ -1272,6 +1278,8 @@ static int doBatchCodeCompletion(const CompilerInvocation &InitInvok,
12721278
// Consumer.
12731279
ide::CodeCompletionContext CompletionContext(CompletionCache);
12741280
CompletionContext.setAnnotateResult(CodeCompletionAnnotateResults);
1281+
CompletionContext.setAddInitsToTopLevel(CodeCompletionAddInitsToTopLevel);
1282+
CompletionContext.setCallPatternHeuristics(CodeCompletionCallPatternHeuristics);
12751283
std::unique_ptr<CodeCompletionCallbacksFactory> callbacksFactory(
12761284
ide::makeCodeCompletionCallbacksFactory(CompletionContext,
12771285
*Consumer));
@@ -3835,7 +3843,7 @@ int main(int argc, char *argv[]) {
38353843
llvm::outs(), options::CodeCompletionKeywords,
38363844
options::CodeCompletionComments,
38373845
options::CodeCompletionSourceText,
3838-
options::CodeCOmpletionAnnotateResults);
3846+
options::CodeCompletionAnnotateResults);
38393847
for (StringRef filename : options::InputFilenames) {
38403848
auto resultsOpt = ide::OnDiskCodeCompletionCache::getFromFile(filename);
38413849
if (!resultsOpt) {
@@ -3965,10 +3973,6 @@ int main(int argc, char *argv[]) {
39653973
options::ImportObjCHeader;
39663974
InitInvok.getLangOptions().EnableAccessControl =
39673975
!options::DisableAccessControl;
3968-
InitInvok.getLangOptions().CodeCompleteInitsInPostfixExpr |=
3969-
options::CodeCompleteInitsInPostfixExpr;
3970-
InitInvok.getLangOptions().CodeCompleteCallPatternHeuristics |=
3971-
options::CodeCompleteCallPatternHeuristics;
39723976
InitInvok.getLangOptions().EnableSwift3ObjCInference =
39733977
options::EnableSwift3ObjCInference;
39743978
InitInvok.getClangImporterOptions().ImportForwardDeclarations |=
@@ -4076,7 +4080,9 @@ int main(int argc, char *argv[]) {
40764080
options::CodeCompletionDiagnostics,
40774081
options::CodeCompletionKeywords,
40784082
options::CodeCompletionComments,
4079-
options::CodeCOmpletionAnnotateResults,
4083+
options::CodeCompletionAnnotateResults,
4084+
options::CodeCompleteInitsInPostfixExpr,
4085+
options::CodeCompleteCallPatternHeuristics,
40804086
options::CodeCompletionSourceText);
40814087
break;
40824088

@@ -4092,7 +4098,9 @@ int main(int argc, char *argv[]) {
40924098
options::CodeCompletionDiagnostics,
40934099
options::CodeCompletionKeywords,
40944100
options::CodeCompletionComments,
4095-
options::CodeCOmpletionAnnotateResults,
4101+
options::CodeCompletionAnnotateResults,
4102+
options::CodeCompleteInitsInPostfixExpr,
4103+
options::CodeCompleteCallPatternHeuristics,
40964104
options::CodeCompletionSourceText);
40974105
break;
40984106

0 commit comments

Comments
 (0)