Skip to content

Commit 040466a

Browse files
authored
Merge pull request #11258 from marcelofabri/add-subscript-to-sourcekit-structure
2 parents 5528999 + c4dad0c commit 040466a

File tree

5 files changed

+42
-2
lines changed

5 files changed

+42
-2
lines changed

include/swift/IDE/SyntaxModel.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ enum class SyntaxStructureKind : uint8_t {
9393
EnumCase,
9494
EnumElement,
9595
TypeAlias,
96+
Subscript,
9697

9798
ForEachStatement,
9899
ForStatement,
@@ -139,13 +140,14 @@ struct SyntaxStructureNode {
139140
std::vector<CharSourceRange> InheritedTypeRanges;
140141
std::vector<SyntaxStructureElement> Elements;
141142

142-
bool isVariable() const {
143+
bool hasSubstructure() const {
143144
switch (Kind) {
144145
case SyntaxStructureKind::GlobalVariable:
145146
case SyntaxStructureKind::InstanceVariable:
146147
case SyntaxStructureKind::StaticVariable:
147148
case SyntaxStructureKind::ClassVariable:
148149
case SyntaxStructureKind::Parameter:
150+
case SyntaxStructureKind::Subscript:
149151
return true;
150152
default:
151153
return false;

lib/IDE/SyntaxModel.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,16 @@ bool ModelASTWalker::walkToDeclPre(Decl *D) {
964964
TypeAliasD->getName().getLength());
965965
SN.Attrs = TypeAliasD->getAttrs();
966966
pushStructureNode(SN, TypeAliasD);
967+
} else if (auto *SubscriptD = dyn_cast<SubscriptDecl>(D)) {
968+
SyntaxStructureNode SN;
969+
SN.Dcl = SubscriptD;
970+
SN.Kind = SyntaxStructureKind::Subscript;
971+
SN.Range = charSourceRangeFromSourceRange(SM,
972+
SubscriptD->getSourceRange());
973+
SN.BodyRange = innerCharSourceRangeFromSourceRange(SM,
974+
SubscriptD->getBracesRange());
975+
SN.Attrs = SubscriptD->getAttrs();
976+
pushStructureNode(SN, SubscriptD);
967977
}
968978

969979
return true;
@@ -1253,7 +1263,7 @@ bool ModelASTWalker::popStructureNode() {
12531263

12541264
// VarDecls are popped before we see their TypeRepr, so if we pass the token
12551265
// nodes now they will not change from identifier to a type-identifier.
1256-
if (!Node.isVariable()) {
1266+
if (!Node.hasSubstructure()) {
12571267
if (!passTokenNodesUntil(Node.Range.getEnd(), IncludeNodeAtLocation))
12581268
return false;
12591269
}

test/IDE/structure.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,28 @@ class A {
187187

188188
// CHECK: <typealias>typealias <name>OtherA</name> = A</typealias>
189189
typealias OtherA = A
190+
191+
class SubscriptTest {
192+
subscript(index: Int) -> Int {
193+
return 0
194+
}
195+
// CHECK: <subscript>subscript(<param>index: Int</param>) -> Int {
196+
// CHECK: return 0
197+
// CHECK: }</subscript>
198+
199+
subscript(string: String) -> Int {
200+
get {
201+
return 0
202+
}
203+
set(value) {
204+
print(value)
205+
}
206+
}
207+
// CHECK: <subscript>subscript(<param>string: String</param>) -> Int {
208+
// CHECK: get {
209+
// CHECK: return 0
210+
// CHECK: }
211+
// CHECK: set(<param>value</param>) {
212+
// CHECK: <call><name>print</name>(value)</call>
213+
// CHECK: }</subscript>
214+
}

tools/SourceKit/lib/SwiftLang/SwiftLangSupport.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,8 @@ UIdent SwiftLangSupport::getUIDForSyntaxStructureKind(
377377
return KindDeclEnumElement;
378378
case SyntaxStructureKind::TypeAlias:
379379
return KindDeclTypeAlias;
380+
case SyntaxStructureKind::Subscript:
381+
return KindDeclSubscript;
380382
case SyntaxStructureKind::Parameter:
381383
return KindDeclVarParam;
382384
case SyntaxStructureKind::ForEachStatement:

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,6 +1070,7 @@ class StructureAnnotator : public ide::SyntaxModelWalker {
10701070
case SyntaxStructureKind::EnumCase: return "enum-case";
10711071
case SyntaxStructureKind::EnumElement: return "enum-elem";
10721072
case SyntaxStructureKind::TypeAlias: return "typealias";
1073+
case SyntaxStructureKind::Subscript: return "subscript";
10731074
case SyntaxStructureKind::Parameter: return "param";
10741075
case SyntaxStructureKind::ForEachStatement: return "foreach";
10751076
case SyntaxStructureKind::ForStatement: return "for";

0 commit comments

Comments
 (0)