Skip to content

Commit 601a213

Browse files
authored
Merge pull request #22841 from rintaro/ide-completion-addparameters
[CodeCompletion] Consolidate parameter list processing functions
2 parents 041eda1 + 63d1ba3 commit 601a213

27 files changed

+470
-435
lines changed

lib/IDE/CodeCompletion.cpp

Lines changed: 155 additions & 143 deletions
Large diffs are not rendered by default.

lib/IDE/CodeCompletionResultBuilder.h

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -322,26 +322,20 @@ class CodeCompletionResultBuilder {
322322
addSimpleChunk(CodeCompletionString::Chunk::ChunkKind::CallParameterBegin);
323323

324324
if (!Name.empty()) {
325-
StringRef NameStr = Name.str();
326-
327-
// 'self' is a keyword, we cannot allow to insert it into the source
328-
// buffer.
329-
bool IsAnnotation = (NameStr == "self");
330-
331325
llvm::SmallString<16> EscapedKeyword;
332326
addChunkWithText(
333327
CodeCompletionString::Chunk::ChunkKind::CallParameterName,
334-
// if the name is not annotation, we need to escape keyword
335-
IsAnnotation ? NameStr
336-
: escapeArgumentLabel(NameStr, !Outermost,
337-
EscapedKeyword));
338-
if (IsAnnotation)
339-
getLastChunk().setIsAnnotation();
340-
328+
escapeArgumentLabel(Name.str(), !Outermost, EscapedKeyword));
329+
addChunkWithTextNoCopy(
330+
CodeCompletionString::Chunk::ChunkKind::CallParameterColon, ": ");
331+
} else if (!LocalName.empty()) {
332+
// Use local (non-API) parameter name if we have nothing else.
333+
llvm::SmallString<16> EscapedKeyword;
334+
addChunkWithText(
335+
CodeCompletionString::Chunk::ChunkKind::CallParameterInternalName,
336+
escapeArgumentLabel(LocalName.str(), !Outermost, EscapedKeyword));
341337
addChunkWithTextNoCopy(
342338
CodeCompletionString::Chunk::ChunkKind::CallParameterColon, ": ");
343-
if (IsAnnotation)
344-
getLastChunk().setIsAnnotation();
345339
}
346340

347341
// 'inout' arguments are printed specially.
@@ -351,16 +345,6 @@ class CodeCompletionResultBuilder {
351345
Ty = Ty->getInOutObjectType();
352346
}
353347

354-
if (Name.empty() && !LocalName.empty()) {
355-
llvm::SmallString<16> EscapedKeyword;
356-
// Use local (non-API) parameter name if we have nothing else.
357-
addChunkWithText(
358-
CodeCompletionString::Chunk::ChunkKind::CallParameterInternalName,
359-
escapeArgumentLabel(LocalName.str(), !Outermost, EscapedKeyword));
360-
addChunkWithTextNoCopy(
361-
CodeCompletionString::Chunk::ChunkKind::CallParameterColon, ": ");
362-
}
363-
364348
// If the parameter is of the type @autoclosure ()->output, then the
365349
// code completion should show the parameter of the output type
366350
// instead of the function type ()->output.

