Skip to content

Commit 4dca0c6

Browse files
committed
[completion] Clarify and simplify not-recommended state
Combine IsNotRecommended with NotRecommendedReason and improve the names of the existing cases to more clearly identify the cases they apply to. Now all not-recommended completions are required to have a reason.
1 parent 3b3f173 commit 4dca0c6

File tree

5 files changed

+42
-52
lines changed

5 files changed

+42
-52
lines changed

include/swift/IDE/CodeCompletion.h

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -600,20 +600,20 @@ class CodeCompletionResult {
600600
};
601601

602602
enum NotRecommendedReason {
603-
Redundant,
603+
None = 0,
604+
RedundantImport,
604605
Deprecated,
605-
InvalidContext,
606+
InvalidAsyncContext,
606607
CrossActorReference,
607-
NoReason,
608+
VariableUsedInOwnDefinition,
608609
};
609610

610611
private:
611612
unsigned Kind : 3;
612613
unsigned AssociatedKind : 8;
613614
unsigned KnownOperatorKind : 6;
614615
unsigned SemanticContext : 3;
615-
unsigned NotRecommended : 1;
616-
unsigned NotRecReason : 3;
616+
unsigned NotRecommended : 4;
617617
unsigned IsSystem : 1;
618618

619619
/// The number of bytes to the left of the code completion point that
@@ -644,11 +644,9 @@ class CodeCompletionResult {
644644
CodeCompletionOperatorKind::None,
645645
StringRef BriefDocComment = StringRef())
646646
: Kind(Kind), KnownOperatorKind(unsigned(KnownOperatorKind)),
647-
SemanticContext(unsigned(SemanticContext)), NotRecommended(false),
648-
NotRecReason(NotRecommendedReason::NoReason),
647+
SemanticContext(unsigned(SemanticContext)), NotRecommended(None),
649648
NumBytesToErase(NumBytesToErase), CompletionString(CompletionString),
650-
BriefDocComment(BriefDocComment),
651-
TypeDistance(TypeDistance) {
649+
BriefDocComment(BriefDocComment), TypeDistance(TypeDistance) {
652650
assert(Kind != Declaration && "use the other constructor");
653651
assert(CompletionString);
654652
if (isOperator() && KnownOperatorKind == CodeCompletionOperatorKind::None)
@@ -670,8 +668,7 @@ class CodeCompletionResult {
670668
ExpectedTypeRelation TypeDistance,
671669
StringRef BriefDocComment = StringRef())
672670
: Kind(Keyword), KnownOperatorKind(0),
673-
SemanticContext(unsigned(SemanticContext)), NotRecommended(false),
674-
NotRecReason(NotRecommendedReason::NoReason),
671+
SemanticContext(unsigned(SemanticContext)), NotRecommended(None),
675672
NumBytesToErase(NumBytesToErase), CompletionString(CompletionString),
676673
BriefDocComment(BriefDocComment), TypeDistance(TypeDistance) {
677674
assert(CompletionString);
@@ -688,8 +685,7 @@ class CodeCompletionResult {
688685
CodeCompletionString *CompletionString,
689686
ExpectedTypeRelation TypeDistance)
690687
: Kind(Literal), KnownOperatorKind(0),
691-
SemanticContext(unsigned(SemanticContext)), NotRecommended(false),
692-
NotRecReason(NotRecommendedReason::NoReason),
688+
SemanticContext(unsigned(SemanticContext)), NotRecommended(None),
693689
NumBytesToErase(NumBytesToErase), CompletionString(CompletionString),
694690
TypeDistance(TypeDistance) {
695691
AssociatedKind = static_cast<unsigned>(LiteralKind);
@@ -706,19 +702,17 @@ class CodeCompletionResult {
706702
unsigned NumBytesToErase,
707703
CodeCompletionString *CompletionString,
708704
const Decl *AssociatedDecl, StringRef ModuleName,
709-
bool NotRecommended,
710705
CodeCompletionResult::NotRecommendedReason NotRecReason,
711706
StringRef BriefDocComment,
712707
ArrayRef<StringRef> AssociatedUSRs,
713708
ArrayRef<std::pair<StringRef, StringRef>> DocWords,
714709
enum ExpectedTypeRelation TypeDistance)
715710
: Kind(ResultKind::Declaration), KnownOperatorKind(0),
716711
SemanticContext(unsigned(SemanticContext)),
717-
NotRecommended(NotRecommended), NotRecReason(NotRecReason),
718-
NumBytesToErase(NumBytesToErase), CompletionString(CompletionString),
719-
ModuleName(ModuleName), BriefDocComment(BriefDocComment),
720-
AssociatedUSRs(AssociatedUSRs), DocWords(DocWords),
721-
TypeDistance(TypeDistance) {
712+
NotRecommended(NotRecReason), NumBytesToErase(NumBytesToErase),
713+
CompletionString(CompletionString), ModuleName(ModuleName),
714+
BriefDocComment(BriefDocComment), AssociatedUSRs(AssociatedUSRs),
715+
DocWords(DocWords), TypeDistance(TypeDistance) {
722716
assert(AssociatedDecl && "should have a decl");
723717
AssociatedKind = unsigned(getCodeCompletionDeclKind(AssociatedDecl));
724718
IsSystem = getDeclIsSystem(AssociatedDecl);
@@ -735,7 +729,7 @@ class CodeCompletionResult {
735729
unsigned NumBytesToErase,
736730
CodeCompletionString *CompletionString,
737731
CodeCompletionDeclKind DeclKind, bool IsSystem,
738-
StringRef ModuleName, bool NotRecommended,
732+
StringRef ModuleName,
739733
CodeCompletionResult::NotRecommendedReason NotRecReason,
740734
StringRef BriefDocComment,
741735
ArrayRef<StringRef> AssociatedUSRs,
@@ -745,11 +739,11 @@ class CodeCompletionResult {
745739
: Kind(ResultKind::Declaration),
746740
KnownOperatorKind(unsigned(KnownOperatorKind)),
747741
SemanticContext(unsigned(SemanticContext)),
748-
NotRecommended(NotRecommended), NotRecReason(NotRecReason),
749-
IsSystem(IsSystem), NumBytesToErase(NumBytesToErase),
750-
CompletionString(CompletionString), ModuleName(ModuleName),
751-
BriefDocComment(BriefDocComment), AssociatedUSRs(AssociatedUSRs),
752-
DocWords(DocWords), TypeDistance(TypeDistance) {
742+
NotRecommended(NotRecReason), IsSystem(IsSystem),
743+
NumBytesToErase(NumBytesToErase), CompletionString(CompletionString),
744+
ModuleName(ModuleName), BriefDocComment(BriefDocComment),
745+
AssociatedUSRs(AssociatedUSRs), DocWords(DocWords),
746+
TypeDistance(TypeDistance) {
753747
AssociatedKind = static_cast<unsigned>(DeclKind);
754748
assert(CompletionString);
755749
assert(!isOperator() ||
@@ -800,16 +794,14 @@ class CodeCompletionResult {
800794
}
801795

802796
NotRecommendedReason getNotRecommendedReason() const {
803-
return static_cast<NotRecommendedReason>(NotRecReason);
797+
return static_cast<NotRecommendedReason>(NotRecommended);
804798
}
805799

806800
SemanticContextKind getSemanticContext() const {
807801
return static_cast<SemanticContextKind>(SemanticContext);
808802
}
809803

810-
bool isNotRecommended() const {
811-
return NotRecommended;
812-
}
804+
bool isNotRecommended() const { return NotRecommended != None; }
813805

814806
unsigned getNumBytesToErase() const {
815807
return NumBytesToErase;

lib/IDE/CodeCompletion.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,8 +1294,7 @@ CodeCompletionResult *CodeCompletionResultBuilder::takeResult() {
12941294

12951295
return new (*Sink.Allocator) CodeCompletionResult(
12961296
SemanticContext, NumBytesToErase, CCS, AssociatedDecl, ModuleName,
1297-
/*NotRecommended=*/IsNotRecommended, NotRecReason,
1298-
copyString(*Sink.Allocator, BriefComment),
1297+
NotRecReason, copyString(*Sink.Allocator, BriefComment),
12991298
copyAssociatedUSRs(*Sink.Allocator, AssociatedDecl),
13001299
copyArray(*Sink.Allocator, CommentWords), ExpectedTypeRelation);
13011300
}
@@ -2090,8 +2089,8 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
20902089
Builder.addBaseName(MD->getNameStr());
20912090
Builder.addTypeAnnotation("Module");
20922091
if (Pair.second)
2093-
Builder.setNotRecommended(CodeCompletionResult::NotRecommendedReason::
2094-
Redundant);
2092+
Builder.setNotRecommended(
2093+
CodeCompletionResult::NotRecommendedReason::RedundantImport);
20952094
}
20962095
}
20972096

@@ -2155,7 +2154,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
21552154

21562155
// Imported modules are not recommended.
21572156
if (ImportedModules.count(MD->getNameStr()) != 0)
2158-
Reason = CodeCompletionResult::NotRecommendedReason::Redundant;
2157+
Reason = CodeCompletionResult::NotRecommendedReason::RedundantImport;
21592158

21602159
addModuleName(MD, Reason);
21612160
}
@@ -2582,13 +2581,13 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
25822581
if (Kind == LookupKind::ValueInDeclContext) {
25832582
if (auto accessor = dyn_cast<AccessorDecl>(CurrDeclContext)) {
25842583
if (accessor->getStorage() == VD && accessor->isGetter())
2585-
NotRecommended = CodeCompletionResult::NoReason;
2584+
NotRecommended = CodeCompletionResult::VariableUsedInOwnDefinition;
25862585
}
25872586
}
25882587
bool implicitlyAsync = false;
25892588
analyzeActorIsolation(VD, VarType, implicitlyAsync, NotRecommended);
25902589
if (!NotRecommended && implicitlyAsync && !CanCurrDeclContextHandleAsync) {
2591-
NotRecommended = CodeCompletionResult::InvalidContext;
2590+
NotRecommended = CodeCompletionResult::InvalidAsyncContext;
25922591
}
25932592

25942593
CommandWordsPairs Pairs;
@@ -2930,7 +2929,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
29302929

29312930
if (AFT->isAsync() && !CanCurrDeclContextHandleAsync) {
29322931
Builder.setNotRecommended(
2933-
CodeCompletionResult::NotRecommendedReason::InvalidContext);
2932+
CodeCompletionResult::NotRecommendedReason::InvalidAsyncContext);
29342933
}
29352934
};
29362935

@@ -3046,7 +3045,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
30463045
if (!NotRecommended && !IsImplicitlyCurriedInstanceMethod &&
30473046
((AFT && AFT->isAsync()) || implictlyAsync) &&
30483047
!CanCurrDeclContextHandleAsync) {
3049-
NotRecommended = CodeCompletionResult::InvalidContext;
3048+
NotRecommended = CodeCompletionResult::InvalidAsyncContext;
30503049
}
30513050

30523051
// Add the method, possibly including any default arguments.
@@ -3245,7 +3244,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
32453244

32463245
if (ConstructorType->isAsync() && !CanCurrDeclContextHandleAsync) {
32473246
Builder.setNotRecommended(
3248-
CodeCompletionResult::NotRecommendedReason::InvalidContext);
3247+
CodeCompletionResult::NotRecommendedReason::InvalidAsyncContext);
32493248
}
32503249
};
32513250

@@ -3296,7 +3295,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
32963295
analyzeActorIsolation(SD, subscriptType, implictlyAsync, NotRecommended);
32973296

32983297
if (!NotRecommended && implictlyAsync && !CanCurrDeclContextHandleAsync) {
3299-
NotRecommended = CodeCompletionResult::InvalidContext;
3298+
NotRecommended = CodeCompletionResult::InvalidAsyncContext;
33003299
}
33013300

33023301
CommandWordsPairs Pairs;

lib/IDE/CodeCompletionCache.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,8 @@ static bool readCachedModule(llvm::MemoryBuffer *in,
192192
auto declKind = static_cast<CodeCompletionDeclKind>(*cursor++);
193193
auto opKind = static_cast<CodeCompletionOperatorKind>(*cursor++);
194194
auto context = static_cast<SemanticContextKind>(*cursor++);
195-
auto notRecommended = static_cast<bool>(*cursor++);
195+
auto notRecommended =
196+
static_cast<CodeCompletionResult::NotRecommendedReason>(*cursor++);
196197
auto isSystem = static_cast<bool>(*cursor++);
197198
auto numBytesToErase = static_cast<unsigned>(*cursor++);
198199
auto oldCursor = cursor;
@@ -228,9 +229,10 @@ static bool readCachedModule(llvm::MemoryBuffer *in,
228229
if (kind == CodeCompletionResult::Declaration) {
229230
result = new (*V.Sink.Allocator) CodeCompletionResult(
230231
context, numBytesToErase, string, declKind, isSystem, moduleName,
231-
notRecommended, CodeCompletionResult::NotRecommendedReason::NoReason,
232-
briefDocComment, copyArray(*V.Sink.Allocator, ArrayRef<StringRef>(assocUSRs)),
233-
copyArray(*V.Sink.Allocator, ArrayRef<std::pair<StringRef, StringRef>>(declKeywords)),
232+
notRecommended, briefDocComment,
233+
copyArray(*V.Sink.Allocator, ArrayRef<StringRef>(assocUSRs)),
234+
copyArray(*V.Sink.Allocator,
235+
ArrayRef<std::pair<StringRef, StringRef>>(declKeywords)),
234236
CodeCompletionResult::Unknown, opKind);
235237
} else {
236238
result = new (*V.Sink.Allocator)
@@ -349,7 +351,7 @@ static void writeCachedModule(llvm::raw_ostream &out,
349351
else
350352
LE.write(static_cast<uint8_t>(CodeCompletionOperatorKind::None));
351353
LE.write(static_cast<uint8_t>(R->getSemanticContext()));
352-
LE.write(static_cast<uint8_t>(R->isNotRecommended()));
354+
LE.write(static_cast<uint8_t>(R->getNotRecommendedReason()));
353355
LE.write(static_cast<uint8_t>(R->isSystem()));
354356
LE.write(static_cast<uint8_t>(R->getNumBytesToErase()));
355357
LE.write(

lib/IDE/CodeCompletionResultBuilder.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,8 @@ class CodeCompletionResultBuilder {
8989
CodeCompletionResult::Unknown;
9090
bool Cancelled = false;
9191
ArrayRef<std::pair<StringRef, StringRef>> CommentWords;
92-
bool IsNotRecommended = false;
9392
CodeCompletionResult::NotRecommendedReason NotRecReason =
94-
CodeCompletionResult::NotRecommendedReason::NoReason;
93+
CodeCompletionResult::NotRecommendedReason::None;
9594
StringRef BriefDocComment = StringRef();
9695

9796
void addChunkWithText(CodeCompletionString::Chunk::ChunkKind Kind,
@@ -148,7 +147,6 @@ class CodeCompletionResultBuilder {
148147
void setLiteralKind(CodeCompletionLiteralKind kind) { LiteralKind = kind; }
149148
void setKeywordKind(CodeCompletionKeywordKind kind) { KeywordKind = kind; }
150149
void setNotRecommended(CodeCompletionResult::NotRecommendedReason Reason) {
151-
IsNotRecommended = true;
152150
NotRecReason = Reason;
153151
}
154152

tools/SourceKit/lib/SwiftLang/CodeCompletionOrganizer.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,10 +1169,9 @@ Completion *CompletionBuilder::finish() {
11691169
base = SwiftResult(
11701170
semanticContext, current.getNumBytesToErase(), completionString,
11711171
current.getAssociatedDeclKind(), current.isSystem(),
1172-
current.getModuleName(), current.isNotRecommended(),
1173-
current.getNotRecommendedReason(), current.getBriefDocComment(),
1174-
current.getAssociatedUSRs(), current.getDeclKeywords(),
1175-
typeRelation, opKind);
1172+
current.getModuleName(), current.getNotRecommendedReason(),
1173+
current.getBriefDocComment(), current.getAssociatedUSRs(),
1174+
current.getDeclKeywords(), typeRelation, opKind);
11761175
} else {
11771176
base = SwiftResult(current.getKind(), semanticContext,
11781177
current.getNumBytesToErase(), completionString,

0 commit comments

Comments
 (0)