Skip to content

Commit c452f96

Browse files
committed
[CodeCompletion] Add a field NotRecommendedReason to code completion result to indicate why an item is not recommended, NFC. rdar://25415947
As implied in rdar://24818863, striking through a module name may be an overkill to suggest the module is redundant to import. We try to fine-grain not-recommended-reason so that proper UI cue can be adopted in the future.
1 parent fc9e493 commit c452f96

File tree

6 files changed

+53
-17
lines changed

6 files changed

+53
-17
lines changed

include/swift/IDE/CodeCompletion.h

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -495,11 +495,21 @@ class CodeCompletionResult {
495495
Identical,
496496
};
497497

498+
enum NotRecommendedReason {
499+
500+
Redundant,
501+
502+
TypeMismatch,
503+
504+
NoReason,
505+
};
506+
498507
private:
499508
unsigned Kind : 2;
500509
unsigned AssociatedKind : 8;
501510
unsigned SemanticContext : 3;
502511
unsigned NotRecommended : 1;
512+
unsigned NotRecReason : 3;
503513

504514
/// The number of bytes to the left of the code completion point that
505515
/// should be erased first if this completion string is inserted in the
@@ -527,8 +537,9 @@ class CodeCompletionResult {
527537
CodeCompletionString *CompletionString,
528538
ExpectedTypeRelation TypeDistance = Unrelated)
529539
: Kind(Kind), SemanticContext(unsigned(SemanticContext)),
530-
NotRecommended(false), NumBytesToErase(NumBytesToErase),
531-
CompletionString(CompletionString), TypeDistance(TypeDistance) {
540+
NotRecommended(false), NotRecReason(NotRecommendedReason::NoReason),
541+
NumBytesToErase(NumBytesToErase), CompletionString(CompletionString),
542+
TypeDistance(TypeDistance) {
532543
assert(Kind != Declaration && "use the other constructor");
533544
assert(CompletionString);
534545
AssociatedKind = 0;
@@ -543,8 +554,9 @@ class CodeCompletionResult {
543554
CodeCompletionString *CompletionString,
544555
ExpectedTypeRelation TypeDistance = Unrelated)
545556
: Kind(Keyword), SemanticContext(unsigned(SemanticContext)),
546-
NotRecommended(false), NumBytesToErase(NumBytesToErase),
547-
CompletionString(CompletionString), TypeDistance(TypeDistance) {
557+
NotRecommended(false), NotRecReason(NotRecommendedReason::NoReason),
558+
NumBytesToErase(NumBytesToErase), CompletionString(CompletionString),
559+
TypeDistance(TypeDistance) {
548560
assert(CompletionString);
549561
AssociatedKind = static_cast<unsigned>(Kind);
550562
}
@@ -558,8 +570,9 @@ class CodeCompletionResult {
558570
CodeCompletionString *CompletionString,
559571
ExpectedTypeRelation TypeDistance)
560572
: Kind(Literal), SemanticContext(unsigned(SemanticContext)),
561-
NotRecommended(false), NumBytesToErase(NumBytesToErase),
562-
CompletionString(CompletionString), TypeDistance(TypeDistance) {
573+
NotRecommended(false), NotRecReason(NotRecommendedReason::NoReason),
574+
NumBytesToErase(NumBytesToErase), CompletionString(CompletionString),
575+
TypeDistance(TypeDistance) {
563576
AssociatedKind = static_cast<unsigned>(LiteralKind);
564577
assert(CompletionString);
565578
}
@@ -573,13 +586,16 @@ class CodeCompletionResult {
573586
unsigned NumBytesToErase,
574587
CodeCompletionString *CompletionString,
575588
const Decl *AssociatedDecl, StringRef ModuleName,
576-
bool NotRecommended, StringRef BriefDocComment,
589+
bool NotRecommended,
590+
CodeCompletionResult::NotRecommendedReason NotRecReason,
591+
StringRef BriefDocComment,
577592
ArrayRef<StringRef> AssociatedUSRs,
578593
ArrayRef<std::pair<StringRef, StringRef>> DocWords,
579594
enum ExpectedTypeRelation TypeDistance)
580595
: Kind(ResultKind::Declaration),
581596
SemanticContext(unsigned(SemanticContext)),
582-
NotRecommended(NotRecommended), NumBytesToErase(NumBytesToErase),
597+
NotRecommended(NotRecommended), NotRecReason(NotRecReason),
598+
NumBytesToErase(NumBytesToErase),
583599
CompletionString(CompletionString), ModuleName(ModuleName),
584600
BriefDocComment(BriefDocComment), AssociatedUSRs(AssociatedUSRs),
585601
DocWords(DocWords), TypeDistance(TypeDistance) {
@@ -593,12 +609,15 @@ class CodeCompletionResult {
593609
unsigned NumBytesToErase,
594610
CodeCompletionString *CompletionString,
595611
CodeCompletionDeclKind DeclKind, StringRef ModuleName,
596-
bool NotRecommended, StringRef BriefDocComment,
612+
bool NotRecommended,
613+
CodeCompletionResult::NotRecommendedReason NotRecReason,
614+
StringRef BriefDocComment,
597615
ArrayRef<StringRef> AssociatedUSRs,
598616
ArrayRef<std::pair<StringRef, StringRef>> DocWords)
599617
: Kind(ResultKind::Declaration),
600618
SemanticContext(unsigned(SemanticContext)),
601-
NotRecommended(NotRecommended), NumBytesToErase(NumBytesToErase),
619+
NotRecommended(NotRecommended), NotRecReason(NotRecReason),
620+
NumBytesToErase(NumBytesToErase),
602621
CompletionString(CompletionString), ModuleName(ModuleName),
603622
BriefDocComment(BriefDocComment), AssociatedUSRs(AssociatedUSRs),
604623
DocWords(DocWords) {
@@ -641,6 +660,10 @@ class CodeCompletionResult {
641660
return static_cast<ExpectedTypeRelation>(TypeDistance);
642661
}
643662

663+
NotRecommendedReason getNotRecommendedReason() const {
664+
return static_cast<NotRecommendedReason>(NotRecReason);
665+
}
666+
644667
SemanticContextKind getSemanticContext() const {
645668
return static_cast<SemanticContextKind>(SemanticContext);
646669
}

lib/IDE/CodeCompletion.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -899,12 +899,14 @@ CodeCompletionResult *CodeCompletionResultBuilder::takeResult() {
899899
typeRelation =
900900
calculateMaxTypeRelationForDecl(AssociatedDecl, ExpectedDeclTypes);
901901

902-
if (typeRelation == CodeCompletionResult::Invalid)
902+
if (typeRelation == CodeCompletionResult::Invalid) {
903903
IsNotRecommended = true;
904+
NotRecReason = CodeCompletionResult::NotRecommendedReason::TypeMismatch;
905+
}
904906

905907
return new (*Sink.Allocator) CodeCompletionResult(
906908
SemanticContext, NumBytesToErase, CCS, AssociatedDecl, ModuleName,
907-
/*NotRecommended=*/IsNotRecommended,
909+
/*NotRecommended=*/IsNotRecommended, NotRecReason,
908910
copyString(*Sink.Allocator, BriefComment),
909911
copyAssociatedUSRs(*Sink.Allocator, AssociatedDecl),
910912
copyStringPairArray(*Sink.Allocator, CommentWords), typeRelation);
@@ -1603,7 +1605,9 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
16031605
Builder.setAssociatedDecl(MD);
16041606
Builder.addTextChunk(MD->getNameStr());
16051607
Builder.addTypeAnnotation("Module");
1606-
Builder.setNotRecommended(Pair.second);
1608+
if (Pair.second)
1609+
Builder.setNotRecommended(CodeCompletionResult::NotRecommendedReason::
1610+
Redundant);
16071611
}
16081612
}
16091613

@@ -1654,7 +1658,9 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
16541658
Builder.addTypeAnnotation("Module");
16551659

16561660
// Imported modules are not recommended.
1657-
Builder.setNotRecommended(ImportedModules.count(MD->getNameStr()) != 0);
1661+
if (ImportedModules.count(MD->getNameStr()) != 0)
1662+
Builder.setNotRecommended(CodeCompletionResult::NotRecommendedReason::
1663+
Redundant);
16581664
}
16591665
}
16601666
}

lib/IDE/CodeCompletionCache.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,9 @@ static bool readCachedModule(llvm::MemoryBuffer *in,
234234
if (kind == CodeCompletionResult::Declaration) {
235235
result = new (*V.Sink.Allocator)
236236
CodeCompletionResult(context, numBytesToErase, string, declKind,
237-
moduleName, notRecommended, briefDocComment,
237+
moduleName, notRecommended,
238+
CodeCompletionResult::NotRecommendedReason::NoReason,
239+
briefDocComment,
238240
copyStringArray(*V.Sink.Allocator, assocUSRs),
239241
copyStringPairArray(*V.Sink.Allocator, declKeywords));
240242
} else {

lib/IDE/CodeCompletionResultBuilder.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ class CodeCompletionResultBuilder {
4747
bool Cancelled = false;
4848
ArrayRef<std::pair<StringRef, StringRef>> CommentWords;
4949
bool IsNotRecommended = false;
50+
CodeCompletionResult::NotRecommendedReason NotRecReason =
51+
CodeCompletionResult::NotRecommendedReason::NoReason;
5052

5153
void addChunkWithText(CodeCompletionString::Chunk::ChunkKind Kind,
5254
StringRef Text);
@@ -94,8 +96,9 @@ class CodeCompletionResultBuilder {
9496

9597
void setLiteralKind(CodeCompletionLiteralKind kind) { LiteralKind = kind; }
9698
void setKeywordKind(CodeCompletionKeywordKind kind) { KeywordKind = kind; }
97-
void setNotRecommended(bool NotRecommended = true) {
98-
IsNotRecommended = NotRecommended;
99+
void setNotRecommended(CodeCompletionResult::NotRecommendedReason Reason) {
100+
IsNotRecommended = true;
101+
NotRecReason = Reason;
99102
}
100103

101104
void

tools/SourceKit/lib/SwiftLang/CodeCompletionOrganizer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,6 +1159,7 @@ Completion *CompletionBuilder::finish() {
11591159
base = SwiftResult(semanticContext, current.getNumBytesToErase(),
11601160
completionString, current.getAssociatedDeclKind(),
11611161
current.getModuleName(), current.isNotRecommended(),
1162+
current.getNotRecommendedReason(),
11621163
current.getBriefDocComment(),
11631164
current.getAssociatedUSRs(),
11641165
current.getDeclKeywords());

tools/SourceKit/lib/SwiftLang/SwiftCompletion.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,7 @@ bool SwiftToSourceKitCompletionAdapter::handleResult(
521521
Info.ModuleName = Result->getModuleName();
522522
Info.DocBrief = Result->getBriefDocComment();
523523
Info.NotRecommended = Result->isNotRecommended();
524+
524525
Info.NumBytesToErase = Result->getNumBytesToErase();
525526

526527
// Extended result values.

0 commit comments

Comments
 (0)