Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion include/swift/IDE/SyntaxModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ enum class SyntaxStructureKind : uint8_t {
EnumCase,
EnumElement,
TypeAlias,
Subscript,

ForEachStatement,
ForStatement,
Expand Down Expand Up @@ -139,13 +140,14 @@ struct SyntaxStructureNode {
std::vector<CharSourceRange> InheritedTypeRanges;
std::vector<SyntaxStructureElement> Elements;

bool isVariable() const {
bool hasSubstructure() const {
switch (Kind) {
case SyntaxStructureKind::GlobalVariable:
case SyntaxStructureKind::InstanceVariable:
case SyntaxStructureKind::StaticVariable:
case SyntaxStructureKind::ClassVariable:
case SyntaxStructureKind::Parameter:
case SyntaxStructureKind::Subscript:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this is the proper solution or if it's better to:

  1. Rename this function
  2. Whenever using isVariable also check if Kind == Subscript

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens if we don't add this line here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the syntax kinds of get and set inside subscripts become identifier instead of keyword

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This accessor (isVariable()) is only being used in one place in the Syntax Model and only for things that have substructure that needs to be walked. Would it be OK to just rename it hasSubstructure() or something?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @CodaFi . Renaming it sounds a right approach to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed and amended the commit.

return true;
default:
return false;
Expand Down
12 changes: 11 additions & 1 deletion lib/IDE/SyntaxModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,16 @@ bool ModelASTWalker::walkToDeclPre(Decl *D) {
TypeAliasD->getName().getLength());
SN.Attrs = TypeAliasD->getAttrs();
pushStructureNode(SN, TypeAliasD);
} else if (auto *SubscriptD = dyn_cast<SubscriptDecl>(D)) {
SyntaxStructureNode SN;
SN.Dcl = SubscriptD;
SN.Kind = SyntaxStructureKind::Subscript;
SN.Range = charSourceRangeFromSourceRange(SM,
SubscriptD->getSourceRange());
SN.BodyRange = innerCharSourceRangeFromSourceRange(SM,
SubscriptD->getBracesRange());
SN.Attrs = SubscriptD->getAttrs();
pushStructureNode(SN, SubscriptD);
}

return true;
Expand Down Expand Up @@ -1253,7 +1263,7 @@ bool ModelASTWalker::popStructureNode() {

// VarDecls are popped before we see their TypeRepr, so if we pass the token
// nodes now they will not change from identifier to a type-identifier.
if (!Node.isVariable()) {
if (!Node.hasSubstructure()) {
if (!passTokenNodesUntil(Node.Range.getEnd(), IncludeNodeAtLocation))
return false;
}
Expand Down
25 changes: 25 additions & 0 deletions test/IDE/structure.swift
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,28 @@ class A {

// CHECK: <typealias>typealias <name>OtherA</name> = A</typealias>
typealias OtherA = A

class SubscriptTest {
subscript(index: Int) -> Int {
return 0
}
// CHECK: <subscript>subscript(<param>index: Int</param>) -> Int {
// CHECK: return 0
// CHECK: }</subscript>

subscript(string: String) -> Int {
get {
return 0
}
set(value) {
print(value)
}
}
// CHECK: <subscript>subscript(<param>string: String</param>) -> Int {
// CHECK: get {
// CHECK: return 0
// CHECK: }
// CHECK: set(<param>value</param>) {
// CHECK: <call><name>print</name>(value)</call>
// CHECK: }</subscript>
}
2 changes: 2 additions & 0 deletions tools/SourceKit/lib/SwiftLang/SwiftLangSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,8 @@ UIdent SwiftLangSupport::getUIDForSyntaxStructureKind(
return KindDeclEnumElement;
case SyntaxStructureKind::TypeAlias:
return KindDeclTypeAlias;
case SyntaxStructureKind::Subscript:
return KindDeclSubscript;
case SyntaxStructureKind::Parameter:
return KindDeclVarParam;
case SyntaxStructureKind::ForEachStatement:
Expand Down
1 change: 1 addition & 0 deletions tools/swift-ide-test/swift-ide-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,7 @@ class StructureAnnotator : public ide::SyntaxModelWalker {
case SyntaxStructureKind::EnumCase: return "enum-case";
case SyntaxStructureKind::EnumElement: return "enum-elem";
case SyntaxStructureKind::TypeAlias: return "typealias";
case SyntaxStructureKind::Subscript: return "subscript";
case SyntaxStructureKind::Parameter: return "param";
case SyntaxStructureKind::ForEachStatement: return "foreach";
case SyntaxStructureKind::ForStatement: return "for";
Expand Down