@@ -1086,6 +1086,7 @@ class CodeCompletionCallbacksImpl : public CodeCompletionCallbacks {
1086
1086
bool HasSpace = false ;
1087
1087
bool HasRParen = false ;
1088
1088
bool ShouldCompleteCallPatternAfterParen = true ;
1089
+ bool PreferFunctionReferencesToCalls = false ;
1089
1090
Optional<DeclKind> AttTargetDK;
1090
1091
1091
1092
SmallVector<StringRef, 3 > ParsedKeywords;
@@ -1419,6 +1420,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
1419
1420
bool HaveRParen = false ;
1420
1421
bool IsSuperRefExpr = false ;
1421
1422
bool IsDynamicLookup = false ;
1423
+ bool PreferFunctionReferencesToCalls = false ;
1422
1424
bool HaveLeadingSpace = false ;
1423
1425
1424
1426
// / \brief True if we are code completing inside a static method.
@@ -1566,6 +1568,10 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
1566
1568
IsDynamicLookup = true ;
1567
1569
}
1568
1570
1571
+ void setPreferFunctionReferencesToCalls () {
1572
+ PreferFunctionReferencesToCalls = true ;
1573
+ }
1574
+
1569
1575
void setHaveLeadingSpace (bool value) { HaveLeadingSpace = value; }
1570
1576
1571
1577
void addExpressionSpecificDecl (const Decl *D) {
@@ -1985,7 +1991,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
1985
1991
Builder.addRightParen ();
1986
1992
}
1987
1993
1988
- void addPoundSelector () {
1994
+ void addPoundSelector (bool needPound ) {
1989
1995
// #selector is only available when the Objective-C runtime is.
1990
1996
if (!Ctx.LangOpts .EnableObjCInterop ) return ;
1991
1997
@@ -1994,7 +2000,10 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
1994
2000
CodeCompletionResult::ResultKind::Keyword,
1995
2001
SemanticContextKind::ExpressionSpecific,
1996
2002
ExpectedTypes);
1997
- Builder.addTextChunk (" selector" );
2003
+ if (needPound)
2004
+ Builder.addTextChunk (" #selector" );
2005
+ else
2006
+ Builder.addTextChunk (" selector" );
1998
2007
Builder.addLeftParen ();
1999
2008
Builder.addSimpleTypedParameter (" @objc method" , /* isVarArg=*/ false );
2000
2009
Builder.addRightParen ();
@@ -2399,6 +2408,43 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
2399
2408
Builder.addDeclAttrKeyword (Name, Annotation);
2400
2409
}
2401
2410
2411
+ // / Add the compound function name for the given function.
2412
+ void addCompoundFunctionName (AbstractFunctionDecl *AFD,
2413
+ DeclVisibilityKind Reason) {
2414
+ CommandWordsPairs Pairs;
2415
+ CodeCompletionResultBuilder Builder (
2416
+ Sink, CodeCompletionResult::ResultKind::Declaration,
2417
+ getSemanticContext (AFD, Reason), ExpectedTypes);
2418
+ setClangDeclKeywords (AFD, Pairs, Builder);
2419
+ Builder.setAssociatedDecl (AFD);
2420
+
2421
+ // Base name
2422
+ addLeadingDot (Builder);
2423
+ Builder.addTextChunk (AFD->getFullName ().getBaseName ().str ());
2424
+
2425
+ // Add the argument labels.
2426
+ auto ArgLabels = AFD->getFullName ().getArgumentNames ();
2427
+ if (ArgLabels.size () > 0 ) {
2428
+ if (!HaveLParen)
2429
+ Builder.addLeftParen ();
2430
+ else
2431
+ Builder.addAnnotatedLeftParen ();
2432
+
2433
+ for (auto ArgLabel : ArgLabels) {
2434
+ if (ArgLabel.empty ())
2435
+ Builder.addTextChunk (" _" );
2436
+ else
2437
+ Builder.addTextChunk (ArgLabel.str ());
2438
+ Builder.addTextChunk (" :" );
2439
+ }
2440
+
2441
+ if (!HaveRParen)
2442
+ Builder.addRightParen ();
2443
+ else
2444
+ Builder.addAnnotatedRightParen ();
2445
+ }
2446
+ }
2447
+
2402
2448
// Implement swift::VisibleDeclConsumer.
2403
2449
void foundDecl (ValueDecl *D, DeclVisibilityKind Reason) override {
2404
2450
// Hide private stdlib declarations.
@@ -2421,6 +2467,12 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
2421
2467
switch (Kind) {
2422
2468
case LookupKind::ValueExpr:
2423
2469
if (auto *CD = dyn_cast<ConstructorDecl>(D)) {
2470
+ // Do we want compound function names here?
2471
+ if (PreferFunctionReferencesToCalls) {
2472
+ addCompoundFunctionName (CD, Reason);
2473
+ return ;
2474
+ }
2475
+
2424
2476
if (auto MT = ExprType->getRValueType ()->getAs <AnyMetatypeType>()) {
2425
2477
if (HaveDot) {
2426
2478
Type Ty;
@@ -2478,6 +2530,12 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
2478
2530
if (FD->isAccessor ())
2479
2531
return ;
2480
2532
2533
+ // Do we want compound function names here?
2534
+ if (PreferFunctionReferencesToCalls) {
2535
+ addCompoundFunctionName (FD, Reason);
2536
+ return ;
2537
+ }
2538
+
2481
2539
addMethodCall (FD, Reason);
2482
2540
return ;
2483
2541
}
@@ -3152,6 +3210,19 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
3152
3210
}
3153
3211
3154
3212
addValueLiteralCompletions ();
3213
+
3214
+ // If the expected type is ObjectiveC.Selector, add #selector.
3215
+ if (Ctx.LangOpts .EnableObjCInterop ) {
3216
+ for (auto T : ExpectedTypes) {
3217
+ if (auto structDecl = T->getStructOrBoundGenericStruct ()) {
3218
+ if (structDecl->getName () == Ctx.Id_Selector &&
3219
+ structDecl->getParentModule ()->getName () == Ctx.Id_ObjectiveC ) {
3220
+ addPoundSelector (/* needPound=*/ true );
3221
+ break ;
3222
+ }
3223
+ }
3224
+ }
3225
+ }
3155
3226
}
3156
3227
3157
3228
struct LookupByName : public swift ::VisibleDeclConsumer {
@@ -3743,6 +3814,9 @@ void CodeCompletionCallbacksImpl::completeDotExpr(Expr *E, SourceLoc DotLoc) {
3743
3814
return ;
3744
3815
3745
3816
Kind = CompletionKind::DotExpr;
3817
+ if (InObjCSelectorExpr)
3818
+ PreferFunctionReferencesToCalls = true ;
3819
+
3746
3820
ParsedExpr = E;
3747
3821
this ->DotLoc = DotLoc;
3748
3822
CurDeclContext = P.CurDeclContext ;
@@ -3764,6 +3838,9 @@ void CodeCompletionCallbacksImpl::completePostfixExprBeginning(CodeCompletionExp
3764
3838
return ;
3765
3839
3766
3840
Kind = CompletionKind::PostfixExprBeginning;
3841
+ if (InObjCSelectorExpr)
3842
+ PreferFunctionReferencesToCalls = true ;
3843
+
3767
3844
CurDeclContext = P.CurDeclContext ;
3768
3845
CStyleForLoopIterationVariable =
3769
3846
CodeCompletionCallbacks::CStyleForLoopIterationVariable;
@@ -3779,6 +3856,9 @@ void CodeCompletionCallbacksImpl::completePostfixExpr(Expr *E, bool hasSpace) {
3779
3856
3780
3857
HasSpace = hasSpace;
3781
3858
Kind = CompletionKind::PostfixExpr;
3859
+ if (InObjCSelectorExpr)
3860
+ PreferFunctionReferencesToCalls = true ;
3861
+
3782
3862
ParsedExpr = E;
3783
3863
CurDeclContext = P.CurDeclContext ;
3784
3864
}
@@ -3817,6 +3897,9 @@ void CodeCompletionCallbacksImpl::completeExprSuper(SuperRefExpr *SRE) {
3817
3897
return ;
3818
3898
3819
3899
Kind = CompletionKind::SuperExpr;
3900
+ if (InObjCSelectorExpr)
3901
+ PreferFunctionReferencesToCalls = true ;
3902
+
3820
3903
ParsedExpr = SRE;
3821
3904
CurDeclContext = P.CurDeclContext ;
3822
3905
}
@@ -3827,6 +3910,9 @@ void CodeCompletionCallbacksImpl::completeExprSuperDot(SuperRefExpr *SRE) {
3827
3910
return ;
3828
3911
3829
3912
Kind = CompletionKind::SuperExprDot;
3913
+ if (InObjCSelectorExpr)
3914
+ PreferFunctionReferencesToCalls = true ;
3915
+
3830
3916
ParsedExpr = SRE;
3831
3917
CurDeclContext = P.CurDeclContext ;
3832
3918
}
@@ -4360,6 +4446,8 @@ void CodeCompletionCallbacksImpl::doneParsing() {
4360
4446
if (ExprType) {
4361
4447
Lookup.setIsStaticMetatype (ParsedExpr->isStaticallyDerivedMetatype ());
4362
4448
}
4449
+ if (PreferFunctionReferencesToCalls)
4450
+ Lookup.setPreferFunctionReferencesToCalls ();
4363
4451
4364
4452
auto DoPostfixExprBeginning = [&] (){
4365
4453
if (CStyleForLoopIterationVariable)
@@ -4584,7 +4672,7 @@ void CodeCompletionCallbacksImpl::doneParsing() {
4584
4672
4585
4673
case CompletionKind::AfterPound: {
4586
4674
Lookup.addPoundAvailable (ParentStmtKind);
4587
- Lookup.addPoundSelector ();
4675
+ Lookup.addPoundSelector (/* needPound= */ false );
4588
4676
break ;
4589
4677
}
4590
4678
}
0 commit comments