Skip to content

Commit b6ecf9c

Browse files
marcelofabrinkcsgexi
authored andcommitted
[SourceKit] Introduce "source.lang.swift.expr.closure" in structure (SR-6116) (#13617)
1 parent f2262bc commit b6ecf9c

File tree

7 files changed

+48
-16
lines changed

7 files changed

+48
-16
lines changed

include/swift/IDE/SyntaxModel.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ enum class SyntaxStructureKind : uint8_t {
111111
ArrayExpression,
112112
DictionaryExpression,
113113
ObjectLiteralExpression,
114-
TupleExpression
114+
TupleExpression,
115+
ClosureExpression
115116
};
116117

117118
enum class SyntaxStructureElementKind : uint8_t {

lib/IDE/SyntaxModel.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,16 @@ std::pair<bool, Expr *> ModelASTWalker::walkToExprPre(Expr *E) {
526526

527527
pushStructureNode(SN, Tup);
528528
}
529+
} else if (auto *Closure = dyn_cast<ClosureExpr>(E)) {
530+
SyntaxStructureNode SN;
531+
SN.Kind = SyntaxStructureKind::ClosureExpression;
532+
SN.Range = charSourceRangeFromSourceRange(SM, E->getSourceRange());
533+
SN.BodyRange = innerCharSourceRangeFromSourceRange(SM, E->getSourceRange());
534+
if (Closure->hasExplicitResultType())
535+
SN.TypeRange = charSourceRangeFromSourceRange(SM,
536+
Closure->getExplicitResultTypeLoc().getSourceRange());
537+
538+
pushStructureNode(SN, Closure);
529539
}
530540

531541
return { true, E };

test/IDE/structure.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ func <#test1#> () {
119119
let myArray = [<#item1#>, <#item2#>]
120120

121121
// CHECK: <ffunc>func <name>test1()</name> {
122-
// CHECK: <call><name>dispatch_async</name>(<arg><call><name>dispatch_get_main_queue</name>()</call></arg>, <arg><brace>{}</brace></arg>)</call>
123-
// CHECK: <call><name>dispatch_async</name>(<arg><call><name>dispatch_get_main_queue</name>()</call></arg>) <arg><brace>{}</brace></arg></call>
122+
// CHECK: <call><name>dispatch_async</name>(<arg><call><name>dispatch_get_main_queue</name>()</call></arg>, <arg><closure><brace>{}</brace></closure></arg>)</call>
123+
// CHECK: <call><name>dispatch_async</name>(<arg><call><name>dispatch_get_main_queue</name>()</call></arg>) <arg><closure><brace>{}</brace></closure></arg></call>
124124
// CHECK: }</ffunc>
125125
func test1() {
126126
dispatch_async(dispatch_get_main_queue(), {})
@@ -149,8 +149,8 @@ enum Rawness : Int {
149149
case Two = 2, Three = 3
150150
}
151151

152-
// CHECK: <ffunc>func <name>rethrowFunc(<param>_ f: <type>() throws -> ()</type></param>)</name> rethrows {}</ffunc>
153-
func rethrowFunc(_ f: () throws -> ()) rethrows {}
152+
// CHECK: <ffunc>func <name>rethrowFunc(<param>_ f: <type>() throws -> ()</type> = <closure><brace>{}</brace></closure></param>)</name> rethrows {}</ffunc>
153+
func rethrowFunc(_ f: () throws -> () = {}) rethrows {}
154154

155155
class NestedPoundIf{
156156
func foo1() {
@@ -179,7 +179,7 @@ class NestedPoundIf{
179179
class A {
180180
func foo(_ i : Int, animations: () -> ()) {}
181181
func perform() {foo(5, animations: {})}
182-
// CHECK: <ifunc>func <name>perform()</name> {<call><name>foo</name>(<arg>5</arg>, <arg><name>animations</name>: <brace>{}</brace></arg>)</call>}</ifunc>
182+
// CHECK: <ifunc>func <name>perform()</name> {<call><name>foo</name>(<arg>5</arg>, <arg><name>animations</name>: <closure><brace>{}</brace></closure></arg>)</call>}</ifunc>
183183
}
184184

185185
// CHECK: <typealias>typealias <name>OtherA</name> = A</typealias>
@@ -250,3 +250,9 @@ struct Tuples {
250250
}
251251
}
252252

253+
completion(a: 1) { (x: Int, y: Int) -> Int in
254+
return x + y
255+
}
256+
// CHECK: <call><name>completion</name>(<arg><name>a</name>: 1</arg>) <arg><closure>{ (<param>x: <type>Int</type></param>, <param>y: <type>Int</type></param>) -> <type>Int</type> in
257+
// CHECK: return x + y
258+
// CHECK: }</closure></arg></call>

test/SourceKit/DocumentStructure/structure.swift.invalid.response

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
]
3939
},
4040
{
41-
key.kind: source.lang.swift.stmt.brace,
41+
key.kind: source.lang.swift.expr.closure,
4242
key.offset: 57,
4343
key.length: 24,
4444
key.nameoffset: 0,
@@ -47,15 +47,26 @@
4747
key.bodylength: 22,
4848
key.substructure: [
4949
{
50-
key.kind: source.lang.swift.decl.class,
51-
key.accessibility: source.lang.swift.accessibility.private,
52-
key.name: "MyCoolClass",
53-
key.offset: 59,
54-
key.length: 22,
55-
key.nameoffset: 65,
56-
key.namelength: 11,
57-
key.bodyoffset: 78,
58-
key.bodylength: 2
50+
key.kind: source.lang.swift.stmt.brace,
51+
key.offset: 57,
52+
key.length: 24,
53+
key.nameoffset: 0,
54+
key.namelength: 0,
55+
key.bodyoffset: 58,
56+
key.bodylength: 22,
57+
key.substructure: [
58+
{
59+
key.kind: source.lang.swift.decl.class,
60+
key.accessibility: source.lang.swift.accessibility.private,
61+
key.name: "MyCoolClass",
62+
key.offset: 59,
63+
key.length: 22,
64+
key.nameoffset: 65,
65+
key.namelength: 11,
66+
key.bodyoffset: 78,
67+
key.bodylength: 2
68+
}
69+
]
5970
}
6071
]
6172
}

tools/SourceKit/include/SourceKit/Core/ProtocolUIDs.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ KIND(ExprArray, "source.lang.swift.expr.array")
273273
KIND(ExprDictionary, "source.lang.swift.expr.dictionary")
274274
KIND(ExprObjectLiteral, "source.lang.swift.expr.object_literal")
275275
KIND(ExprTuple, "source.lang.swift.expr.tuple")
276+
KIND(ExprClosure, "source.lang.swift.expr.closure")
276277
KIND(StructureElemId, "source.lang.swift.structure.elem.id")
277278
KIND(StructureElemExpr, "source.lang.swift.structure.elem.expr")
278279
KIND(StructureElemInitExpr, "source.lang.swift.structure.elem.init_expr")

tools/SourceKit/lib/SwiftLang/SwiftLangSupport.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,8 @@ UIdent SwiftLangSupport::getUIDForSyntaxStructureKind(
423423
return KindExprObjectLiteral;
424424
case SyntaxStructureKind::TupleExpression:
425425
return KindExprTuple;
426+
case SyntaxStructureKind::ClosureExpression:
427+
return KindExprClosure;
426428
case SyntaxStructureKind::Argument:
427429
return KindExprArg;
428430
}

tools/swift-ide-test/swift-ide-test.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,6 +1106,7 @@ class StructureAnnotator : public ide::SyntaxModelWalker {
11061106
case SyntaxStructureKind::ObjectLiteralExpression:
11071107
return "object-literal-expression";
11081108
case SyntaxStructureKind::TupleExpression: return "tuple";
1109+
case SyntaxStructureKind::ClosureExpression: return "closure";
11091110
}
11101111
llvm_unreachable("unhandled tag?");
11111112
}

0 commit comments

Comments
 (0)