@@ -873,19 +873,88 @@ void CompletionLookup::addVarDeclRef(const VarDecl *VD,
873
873
Builder.addFlair (CodeCompletionFlairBit::ExpressionSpecific);
874
874
}
875
875
876
+ // / Return whether \p param has a non-desirable default value for code
877
+ // / completion.
878
+ // /
879
+ // / 'ClangImporter::Implementation::inferDefaultArgument()' automatically adds
880
+ // / default values for some parameters;
881
+ // / * NS_OPTIONS enum type with the name '...Options'.
882
+ // / * NSDictionary and labeled 'options', 'attributes', or 'userInfo'.
883
+ // /
884
+ // / But sometimes, this behavior isn't really desirable. This function add a
885
+ // / heuristic where if a parameter matches all the following condition, we
886
+ // / consider the imported default value is _not_ desirable:
887
+ // / * it is the first parameter,
888
+ // / * it doesn't have an argument label, and
889
+ // / * the imported function base name ends with those words
890
+ // / For example, ClangImporter imports:
891
+ // /
892
+ // / -(void)addAttributes:(NSDictionary *)attrs, options:(NSDictionary *)opts;
893
+ // /
894
+ // / as:
895
+ // /
896
+ // / func addAttributes(_ attrs: [AnyHashable:Any] = [:],
897
+ // / options opts: [AnyHashable:Any] = [:])
898
+ // /
899
+ // / In this case, we don't want 'attrs' defaulted because the function name have
900
+ // / 'Attribute' in its name so calling 'value.addAttribute()' doesn't make
901
+ // / sense, but we _do_ want to keep 'opts' defaulted.
902
+ // /
903
+ // / Note that:
904
+ // /
905
+ // / -(void)performWithOptions:(NSDictionary *) opts;
906
+ // /
907
+ // / This doesn't match the condition because the base name of the function in
908
+ // / Swift is 'peform':
909
+ // /
910
+ // / func perform(options opts: [AnyHashable:Any] = [:])
911
+ // /
912
+ bool isNonDesirableImportedDefaultArg (const ParamDecl *param) {
913
+ auto kind = param->getDefaultArgumentKind ();
914
+ if (kind != DefaultArgumentKind::EmptyArray &&
915
+ kind != DefaultArgumentKind::EmptyDictionary)
916
+ return false ;
917
+
918
+ if (!param->getArgumentName ().empty ())
919
+ return false ;
920
+
921
+ auto *func = dyn_cast<FuncDecl>(param->getDeclContext ());
922
+ if (!func->hasClangNode ())
923
+ return false ;
924
+ if (func->getParameters ()->front () != param)
925
+ return false ;
926
+ if (func->getBaseName ().isSpecial ())
927
+ return false ;
928
+
929
+ auto baseName = func->getBaseName ().getIdentifier ().str ();
930
+ switch (kind) {
931
+ case DefaultArgumentKind::EmptyArray:
932
+ return (baseName.endswith (" Options" ));
933
+ case DefaultArgumentKind::EmptyDictionary:
934
+ return (baseName.endswith (" Options" ) || baseName.endswith (" Attributes" ) ||
935
+ baseName.endswith (" UserInfo" ));
936
+ default :
937
+ llvm_unreachable (" unhandled DefaultArgumentKind" );
938
+ }
939
+ }
940
+
876
941
bool CompletionLookup::hasInterestingDefaultValue (const ParamDecl *param) {
877
942
if (!param)
878
943
return false ;
879
944
880
945
switch (param->getDefaultArgumentKind ()) {
881
946
case DefaultArgumentKind::Normal:
882
947
case DefaultArgumentKind::NilLiteral:
883
- case DefaultArgumentKind::EmptyArray:
884
- case DefaultArgumentKind::EmptyDictionary:
885
948
case DefaultArgumentKind::StoredProperty:
886
949
case DefaultArgumentKind::Inherited:
887
950
return true ;
888
951
952
+ case DefaultArgumentKind::EmptyArray:
953
+ case DefaultArgumentKind::EmptyDictionary:
954
+ if (isNonDesirableImportedDefaultArg (param))
955
+ return false ;
956
+ return true ;
957
+
889
958
case DefaultArgumentKind::None:
890
959
#define MAGIC_IDENTIFIER (NAME, STRING, SYNTAX_KIND ) \
891
960
case DefaultArgumentKind::NAME:
@@ -894,7 +963,7 @@ bool CompletionLookup::hasInterestingDefaultValue(const ParamDecl *param) {
894
963
}
895
964
}
896
965
897
- bool CompletionLookup::addItemWithoutDefaultArgs (
966
+ bool CompletionLookup::shouldAddItemWithoutDefaultArgs (
898
967
const AbstractFunctionDecl *func) {
899
968
if (!func || !Sink.addCallWithNoDefaultArgs )
900
969
return false ;
@@ -924,7 +993,8 @@ bool CompletionLookup::addCallArgumentPatterns(
924
993
bool hasDefault = false ;
925
994
if (!declParams.empty ()) {
926
995
const ParamDecl *PD = declParams[i];
927
- hasDefault = PD->isDefaultArgument ();
996
+ hasDefault =
997
+ PD->isDefaultArgument () && !isNonDesirableImportedDefaultArg (PD);
928
998
// Skip default arguments if we're either not including them or they
929
999
// aren't interesting
930
1000
if (hasDefault &&
@@ -1189,7 +1259,7 @@ void CompletionLookup::addFunctionCallPattern(
1189
1259
if (isImplicitlyCurriedInstanceMethod) {
1190
1260
addPattern ({AFD->getImplicitSelfDecl ()}, /* includeDefaultArgs=*/ true );
1191
1261
} else {
1192
- if (addItemWithoutDefaultArgs (AFD))
1262
+ if (shouldAddItemWithoutDefaultArgs (AFD))
1193
1263
addPattern (AFD->getParameters ()->getArray (),
1194
1264
/* includeDefaultArgs=*/ false );
1195
1265
addPattern (AFD->getParameters ()->getArray (),
@@ -1385,7 +1455,7 @@ void CompletionLookup::addMethodCall(const FuncDecl *FD,
1385
1455
if (trivialTrailingClosure)
1386
1456
addMethodImpl (/* includeDefaultArgs=*/ false ,
1387
1457
/* trivialTrailingClosure=*/ true );
1388
- if (addItemWithoutDefaultArgs (FD))
1458
+ if (shouldAddItemWithoutDefaultArgs (FD))
1389
1459
addMethodImpl (/* includeDefaultArgs=*/ false );
1390
1460
addMethodImpl (/* includeDefaultArgs=*/ true );
1391
1461
}
@@ -1475,7 +1545,7 @@ void CompletionLookup::addConstructorCall(const ConstructorDecl *CD,
1475
1545
}
1476
1546
};
1477
1547
1478
- if (ConstructorType && addItemWithoutDefaultArgs (CD))
1548
+ if (ConstructorType && shouldAddItemWithoutDefaultArgs (CD))
1479
1549
addConstructorImpl (/* includeDefaultArgs=*/ false );
1480
1550
addConstructorImpl ();
1481
1551
}
0 commit comments