mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
[CodeComplete] Default parameter names of completed closure to internal names
If have a function that takes a trailing closure as follows
```
func sort(callback: (_ left: Int, _ right: Int) -> Bool) {}
```
completing a call to `sort` and expanding the trailing closure results in
```
sort { <#Int#>, <#Int#> in
<#code#>
}
```
We should be doing a better job here and defaulting the trailing closure's to the internal names specified in the function signature. I.e. the final result should be
```
sort { left, right in
<#code#>
}
```
This commit does exactly that.
Firstly, it keeps track of the closure's internal names (as specified in the declaration of `sort`) in the closure's type through a new `InternalLabel` property in `AnyFunctionType::Param`. Once the type containing the parameter gets canonicalized, the internal label is dropped.
Secondly, it adds a new option to `ASTPrinter` to always try and print parameter labels. With this option set to true, it will always print external paramter labels and, if they are present, print the internal parameter label as `_ <internalLabel>`.
Finally, we can use this new printing mode to print the trailing closure’s type as
```
<#T##callback: (Int, Int) -> Bool##(_ left: Int, _ right: Int) -> Bool#>
```
This is already correctly expanded by code-expand to the desired result. I also added a test case for that behaviour.
This commit is contained in:
@@ -1001,6 +1001,7 @@ void CodeCompletionResultBuilder::addCallParameter(Identifier Name,
|
||||
PO.SkipAttributes = true;
|
||||
PO.OpaqueReturnTypePrinting =
|
||||
PrintOptions::OpaqueReturnTypePrintingMode::WithoutOpaqueKeyword;
|
||||
PO.AlwaysTryPrintParameterLabels = true;
|
||||
if (ContextTy)
|
||||
PO.setBaseType(ContextTy);
|
||||
|
||||
@@ -1017,6 +1018,8 @@ void CodeCompletionResultBuilder::addCallParameter(Identifier Name,
|
||||
|
||||
if (param.hasLabel()) {
|
||||
OS << param.getLabel();
|
||||
} else if (param.hasInternalLabel()) {
|
||||
OS << param.getInternalLabel();
|
||||
} else {
|
||||
OS << "<#";
|
||||
if (param.isInOut())
|
||||
@@ -2337,8 +2340,7 @@ public:
|
||||
SmallVector<AnyFunctionType::Param, 8> erasedParams;
|
||||
for (const auto ¶m : genericFuncType->getParams()) {
|
||||
auto erasedTy = eraseArchetypes(param.getPlainType(), genericSig);
|
||||
erasedParams.emplace_back(erasedTy, param.getLabel(),
|
||||
param.getParameterFlags());
|
||||
erasedParams.emplace_back(param.withType(erasedTy));
|
||||
}
|
||||
return GenericFunctionType::get(genericSig,
|
||||
erasedParams,
|
||||
|
||||
Reference in New Issue
Block a user