@@ -1379,7 +1379,7 @@ class CodeCompletionCallbacksImpl : public CodeCompletionCallbacks {
1379
1379
void completeUnresolvedMember (CodeCompletionExpr *E,
1380
1380
SourceLoc DotLoc) override ;
1381
1381
void completeAssignmentRHS (AssignExpr *E) override ;
1382
- void completeCallArg (CodeCompletionExpr *E) override ;
1382
+ void completeCallArg (CodeCompletionExpr *E, bool isFirst ) override ;
1383
1383
void completeReturnStmt (CodeCompletionExpr *E) override ;
1384
1384
void completeYieldStmt (CodeCompletionExpr *E,
1385
1385
Optional<unsigned > yieldIndex) override ;
@@ -3710,81 +3710,14 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
3710
3710
return ;
3711
3711
3712
3712
ModuleDecl *CurrModule = CurrDeclContext->getParentModule ();
3713
+ DeclContext *DC = const_cast <DeclContext *>(CurrDeclContext);
3713
3714
3714
3715
// We can only say .foo where foo is a static member of the contextual
3715
3716
// type and has the same type (or if the member is a function, then the
3716
3717
// same result type) as the contextual type.
3717
3718
FilteredDeclConsumer consumer (*this , [=](ValueDecl *VD,
3718
3719
DeclVisibilityKind Reason) {
3719
-
3720
- if (VD->isOperator ())
3721
- return false ;
3722
-
3723
- if (!VD->hasInterfaceType ()) {
3724
- TypeResolver->resolveDeclSignature (VD);
3725
- if (!VD->hasInterfaceType ())
3726
- return false ;
3727
- }
3728
-
3729
- if (T->getOptionalObjectType () &&
3730
- VD->getModuleContext ()->isStdlibModule ()) {
3731
- // In optional context, ignore '.init(<some>)', 'init(nilLiteral:)',
3732
- if (isa<ConstructorDecl>(VD))
3733
- return false ;
3734
- // TODO: Ignore '.some(<Wrapped>)' and '.none' too *in expression
3735
- // context*. They are useful in pattern context though.
3736
- }
3737
-
3738
- // Enum element decls can always be referenced by implicit member
3739
- // expression.
3740
- if (isa<EnumElementDecl>(VD))
3741
- return true ;
3742
-
3743
- // Only non-failable constructors are implicitly referenceable.
3744
- if (auto CD = dyn_cast<ConstructorDecl>(VD)) {
3745
- switch (CD->getFailability ()) {
3746
- case OTK_None:
3747
- case OTK_ImplicitlyUnwrappedOptional:
3748
- return true ;
3749
- case OTK_Optional:
3750
- return false ;
3751
- }
3752
- }
3753
-
3754
- // Otherwise, check the result type matches the contextual type.
3755
- auto declTy = T->getTypeOfMember (CurrModule, VD);
3756
- if (declTy->is <ErrorType>())
3757
- return false ;
3758
-
3759
- DeclContext *DC = const_cast <DeclContext *>(CurrDeclContext);
3760
-
3761
- // Member types can also be implicitly referenceable as long as it's
3762
- // convertible to the contextual type.
3763
- if (auto CD = dyn_cast<TypeDecl>(VD)) {
3764
- declTy = declTy->getMetatypeInstanceType ();
3765
-
3766
- // Emit construction for the same type via typealias doesn't make sense
3767
- // because we are emitting all `.init()`s.
3768
- if (declTy->isEqual (T))
3769
- return false ;
3770
- return swift::isConvertibleTo (declTy, T, *DC);
3771
- }
3772
-
3773
- // Only static member can be referenced.
3774
- if (!VD->isStatic ())
3775
- return false ;
3776
-
3777
- if (isa<FuncDecl>(VD)) {
3778
- // Strip '(Self.Type) ->' and parameters.
3779
- declTy = declTy->castTo <AnyFunctionType>()->getResult ();
3780
- declTy = declTy->castTo <AnyFunctionType>()->getResult ();
3781
- } else if (auto FT = declTy->getAs <AnyFunctionType>()) {
3782
- // The compiler accepts 'static var factory: () -> T' for implicit
3783
- // member expression.
3784
- // FIXME: This emits just 'factory'. We should emit 'factory()' instead.
3785
- declTy = FT->getResult ();
3786
- }
3787
- return declTy->isEqual (T) || swift::isConvertibleTo (declTy, T, *DC);
3720
+ return isReferenceableByImplicitMemberExpr (CurrModule, DC, T, VD);
3788
3721
});
3789
3722
3790
3723
auto baseType = MetatypeType::get (T);
@@ -4700,10 +4633,26 @@ void CodeCompletionCallbacksImpl::completeAssignmentRHS(AssignExpr *E) {
4700
4633
Kind = CompletionKind::AssignmentRHS;
4701
4634
}
4702
4635
4703
- void CodeCompletionCallbacksImpl::completeCallArg (CodeCompletionExpr *E) {
4636
+ void CodeCompletionCallbacksImpl::completeCallArg (CodeCompletionExpr *E,
4637
+ bool isFirst) {
4704
4638
CurDeclContext = P.CurDeclContext ;
4705
4639
CodeCompleteTokenExpr = E;
4706
4640
Kind = CompletionKind::CallArg;
4641
+
4642
+ ShouldCompleteCallPatternAfterParen = false ;
4643
+ if (isFirst) {
4644
+ ShouldCompleteCallPatternAfterParen = true ;
4645
+ if (Context.LangOpts .CodeCompleteCallPatternHeuristics ) {
4646
+ // Lookahead one token to decide what kind of call completions to provide.
4647
+ // When it appears that there is already code for the call present, just
4648
+ // complete values and/or argument labels. Otherwise give the entire call
4649
+ // pattern.
4650
+ Token next = P.peekToken ();
4651
+ if (!next.isAtStartOfLine () && !next.is (tok::eof) && !next.is (tok::r_paren)) {
4652
+ ShouldCompleteCallPatternAfterParen = false ;
4653
+ }
4654
+ }
4655
+ }
4707
4656
}
4708
4657
4709
4658
void CodeCompletionCallbacksImpl::completeReturnStmt (CodeCompletionExpr *E) {
@@ -5408,13 +5357,32 @@ void CodeCompletionCallbacksImpl::doneParsing() {
5408
5357
}
5409
5358
case CompletionKind::CallArg : {
5410
5359
ExprContextInfo ContextInfo (CurDeclContext, CodeCompleteTokenExpr);
5411
- if (!ContextInfo.getPossibleNames ().empty ()) {
5360
+
5361
+ bool shouldPerformGlobalCompletion = true ;
5362
+
5363
+ if (ShouldCompleteCallPatternAfterParen &&
5364
+ !ContextInfo.getPossibleCallees ().empty ()) {
5365
+ Lookup.setHaveLParen (true );
5366
+ for (auto &typeAndDecl : ContextInfo.getPossibleCallees ())
5367
+ Lookup.tryFunctionCallCompletions (typeAndDecl.first ,
5368
+ typeAndDecl.second );
5369
+ Lookup.setHaveLParen (false );
5370
+
5371
+ shouldPerformGlobalCompletion =
5372
+ !Lookup.FoundFunctionCalls ||
5373
+ (Lookup.FoundFunctionCalls &&
5374
+ Lookup.FoundFunctionsWithoutFirstKeyword );
5375
+ } else if (!ContextInfo.getPossibleNames ().empty ()) {
5412
5376
Lookup.addArgNameCompletionResults (ContextInfo.getPossibleNames ());
5413
- break ;
5377
+
5378
+ shouldPerformGlobalCompletion = !ContextInfo.getPossibleTypes ().empty ();
5379
+ }
5380
+
5381
+ if (shouldPerformGlobalCompletion) {
5382
+ Lookup.setExpectedTypes (ContextInfo.getPossibleTypes (),
5383
+ ContextInfo.isSingleExpressionBody ());
5384
+ DoPostfixExprBeginning ();
5414
5385
}
5415
- Lookup.setExpectedTypes (ContextInfo.getPossibleTypes (),
5416
- ContextInfo.isSingleExpressionBody ());
5417
- DoPostfixExprBeginning ();
5418
5386
break ;
5419
5387
}
5420
5388
0 commit comments