Skip to content

[ASTPrinter/CodeCompletion] Stop printing base type when possible #28165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
using namespace swift;

void PrintOptions::setBaseType(Type T) {
if (T->is<ErrorType>())
return;
TransformContext = TypeTransformContext(T);
}

Expand Down Expand Up @@ -670,6 +672,7 @@ class PrintAST : public ASTVisitor<PrintAST> {
FreshOptions.ExcludeAttrList = options.ExcludeAttrList;
FreshOptions.ExclusiveAttrList = options.ExclusiveAttrList;
FreshOptions.PrintOptionalAsImplicitlyUnwrapped = options.PrintOptionalAsImplicitlyUnwrapped;
FreshOptions.TransformContext = options.TransformContext;
T.print(Printer, FreshOptions);
return;
}
Expand Down Expand Up @@ -700,6 +703,8 @@ class PrintAST : public ASTVisitor<PrintAST> {
}

T = T.subst(subMap, SubstFlags::DesugarMemberTypes);

options.TransformContext = TypeTransformContext(CurrentType);
}

printTypeWithOptions(T, options);
Expand Down Expand Up @@ -3591,7 +3596,6 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
// know we're printing a type member so it escapes `Type` and `Protocol`.
if (auto parent = Ty->getParent()) {
visitParentType(parent);
Printer << ".";
NameContext = PrintNameContext::TypeMember;
} else if (shouldPrintFullyQualified(Ty)) {
printModuleContext(Ty);
Expand Down Expand Up @@ -3731,13 +3735,32 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
}

void visitParentType(Type T) {
/// Don't print the parent type if it's being printed in that type context.
if (Options.TransformContext) {
if (auto currentType = Options.TransformContext->getBaseType()) {
auto printingType = T;
if (currentType->hasArchetype())
currentType = currentType->mapTypeOutOfContext();

if (auto errorTy = printingType->getAs<ErrorType>())
if (auto origTy = errorTy->getOriginalType())
printingType = origTy;

if (printingType->hasArchetype())
printingType = printingType->mapTypeOutOfContext();

if (currentType->isEqual(printingType))
return;
}
}
PrintOptions innerOptions = Options;
innerOptions.SynthesizeSugarOnTypes = false;

if (auto sugarType = dyn_cast<SyntaxSugarType>(T.getPointer()))
T = sugarType->getImplementationType();

TypePrinter(Printer, innerOptions).visit(T);
TypePrinter(Printer, innerOptions).printWithParensIfNotSimple(T);
Printer << ".";
}

void visitEnumType(EnumType *T) {
Expand Down Expand Up @@ -4247,8 +4270,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
}

void visitNestedArchetypeType(NestedArchetypeType *T) {
printWithParensIfNotSimple(T->getParent());
Printer << ".";
visitParentType(T->getParent());
printArchetypeCommon(T);
}

Expand Down Expand Up @@ -4339,7 +4361,6 @@ class TypePrinter : public TypeVisitor<TypePrinter> {

void visitDependentMemberType(DependentMemberType *T) {
visitParentType(T->getBase());
Printer << ".";
Printer.printName(T->getName());
}

Expand Down
52 changes: 28 additions & 24 deletions lib/IDE/CodeCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1923,6 +1923,8 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
PrintOptions PO;
PO.OpaqueReturnTypePrinting =
PrintOptions::OpaqueReturnTypePrintingMode::WithoutOpaqueKeyword;
if (auto typeContext = CurrDeclContext->getInnermostTypeContext())
PO.setBaseType(typeContext->getDeclaredTypeInContext());
Builder.addTypeAnnotation(T.getString(PO));
}
}
Expand All @@ -1944,6 +1946,8 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
PO.PrintOptionalAsImplicitlyUnwrapped = true;
PO.OpaqueReturnTypePrinting =
PrintOptions::OpaqueReturnTypePrintingMode::WithoutOpaqueKeyword;
if (auto typeContext = CurrDeclContext->getInnermostTypeContext())
PO.setBaseType(typeContext->getDeclaredTypeInContext());
Builder.addTypeAnnotation(T.getString(PO) + suffix);
}

