Skip to content

Commit bd5b301

Browse files
authored
Merge pull request #38453 from rintaro/ide-completion-overrideannotation-rdarrdar62617558
[CodeCompletion] Annotate override completions
2 parents 42bc8fb + 7e7b2c3 commit bd5b301

15 files changed

+908
-343
lines changed

include/swift/AST/ASTPrinter.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,20 @@ enum class PrintStructureKind {
8686
TupleElement,
8787
NumberLiteral,
8888
StringLiteral,
89+
/// ' = defaultValue'.
90+
DefaultArgumentClause,
91+
/// '<T, U: Requirement>'.
92+
DeclGenericParameterClause,
93+
/// 'where T: Collection, T.Element: Equtable'.
94+
DeclGenericRequirementClause,
95+
/// ' async throws'.
96+
EffectsSpecifiers,
97+
/// ' -> ResultTy' or ': ResultTy'.
98+
DeclResultTypeClause,
99+
/// '(a: Int, b param: String)' in function declarations.
100+
FunctionParameterList,
101+
/// '@attribute ParamTy...' in parameter declarations.
102+
FunctionParameterType,
89103
};
90104

91105
/// An abstract class used to print an AST.

include/swift/IDE/CodeCompletion.h

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,14 @@ class CodeCompletionStringChunk {
132132
Equal,
133133
Whitespace,
134134

135+
/// The first chunk of a whole generic parameter clause.
136+
/// E.g '<T, C: Collection>'
137+
GenericParameterClauseBegin,
138+
139+
/// The first chunk of a generic quirement clause.
140+
/// E.g. 'where T: Collection, T.Element == Int'
141+
GenericRequirementClauseBegin,
142+
135143
/// The first chunk of a substring that describes the parameter for a
136144
/// generic type.
137145
GenericParameterBegin,
@@ -194,6 +202,30 @@ class CodeCompletionStringChunk {
194202
/// desired.
195203
OptionalMethodCallTail,
196204

205+
/// The first chunk of a substring that describes the single parameter
206+
/// declaration for a parameter clause.
207+
ParameterDeclBegin,
208+
209+
ParameterDeclExternalName,
210+
211+
ParameterDeclLocalName,
212+
213+
ParameterDeclColon,
214+
215+
ParameterDeclTypeBegin,
216+
217+
/// Default argument clause for parameter declarations.
218+
DefaultArgumentClauseBegin,
219+
220+
/// First chunk for effect specifiers. i.e. 'async' and 'throws'.
221+
EffectsSpecifierClauseBegin,
222+
223+
/// First chunk for result type clause i.e. ' -> ResultTy' or ': ResultTy'.
224+
DeclResultTypeClauseBegin,
225+
226+
/// First chunk for attribute and modifier list i.e. 'override public'
227+
AttributeAndModifierListBegin,
228+
197229
/// Specifies the type of the whole entity that is returned in this code
198230
/// completion result. For example, for variable references it is the
199231
/// variable type, for function calls it is the return type.
@@ -219,7 +251,15 @@ class CodeCompletionStringChunk {
219251
Kind == ChunkKind::GenericParameterBegin ||
220252
Kind == ChunkKind::OptionalBegin ||
221253
Kind == ChunkKind::CallArgumentTypeBegin ||
222-
Kind == ChunkKind::TypeAnnotationBegin;
254+
Kind == ChunkKind::TypeAnnotationBegin ||
255+
Kind == ChunkKind::ParameterDeclBegin ||
256+
Kind == ChunkKind::ParameterDeclTypeBegin ||
257+
Kind == ChunkKind::DefaultArgumentClauseBegin ||
258+
Kind == ChunkKind::GenericParameterClauseBegin ||
259+
Kind == ChunkKind::EffectsSpecifierClauseBegin ||
260+
Kind == ChunkKind::DeclResultTypeClauseBegin ||
261+
Kind == ChunkKind::GenericRequirementClauseBegin ||
262+
Kind == ChunkKind::AttributeAndModifierListBegin;
223263
}
224264

225265
static bool chunkHasText(ChunkKind Kind) {
@@ -249,11 +289,14 @@ class CodeCompletionStringChunk {
249289
Kind == ChunkKind::CallArgumentName ||
250290
Kind == ChunkKind::CallArgumentInternalName ||
251291
Kind == ChunkKind::CallArgumentColon ||
252-
Kind == ChunkKind::DeclAttrParamColon ||
253-
Kind == ChunkKind::DeclAttrParamKeyword ||
254292
Kind == ChunkKind::CallArgumentType ||
255293
Kind == ChunkKind::CallArgumentClosureType ||
256294
Kind == ChunkKind::CallArgumentClosureExpr ||
295+
Kind == ChunkKind::ParameterDeclExternalName ||
296+
Kind == ChunkKind::ParameterDeclLocalName ||
297+
Kind == ChunkKind::ParameterDeclColon ||
298+
Kind == ChunkKind::DeclAttrParamColon ||
299+
Kind == ChunkKind::DeclAttrParamKeyword ||
257300
Kind == ChunkKind::GenericParameterName ||
258301
Kind == ChunkKind::DynamicLookupMethodCallTail ||
259302
Kind == ChunkKind::OptionalMethodCallTail ||
@@ -1053,17 +1096,20 @@ class PrintingCodeCompletionConsumer
10531096
llvm::raw_ostream &OS;
10541097
bool IncludeKeywords;
10551098
bool IncludeComments;
1099+
bool IncludeSourceText;
10561100
bool PrintAnnotatedDescription;
10571101
bool RequiresSourceFileInfo = false;
10581102

10591103
public:
10601104
PrintingCodeCompletionConsumer(llvm::raw_ostream &OS,
10611105
bool IncludeKeywords = true,
10621106
bool IncludeComments = true,
1107+
bool IncludeSourceText = false,
10631108
bool PrintAnnotatedDescription = false)
10641109
: OS(OS),
10651110
IncludeKeywords(IncludeKeywords),
10661111
IncludeComments(IncludeComments),
1112+
IncludeSourceText(IncludeSourceText),
10671113
PrintAnnotatedDescription(PrintAnnotatedDescription) {}
10681114

10691115
void handleResults(CodeCompletionContext &context) override;

lib/AST/ASTPrinter.cpp

Lines changed: 103 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2224,8 +2224,11 @@ void PrintAST::printMembers(ArrayRef<Decl *> members, bool needComma,
22242224

22252225
void PrintAST::printGenericDeclGenericParams(GenericContext *decl) {
22262226
if (decl->isGeneric())
2227-
if (auto GenericSig = decl->getGenericSignature())
2227+
if (auto GenericSig = decl->getGenericSignature()) {
2228+
Printer.printStructurePre(PrintStructureKind::DeclGenericParameterClause);
22282229
printGenericSignature(GenericSig, PrintParams | InnermostOnly);
2230+
Printer.printStructurePost(PrintStructureKind::DeclGenericParameterClause);
2231+
}
22292232
}
22302233

22312234
void PrintAST::printDeclGenericRequirements(GenericContext *decl) {
@@ -2239,12 +2242,14 @@ void PrintAST::printDeclGenericRequirements(GenericContext *decl) {
22392242
if (parentSig && parentSig->isEqual(genericSig))
22402243
return;
22412244

2245+
Printer.printStructurePre(PrintStructureKind::DeclGenericParameterClause);
22422246
printGenericSignature(genericSig, PrintRequirements,
22432247
[parentSig](const Requirement &req) {
22442248
if (parentSig)
22452249
return !parentSig->isRequirementSatisfied(req);
22462250
return true;
22472251
});
2252+
Printer.printStructurePost(PrintStructureKind::DeclGenericParameterClause);
22482253
}
22492254

22502255
void PrintAST::printInherited(const Decl *decl) {
@@ -3236,25 +3241,32 @@ void PrintAST::visitVarDecl(VarDecl *decl) {
32363241
Printer.printName(decl->getName(), getTypeMemberPrintNameContext(decl));
32373242
});
32383243

3239-
auto type = decl->getInterfaceType();
3240-
Printer << ": ";
3241-
TypeLoc tyLoc;
3242-
if (auto *repr = decl->getTypeReprOrParentPatternTypeRepr()) {
3243-
tyLoc = TypeLoc(repr, type);
3244-
} else {
3245-
tyLoc = TypeLoc::withoutLoc(type);
3246-
}
3247-
Printer.printDeclResultTypePre(decl, tyLoc);
3244+
{
3245+
Printer.printStructurePre(PrintStructureKind::DeclResultTypeClause);
3246+
SWIFT_DEFER {
3247+
Printer.printStructurePost(PrintStructureKind::DeclResultTypeClause);
3248+
};
32483249

3249-
// HACK: When printing result types for vars with opaque result types,
3250-
// always print them using the `some` keyword instead of printing
3251-
// the full stable reference.
3252-
llvm::SaveAndRestore<PrintOptions::OpaqueReturnTypePrintingMode>
3253-
x(Options.OpaqueReturnTypePrinting,
3254-
PrintOptions::OpaqueReturnTypePrintingMode::WithOpaqueKeyword);
3250+
auto type = decl->getInterfaceType();
3251+
Printer << ": ";
3252+
TypeLoc tyLoc;
3253+
if (auto *repr = decl->getTypeReprOrParentPatternTypeRepr()) {
3254+
tyLoc = TypeLoc(repr, type);
3255+
} else {
3256+
tyLoc = TypeLoc::withoutLoc(type);
3257+
}
3258+
Printer.printDeclResultTypePre(decl, tyLoc);
32553259

3256-
printTypeLocForImplicitlyUnwrappedOptional(
3257-
tyLoc, decl->isImplicitlyUnwrappedOptional());
3260+
// HACK: When printing result types for vars with opaque result types,
3261+
// always print them using the `some` keyword instead of printing
3262+
// the full stable reference.
3263+
llvm::SaveAndRestore<PrintOptions::OpaqueReturnTypePrintingMode>
3264+
x(Options.OpaqueReturnTypePrinting,
3265+
PrintOptions::OpaqueReturnTypePrintingMode::WithOpaqueKeyword);
3266+
3267+
printTypeLocForImplicitlyUnwrappedOptional(
3268+
tyLoc, decl->isImplicitlyUnwrappedOptional());
3269+
}
32583270

32593271
printAccessors(decl);
32603272
}
@@ -3332,20 +3344,31 @@ void PrintAST::printOneParameter(const ParamDecl *param,
33323344
TheTypeLoc.setType(BGT->getGenericArgs()[0]);
33333345
}
33343346

3335-
if (!param->isVariadic() &&
3336-
!willUseTypeReprPrinting(TheTypeLoc, CurrentType, Options)) {
3337-
auto type = TheTypeLoc.getType();
3338-
printParameterFlags(Printer, Options, paramFlags,
3339-
isEscaping(type));
3340-
}
3347+
{
3348+
Printer.printStructurePre(PrintStructureKind::FunctionParameterType);
3349+
SWIFT_DEFER {
3350+
Printer.printStructurePost(PrintStructureKind::FunctionParameterType);
3351+
};
3352+
if (!param->isVariadic() &&
3353+
!willUseTypeReprPrinting(TheTypeLoc, CurrentType, Options)) {
3354+
auto type = TheTypeLoc.getType();
3355+
printParameterFlags(Printer, Options, paramFlags,
3356+
isEscaping(type));
3357+
}
33413358

3342-
printTypeLocForImplicitlyUnwrappedOptional(
3343-
TheTypeLoc, param->isImplicitlyUnwrappedOptional());
3359+
printTypeLocForImplicitlyUnwrappedOptional(
3360+
TheTypeLoc, param->isImplicitlyUnwrappedOptional());
33443361

3345-
if (param->isVariadic())
3346-
Printer << "...";
3362+
if (param->isVariadic())
3363+
Printer << "...";
3364+
}
33473365

33483366
if (param->isDefaultArgument() && Options.PrintDefaultArgumentValue) {
3367+
Printer.callPrintStructurePre(PrintStructureKind::DefaultArgumentClause);
3368+
SWIFT_DEFER {
3369+
Printer.printStructurePost(PrintStructureKind::DefaultArgumentClause);
3370+
};
3371+
33493372
SmallString<128> scratch;
33503373
auto defaultArgStr = param->getDefaultValueStringRepresentation(scratch);
33513374

@@ -3368,6 +3391,10 @@ void PrintAST::printOneParameter(const ParamDecl *param,
33683391
void PrintAST::printParameterList(ParameterList *PL,
33693392
ArrayRef<AnyFunctionType::Param> params,
33703393
bool isAPINameByDefault) {
3394+
Printer.printStructurePre(PrintStructureKind::FunctionParameterList);
3395+
SWIFT_DEFER {
3396+
Printer.printStructurePost(PrintStructureKind::FunctionParameterList);
3397+
};
33713398
Printer << "(";
33723399
const unsigned paramSize = params.size();
33733400
for (unsigned i = 0, e = PL->size(); i != e; ++i) {
@@ -3398,19 +3425,25 @@ void PrintAST::printFunctionParameters(AbstractFunctionDecl *AFD) {
33983425
printParameterList(BodyParams, parameterListTypes,
33993426
AFD->argumentNameIsAPIByDefault());
34003427

3401-
if (AFD->hasAsync()) {
3402-
Printer << " ";
3403-
if (AFD->getAttrs().hasAttribute<ReasyncAttr>())
3404-
Printer.printKeyword("reasync", Options);
3405-
else
3406-
Printer.printKeyword("async", Options);
3407-
}
3428+
if (AFD->hasAsync() || AFD->hasThrows()) {
3429+
Printer.printStructurePre(PrintStructureKind::EffectsSpecifiers);
3430+
SWIFT_DEFER {
3431+
Printer.printStructurePost(PrintStructureKind::EffectsSpecifiers);
3432+
};
3433+
if (AFD->hasAsync()) {
3434+
Printer << " ";
3435+
if (AFD->getAttrs().hasAttribute<ReasyncAttr>())
3436+
Printer.printKeyword("reasync", Options);
3437+
else
3438+
Printer.printKeyword("async", Options);
3439+
}
34083440

3409-
if (AFD->hasThrows()) {
3410-
if (AFD->getAttrs().hasAttribute<RethrowsAttr>())
3411-
Printer << " " << tok::kw_rethrows;
3412-
else
3413-
Printer << " " << tok::kw_throws;
3441+
if (AFD->hasThrows()) {
3442+
if (AFD->getAttrs().hasAttribute<RethrowsAttr>())
3443+
Printer << " " << tok::kw_rethrows;
3444+
else
3445+
Printer << " " << tok::kw_throws;
3446+
}
34143447
}
34153448
}
34163449

@@ -3522,6 +3555,10 @@ void PrintAST::visitFuncDecl(FuncDecl *decl) {
35223555

35233556
Type ResultTy = decl->getResultInterfaceType();
35243557
if (ResultTy && !ResultTy->isVoid()) {
3558+
Printer.printStructurePre(PrintStructureKind::DeclResultTypeClause);
3559+
SWIFT_DEFER {
3560+
Printer.printStructurePost(PrintStructureKind::DeclResultTypeClause);
3561+
};
35253562
TypeLoc ResultTyLoc(decl->getResultTypeRepr(), ResultTy);
35263563

35273564
// When printing a protocol requirement with types substituted for a
@@ -3699,23 +3736,32 @@ void PrintAST::visitSubscriptDecl(SubscriptDecl *decl) {
36993736
printParameterList(decl->getIndices(), params,
37003737
/*isAPINameByDefault*/false);
37013738
});
3702-
Printer << " -> ";
3703-
3704-
TypeLoc elementTy(decl->getElementTypeRepr(),
3705-
decl->getElementInterfaceType());
3706-
Printer.printDeclResultTypePre(decl, elementTy);
3707-
Printer.callPrintStructurePre(PrintStructureKind::FunctionReturnType);
3708-
3709-
// HACK: When printing result types for subscripts with opaque result types,
3710-
// always print them using the `some` keyword instead of printing
3711-
// the full stable reference.
3712-
llvm::SaveAndRestore<PrintOptions::OpaqueReturnTypePrintingMode>
3713-
x(Options.OpaqueReturnTypePrinting,
3714-
PrintOptions::OpaqueReturnTypePrintingMode::WithOpaqueKeyword);
3715-
3716-
printTypeLocForImplicitlyUnwrappedOptional(
3717-
elementTy, decl->isImplicitlyUnwrappedOptional());
3718-
Printer.printStructurePost(PrintStructureKind::FunctionReturnType);
3739+
3740+
{
3741+
Printer.printStructurePre(PrintStructureKind::DeclResultTypeClause);
3742+
SWIFT_DEFER {
3743+
Printer.printStructurePost(PrintStructureKind::DeclResultTypeClause);
3744+
};
3745+
3746+
Printer << " -> ";
3747+
3748+
TypeLoc elementTy(decl->getElementTypeRepr(),
3749+
decl->getElementInterfaceType());
3750+
Printer.printDeclResultTypePre(decl, elementTy);
3751+
Printer.callPrintStructurePre(PrintStructureKind::FunctionReturnType);
3752+
3753+
// HACK: When printing result types for subscripts with opaque result types,
3754+
// always print them using the `some` keyword instead of printing
3755+
// the full stable reference.
3756+
llvm::SaveAndRestore<PrintOptions::OpaqueReturnTypePrintingMode>
3757+
x(Options.OpaqueReturnTypePrinting,
3758+
PrintOptions::OpaqueReturnTypePrintingMode::WithOpaqueKeyword);
3759+
3760+
printTypeLocForImplicitlyUnwrappedOptional(
3761+
elementTy, decl->isImplicitlyUnwrappedOptional());
3762+
Printer.printStructurePost(PrintStructureKind::FunctionReturnType);
3763+
}
3764+
37193765
printDeclGenericRequirements(decl);
37203766
printAccessors(decl);
37213767
}

0 commit comments

Comments
 (0)