Skip to content

Commit afc24df

Browse files
committed
[CodeCompletion] Remove two completion options from LangOptions
"add inits to toplevel" and "call pattern heuristics" are only used in code completion. Move them from LangOptions to CodeCompletionContext so that they don't affect compiler arguments.
1 parent 845b2f9 commit afc24df

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
@@ -501,12 +501,6 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
501501
}
502502
}
503503

504-
Opts.CodeCompleteInitsInPostfixExpr |=
505-
Args.hasArg(OPT_code_complete_inits_in_postfix_expr);
506-
507-
Opts.CodeCompleteCallPatternHeuristics |=
508-
Args.hasArg(OPT_code_complete_call_pattern_heuristics);
509-
510504
if (auto A = Args.getLastArg(OPT_enable_target_os_checking,
511505
OPT_disable_target_os_checking)) {
512506
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
@@ -466,7 +466,7 @@ CodeCompletionSourceText("code-completion-sourcetext",
466466
llvm::cl::init(false));
467467

468468
static llvm::cl::opt<bool>
469-
CodeCOmpletionAnnotateResults("code-completion-annotate-results",
469+
CodeCompletionAnnotateResults("code-completion-annotate-results",
470470
llvm::cl::desc("annotate completion results with XML"),
471471
llvm::cl::cat(Category),
472472
llvm::cl::init(false));
@@ -920,6 +920,8 @@ static int doCodeCompletion(const CompilerInvocation &InitInvok,
920920
bool CodeCompletionKeywords,
921921
bool CodeCompletionComments,
922922
bool CodeCompletionAnnotateResults,
923+
bool CodeCompletionAddInitsToTopLevel,
924+
bool CodeCompletionCallPatternHeuristics,
923925
bool CodeCompletionSourceText) {
924926
std::unique_ptr<ide::OnDiskCodeCompletionCache> OnDiskCache;
925927
if (!options::CompletionCachePath.empty()) {
@@ -929,6 +931,8 @@ static int doCodeCompletion(const CompilerInvocation &InitInvok,
929931
ide::CodeCompletionCache CompletionCache(OnDiskCache.get());
930932
ide::CodeCompletionContext CompletionContext(CompletionCache);
931933
CompletionContext.setAnnotateResult(CodeCompletionAnnotateResults);
934+
CompletionContext.setAddInitsToTopLevel(CodeCompletionAddInitsToTopLevel);
935+
CompletionContext.setCallPatternHeuristics(CodeCompletionCallPatternHeuristics);
932936

933937
// Create a CodeCompletionConsumer.
934938
std::unique_ptr<ide::CodeCompletionConsumer> Consumer(
@@ -1125,6 +1129,8 @@ static int doBatchCodeCompletion(const CompilerInvocation &InitInvok,
11251129
bool CodeCompletionKeywords,
11261130
bool CodeCompletionComments,
11271131
bool CodeCompletionAnnotateResults,
1132+
bool CodeCompletionAddInitsToTopLevel,
1133+
bool CodeCompletionCallPatternHeuristics,
11281134
bool CodeCompletionSourceText) {
11291135
auto FileBufOrErr = llvm::MemoryBuffer::getFile(SourceFilename);
11301136
if (!FileBufOrErr) {
@@ -1267,6 +1273,8 @@ static int doBatchCodeCompletion(const CompilerInvocation &InitInvok,
12671273
// Consumer.
12681274
ide::CodeCompletionContext CompletionContext(CompletionCache);
12691275
CompletionContext.setAnnotateResult(CodeCompletionAnnotateResults);
1276+
CompletionContext.setAddInitsToTopLevel(CodeCompletionAddInitsToTopLevel);
1277+
CompletionContext.setCallPatternHeuristics(CodeCompletionCallPatternHeuristics);
12701278
std::unique_ptr<CodeCompletionCallbacksFactory> callbacksFactory(
12711279
ide::makeCodeCompletionCallbacksFactory(CompletionContext,
12721280
*Consumer));
@@ -3822,7 +3830,7 @@ int main(int argc, char *argv[]) {
38223830
llvm::outs(), options::CodeCompletionKeywords,
38233831
options::CodeCompletionComments,
38243832
options::CodeCompletionSourceText,
3825-
options::CodeCOmpletionAnnotateResults);
3833+
options::CodeCompletionAnnotateResults);
38263834
for (StringRef filename : options::InputFilenames) {
38273835
auto resultsOpt = ide::OnDiskCodeCompletionCache::getFromFile(filename);
38283836
if (!resultsOpt) {
@@ -3946,10 +3954,6 @@ int main(int argc, char *argv[]) {
39463954
options::ImportObjCHeader;
39473955
InitInvok.getLangOptions().EnableAccessControl =
39483956
!options::DisableAccessControl;
3949-
InitInvok.getLangOptions().CodeCompleteInitsInPostfixExpr |=
3950-
options::CodeCompleteInitsInPostfixExpr;
3951-
InitInvok.getLangOptions().CodeCompleteCallPatternHeuristics |=
3952-
options::CodeCompleteCallPatternHeuristics;
39533957
InitInvok.getLangOptions().EnableSwift3ObjCInference =
39543958
options::EnableSwift3ObjCInference;
39553959
InitInvok.getClangImporterOptions().ImportForwardDeclarations |=
@@ -4057,7 +4061,9 @@ int main(int argc, char *argv[]) {
40574061
options::CodeCompletionDiagnostics,
40584062
options::CodeCompletionKeywords,
40594063
options::CodeCompletionComments,
4060-
options::CodeCOmpletionAnnotateResults,
4064+
options::CodeCompletionAnnotateResults,
4065+
options::CodeCompleteInitsInPostfixExpr,
4066+
options::CodeCompleteCallPatternHeuristics,
40614067
options::CodeCompletionSourceText);
40624068
break;
40634069

@@ -4073,7 +4079,9 @@ int main(int argc, char *argv[]) {
40734079
options::CodeCompletionDiagnostics,
40744080
options::CodeCompletionKeywords,
40754081
options::CodeCompletionComments,
4076-
options::CodeCOmpletionAnnotateResults,
4082+
options::CodeCompletionAnnotateResults,
4083+
options::CodeCompleteInitsInPostfixExpr,
4084+
options::CodeCompleteCallPatternHeuristics,
40774085
options::CodeCompletionSourceText);
40784086
break;
40794087

0 commit comments

Comments
 (0)