@@ -2246,37 +2246,29 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
2246
2246
// #selector is only available when the Objective-C runtime is.
2247
2247
if (!Ctx.LangOpts .EnableObjCInterop ) return ;
2248
2248
2249
- // After #, this is a very likely result. When just in a String context,
2250
- // it's not.
2251
- auto semanticContext = needPound ? SemanticContextKind::None
2252
- : SemanticContextKind::ExpressionSpecific;
2253
-
2254
2249
CodeCompletionResultBuilder Builder (
2255
- Sink,
2256
- CodeCompletionResult::ResultKind::Keyword,
2257
- semanticContext, expectedTypeContext);
2250
+ Sink, CodeCompletionResult::ResultKind::Keyword,
2251
+ SemanticContextKind::None, {});
2258
2252
if (needPound)
2259
2253
Builder.addTextChunk (" #selector" );
2260
2254
else
2261
2255
Builder.addTextChunk (" selector" );
2262
2256
Builder.addLeftParen ();
2263
2257
Builder.addSimpleTypedParameter (" @objc method" , /* IsVarArg=*/ false );
2264
2258
Builder.addRightParen ();
2259
+ Builder.addTypeAnnotation (" Selector" );
2260
+ // This function is called only if the context type is 'Selector'.
2261
+ Builder.setExpectedTypeRelation (
2262
+ CodeCompletionResult::ExpectedTypeRelation::Identical);
2265
2263
}
2266
2264
2267
2265
void addPoundKeyPath (bool needPound) {
2268
2266
// #keyPath is only available when the Objective-C runtime is.
2269
2267
if (!Ctx.LangOpts .EnableObjCInterop ) return ;
2270
2268
2271
- // After #, this is a very likely result. When just in a String context,
2272
- // it's not.
2273
- auto semanticContext = needPound ? SemanticContextKind::None
2274
- : SemanticContextKind::ExpressionSpecific;
2275
-
2276
2269
CodeCompletionResultBuilder Builder (
2277
- Sink,
2278
- CodeCompletionResult::ResultKind::Keyword,
2279
- semanticContext, expectedTypeContext);
2270
+ Sink, CodeCompletionResult::ResultKind::Keyword,
2271
+ SemanticContextKind::None, {});
2280
2272
if (needPound)
2281
2273
Builder.addTextChunk (" #keyPath" );
2282
2274
else
@@ -2285,6 +2277,10 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
2285
2277
Builder.addSimpleTypedParameter (" @objc property sequence" ,
2286
2278
/* IsVarArg=*/ false );
2287
2279
Builder.addRightParen ();
2280
+ Builder.addTypeAnnotation (" String" );
2281
+ // This function is called only if the context type is 'String'.
2282
+ Builder.setExpectedTypeRelation (
2283
+ CodeCompletionResult::ExpectedTypeRelation::Identical);
2288
2284
}
2289
2285
2290
2286
SemanticContextKind getSemanticContextKind (const ValueDecl *VD) {
@@ -3679,6 +3675,38 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
3679
3675
}
3680
3676
}
3681
3677
3678
+ void addObjCPoundKeywordCompletions (bool needPound) {
3679
+ if (!Ctx.LangOpts .EnableObjCInterop )
3680
+ return ;
3681
+
3682
+ // If the expected type is ObjectiveC.Selector, add #selector. If
3683
+ // it's String, add #keyPath.
3684
+ bool addedSelector = false ;
3685
+ bool addedKeyPath = false ;
3686
+
3687
+ for (auto T : expectedTypeContext.possibleTypes ) {
3688
+ T = T->lookThroughAllOptionalTypes ();
3689
+ if (auto structDecl = T->getStructOrBoundGenericStruct ()) {
3690
+ if (!addedSelector && structDecl->getName () == Ctx.Id_Selector &&
3691
+ structDecl->getParentModule ()->getName () == Ctx.Id_ObjectiveC ) {
3692
+ addPoundSelector (needPound);
3693
+ if (addedKeyPath)
3694
+ break ;
3695
+ addedSelector = true ;
3696
+ continue ;
3697
+ }
3698
+ }
3699
+
3700
+ if (!addedKeyPath && T->getAnyNominal () == Ctx.getStringDecl ()) {
3701
+ addPoundKeyPath (needPound);
3702
+ if (addedSelector)
3703
+ break ;
3704
+ addedKeyPath = true ;
3705
+ continue ;
3706
+ }
3707
+ }
3708
+ }
3709
+
3682
3710
struct FilteredDeclConsumer : public swift ::VisibleDeclConsumer {
3683
3711
swift::VisibleDeclConsumer &Consumer;
3684
3712
DeclFilter Filter;
@@ -3742,33 +3770,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
3742
3770
addPoundLiteralCompletions (/* needPound=*/ true );
3743
3771
}
3744
3772
3745
-
3746
- // If the expected type is ObjectiveC.Selector, add #selector. If
3747
- // it's String, add #keyPath.
3748
- if (Ctx.LangOpts .EnableObjCInterop ) {
3749
- bool addedSelector = false ;
3750
- bool addedKeyPath = false ;
3751
- for (auto T : expectedTypeContext.possibleTypes ) {
3752
- T = T->lookThroughAllOptionalTypes ();
3753
- if (auto structDecl = T->getStructOrBoundGenericStruct ()) {
3754
- if (!addedSelector &&
3755
- structDecl->getName () == Ctx.Id_Selector &&
3756
- structDecl->getParentModule ()->getName () == Ctx.Id_ObjectiveC ) {
3757
- addPoundSelector (/* needPound=*/ true );
3758
- if (addedKeyPath) break ;
3759
- addedSelector = true ;
3760
- continue ;
3761
- }
3762
- }
3763
-
3764
- if (!addedKeyPath && T->getAnyNominal () == Ctx.getStringDecl ()) {
3765
- addPoundKeyPath (/* needPound=*/ true );
3766
- if (addedSelector) break ;
3767
- addedKeyPath = true ;
3768
- continue ;
3769
- }
3770
- }
3771
- }
3773
+ addObjCPoundKeywordCompletions (/* needPound=*/ true );
3772
3774
}
3773
3775
3774
3776
void getUnresolvedMemberCompletions (Type T) {
@@ -5475,8 +5477,7 @@ void CodeCompletionCallbacksImpl::doneParsing() {
5475
5477
5476
5478
Lookup.addPoundAvailable (ParentStmtKind);
5477
5479
Lookup.addPoundLiteralCompletions (/* needPound=*/ false );
5478
- Lookup.addPoundSelector (/* needPound=*/ false );
5479
- Lookup.addPoundKeyPath (/* needPound=*/ false );
5480
+ Lookup.addObjCPoundKeywordCompletions (/* needPound=*/ false );
5480
5481
break ;
5481
5482
}
5482
5483
0 commit comments