lib/IDE/ExprContextAnalysis.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,8 @@ void collectPossibleCalleesByQualifiedLookup(
304304

305305
SmallVector<ValueDecl *, 2> decls;
306306
auto resolver = DC.getASTContext().getLazyResolver();
307-
if (!DC.lookupQualified(baseTy, name, NL_QualifiedDefault, resolver, decls))
307+
if (!DC.lookupQualified(baseTy->getMetatypeInstanceType(), name,
308+
NL_QualifiedDefault, resolver, decls))
308309
return;
309310

310311
for (auto *VD : decls) {
@@ -315,13 +316,22 @@ void collectPossibleCalleesByQualifiedLookup(
315316
if (!VD->hasInterfaceType())
316317
continue;
317318
Type declaredMemberType = VD->getInterfaceType();
318-
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(VD))
319-
if (AFD->getDeclContext()->isTypeContext())
319+
if (VD->getDeclContext()->isTypeContext()) {
320+
if (auto *FD = dyn_cast<FuncDecl>(VD)) {
321+
if (!baseTy->is<AnyMetatypeType>())
322+
declaredMemberType =
323+
declaredMemberType->castTo<AnyFunctionType>()->getResult();
324+
}
325+
if (auto *CD = dyn_cast<ConstructorDecl>(VD)) {
326+
if (!baseTy->is<AnyMetatypeType>())
327+
continue;
320328
declaredMemberType =
321329
declaredMemberType->castTo<AnyFunctionType>()->getResult();
330+
}
331+
}
322332

323-
auto fnType =
324-
baseTy->getTypeOfMember(DC.getParentModule(), VD, declaredMemberType);
333+
auto fnType = baseTy->getMetatypeInstanceType()->getTypeOfMember(
334+
DC.getParentModule(), VD, declaredMemberType);
325335

326336
if (!fnType)
327337
continue;
@@ -341,8 +351,8 @@ void collectPossibleCalleesByQualifiedLookup(
341351
DC.getASTContext(), &DC, CompletionTypeCheckKind::Normal, baseExpr, ref);
342352
if (!baseTyOpt)
343353
return;
344-
auto baseTy = (*baseTyOpt)->getRValueType()->getMetatypeInstanceType();
345-
if (!baseTy->mayHaveMembers())
354+
auto baseTy = (*baseTyOpt)->getRValueType();
355+
if (!baseTy->getMetatypeInstanceType()->mayHaveMembers())
346356
return;
347357

348358
collectPossibleCalleesByQualifiedLookup(DC, baseTy, name, candidates);
@@ -387,7 +397,7 @@ bool collectPossibleCalleesForApply(
387397
auto baseTy = AMT->getInstanceType();
388398
if (baseTy->mayHaveMembers())
389399
collectPossibleCalleesByQualifiedLookup(
390-
DC, baseTy, DeclBaseName::createConstructor(), candidates);
400+
DC, AMT, DeclBaseName::createConstructor(), candidates);
391401
}
392402
}
393403

