Skip to content

Commit ea1b18f

Browse files
authored
Merge pull request swiftlang#29667 from rintaro/ide-completion-typerelation-rdar59201827
[CodeCompletion] Introduce 'NotApplicable' and 'Unknown' type relation
2 parents 4a97914 + cc8d496 commit ea1b18f

14 files changed

+189
-68
lines changed

include/swift/IDE/CodeCompletion.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,11 @@ class CodeCompletionResult {
527527
/// Describes the relationship between the type of the completion results and
528528
/// the expected type at the code completion position.
529529
enum ExpectedTypeRelation {
530+
/// The result does not have a type (e.g. keyword).
531+
NotApplicable,
532+
533+
/// The type relation have not been calculated.
534+
Unknown,
530535

531536
/// The relationship of the result's type to the expected type is not
532537
/// invalid, not convertible, and not identical.
@@ -580,7 +585,7 @@ class CodeCompletionResult {
580585
CodeCompletionResult(ResultKind Kind, SemanticContextKind SemanticContext,
581586
unsigned NumBytesToErase,
582587
CodeCompletionString *CompletionString,
583-
ExpectedTypeRelation TypeDistance = Unrelated,
588+
ExpectedTypeRelation TypeDistance,
584589
CodeCompletionOperatorKind KnownOperatorKind =
585590
CodeCompletionOperatorKind::None)
586591
: Kind(Kind), KnownOperatorKind(unsigned(KnownOperatorKind)),
@@ -605,7 +610,7 @@ class CodeCompletionResult {
605610
SemanticContextKind SemanticContext,
606611
unsigned NumBytesToErase,
607612
CodeCompletionString *CompletionString,
608-
ExpectedTypeRelation TypeDistance = Unrelated)
613+
ExpectedTypeRelation TypeDistance)
609614
: Kind(Keyword), KnownOperatorKind(0),
610615
SemanticContext(unsigned(SemanticContext)), NotRecommended(false),
611616
NotRecReason(NotRecommendedReason::NoReason),
@@ -684,7 +689,7 @@ class CodeCompletionResult {
684689
AssociatedUSRs(AssociatedUSRs), DocWords(DocWords) {
685690
AssociatedKind = static_cast<unsigned>(DeclKind);
686691
assert(CompletionString);
687-
TypeDistance = ExpectedTypeRelation::Unrelated;
692+
TypeDistance = ExpectedTypeRelation::Unknown;
688693
assert(!isOperator() ||
689694
getOperatorKind() != CodeCompletionOperatorKind::None);
690695
}

lib/IDE/CodeCompletion.cpp

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ void CodeCompletionResult::print(raw_ostream &OS) const {
695695
Prefix.append(Twine(NumBytesToErase).str());
696696
Prefix.append("]");
697697
}
698-
switch (TypeDistance) {
698+
switch (getExpectedTypeRelation()) {
699699
case ExpectedTypeRelation::Invalid:
700700
Prefix.append("/TypeRelation[Invalid]");
701701
break;
@@ -705,6 +705,8 @@ void CodeCompletionResult::print(raw_ostream &OS) const {
705705
case ExpectedTypeRelation::Convertible:
706706
Prefix.append("/TypeRelation[Convertible]");
707707
break;
708+
case ExpectedTypeRelation::NotApplicable:
709+
case ExpectedTypeRelation::Unknown:
708710
case ExpectedTypeRelation::Unrelated:
709711
break;
710712
}
@@ -903,6 +905,9 @@ static CodeCompletionResult::ExpectedTypeRelation
903905
calculateMaxTypeRelationForDecl(
904906
const Decl *D, const ExpectedTypeContext &typeContext,
905907
bool IsImplicitlyCurriedInstanceMethod = false) {
908+
if (typeContext.empty())
909+
return CodeCompletionResult::ExpectedTypeRelation::Unknown;
910+
906911
auto Result = CodeCompletionResult::ExpectedTypeRelation::Unrelated;
907912
for (auto Type : typeContext.possibleTypes) {
908913
// Do not use Void type context for a single-expression body, since the
@@ -1034,7 +1039,7 @@ CodeCompletionResult *CodeCompletionResultBuilder::takeResult() {
10341039
}
10351040

10361041
auto typeRelation = ExpectedTypeRelation;
1037-
if (typeRelation == CodeCompletionResult::Unrelated)
1042+
if (typeRelation == CodeCompletionResult::Unknown)
10381043
typeRelation =
10391044
calculateMaxTypeRelationForDecl(AssociatedDecl, declTypeContext);
10401045

@@ -3610,8 +3615,13 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
36103615
}
36113616

36123617
// Fallback to showing the default type.
3613-
if (!defaultTypeName.empty())
3618+
if (!defaultTypeName.empty()) {
36143619
builder.addTypeAnnotation(defaultTypeName);
3620+
builder.setExpectedTypeRelation(
3621+
expectedTypeContext.possibleTypes.empty()
3622+
? CodeCompletionResult::ExpectedTypeRelation::Unknown
3623+
: CodeCompletionResult::ExpectedTypeRelation::Unrelated);
3624+
}
36153625
}
36163626

36173627
/// Add '#file', '#line', et at.
@@ -4308,6 +4318,8 @@ class CompletionOverrideLookup : public swift::VisibleDeclConsumer {
43084318
CodeCompletionResultBuilder Builder(
43094319
Sink, CodeCompletionResult::ResultKind::Declaration,
43104320
SemanticContextKind::Super, {});
4321+
Builder.setExpectedTypeRelation(
4322+
CodeCompletionResult::ExpectedTypeRelation::NotApplicable);
43114323
Builder.setAssociatedDecl(FD);
43124324
addValueOverride(FD, Reason, dynamicLookupInfo, Builder, hasFuncIntroducer);
43134325
Builder.addBraceStmtWithCursor();
@@ -4335,6 +4347,8 @@ class CompletionOverrideLookup : public swift::VisibleDeclConsumer {
43354347
CodeCompletionResultBuilder Builder(
43364348
Sink, CodeCompletionResult::ResultKind::Declaration,
43374349
SemanticContextKind::Super, {});
4350+
Builder.setExpectedTypeRelation(
4351+
CodeCompletionResult::ExpectedTypeRelation::NotApplicable);
43384352
Builder.setAssociatedDecl(SD);
43394353
addValueOverride(SD, Reason, dynamicLookupInfo, Builder, false);
43404354
Builder.addBraceStmtWithCursor();
@@ -4345,6 +4359,8 @@ class CompletionOverrideLookup : public swift::VisibleDeclConsumer {
43454359
CodeCompletionResultBuilder Builder(Sink,
43464360
CodeCompletionResult::ResultKind::Declaration,
43474361
SemanticContextKind::Super, {});
4362+
Builder.setExpectedTypeRelation(
4363+
CodeCompletionResult::ExpectedTypeRelation::NotApplicable);
43484364
Builder.setAssociatedDecl(ATD);
43494365
if (!hasTypealiasIntroducer && !hasAccessModifier)
43504366
addAccessControl(ATD, Builder);
@@ -4361,6 +4377,8 @@ class CompletionOverrideLookup : public swift::VisibleDeclConsumer {
43614377
Sink,
43624378
CodeCompletionResult::ResultKind::Declaration,
43634379
SemanticContextKind::Super, {});
4380+
Builder.setExpectedTypeRelation(
4381+
CodeCompletionResult::ExpectedTypeRelation::NotApplicable);
43644382
Builder.setAssociatedDecl(CD);
43654383

43664384
if (!hasAccessModifier)
@@ -4845,16 +4863,19 @@ static bool isClangSubModule(ModuleDecl *TheModule) {
48454863
return false;
48464864
}
48474865

4848-
static void addKeyword(CodeCompletionResultSink &Sink, StringRef Name,
4849-
CodeCompletionKeywordKind Kind,
4850-
StringRef TypeAnnotation = "") {
4851-
CodeCompletionResultBuilder Builder(
4852-
Sink, CodeCompletionResult::ResultKind::Keyword,
4853-
SemanticContextKind::None, {});
4854-
Builder.setKeywordKind(Kind);
4855-
Builder.addTextChunk(Name);
4856-
if (!TypeAnnotation.empty())
4857-
Builder.addTypeAnnotation(TypeAnnotation);
4866+
static void
4867+
addKeyword(CodeCompletionResultSink &Sink, StringRef Name,
4868+
CodeCompletionKeywordKind Kind, StringRef TypeAnnotation = "",
4869+
CodeCompletionResult::ExpectedTypeRelation TypeRelation =
4870+
CodeCompletionResult::ExpectedTypeRelation::NotApplicable) {
4871+
CodeCompletionResultBuilder Builder(Sink,
4872+
CodeCompletionResult::ResultKind::Keyword,
4873+
SemanticContextKind::None, {});
4874+
Builder.setKeywordKind(Kind);
4875+
Builder.addTextChunk(Name);
4876+
if (!TypeAnnotation.empty())
4877+
Builder.addTypeAnnotation(TypeAnnotation);
4878+
Builder.setExpectedTypeRelation(TypeRelation);
48584879
}
48594880

48604881
static void addDeclKeywords(CodeCompletionResultSink &Sink) {

lib/IDE/CodeCompletionCache.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ static bool readCachedModule(llvm::MemoryBuffer *in,
255255
} else {
256256
result = new (*V.Sink.Allocator)
257257
CodeCompletionResult(kind, context, numBytesToErase, string,
258-
CodeCompletionResult::Unrelated, opKind);
258+
CodeCompletionResult::NotApplicable, opKind);
259259
}
260260

261261
V.Sink.Results.push_back(result);

lib/IDE/CodeCompletionResultBuilder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class CodeCompletionResultBuilder {
6565
CurrentModule;
6666
ExpectedTypeContext declTypeContext;
6767
CodeCompletionResult::ExpectedTypeRelation ExpectedTypeRelation =
68-
CodeCompletionResult::Unrelated;
68+
CodeCompletionResult::Unknown;
6969
bool Cancelled = false;
7070
ArrayRef<std::pair<StringRef, StringRef>> CommentWords;
7171
bool IsNotRecommended = false;

test/SourceKit/CodeComplete/complete_constructor.swift.response

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
key.description: "(arg1: Int, arg2: Int)",
88
key.typename: "Foo",
99
key.context: source.codecompletion.context.thisclass,
10-
key.typerelation: source.codecompletion.typerelation.unrelated,
10+
key.typerelation: source.codecompletion.typerelation.unknown,
1111
key.num_bytes_to_erase: 0,
1212
key.associated_usrs: "s:20complete_constructor3FooC4arg14arg2ACSi_Sitcfc",
1313
key.modulename: "complete_constructor"

test/SourceKit/CodeComplete/complete_from_clang_module.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Foo
99
// CHECK-NEXT: key.typename: "Int32",
1010
// CHECK-NEXT: key.doc.brief: "Aaa. fooIntVar. Bbb.",
1111
// CHECK-NEXT: key.context: source.codecompletion.context.othermodule,
12-
// CHECK-NEXT: key.typerelation: source.codecompletion.typerelation.unrelated,
12+
// CHECK-NEXT: key.typerelation: source.codecompletion.typerelation.unknown,
1313
// CHECK-NEXT: key.num_bytes_to_erase: 0,
1414
// CHECK-NEXT: key.associated_usrs: "c:@fooIntVar",
1515
// CHECK-NEXT: key.modulename: "Foo"

test/SourceKit/CodeComplete/complete_member.swift.response

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
key.description: "self",
4545
key.typename: "FooProtocol",
4646
key.context: source.codecompletion.context.thisclass,
47-
key.typerelation: source.codecompletion.typerelation.unrelated,
47+
key.typerelation: source.codecompletion.typerelation.unknown,
4848
key.num_bytes_to_erase: 0
4949
}
5050
]

test/SourceKit/CodeComplete/complete_optionalmethod.swift.response

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
key.description: "self",
2020
key.typename: "T",
2121
key.context: source.codecompletion.context.thisclass,
22-
key.typerelation: source.codecompletion.typerelation.unrelated,
22+
key.typerelation: source.codecompletion.typerelation.unknown,
2323
key.num_bytes_to_erase: 0
2424
}
2525
]

0 commit comments

Comments
 (0)