Skip to content

Commit cfe1e35

Browse files
rintarokitasuke
authored andcommitted
[SourceKit] Force print '?' for optional method call in code completion. (swiftlang#16910)
Calling '@objc optional func' requires '?' or '!' after its name. When completing method calls for them, 'key.sourcetext' should have '?' whereas 'key.name' shouldn't. Note that we deliberately do not use optional type name for 'key.typename'. This is consistent with optional chain '?.<propertyName>' behavior. rdar://problem/37904574
1 parent 2a6f58d commit cfe1e35

File tree

6 files changed

+41
-5
lines changed

6 files changed

+41
-5
lines changed

lib/IDE/CodeCompletion.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,6 +1232,7 @@ void CodeCompletionString::getName(raw_ostream &OS) const {
12321232
case ChunkKind::TypeAnnotation:
12331233
case ChunkKind::CallParameterClosureType:
12341234
case ChunkKind::DeclAttrParamColon:
1235+
case ChunkKind::OptionalMethodCallTail:
12351236
continue;
12361237
case ChunkKind::ThrowsKeyword:
12371238
case ChunkKind::RethrowsKeyword:

lib/IDE/CodeCompletionResultBuilder.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,8 +426,7 @@ class CodeCompletionResultBuilder {
426426

427427
void addOptionalMethodCallTail() {
428428
addChunkWithTextNoCopy(
429-
CodeCompletionString::Chunk::ChunkKind::OptionalMethodCallTail, "!");
430-
getLastChunk().setIsAnnotation();
429+
CodeCompletionString::Chunk::ChunkKind::OptionalMethodCallTail, "?");
431430
}
432431

433432
void addTypeAnnotation(StringRef Type) {

test/IDE/complete_members_optional.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func optionalMembers1(_ a: HasOptionalMembers1) {
2929
a.#^OPTIONAL_MEMBERS_1^#
3030
}
3131
// OPTIONAL_MEMBERS_1: Begin completions, 3 items
32-
// OPTIONAL_MEMBERS_1-DAG: Decl[InstanceMethod]/CurrNominal: optionalInstanceFunc!()[#Int#]{{; name=.+$}}
32+
// OPTIONAL_MEMBERS_1-DAG: Decl[InstanceMethod]/CurrNominal: optionalInstanceFunc?()[#Int#]{{; name=.+$}}
3333
// OPTIONAL_MEMBERS_1-DAG: Decl[InstanceVar]/CurrNominal: optionalInstanceProperty[#Int?#]{{; name=.+$}}
3434
// OPTIONAL_MEMBERS_1-DAG: Keyword[self]/CurrNominal: self[#HasOptionalMembers1#]; name=self
3535
// OPTIONAL_MEMBERS_1: End completions
@@ -38,8 +38,8 @@ func optionalMembers2<T : HasOptionalMembers1>(_ a: T) {
3838
T.#^OPTIONAL_MEMBERS_2^#
3939
}
4040
// OPTIONAL_MEMBERS_2: Begin completions, 4 items
41-
// OPTIONAL_MEMBERS_2-DAG: Decl[InstanceMethod]/Super: optionalInstanceFunc!({#self: HasOptionalMembers1#})[#() -> Int#]{{; name=.+$}}
42-
// OPTIONAL_MEMBERS_2-DAG: Decl[StaticMethod]/Super: optionalClassFunc!()[#Int#]{{; name=.+$}}
41+
// OPTIONAL_MEMBERS_2-DAG: Decl[InstanceMethod]/Super: optionalInstanceFunc?({#self: HasOptionalMembers1#})[#() -> Int#]{{; name=.+$}}
42+
// OPTIONAL_MEMBERS_2-DAG: Decl[StaticMethod]/Super: optionalClassFunc?()[#Int#]{{; name=.+$}}
4343
// OPTIONAL_MEMBERS_2-DAG: Decl[StaticVar]/Super: optionalClassProperty[#Int?#]{{; name=.+$}}
4444
// OPTIONAL_MEMBERS_2-DAG: Keyword[self]/CurrNominal: self[#T.Type#]; name=self
4545
// OPTIONAL_MEMBERS_2: End completions
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@objc protocol Proto {
2+
@objc optional func optionalMethod() -> Int
3+
}
4+
5+
func test<T : Proto>(obj: T) {
6+
let _ = obj.
7+
}
8+
9+
// RUN: %sourcekitd-test -req=complete -pos=6:15 %s -- %s > %t.response
10+
// RUN: diff -u %s.response %t.response
11+
// REQUIRES: objc_interop
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
key.results: [
3+
{
4+
key.kind: source.lang.swift.decl.function.method.instance,
5+
key.name: "optionalMethod()",
6+
key.sourcetext: "optionalMethod?()",
7+
key.description: "optionalMethod?()",
8+
key.typename: "Int",
9+
key.context: source.codecompletion.context.superclass,
10+
key.num_bytes_to_erase: 0,
11+
key.associated_usrs: "c:@M@complete_optionalmethod@objc(pl)Proto(im)optionalMethod",
12+
key.modulename: "complete_optionalmethod"
13+
},
14+
{
15+
key.kind: source.lang.swift.keyword,
16+
key.name: "self",
17+
key.sourcetext: "self",
18+
key.description: "self",
19+
key.typename: "T",
20+
key.context: source.codecompletion.context.thisclass,
21+
key.num_bytes_to_erase: 0
22+
}
23+
]
24+
}

tools/SourceKit/lib/SwiftLang/CodeCompletionOrganizer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,6 +1212,7 @@ void CompletionBuilder::getFilterName(CodeCompletionString *str,
12121212
case ChunkKind::Whitespace:
12131213
case ChunkKind::Ellipsis:
12141214
case ChunkKind::Ampersand:
1215+
case ChunkKind::OptionalMethodCallTail:
12151216
continue;
12161217
case ChunkKind::CallParameterColon:
12171218
// Since we don't add the type, also don't add the space after ':'.

0 commit comments

Comments
 (0)