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