Expand Down Expand Up @@ -2288,9 +2292,12 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {

if (NeedComma)
Builder.addComma();
Type contextTy;
if (auto typeContext = CurrDeclContext->getInnermostTypeContext())
contextTy = typeContext->getDeclaredTypeInContext();

Builder.addCallParameter(argName, bodyName, paramTy, isVariadic, isInOut,
isIUO, isAutoclosure);
Builder.addCallParameter(argName, bodyName, paramTy, contextTy,
isVariadic, isInOut, isIUO, isAutoclosure);

modifiedBuilder = true;
NeedComma = true;
Expand Down Expand Up @@ -2636,6 +2643,8 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
PO.OpaqueReturnTypePrinting =
PrintOptions::OpaqueReturnTypePrintingMode::WithoutOpaqueKeyword;
PO.PrintOptionalAsImplicitlyUnwrapped = IsIUO;
if (auto typeContext = CurrDeclContext->getInnermostTypeContext())
PO.setBaseType(typeContext->getDeclaredTypeInContext());
ResultType.print(OS, PO);
}
}
Expand Down Expand Up @@ -3472,9 +3481,10 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
builder.addEqual();
builder.addWhitespace(" ");
assert(RHSType && resultType);
builder.addCallParameter(Identifier(), Identifier(), RHSType,
/*IsVarArg*/ false, /*IsInOut*/ false,
/*isIUO*/ false, /*isAutoClosure*/ false);
Type contextTy;
if (auto typeContext = CurrDeclContext->getInnermostTypeContext())
contextTy = typeContext->getDeclaredTypeInContext();
builder.addCallParameter(Identifier(), RHSType, contextTy);
addTypeAnnotation(builder, resultType);
}

Expand All @@ -3495,10 +3505,12 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
builder.addWhitespace(" ");
builder.addTextChunk(op->getName().str());
builder.addWhitespace(" ");
if (RHSType)
builder.addCallParameter(Identifier(), Identifier(), RHSType,
/*IsVarArg*/ false, /*IsInOut*/ false,
/*isIUO*/ false, /*isAutoClosure*/ false);
if (RHSType) {
Type contextTy;
if (auto typeContext = CurrDeclContext->getInnermostTypeContext())
contextTy = typeContext->getDeclaredTypeInContext();
builder.addCallParameter(Identifier(), RHSType, contextTy);
}
if (resultType)
addTypeAnnotation(builder, resultType);
}
Expand Down Expand Up @@ -3722,21 +3734,13 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
addFromProto(LK::ColorLiteral, "", [&](Builder &builder) {
builder.addTextChunk("#colorLiteral");
builder.addLeftParen();
builder.addCallParameter(context.getIdentifier("red"), floatType,
/*IsVarArg*/ false, /*IsInOut*/ false,
/*isIUO*/ false, /*isAutoClosure*/ false);
builder.addCallParameter(context.getIdentifier("red"), floatType);
builder.addComma();
builder.addCallParameter(context.getIdentifier("green"), floatType,
/*IsVarArg*/ false, /*IsInOut*/ false,
/*isIUO*/ false, /*isAutoClosure*/ false);
builder.addCallParameter(context.getIdentifier("green"), floatType);
builder.addComma();
builder.addCallParameter(context.getIdentifier("blue"), floatType,
/*IsVarArg*/ false, /*IsInOut*/ false,
/*isIUO*/ false, /*isAutoClosure*/ false);
builder.addCallParameter(context.getIdentifier("blue"), floatType);
builder.addComma();
builder.addCallParameter(context.getIdentifier("alpha"), floatType,
/*IsVarArg*/ false, /*IsInOut*/ false,
/*isIUO*/ false, /*isAutoClosure*/ false);
builder.addCallParameter(context.getIdentifier("alpha"), floatType);
builder.addRightParen();
});

Expand All @@ -3745,9 +3749,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
builder.addTextChunk("#imageLiteral");
builder.addLeftParen();
builder.addCallParameter(context.getIdentifier("resourceName"),
stringType, /*IsVarArg*/ false,
/*IsInOut*/ false, /*isIUO*/ false,
/*isAutoClosure*/ false);
stringType);
builder.addRightParen();
});

