@@ -431,6 +431,9 @@ void CodeCompletionString::print(raw_ostream &OS) const {
431
431
OS << C.getText ();
432
432
OS << " #]" ;
433
433
break ;
434
+ case ChunkKind::CallParameterClosureExpr:
435
+ OS << " {" << C.getText () << " |}" ;
436
+ break ;
434
437
case ChunkKind::BraceStmtWithCursor:
435
438
OS << " {|}" ;
436
439
break ;
@@ -870,7 +873,8 @@ void CodeCompletionResultBuilder::addCallParameter(Identifier Name,
870
873
bool IsInOut,
871
874
bool IsIUO,
872
875
bool isAutoClosure,
873
- bool useUnderscoreLabel) {
876
+ bool useUnderscoreLabel,
877
+ bool isLabeledTrailingClosure) {
874
878
CurrentNestingLevel++;
875
879
using ChunkKind = CodeCompletionString::Chunk::ChunkKind;
876
880
@@ -967,7 +971,8 @@ void CodeCompletionResultBuilder::addCallParameter(Identifier Name,
967
971
// function type.
968
972
Ty = Ty->lookThroughAllOptionalTypes ();
969
973
if (auto AFT = Ty->getAs <AnyFunctionType>()) {
970
- // If this is a closure type, add ChunkKind::CallParameterClosureType.
974
+ // If this is a closure type, add ChunkKind::CallParameterClosureType or
975
+ // ChunkKind::CallParameterClosureExpr for labeled trailing closures.
971
976
PrintOptions PO;
972
977
PO.PrintFunctionRepresentationAttrs =
973
978
PrintOptions::FunctionRepresentationMode::None;
@@ -976,9 +981,51 @@ void CodeCompletionResultBuilder::addCallParameter(Identifier Name,
976
981
PrintOptions::OpaqueReturnTypePrintingMode::WithoutOpaqueKeyword;
977
982
if (ContextTy)
978
983
PO.setBaseType (ContextTy);
979
- addChunkWithText (
980
- CodeCompletionString::Chunk::ChunkKind::CallParameterClosureType,
981
- AFT->getString (PO));
984
+
985
+ if (isLabeledTrailingClosure) {
986
+ // Expand the closure body.
987
+ SmallString<32 > buffer;
988
+ llvm::raw_svector_ostream OS (buffer);
989
+
990
+ bool returnsVoid = AFT->getResult ()->isVoid ();
991
+ bool hasSignature = !returnsVoid || !AFT->getParams ().empty ();
992
+ if (hasSignature)
993
+ OS << " (" ;
994
+ bool firstParam = true ;
995
+ for (const auto ¶m : AFT->getParams ()) {
996
+ if (!firstParam)
997
+ OS << " , " ;
998
+ firstParam = false ;
999
+
1000
+ if (param.hasLabel ()) {
1001
+ OS << param.getLabel ();
1002
+ } else {
1003
+ OS << " <#" ;
1004
+ if (param.isInOut ())
1005
+ OS << " inout " ;
1006
+ OS << param.getPlainType ()->getString (PO);
1007
+ if (param.isVariadic ())
1008
+ OS << " ..." ;
1009
+ OS << " #>" ;
1010
+ }
1011
+ }
1012
+ if (hasSignature)
1013
+ OS << " )" ;
1014
+ if (!returnsVoid)
1015
+ OS << " -> " << AFT->getResult ()->getString (PO);
1016
+
1017
+ if (hasSignature)
1018
+ OS << " in" ;
1019
+
1020
+ addChunkWithText (
1021
+ CodeCompletionString::Chunk::ChunkKind::CallParameterClosureExpr,
1022
+ OS.str ());
1023
+ } else {
1024
+ // Add the closure type.
1025
+ addChunkWithText (
1026
+ CodeCompletionString::Chunk::ChunkKind::CallParameterClosureType,
1027
+ AFT->getString (PO));
1028
+ }
982
1029
}
983
1030
984
1031
if (IsVarArg)
@@ -1337,6 +1384,7 @@ Optional<unsigned> CodeCompletionString::getFirstTextChunkIndex(
1337
1384
case ChunkKind::DeclAttrParamColon:
1338
1385
case ChunkKind::CallParameterType:
1339
1386
case ChunkKind::CallParameterClosureType:
1387
+ case ChunkKind::CallParameterClosureExpr:
1340
1388
case ChunkKind::OptionalBegin:
1341
1389
case ChunkKind::GenericParameterBegin:
1342
1390
case ChunkKind::DynamicLookupMethodCallTail:
@@ -1370,6 +1418,7 @@ void CodeCompletionString::getName(raw_ostream &OS) const {
1370
1418
switch (C.getKind ()) {
1371
1419
case ChunkKind::TypeAnnotation:
1372
1420
case ChunkKind::CallParameterClosureType:
1421
+ case ChunkKind::CallParameterClosureExpr:
1373
1422
case ChunkKind::DeclAttrParamColon:
1374
1423
case ChunkKind::OptionalMethodCallTail:
1375
1424
continue ;
@@ -2523,7 +2572,8 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
2523
2572
2524
2573
Builder.addCallParameter (argName, bodyName, paramTy, contextTy,
2525
2574
isVariadic, isInOut, isIUO, isAutoclosure,
2526
- /* useUnderscoreLabel=*/ false );
2575
+ /* useUnderscoreLabel=*/ false ,
2576
+ /* isLabeledTrailingClosure=*/ false );
2527
2577
2528
2578
modifiedBuilder = true ;
2529
2579
NeedComma = true ;
@@ -4159,7 +4209,8 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
4159
4209
}
4160
4210
4161
4211
void addCallArgumentCompletionResults (
4162
- ArrayRef<PossibleParamInfo> ParamInfos) {
4212
+ ArrayRef<PossibleParamInfo> ParamInfos,
4213
+ bool isLabeledTrailingClosure = false ) {
4163
4214
Type ContextType;
4164
4215
if (auto typeContext = CurrDeclContext->getInnermostTypeContext ())
4165
4216
ContextType = typeContext->getDeclaredTypeInContext ();
@@ -4175,7 +4226,8 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
4175
4226
Arg->getPlainType (), ContextType,
4176
4227
Arg->isVariadic (), Arg->isInOut (),
4177
4228
/* isIUO=*/ false , Arg->isAutoClosure (),
4178
- /* useUnderscoreLabel=*/ true );
4229
+ /* useUnderscoreLabel=*/ true ,
4230
+ isLabeledTrailingClosure);
4179
4231
auto Ty = Arg->getPlainType ();
4180
4232
if (Arg->isInOut ()) {
4181
4233
Ty = InOutType::get (Ty);
@@ -5926,7 +5978,8 @@ void CodeCompletionCallbacksImpl::doneParsing() {
5926
5978
5927
5979
bool allRequired = false ;
5928
5980
if (!params.empty ()) {
5929
- Lookup.addCallArgumentCompletionResults (params);
5981
+ Lookup.addCallArgumentCompletionResults (
5982
+ params, /* isLabeledTrailingClosure=*/ true );
5930
5983
allRequired = llvm::all_of (
5931
5984
params, [](const PossibleParamInfo &P) { return P.IsRequired ; });
5932
5985
}
0 commit comments