@@ -1641,6 +1641,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
16411641
16421642 void setExpectedTypes (ArrayRef<Type> Types, bool isSingleExpressionBody) {
16431643 expectedTypeContext.isSingleExpressionBody = isSingleExpressionBody;
1644+ expectedTypeContext.possibleTypes .clear ();
16441645 expectedTypeContext.possibleTypes .reserve (Types.size ());
16451646 for (auto T : Types)
16461647 if (T)
@@ -2242,22 +2243,56 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
22422243 Builder.addRightParen ();
22432244 }
22442245
2246+ SemanticContextKind getSemanticContextKind (const AbstractFunctionDecl *AFD) {
2247+ // FIXME: to get the corect semantic context we need to know how lookup
2248+ // would have found the declaration AFD. For now, just infer a reasonable
2249+ // semantics.
2250+
2251+ if (!AFD)
2252+ return SemanticContextKind::CurrentModule;
2253+
2254+ DeclContext *calleeDC = AFD->getDeclContext ();
2255+
2256+ if (calleeDC->isTypeContext ())
2257+ // FIXME: We should distinguish CurrentNominal and Super. We need to
2258+ // propagate the base type to do that.
2259+ return SemanticContextKind::CurrentNominal;
2260+
2261+ if (calleeDC->isLocalContext ())
2262+ return SemanticContextKind::Local;
2263+ if (calleeDC->getParentModule () == CurrDeclContext->getParentModule ())
2264+ return SemanticContextKind::CurrentModule;
2265+
2266+ return SemanticContextKind::OtherModule;
2267+ }
2268+
22452269 void addFunctionCallPattern (const AnyFunctionType *AFT,
22462270 const AbstractFunctionDecl *AFD = nullptr ) {
2271+ if (AFD) {
2272+ auto genericSig =
2273+ AFD->getInnermostDeclContext ()->getGenericSignatureOfContext ();
2274+ AFT = eraseArchetypes (CurrDeclContext->getParentModule (),
2275+ const_cast <AnyFunctionType *>(AFT), genericSig)
2276+ ->castTo <AnyFunctionType>();
2277+ }
22472278
22482279 // Add the pattern, possibly including any default arguments.
22492280 auto addPattern = [&](ArrayRef<const ParamDecl *> declParams = {},
22502281 bool includeDefaultArgs = true ) {
2251- // FIXME: to get the corect semantic context we need to know how lookup
2252- // would have found the declaration AFD. For now, just choose a reasonable
2253- // default, it's most likely to be CurrentModule or CurrentNominal.
2282+ CommandWordsPairs Pairs;
22542283 CodeCompletionResultBuilder Builder (
2255- Sink, CodeCompletionResult::ResultKind::Pattern,
2256- SemanticContextKind::CurrentModule, expectedTypeContext);
2284+ Sink,
2285+ AFD ? CodeCompletionResult::ResultKind::Declaration
2286+ : CodeCompletionResult::ResultKind::Pattern,
2287+ getSemanticContextKind (AFD), expectedTypeContext);
22572288 if (!HaveLParen)
22582289 Builder.addLeftParen ();
22592290 else
22602291 Builder.addAnnotatedLeftParen ();
2292+ if (AFD) {
2293+ Builder.setAssociatedDecl (AFD);
2294+ setClangDeclKeywords (AFD, Pairs, Builder);
2295+ }
22612296
22622297 addCallArgumentPatterns (Builder, AFT->getParams (), declParams,
22632298 includeDefaultArgs);
@@ -5066,14 +5101,17 @@ void CodeCompletionCallbacksImpl::doneParsing() {
50665101 Lookup.setHaveLParen (true );
50675102
50685103 ExprContextInfo ContextInfo (CurDeclContext, CodeCompleteTokenExpr);
5069- Lookup.setExpectedTypes (ContextInfo.getPossibleTypes (),
5070- ContextInfo.isSingleExpressionBody ());
5104+
50715105 if (ShouldCompleteCallPatternAfterParen) {
5072- if (ExprType) {
5106+ ExprContextInfo ParentContextInfo (CurDeclContext, ParsedExpr);
5107+ Lookup.setExpectedTypes (ParentContextInfo.getPossibleTypes (),
5108+ ParentContextInfo.isSingleExpressionBody ());
5109+ if (ExprType && ((*ExprType)->is <AnyFunctionType>() ||
5110+ (*ExprType)->is <AnyMetatypeType>())) {
50735111 Lookup.getValueExprCompletions (*ExprType, ReferencedDecl.getDecl ());
50745112 } else {
50755113 for (auto &typeAndDecl : ContextInfo.getPossibleCallees ())
5076- Lookup.getValueExprCompletions (typeAndDecl.first , typeAndDecl.second );
5114+ Lookup.tryFunctionCallCompletions (typeAndDecl.first , typeAndDecl.second );
50775115 }
50785116 } else {
50795117 // Add argument labels, then fallthrough to get values.
@@ -5083,6 +5121,8 @@ void CodeCompletionCallbacksImpl::doneParsing() {
50835121 if (!Lookup.FoundFunctionCalls ||
50845122 (Lookup.FoundFunctionCalls &&
50855123 Lookup.FoundFunctionsWithoutFirstKeyword )) {
5124+ Lookup.setExpectedTypes (ContextInfo.getPossibleTypes (),
5125+ ContextInfo.isSingleExpressionBody ());
50865126 Lookup.setHaveLParen (false );
50875127 DoPostfixExprBeginning ();
50885128 }
0 commit comments