test/IDE/complete_after_self.swift

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ class ThisDerived1 : ThisBase1 {
135135
// COMMON_SELF_NO_DOT_1: Begin completions
136136
// COMMON_SELF_NO_DOT_1-DAG: Decl[InstanceVar]/CurrNominal: .derivedInstanceVar[#Int#]{{; name=.+$}}
137137
// COMMON_SELF_NO_DOT_1-DAG: Decl[InstanceMethod]/CurrNominal: .derivedFunc0()[#Void#]{{; name=.+$}}
138-
// COMMON_SELF_NO_DOT_1-DAG: Decl[Subscript]/CurrNominal: [{#Double#}][#Int#]{{; name=.+$}}
138+
// COMMON_SELF_NO_DOT_1-DAG: Decl[Subscript]/CurrNominal: [{#(i): Double#}][#Int#]{{; name=.+$}}
139139
// COMMON_SELF_NO_DOT_1-DAG: Decl[Subscript]/CurrNominal: [{#s: String#}][#Int#]{{; name=.+$}}
140140
// COMMON_SELF_NO_DOT_1-DAG: Decl[InstanceMethod]/CurrNominal: .test1()[#Void#]{{; name=.+$}}
141141
// COMMON_SELF_NO_DOT_1-DAG: Decl[InstanceMethod]/CurrNominal: .test2()[#Void#]{{; name=.+$}}
@@ -145,7 +145,7 @@ class ThisDerived1 : ThisBase1 {
145145
// COMMON_SELF_NO_DOT_1-DAG: Decl[InstanceVar]/Super: .baseInstanceVar[#Int#]{{; name=.+$}}
146146
// COMMON_SELF_NO_DOT_1-DAG: Decl[InstanceMethod]/Super: .baseFunc0()[#Void#]{{; name=.+$}}
147147
// COMMON_SELF_NO_DOT_1-DAG: Decl[InstanceMethod]/Super: .baseFunc1({#(a): Int#})[#Void#]{{; name=.+$}}
148-
// COMMON_SELF_NO_DOT_1-DAG: Decl[Subscript]/Super: [{#Int#}][#Double#]{{; name=.+$}}
148+
// COMMON_SELF_NO_DOT_1-DAG: Decl[Subscript]/Super: [{#(i): Int#}][#Double#]{{; name=.+$}}
149149
// COMMON_SELF_NO_DOT_1-DAG: Decl[InstanceVar]/Super: .baseExtProp[#Int#]{{; name=.+$}}
150150
// COMMON_SELF_NO_DOT_1-DAG: Decl[InstanceMethod]/Super: .baseExtInstanceFunc0()[#Void#]{{; name=.+$}}
151151
// COMMON_SELF_NO_DOT_1-DAG: Decl[InstanceVar]/Super: .baseExtStaticProp[#Int#]{{; name=.+$}}
@@ -212,27 +212,27 @@ class ThisDerived1 : ThisBase1 {
212212
class func staticTest1() {
213213
self#^FUNC_STATIC_SELF_NO_DOT_1^#
214214
// FUNC_STATIC_SELF_NO_DOT_1: Begin completions
215-
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[InstanceMethod]/CurrNominal: .derivedFunc0({#self: ThisDerived1#})[#() -> Void#]
215+
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[InstanceMethod]/CurrNominal: .derivedFunc0({#(self): ThisDerived1#})[#() -> Void#]
216216
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[StaticVar]/CurrNominal: .derivedStaticVar[#Int#]
217217
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[StaticMethod]/CurrNominal: .derivedStaticFunc0()[#Void#]
218218
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[Constructor]/CurrNominal: .init()[#ThisDerived1#]
219219
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[Constructor]/CurrNominal: .init({#a: Int#})[#ThisDerived1#]
220-
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[InstanceMethod]/CurrNominal: .test1({#self: ThisDerived1#})[#() -> Void#]
221-
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[InstanceMethod]/CurrNominal: .test2({#self: ThisDerived1#})[#() -> Void#]
220+
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[InstanceMethod]/CurrNominal: .test1({#(self): ThisDerived1#})[#() -> Void#]
221+
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[InstanceMethod]/CurrNominal: .test2({#(self): ThisDerived1#})[#() -> Void#]
222222
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[StaticMethod]/CurrNominal: .staticTest1()[#Void#]
223223
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[StaticMethod]/CurrNominal: .staticTest2()[#Void#]
224-
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[InstanceMethod]/CurrNominal: .derivedExtInstanceFunc0({#self: ThisDerived1#})[#() -> Void#]
224+
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[InstanceMethod]/CurrNominal: .derivedExtInstanceFunc0({#(self): ThisDerived1#})[#() -> Void#]
225225
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[StaticMethod]/CurrNominal: .derivedExtStaticFunc0()[#Void#]
226226
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[Struct]/CurrNominal: .DerivedExtNestedStruct[#ThisDerived1.DerivedExtNestedStruct#]
227227
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[Class]/CurrNominal: .DerivedExtNestedClass[#ThisDerived1.DerivedExtNestedClass#]
228228
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[Enum]/CurrNominal: .DerivedExtNestedEnum[#ThisDerived1.DerivedExtNestedEnum#]
229229
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[TypeAlias]/CurrNominal: .DerivedExtNestedTypealias[#Int#]
230230
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[Constructor]/CurrNominal: .init({#someExtensionArg: Int#})[#ThisDerived1#]
231-
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[InstanceMethod]/Super: .baseFunc0({#self: ThisBase1#})[#() -> Void#]
232-
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[InstanceMethod]/Super: .baseFunc1({#self: ThisBase1#})[#(Int) -> Void#]
231+
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[InstanceMethod]/Super: .baseFunc0({#(self): ThisBase1#})[#() -> Void#]
232+
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[InstanceMethod]/Super: .baseFunc1({#(self): ThisBase1#})[#(Int) -> Void#]
233233
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[StaticVar]/Super: .baseStaticVar[#Int#]
234234
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[StaticMethod]/Super: .baseStaticFunc0()[#Void#]
235-
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[InstanceMethod]/Super: .baseExtInstanceFunc0({#self: ThisBase1#})[#() -> Void#]
235+
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[InstanceMethod]/Super: .baseExtInstanceFunc0({#(self): ThisBase1#})[#() -> Void#]
236236
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[StaticMethod]/Super: .baseExtStaticFunc0()[#Void#]
237237
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[Struct]/Super: .BaseExtNestedStruct[#ThisBase1.BaseExtNestedStruct#]
238238
// FUNC_STATIC_SELF_NO_DOT_1-NEXT: Decl[Class]/Super: .BaseExtNestedClass[#ThisBase1.BaseExtNestedClass#]
@@ -246,27 +246,27 @@ class ThisDerived1 : ThisBase1 {
246246
self.#^FUNC_STATIC_SELF_DOT_1^#
247247
// FUNC_STATIC_SELF_DOT_1: Begin completions
248248
// FUNC_STATIC_SELF_DOT_1-NEXT: Keyword[self]/CurrNominal: self[#ThisDerived1.Type#]; name=self
249-
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[InstanceMethod]/CurrNominal: derivedFunc0({#self: ThisDerived1#})[#() -> Void#]
249+
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[InstanceMethod]/CurrNominal: derivedFunc0({#(self): ThisDerived1#})[#() -> Void#]
250250
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[StaticVar]/CurrNominal: derivedStaticVar[#Int#]
251251
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[StaticMethod]/CurrNominal: derivedStaticFunc0()[#Void#]
252252
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[Constructor]/CurrNominal: init()[#ThisDerived1#]
253253
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[Constructor]/CurrNominal: init({#a: Int#})[#ThisDerived1#]
254-
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[InstanceMethod]/CurrNominal: test1({#self: ThisDerived1#})[#() -> Void#]
255-
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[InstanceMethod]/CurrNominal: test2({#self: ThisDerived1#})[#() -> Void#]
254+
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[InstanceMethod]/CurrNominal: test1({#(self): ThisDerived1#})[#() -> Void#]
255+
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[InstanceMethod]/CurrNominal: test2({#(self): ThisDerived1#})[#() -> Void#]
256256
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[StaticMethod]/CurrNominal: staticTest1()[#Void#]
257257
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[StaticMethod]/CurrNominal: staticTest2()[#Void#]
258-
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[InstanceMethod]/CurrNominal: derivedExtInstanceFunc0({#self: ThisDerived1#})[#() -> Void#]
258+
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[InstanceMethod]/CurrNominal: derivedExtInstanceFunc0({#(self): ThisDerived1#})[#() -> Void#]
259259
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[StaticMethod]/CurrNominal: derivedExtStaticFunc0()[#Void#]
260260
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[Struct]/CurrNominal: DerivedExtNestedStruct[#ThisDerived1.DerivedExtNestedStruct#]
261261
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[Class]/CurrNominal: DerivedExtNestedClass[#ThisDerived1.DerivedExtNestedClass#]
262262
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[Enum]/CurrNominal: DerivedExtNestedEnum[#ThisDerived1.DerivedExtNestedEnum#]
263263
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[TypeAlias]/CurrNominal: DerivedExtNestedTypealias[#Int#]
264264
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[Constructor]/CurrNominal: init({#someExtensionArg: Int#})[#ThisDerived1#]
265-
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[InstanceMethod]/Super: baseFunc0({#self: ThisBase1#})[#() -> Void#]
266-
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[InstanceMethod]/Super: baseFunc1({#self: ThisBase1#})[#(Int) -> Void#]
265+
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[InstanceMethod]/Super: baseFunc0({#(self): ThisBase1#})[#() -> Void#]
266+
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[InstanceMethod]/Super: baseFunc1({#(self): ThisBase1#})[#(Int) -> Void#]
267267
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[StaticVar]/Super: baseStaticVar[#Int#]
268268
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[StaticMethod]/Super: baseStaticFunc0()[#Void#]
269-
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[InstanceMethod]/Super: baseExtInstanceFunc0({#self: ThisBase1#})[#() -> Void#]
269+
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[InstanceMethod]/Super: baseExtInstanceFunc0({#(self): ThisBase1#})[#() -> Void#]
270270
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[StaticMethod]/Super: baseExtStaticFunc0()[#Void#]
271271
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[Struct]/Super: BaseExtNestedStruct[#ThisBase1.BaseExtNestedStruct#]
272272
// FUNC_STATIC_SELF_DOT_1-NEXT: Decl[Class]/Super: BaseExtNestedClass[#ThisBase1.BaseExtNestedClass#]

test/IDE/complete_after_super.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ extension SuperBaseA {
213213
// COMMON_BASE_A_NO_DOT-DAG: Decl[InstanceVar]/CurrNominal: .baseProp[#Int#]{{; name=.+$}}
214214
// COMMON_BASE_A_NO_DOT-DAG: Decl[InstanceMethod]/CurrNominal: .baseFunc0()[#Void#]{{; name=.+$}}
215215
// COMMON_BASE_A_NO_DOT-DAG: Decl[InstanceMethod]/CurrNominal: .baseFunc1({#(a): Int#})[#Void#]{{; name=.+$}}
216-
// COMMON_BASE_A_NO_DOT-DAG: Decl[Subscript]/CurrNominal: [{#Int#}][#Double#]{{; name=.+$}}
216+
// COMMON_BASE_A_NO_DOT-DAG: Decl[Subscript]/CurrNominal: [{#(i): Int#}][#Double#]{{; name=.+$}}
217217
// COMMON_BASE_A_NO_DOT-DAG: Decl[InstanceVar]/CurrNominal: .baseExtProp[#Int#]{{; name=.+$}}
218218
// COMMON_BASE_A_NO_DOT-DAG: Decl[InstanceMethod]/CurrNominal: .baseExtFunc0()[#Void#]{{; name=.+$}}
219219
// COMMON_BASE_A_NO_DOT: End completions
@@ -297,7 +297,7 @@ class SuperDerivedA : SuperBaseA {
297297
// COMMON_BASE_B_NO_DOT-DAG: Decl[InstanceVar]/CurrNominal: .baseProp[#Int#]{{; name=.+$}}
298298
// COMMON_BASE_B_NO_DOT-DAG: Decl[InstanceMethod]/CurrNominal: .baseFunc0()[#Void#]{{; name=.+$}}
299299
// COMMON_BASE_B_NO_DOT-DAG: Decl[InstanceMethod]/CurrNominal: .baseFunc1({#(a): Int#})[#Void#]{{; name=.+$}}
300-
// COMMON_BASE_B_NO_DOT-DAG: Decl[Subscript]/CurrNominal: [{#Int#}][#Double#]{{; name=.+$}}
300+
// COMMON_BASE_B_NO_DOT-DAG: Decl[Subscript]/CurrNominal: [{#(i): Int#}][#Double#]{{; name=.+$}}
301301
// COMMON_BASE_B_NO_DOT-DAG: Decl[InstanceVar]/CurrNominal: .baseExtProp[#Int#]{{; name=.+$}}
302302
// COMMON_BASE_B_NO_DOT-DAG: Decl[InstanceMethod]/CurrNominal: .baseExtFunc0()[#Void#]{{; name=.+$}}
303303
// COMMON_BASE_B_NO_DOT: End completions

0 commit comments

Comments
 (0)