@@ -1643,6 +1643,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
16431643
16441644 void setExpectedTypes (ArrayRef<Type> Types, bool isSingleExpressionBody) {
16451645 expectedTypeContext.isSingleExpressionBody = isSingleExpressionBody;
1646+ expectedTypeContext.possibleTypes .clear ();
16461647 expectedTypeContext.possibleTypes .reserve (Types.size ());
16471648 for (auto T : Types)
16481649 if (T)
@@ -2238,22 +2239,56 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
22382239 Builder.addRightParen ();
22392240 }
22402241
2242+ SemanticContextKind getSemanticContextKind (const AbstractFunctionDecl *AFD) {
2243+ // FIXME: to get the corect semantic context we need to know how lookup
2244+ // would have found the declaration AFD. For now, just infer a reasonable
2245+ // semantics.
2246+
2247+ if (!AFD)
2248+ return SemanticContextKind::CurrentModule;
2249+
2250+ DeclContext *calleeDC = AFD->getDeclContext ();
2251+
2252+ if (calleeDC->isTypeContext ())
2253+ // FIXME: We should distinguish CurrentNominal and Super. We need to
2254+ // propagate the base type to do that.
2255+ return SemanticContextKind::CurrentNominal;
2256+
2257+ if (calleeDC->isLocalContext ())
2258+ return SemanticContextKind::Local;
2259+ if (calleeDC->getParentModule () == CurrDeclContext->getParentModule ())
2260+ return SemanticContextKind::CurrentModule;
2261+
2262+ return SemanticContextKind::OtherModule;
2263+ }
2264+
22412265 void addFunctionCallPattern (const AnyFunctionType *AFT,
22422266 const AbstractFunctionDecl *AFD = nullptr ) {
2267+ if (AFD) {
2268+ auto genericSig =
2269+ AFD->getInnermostDeclContext ()->getGenericSignatureOfContext ();
2270+ AFT = eraseArchetypes (CurrDeclContext->getParentModule (),
2271+ const_cast <AnyFunctionType *>(AFT), genericSig)
2272+ ->castTo <AnyFunctionType>();
2273+ }
22432274
22442275 // Add the pattern, possibly including any default arguments.
22452276 auto addPattern = [&](ArrayRef<const ParamDecl *> declParams = {},
22462277 bool includeDefaultArgs = true ) {
2247- // FIXME: to get the corect semantic context we need to know how lookup
2248- // would have found the declaration AFD. For now, just choose a reasonable
2249- // default, it's most likely to be CurrentModule or CurrentNominal.
2278+ CommandWordsPairs Pairs;
22502279 CodeCompletionResultBuilder Builder (
2251- Sink, CodeCompletionResult::ResultKind::Pattern,
2252- SemanticContextKind::CurrentModule, expectedTypeContext);
2280+ Sink,
2281+ AFD ? CodeCompletionResult::ResultKind::Declaration
2282+ : CodeCompletionResult::ResultKind::Pattern,
2283+ getSemanticContextKind (AFD), expectedTypeContext);
22532284 if (!HaveLParen)
22542285 Builder.addLeftParen ();
22552286 else
22562287 Builder.addAnnotatedLeftParen ();
2288+ if (AFD) {
2289+ Builder.setAssociatedDecl (AFD);
2290+ setClangDeclKeywords (AFD, Pairs, Builder);
2291+ }
22572292
22582293 addCallArgumentPatterns (Builder, AFT->getParams (), declParams,
22592294 includeDefaultArgs);
@@ -5063,14 +5098,17 @@ void CodeCompletionCallbacksImpl::doneParsing() {
50635098 Lookup.setHaveLParen (true );
50645099
50655100 ExprContextInfo ContextInfo (CurDeclContext, CodeCompleteTokenExpr);
5066- Lookup.setExpectedTypes (ContextInfo.getPossibleTypes (),
5067- ContextInfo.isSingleExpressionBody ());
5101+
50685102 if (ShouldCompleteCallPatternAfterParen) {
5069- if (ExprType) {
5103+ ExprContextInfo ParentContextInfo (CurDeclContext, ParsedExpr);
5104+ Lookup.setExpectedTypes (ParentContextInfo.getPossibleTypes (),
5105+ ParentContextInfo.isSingleExpressionBody ());
5106+ if (ExprType && ((*ExprType)->is <AnyFunctionType>() ||
5107+ (*ExprType)->is <AnyMetatypeType>())) {
50705108 Lookup.getValueExprCompletions (*ExprType, ReferencedDecl.getDecl ());
50715109 } else {
50725110 for (auto &typeAndDecl : ContextInfo.getPossibleCallees ())
5073- Lookup.getValueExprCompletions (typeAndDecl.first , typeAndDecl.second );
5111+ Lookup.tryFunctionCallCompletions (typeAndDecl.first , typeAndDecl.second );
50745112 }
50755113 } else {
50765114 // Add argument labels, then fallthrough to get values.
@@ -5080,6 +5118,8 @@ void CodeCompletionCallbacksImpl::doneParsing() {
50805118 if (!Lookup.FoundFunctionCalls ||
50815119 (Lookup.FoundFunctionCalls &&
50825120 Lookup.FoundFunctionsWithoutFirstKeyword )) {
5121+ Lookup.setExpectedTypes (ContextInfo.getPossibleTypes (),
5122+ ContextInfo.isSingleExpressionBody ());
50835123 Lookup.setHaveLParen (false );
50845124 DoPostfixExprBeginning ();
50855125 }
0 commit comments