Expand Down Expand Up @@ -4391,6 +4393,8 @@ class CompletionOverrideLookup : public swift::VisibleDeclConsumer {
{
llvm::raw_svector_ostream OS(DeclStr);
PrintOptions Options;
if (auto transformType = CurrDeclContext->getDeclaredTypeInContext())
Options.setBaseType(transformType);
Options.PrintImplicitAttrs = false;
Options.SkipAttributes = true;
CD->print(OS, Options);
Expand Down
14 changes: 9 additions & 5 deletions lib/IDE/CodeCompletionResultBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ class CodeCompletionResultBuilder {
}

void addCallParameter(Identifier Name, Identifier LocalName, Type Ty,
bool IsVarArg, bool IsInOut, bool IsIUO,
Type ContextTy, bool IsVarArg, bool IsInOut, bool IsIUO,
bool isAutoClosure) {
CurrentNestingLevel++;

Expand Down Expand Up @@ -388,6 +388,8 @@ class CodeCompletionResultBuilder {
PO.PrintOptionalAsImplicitlyUnwrapped = IsIUO;
PO.OpaqueReturnTypePrinting =
PrintOptions::OpaqueReturnTypePrintingMode::WithoutOpaqueKeyword;
if (ContextTy)
PO.setBaseType(ContextTy);
std::string TypeName = Ty->getString(PO);
addChunkWithText(CodeCompletionString::Chunk::ChunkKind::CallParameterType,
TypeName);
Expand All @@ -402,6 +404,8 @@ class CodeCompletionResultBuilder {
PO.SkipAttributes = true;
PO.OpaqueReturnTypePrinting =
PrintOptions::OpaqueReturnTypePrintingMode::WithoutOpaqueKeyword;
if (ContextTy)
PO.setBaseType(ContextTy);
addChunkWithText(
CodeCompletionString::Chunk::ChunkKind::CallParameterClosureType,
AFT->getString(PO));
Expand All @@ -412,10 +416,10 @@ class CodeCompletionResultBuilder {
CurrentNestingLevel--;
}

void addCallParameter(Identifier Name, Type Ty, bool IsVarArg, bool IsInOut,
bool IsIUO, bool isAutoClosure) {
addCallParameter(Name, Identifier(), Ty, IsVarArg, IsInOut, IsIUO,
isAutoClosure);
void addCallParameter(Identifier Name, Type Ty, Type ContextTy = Type()) {
addCallParameter(Name, Identifier(), Ty, ContextTy,
/*IsVarArg=*/false, /*IsInOut=*/false, /*isIUO=*/false,
/*isAutoClosure=*/false);
}

void addGenericParameter(StringRef Name) {
Expand Down
12 changes: 6 additions & 6 deletions test/IDE/complete_after_self.swift
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,9 @@ class ThisDerived1 : ThisBase1 {
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[StaticMethod]/CurrNominal: .staticTest2()[#Void#]
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[InstanceMethod]/CurrNominal: .derivedExtInstanceFunc0({#(self): ThisDerived1#})[#() -> Void#]
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[StaticMethod]/CurrNominal: .derivedExtStaticFunc0()[#Void#]
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[Struct]/CurrNominal: .DerivedExtNestedStruct[#ThisDerived1.DerivedExtNestedStruct#]
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[Class]/CurrNominal: .DerivedExtNestedClass[#ThisDerived1.DerivedExtNestedClass#]
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[Enum]/CurrNominal: .DerivedExtNestedEnum[#ThisDerived1.DerivedExtNestedEnum#]
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[Struct]/CurrNominal: .DerivedExtNestedStruct[#DerivedExtNestedStruct#]
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[Class]/CurrNominal: .DerivedExtNestedClass[#DerivedExtNestedClass#]
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[Enum]/CurrNominal: .DerivedExtNestedEnum[#DerivedExtNestedEnum#]
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[TypeAlias]/CurrNominal: .DerivedExtNestedTypealias[#Int#]
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[Constructor]/CurrNominal: .init({#someExtensionArg: Int#})[#ThisDerived1#]
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[InstanceMethod]/Super: .baseFunc0({#(self): ThisBase1#})[#() -> Void#]
Expand Down Expand Up @@ -275,9 +275,9 @@ class ThisDerived1 : ThisBase1 {
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[StaticMethod]/CurrNominal: staticTest2()[#Void#]
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[InstanceMethod]/CurrNominal: derivedExtInstanceFunc0({#(self): ThisDerived1#})[#() -> Void#]
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[StaticMethod]/CurrNominal: derivedExtStaticFunc0()[#Void#]
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[Struct]/CurrNominal: DerivedExtNestedStruct[#ThisDerived1.DerivedExtNestedStruct#]
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[Class]/CurrNominal: DerivedExtNestedClass[#ThisDerived1.DerivedExtNestedClass#]
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[Enum]/CurrNominal: DerivedExtNestedEnum[#ThisDerived1.DerivedExtNestedEnum#]
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[Struct]/CurrNominal: DerivedExtNestedStruct[#DerivedExtNestedStruct#]
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[Class]/CurrNominal: DerivedExtNestedClass[#DerivedExtNestedClass#]
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[Enum]/CurrNominal: DerivedExtNestedEnum[#DerivedExtNestedEnum#]
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[TypeAlias]/CurrNominal: DerivedExtNestedTypealias[#Int#]
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[Constructor]/CurrNominal: init({#someExtensionArg: Int#})[#ThisDerived1#]
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[InstanceMethod]/Super: baseFunc0({#(self): ThisBase1#})[#() -> Void#]
Expand Down
Loading