Skip to content

Commit c146c65

Browse files
authored
Merge pull request swiftlang#28165 from rintaro/astprinter-rdar57033931
[ASTPrinter/CodeCompletion] Stop printing base type when possible
2 parents 6f4d65b + 80dbf7e commit c146c65

17 files changed

+258
-156
lines changed

lib/AST/ASTPrinter.cpp

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
using namespace swift;
5757

5858
void PrintOptions::setBaseType(Type T) {
59+
if (T->is<ErrorType>())
60+
return;
5961
TransformContext = TypeTransformContext(T);
6062
}
6163

@@ -670,6 +672,7 @@ class PrintAST : public ASTVisitor<PrintAST> {
670672
FreshOptions.ExcludeAttrList = options.ExcludeAttrList;
671673
FreshOptions.ExclusiveAttrList = options.ExclusiveAttrList;
672674
FreshOptions.PrintOptionalAsImplicitlyUnwrapped = options.PrintOptionalAsImplicitlyUnwrapped;
675+
FreshOptions.TransformContext = options.TransformContext;
673676
T.print(Printer, FreshOptions);
674677
return;
675678
}
@@ -700,6 +703,8 @@ class PrintAST : public ASTVisitor<PrintAST> {
700703
}
701704

702705
T = T.subst(subMap, SubstFlags::DesugarMemberTypes);
706+
707+
options.TransformContext = TypeTransformContext(CurrentType);
703708
}
704709

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

37333737
void visitParentType(Type T) {
3738+
/// Don't print the parent type if it's being printed in that type context.
3739+
if (Options.TransformContext) {
3740+
if (auto currentType = Options.TransformContext->getBaseType()) {
3741+
auto printingType = T;
3742+
if (currentType->hasArchetype())
3743+
currentType = currentType->mapTypeOutOfContext();
3744+
3745+
if (auto errorTy = printingType->getAs<ErrorType>())
3746+
if (auto origTy = errorTy->getOriginalType())
3747+
printingType = origTy;
3748+
3749+
if (printingType->hasArchetype())
3750+
printingType = printingType->mapTypeOutOfContext();
3751+
3752+
if (currentType->isEqual(printingType))
3753+
return;
3754+
}
3755+
}
37343756
PrintOptions innerOptions = Options;
37353757
innerOptions.SynthesizeSugarOnTypes = false;
37363758

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

3740-
TypePrinter(Printer, innerOptions).visit(T);
3762+
TypePrinter(Printer, innerOptions).printWithParensIfNotSimple(T);
3763+
Printer << ".";
37413764
}
37423765

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

42494272
void visitNestedArchetypeType(NestedArchetypeType *T) {
4250-
printWithParensIfNotSimple(T->getParent());
4251-
Printer << ".";
4273+
visitParentType(T->getParent());
42524274
printArchetypeCommon(T);
42534275
}
42544276

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

43404362
void visitDependentMemberType(DependentMemberType *T) {
43414363
visitParentType(T->getBase());
4342-
Printer << ".";
43434364
Printer.printName(T->getName());
43444365
}
43454366

lib/IDE/CodeCompletion.cpp

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1923,6 +1923,8 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
19231923
PrintOptions PO;
19241924
PO.OpaqueReturnTypePrinting =
19251925
PrintOptions::OpaqueReturnTypePrintingMode::WithoutOpaqueKeyword;
1926+
if (auto typeContext = CurrDeclContext->getInnermostTypeContext())
1927+
PO.setBaseType(typeContext->getDeclaredTypeInContext());
19261928
Builder.addTypeAnnotation(T.getString(PO));
19271929
}
19281930
}
@@ -1944,6 +1946,8 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
19441946
PO.PrintOptionalAsImplicitlyUnwrapped = true;
19451947
PO.OpaqueReturnTypePrinting =
19461948
PrintOptions::OpaqueReturnTypePrintingMode::WithoutOpaqueKeyword;
1949+
if (auto typeContext = CurrDeclContext->getInnermostTypeContext())
1950+
PO.setBaseType(typeContext->getDeclaredTypeInContext());
19471951
Builder.addTypeAnnotation(T.getString(PO) + suffix);
19481952
}
19491953

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

22892293
if (NeedComma)
22902294
Builder.addComma();
2295+
Type contextTy;
2296+
if (auto typeContext = CurrDeclContext->getInnermostTypeContext())
2297+
contextTy = typeContext->getDeclaredTypeInContext();
22912298

2292-
Builder.addCallParameter(argName, bodyName, paramTy, isVariadic, isInOut,
2293-
isIUO, isAutoclosure);
2299+
Builder.addCallParameter(argName, bodyName, paramTy, contextTy,
2300+
isVariadic, isInOut, isIUO, isAutoclosure);
22942301

