@@ -218,6 +218,7 @@ void CodeCompletionString::print(raw_ostream &OS) const {
218
218
case ChunkKind::DeclResultTypeClauseBegin:
219
219
case ChunkKind::ParameterDeclTypeBegin:
220
220
case ChunkKind::AttributeAndModifierListBegin:
221
+ case ChunkKind::CallArgumentDefaultBegin:
221
222
assert (I->getNestingLevel () == PrevNestingLevel + 1 );
222
223
closeTags.emplace_back (" " );
223
224
break ;
@@ -293,6 +294,7 @@ void CodeCompletionString::dump() const {
293
294
CASE (DeclAttrParamColon)
294
295
CASE (CallArgumentType)
295
296
CASE (CallArgumentTypeBegin)
297
+ CASE (CallArgumentDefaultBegin)
296
298
CASE (TypeIdSystem)
297
299
CASE (TypeIdUser)
298
300
CASE (CallArgumentClosureType)
@@ -910,8 +912,8 @@ class CodeCompletionStringPrinter : public ASTPrinter {
910
912
911
913
void CodeCompletionResultBuilder::addCallArgument (
912
914
Identifier Name, Identifier LocalName, Type Ty, Type ContextTy,
913
- bool IsVarArg, bool IsInOut, bool IsIUO, bool isAutoClosure ,
914
- bool useUnderscoreLabel , bool isLabeledTrailingClosure ) {
915
+ bool IsVarArg, bool IsInOut, bool IsIUO, bool IsAutoClosure ,
916
+ bool UseUnderscoreLabel , bool IsLabeledTrailingClosure, bool HasDefault ) {
915
917
++CurrentNestingLevel;
916
918
using ChunkKind = CodeCompletionString::Chunk::ChunkKind;
917
919
@@ -952,7 +954,7 @@ void CodeCompletionResultBuilder::addCallArgument(
952
954
escapeKeyword (Name.str (), false , EscapedKeyword));
953
955
addChunkWithTextNoCopy (
954
956
CodeCompletionString::Chunk::ChunkKind::CallArgumentColon, " : " );
955
- } else if (useUnderscoreLabel ) {
957
+ } else if (UseUnderscoreLabel ) {
956
958
addChunkWithTextNoCopy (
957
959
CodeCompletionString::Chunk::ChunkKind::CallArgumentName, " _" );
958
960
addChunkWithTextNoCopy (
@@ -978,7 +980,7 @@ void CodeCompletionResultBuilder::addCallArgument(
978
980
// If the parameter is of the type @autoclosure ()->output, then the
979
981
// code completion should show the parameter of the output type
980
982
// instead of the function type ()->output.
981
- if (isAutoClosure ) {
983
+ if (IsAutoClosure ) {
982
984
// 'Ty' may be ErrorType.
983
985
if (auto funcTy = Ty->getAs <FunctionType>())
984
986
Ty = funcTy->getResult ();
@@ -1004,6 +1006,12 @@ void CodeCompletionResultBuilder::addCallArgument(
1004
1006
addChunkWithText (ChunkKind::CallArgumentType, TypeName);
1005
1007
}
1006
1008
1009
+ if (HasDefault) {
1010
+ withNestedGroup (ChunkKind::CallArgumentDefaultBegin, []() {
1011
+ // Possibly add the actual value in the future
1012
+ });
1013
+ }
1014
+
1007
1015
// Look through optional types and type aliases to find out if we have
1008
1016
// function type.
1009
1017
Ty = Ty->lookThroughAllOptionalTypes ();
@@ -1020,7 +1028,7 @@ void CodeCompletionResultBuilder::addCallArgument(
1020
1028
if (ContextTy)
1021
1029
PO.setBaseType (ContextTy);
1022
1030
1023
- if (isLabeledTrailingClosure ) {
1031
+ if (IsLabeledTrailingClosure ) {
1024
1032
// Expand the closure body.
1025
1033
SmallString<32 > buffer;
1026
1034
llvm::raw_svector_ostream OS (buffer);
@@ -1062,6 +1070,7 @@ void CodeCompletionResultBuilder::addCallArgument(
1062
1070
1063
1071
if (IsVarArg)
1064
1072
addEllipsis ();
1073
+
1065
1074
--CurrentNestingLevel;
1066
1075
}
1067
1076
@@ -1414,6 +1423,7 @@ Optional<unsigned> CodeCompletionString::getFirstTextChunkIndex(
1414
1423
case ChunkKind::CallArgumentColon:
1415
1424
case ChunkKind::CallArgumentTypeBegin:
1416
1425
case ChunkKind::CallArgumentType:
1426
+ case ChunkKind::CallArgumentDefaultBegin:
1417
1427
case ChunkKind::CallArgumentClosureType:
1418
1428
case ChunkKind::CallArgumentClosureExpr:
1419
1429
case ChunkKind::ParameterDeclTypeBegin:
@@ -2746,25 +2756,33 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
2746
2756
Builder.addFlair (CodeCompletionFlairBit::ExpressionSpecific);
2747
2757
}
2748
2758
2749
- static bool hasInterestingDefaultValues (const AbstractFunctionDecl *func) {
2750
- if (!func) return false ;
2759
+ static bool hasInterestingDefaultValue (const ParamDecl *param) {
2760
+ if (!param)
2761
+ return false ;
2751
2762
2752
- for (auto param : *func->getParameters ()) {
2753
- switch (param->getDefaultArgumentKind ()) {
2754
- case DefaultArgumentKind::Normal:
2755
- case DefaultArgumentKind::NilLiteral:
2756
- case DefaultArgumentKind::EmptyArray:
2757
- case DefaultArgumentKind::EmptyDictionary:
2758
- case DefaultArgumentKind::StoredProperty:
2759
- case DefaultArgumentKind::Inherited: // FIXME: include this?
2760
- return true ;
2763
+ switch (param->getDefaultArgumentKind ()) {
2764
+ case DefaultArgumentKind::Normal:
2765
+ case DefaultArgumentKind::NilLiteral:
2766
+ case DefaultArgumentKind::EmptyArray:
2767
+ case DefaultArgumentKind::EmptyDictionary:
2768
+ case DefaultArgumentKind::StoredProperty:
2769
+ case DefaultArgumentKind::Inherited:
2770
+ return true ;
2761
2771
2762
- case DefaultArgumentKind::None:
2772
+ case DefaultArgumentKind::None:
2763
2773
#define MAGIC_IDENTIFIER (NAME, STRING, SYNTAX_KIND ) \
2764
- case DefaultArgumentKind::NAME:
2774
+ case DefaultArgumentKind::NAME:
2765
2775
#include " swift/AST/MagicIdentifierKinds.def"
2766
- break ;
2767
- }
2776
+ return false ;
2777
+ }
2778
+ }
2779
+
2780
+ bool addItemWithoutDefaultArgs (const AbstractFunctionDecl *func) {
2781
+ if (!func || !Sink.addCallWithNoDefaultArgs )
2782
+ return false ;
2783
+ for (auto param : *func->getParameters ()) {
2784
+ if (hasInterestingDefaultValue (param))
2785
+ return true ;
2768
2786
}
2769
2787
return false ;
2770
2788
}
@@ -2780,52 +2798,27 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
2780
2798
assert (declParams.empty () || typeParams.size () == declParams.size ());
2781
2799
2782
2800
bool modifiedBuilder = false ;
2783
-
2784
- // Determine whether we should skip this argument because it is defaulted.
2785
- auto shouldSkipArg = [&](const ParamDecl *PD) -> bool {
2786
- switch (PD->getDefaultArgumentKind ()) {
2787
- case DefaultArgumentKind::None:
2788
- return false ;
2789
-
2790
- case DefaultArgumentKind::Normal:
2791
- case DefaultArgumentKind::StoredProperty:
2792
- case DefaultArgumentKind::Inherited:
2793
- case DefaultArgumentKind::NilLiteral:
2794
- case DefaultArgumentKind::EmptyArray:
2795
- case DefaultArgumentKind::EmptyDictionary:
2796
- return !includeDefaultArgs;
2797
-
2798
- #define MAGIC_IDENTIFIER (NAME, STRING, SYNTAX_KIND ) \
2799
- case DefaultArgumentKind::NAME:
2800
- #include " swift/AST/MagicIdentifierKinds.def"
2801
- // Skip parameters that are defaulted to source location or other
2802
- // caller context information. Users typically don't want to specify
2803
- // these parameters.
2804
- return true ;
2805
- }
2806
-
2807
- llvm_unreachable (" Unhandled DefaultArgumentKind in switch." );
2808
- };
2809
-
2810
- bool NeedComma = false ;
2801
+ bool needComma = false ;
2811
2802
// Iterate over each parameter.
2812
2803
for (unsigned i = 0 ; i != typeParams.size (); ++i) {
2813
2804
auto &typeParam = typeParams[i];
2814
2805
2815
- Identifier argName;
2806
+ Identifier argName = typeParam. getLabel () ;
2816
2807
Identifier bodyName;
2817
2808
bool isIUO = false ;
2818
-
2809
+ bool hasDefault = false ;
2819
2810
if (!declParams.empty ()) {
2820
- auto *PD = declParams[i];
2821
- if (shouldSkipArg (PD))
2811
+ const ParamDecl *PD = declParams[i];
2812
+ hasDefault = PD->isDefaultArgument ();
2813
+ // Skip default arguments if we're either not including them or they
2814
+ // aren't interesting
2815
+ if (hasDefault &&
2816
+ (!includeDefaultArgs || !hasInterestingDefaultValue (PD)))
2822
2817
continue ;
2818
+
2823
2819
argName = PD->getArgumentName ();
2824
2820
bodyName = PD->getParameterName ();
2825
2821
isIUO = PD->isImplicitlyUnwrappedOptional ();
2826
- } else {
2827
- isIUO = false ;
2828
- argName = typeParam.getLabel ();
2829
2822
}
2830
2823
2831
2824
bool isVariadic = typeParam.isVariadic ();
@@ -2835,21 +2828,22 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
2835
2828
if (isVariadic)
2836
2829
paramTy = ParamDecl::getVarargBaseTy (paramTy);
2837
2830
2838
- if (NeedComma)
2839
- Builder.addComma ();
2840
2831
Type contextTy;
2841
2832
if (auto typeContext = CurrDeclContext->getInnermostTypeContext ())
2842
2833
contextTy = typeContext->getDeclaredTypeInContext ();
2843
2834
2835
+ if (needComma)
2836
+ Builder.addComma ();
2844
2837
Builder.addCallArgument (argName, bodyName,
2845
2838
eraseArchetypes (paramTy, genericSig), contextTy,
2846
2839
isVariadic, isInOut, isIUO, isAutoclosure,
2847
- /* useUnderscoreLabel =*/ false ,
2848
- /* isLabeledTrailingClosure =*/ false );
2840
+ /* UseUnderscoreLabel =*/ false ,
2841
+ /* IsLabeledTrailingClosure =*/ false , hasDefault );
2849
2842
2850
2843
modifiedBuilder = true ;
2851
- NeedComma = true ;
2844
+ needComma = true ;
2852
2845
}
2846
+
2853
2847
return modifiedBuilder;
2854
2848
}
2855
2849
@@ -3085,7 +3079,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
3085
3079
if (isImplicitlyCurriedInstanceMethod) {
3086
3080
addPattern ({AFD->getImplicitSelfDecl ()}, /* includeDefaultArgs=*/ true );
3087
3081
} else {
3088
- if (hasInterestingDefaultValues (AFD))
3082
+ if (addItemWithoutDefaultArgs (AFD))
3089
3083
addPattern (AFD->getParameters ()->getArray (),
3090
3084
/* includeDefaultArgs=*/ false );
3091
3085
addPattern (AFD->getParameters ()->getArray (),
@@ -3289,7 +3283,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
3289
3283
if (trivialTrailingClosure)
3290
3284
addMethodImpl (/* includeDefaultArgs=*/ false ,
3291
3285
/* trivialTrailingClosure=*/ true );
3292
- if (hasInterestingDefaultValues (FD))
3286
+ if (addItemWithoutDefaultArgs (FD))
3293
3287
addMethodImpl (/* includeDefaultArgs=*/ false );
3294
3288
addMethodImpl (/* includeDefaultArgs=*/ true );
3295
3289
}
@@ -3379,8 +3373,8 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
3379
3373
}
3380
3374
};
3381
3375
3382
- if (ConstructorType && hasInterestingDefaultValues (CD))
3383
- addConstructorImpl (/* includeDefaultArgs*/ false );
3376
+ if (ConstructorType && addItemWithoutDefaultArgs (CD))
3377
+ addConstructorImpl (/* includeDefaultArgs= */ false );
3384
3378
addConstructorImpl ();
3385
3379
}
3386
3380
@@ -4732,9 +4726,9 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
4732
4726
Builder.addCallArgument (Arg->getLabel (), Identifier (),
4733
4727
Arg->getPlainType (), ContextType,
4734
4728
Arg->isVariadic (), Arg->isInOut (),
4735
- /* isIUO =*/ false , Arg->isAutoClosure (),
4736
- /* useUnderscoreLabel =*/ true ,
4737
- isLabeledTrailingClosure);
4729
+ /* IsIUO =*/ false , Arg->isAutoClosure (),
4730
+ /* UseUnderscoreLabel =*/ true ,
4731
+ isLabeledTrailingClosure, /* HasDefault= */ false );
4738
4732
Builder.addFlair (CodeCompletionFlairBit::ArgumentLabels);
4739
4733
auto Ty = Arg->getPlainType ();
4740
4734
if (Arg->isInOut ()) {
@@ -6660,8 +6654,8 @@ static void deliverCompletionResults(CodeCompletionContext &CompletionContext,
6660
6654
AccessLevel::Internal, TheModule,
6661
6655
SourceFile::ImportQueryKind::PrivateOnly),
6662
6656
CompletionContext.getAddInitsToTopLevel (),
6663
- CompletionContext.getAnnotateResult (),
6664
- };
6657
+ CompletionContext.addCallWithNoDefaultArgs (),
6658
+ CompletionContext. getAnnotateResult () };
6665
6659
6666
6660
using PairType = llvm::DenseSet<swift::ide::CodeCompletionCache::Key,
6667
6661
llvm::DenseMapInfo<CodeCompletionCache::Key>>::iterator;
@@ -7555,6 +7549,7 @@ void SimpleCachingCodeCompletionConsumer::handleResultsAndModules(
7555
7549
Sink.addInitsToTopLevel = context.getAddInitsToTopLevel ();
7556
7550
Sink.enableCallPatternHeuristics = context.getCallPatternHeuristics ();
7557
7551
Sink.includeObjectLiterals = context.includeObjectLiterals ();
7552
+ Sink.addCallWithNoDefaultArgs = context.addCallWithNoDefaultArgs ();
7558
7553
lookupCodeCompletionResultsFromModule (
7559
7554
(*V)->Sink , R.TheModule , R.Key .AccessPath ,
7560
7555
R.Key .ResultsHaveLeadingDot , SF);
0 commit comments