[CodeComplete] In where clauses, suggest Self and the current type

Fixes rdar://70058225
This commit is contained in:
Alex Hoppen
2021-03-26 17:43:35 +01:00
parent 74b5eb7b44
commit 943d7ee621
3 changed files with 55 additions and 17 deletions

View File

@@ -4610,7 +4610,8 @@ public:
}
}
void getGenericRequirementCompletions(DeclContext *DC) {
void getGenericRequirementCompletions(DeclContext *DC,
SourceLoc CodeCompletionLoc) {
auto genericSig = DC->getGenericSignatureOfContext();
if (!genericSig)
return;
@@ -4637,6 +4638,18 @@ public:
CurrDeclContext, IncludeInstanceMembers,
/*includeDerivedRequirements*/false,
/*includeProtocolExtensionMembers*/true);
// We not only allow referencing nested types/typealiases directly, but also
// qualified by the current type. Thus also suggest current self type so the
// user can do a memberwise lookup on it.
if (auto SelfType = typeContext->getSelfNominalTypeDecl()) {
addNominalTypeRef(SelfType, DeclVisibilityKind::LocalVariable,
DynamicLookupInfo());
}
// Self is also valid in all cases in which it can be used in function
// bodies. Suggest it if applicable.
getSelfTypeCompletionInDeclContext(CodeCompletionLoc,
/*isForResultType=*/false);
}
static bool canUseAttributeOnDecl(DeclAttrKind DAK, bool IsInSil,
@@ -4758,6 +4771,9 @@ public:
getAttributeDeclParamCompletions(DAK_Available, 0);
}
/// \p Loc is the location of the code completin token.
/// \p isForDeclResult determines if were are spelling out the result type
/// of a declaration.
void getSelfTypeCompletionInDeclContext(SourceLoc Loc, bool isForDeclResult) {
const DeclContext *typeDC = CurrDeclContext->getInnermostTypeContext();
if (!typeDC)
@@ -6925,9 +6941,11 @@ void CodeCompletionCallbacksImpl::doneParsing() {
break;
}
case CompletionKind::GenericRequirement:
Lookup.getGenericRequirementCompletions(CurDeclContext);
case CompletionKind::GenericRequirement: {
auto Loc = Context.SourceMgr.getCodeCompletionLoc();
Lookup.getGenericRequirementCompletions(CurDeclContext, Loc);
break;
}
case CompletionKind::PrecedenceGroup:
Lookup.getPrecedenceGroupCompletions(SyntxKind);
break;