22952302
modifiedBuilder = true;
22962303
NeedComma = true;
@@ -2636,6 +2643,8 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
26362643
PO.OpaqueReturnTypePrinting =
26372644
PrintOptions::OpaqueReturnTypePrintingMode::WithoutOpaqueKeyword;
26382645
PO.PrintOptionalAsImplicitlyUnwrapped = IsIUO;
2646+
if (auto typeContext = CurrDeclContext->getInnermostTypeContext())
2647+
PO.setBaseType(typeContext->getDeclaredTypeInContext());
26392648
ResultType.print(OS, PO);
26402649
}
26412650
}
@@ -3472,9 +3481,10 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
34723481
builder.addEqual();
34733482
builder.addWhitespace(" ");
34743483
assert(RHSType && resultType);
3475-
builder.addCallParameter(Identifier(), Identifier(), RHSType,
3476-
/*IsVarArg*/ false, /*IsInOut*/ false,
3477-
/*isIUO*/ false, /*isAutoClosure*/ false);
3484+
Type contextTy;
3485+
if (auto typeContext = CurrDeclContext->getInnermostTypeContext())
3486+
contextTy = typeContext->getDeclaredTypeInContext();
3487+
builder.addCallParameter(Identifier(), RHSType, contextTy);
34783488
addTypeAnnotation(builder, resultType);
34793489
}
34803490

@@ -3495,10 +3505,12 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
34953505
builder.addWhitespace(" ");
34963506
builder.addTextChunk(op->getName().str());
34973507
builder.addWhitespace(" ");
3498-
if (RHSType)
3499-
builder.addCallParameter(Identifier(), Identifier(), RHSType,
3500-
/*IsVarArg*/ false, /*IsInOut*/ false,
3501-
/*isIUO*/ false, /*isAutoClosure*/ false);
3508+
if (RHSType) {
3509+
Type contextTy;
3510+
if (auto typeContext = CurrDeclContext->getInnermostTypeContext())
3511+
contextTy = typeContext->getDeclaredTypeInContext();
3512+
builder.addCallParameter(Identifier(), RHSType, contextTy);
3513+
}
35023514
if (resultType)
35033515
addTypeAnnotation(builder, resultType);
35043516
}
@@ -3722,21 +3734,13 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
37223734
addFromProto(LK::ColorLiteral, "", [&](Builder &builder) {
37233735
builder.addTextChunk("#colorLiteral");
37243736
builder.addLeftParen();
3725-
builder.addCallParameter(context.getIdentifier("red"), floatType,
3726-
/*IsVarArg*/ false, /*IsInOut*/ false,
3727-
/*isIUO*/ false, /*isAutoClosure*/ false);
3737+
builder.addCallParameter(context.getIdentifier("red"), floatType);
37283738
builder.addComma();
3729-
builder.addCallParameter(context.getIdentifier("green"), floatType,
3730-
/*IsVarArg*/ false, /*IsInOut*/ false,
3731-
/*isIUO*/ false, /*isAutoClosure*/ false);
3739+
builder.addCallParameter(context.getIdentifier("green"), floatType);
37323740
builder.addComma();
3733-
builder.addCallParameter(context.getIdentifier("blue"), floatType,
3734-
/*IsVarArg*/ false, /*IsInOut*/ false,
3735-
/*isIUO*/ false, /*isAutoClosure*/ false);
3741+
builder.addCallParameter(context.getIdentifier("blue"), floatType);
37363742
builder.addComma();
3737-
builder.addCallParameter(context.getIdentifier("alpha"), floatType,
3738-
/*IsVarArg*/ false, /*IsInOut*/ false,
3739-
/*isIUO*/ false, /*isAutoClosure*/ false);
3743+
builder.addCallParameter(context.getIdentifier("alpha"), floatType);
37403744
builder.addRightParen();
37413745
});
37423746

@@ -3745,9 +3749,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
37453749
builder.addTextChunk("#imageLiteral");
37463750
builder.addLeftParen();
37473751
builder.addCallParameter(context.getIdentifier("resourceName"),
3748-
stringType, /*IsVarArg*/ false,
3749-
/*IsInOut*/ false, /*isIUO*/ false,
3750-
/*isAutoClosure*/ false);
3752+
stringType);
37513753
builder.addRightParen();
37523754
});
37533755

@@ -4391,6 +4393,8 @@ class CompletionOverrideLookup : public swift::VisibleDeclConsumer {
43914393
{
43924394
llvm::raw_svector_ostream OS(DeclStr);
43934395
PrintOptions Options;
4396+
if (auto transformType = CurrDeclContext->getDeclaredTypeInContext())
4397+
Options.setBaseType(transformType);
43944398
Options.PrintImplicitAttrs = false;
43954399
Options.SkipAttributes = true;
43964400
CD->print(OS, Options);

lib/IDE/CodeCompletionResultBuilder.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ class CodeCompletionResultBuilder {
344344
}
345345

346346
void addCallParameter(Identifier Name, Identifier LocalName, Type Ty,
347-
bool IsVarArg, bool IsInOut, bool IsIUO,
347+
Type ContextTy, bool IsVarArg, bool IsInOut, bool IsIUO,
348348
bool isAutoClosure) {
349349
CurrentNestingLevel++;
350350

@@ -388,6 +388,8 @@ class CodeCompletionResultBuilder {
388388
PO.PrintOptionalAsImplicitlyUnwrapped = IsIUO;
389389
PO.OpaqueReturnTypePrinting =
390390
PrintOptions::OpaqueReturnTypePrintingMode::WithoutOpaqueKeyword;
391+
if (ContextTy)
392+
PO.setBaseType(ContextTy);
391393
std::string TypeName = Ty->getString(PO);
392394
addChunkWithText(CodeCompletionString::Chunk::ChunkKind::CallParameterType,
393395
TypeName);
@@ -402,6 +404,8 @@ class CodeCompletionResultBuilder {
402404
PO.SkipAttributes = true;
403405
PO.OpaqueReturnTypePrinting =
404406
PrintOptions::OpaqueReturnTypePrintingMode::WithoutOpaqueKeyword;
407+
if (ContextTy)
408+
PO.setBaseType(ContextTy);
405409
addChunkWithText(
406410
CodeCompletionString::Chunk::ChunkKind::CallParameterClosureType,
407411
AFT->getString(PO));
@@ -412,10 +416,10 @@ class CodeCompletionResultBuilder {
412416
CurrentNestingLevel--;
413417
}
414418

415-
void addCallParameter(Identifier Name, Type Ty, bool IsVarArg, bool IsInOut,
416-
bool IsIUO, bool isAutoClosure) {
417-
addCallParameter(Name, Identifier(), Ty, IsVarArg, IsInOut, IsIUO,
418-
isAutoClosure);
419+
void addCallParameter(Identifier Name, Type Ty, Type ContextTy = Type()) {
420+
addCallParameter(Name, Identifier(), Ty, ContextTy,
421+
/*IsVarArg=*/false, /*IsInOut=*/false, /*isIUO=*/false,
422+
/*isAutoClosure=*/false);
419423
}
420424

421425
void addGenericParameter(StringRef Name) {

test/IDE/complete_after_self.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,9 @@ class ThisDerived1 : ThisBase1 {
240240
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[StaticMethod]/CurrNominal: .staticTest2()[#Void#]
241241
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[InstanceMethod]/CurrNominal: .derivedExtInstanceFunc0({#(self): ThisDerived1#})[#() -> Void#]
242242
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[StaticMethod]/CurrNominal: .derivedExtStaticFunc0()[#Void#]
243-
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[Struct]/CurrNominal: .DerivedExtNestedStruct[#ThisDerived1.DerivedExtNestedStruct#]
244-
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[Class]/CurrNominal: .DerivedExtNestedClass[#ThisDerived1.DerivedExtNestedClass#]
245-
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[Enum]/CurrNominal: .DerivedExtNestedEnum[#ThisDerived1.DerivedExtNestedEnum#]
243+
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[Struct]/CurrNominal: .DerivedExtNestedStruct[#DerivedExtNestedStruct#]
244+
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[Class]/CurrNominal: .DerivedExtNestedClass[#DerivedExtNestedClass#]
245+
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[Enum]/CurrNominal: .DerivedExtNestedEnum[#DerivedExtNestedEnum#]
246246
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[TypeAlias]/CurrNominal: .DerivedExtNestedTypealias[#Int#]
247247
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[Constructor]/CurrNominal: .init({#someExtensionArg: Int#})[#ThisDerived1#]
248248
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[InstanceMethod]/Super: .baseFunc0({#(self): ThisBase1#})[#() -> Void#]
@@ -275,9 +275,9 @@ class ThisDerived1 : ThisBase1 {
275275
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[StaticMethod]/CurrNominal: staticTest2()[#Void#]
276276
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[InstanceMethod]/CurrNominal: derivedExtInstanceFunc0({#(self): ThisDerived1#})[#() -> Void#]
277277
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[StaticMethod]/CurrNominal: derivedExtStaticFunc0()[#Void#]
278-
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[Struct]/CurrNominal: DerivedExtNestedStruct[#ThisDerived1.DerivedExtNestedStruct#]
279-
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[Class]/CurrNominal: DerivedExtNestedClass[#ThisDerived1.DerivedExtNestedClass#]
280-
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[Enum]/CurrNominal: DerivedExtNestedEnum[#ThisDerived1.DerivedExtNestedEnum#]
278+
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[Struct]/CurrNominal: DerivedExtNestedStruct[#DerivedExtNestedStruct#]
279+
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[Class]/CurrNominal: DerivedExtNestedClass[#DerivedExtNestedClass#]
280+
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[Enum]/CurrNominal: DerivedExtNestedEnum[#DerivedExtNestedEnum#]
281281
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[TypeAlias]/CurrNominal: DerivedExtNestedTypealias[#Int#]
282282
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[Constructor]/CurrNominal: init({#someExtensionArg: Int#})[#ThisDerived1#]
283283
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[InstanceMethod]/Super: baseFunc0({#(self): ThisBase1#})[#() -> Void#]

0 commit comments

Comments
 (0)