Skip to content

Minor internal API improvements #255

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 21, 2021
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: 2 additions & 2 deletions Sources/SwiftSyntax/SyntaxChildren.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ struct RawSyntaxChildren: BidirectionalCollection {
if index.indexInParent + 1 < numberOfChildren {
// Compute the next materialized index
let nodeLength = UInt32(node?.totalLength.utf8Length ?? 0)
let advancedIndexInTree = index.indexInTree.advancedBySibling(node)
let advancedIndexInTree = index.indexInTree.advancedBy(node)
return SyntaxChildrenIndex(offset: index.offset + nodeLength,
indexInParent: index.indexInParent + 1,
indexInTree: advancedIndexInTree)
Expand All @@ -176,7 +176,7 @@ struct RawSyntaxChildren: BidirectionalCollection {
// We are reversing a non-end index.
let previousNode = parent.child(at: Int(index.indexInParent - 1))
let previousNodeLength = UInt32(previousNode?.totalLength.utf8Length ?? 0)
let reversedIndexInTree = index.indexInTree.reversedBySibling(previousNode)
let reversedIndexInTree = index.indexInTree.reversedBy(previousNode)
return SyntaxChildrenIndex(offset: index.offset - previousNodeLength,
indexInParent: index.indexInParent - 1,
indexInTree: reversedIndexInTree)
Expand Down
35 changes: 8 additions & 27 deletions Sources/SwiftSyntax/SyntaxData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@ struct AbsoluteSyntaxPosition {
return .init(offset: self.offset, indexInParent: 0)
}

func advancedToEndOfChildren(_ raw: RawSyntax) -> AbsoluteSyntaxPosition {
let newOffset = self.offset + UInt32(truncatingIfNeeded: raw.totalLength.utf8Length)
let newIndexInParent = UInt32(truncatingIfNeeded: raw.numberOfChildren)
return .init(offset: newOffset, indexInParent: newIndexInParent)
}

static var forRoot: AbsoluteSyntaxPosition {
return .init(offset: 0, indexInParent: 0)
}
Expand Down Expand Up @@ -70,12 +64,6 @@ struct AbsoluteSyntaxInfo {
return .init(position: newPosition, nodeId: newNodeId)
}

func advancedToEndOfChildren(_ raw: RawSyntax) -> AbsoluteSyntaxInfo {
let newPosition = position.advancedToEndOfChildren(raw)
let newNodeId = nodeId.advancedToEndOfChildren(raw)
return .init(position: newPosition, nodeId: newNodeId)
}

static var forRoot: AbsoluteSyntaxInfo {
return .init(position: .forRoot, nodeId: .newRoot())
}
Expand All @@ -87,13 +75,17 @@ struct SyntaxIndexInTree: Hashable {

static var zero: SyntaxIndexInTree = SyntaxIndexInTree(indexInTree: 0)

func advancedBySibling(_ raw: RawSyntax?) -> SyntaxIndexInTree {
/// Assuming that this index points to the start of `Raw`, so that it points
/// to the next sibling of `Raw`.
func advancedBy(_ raw: RawSyntax?) -> SyntaxIndexInTree {
let newIndexInTree = self.indexInTree +
UInt32(truncatingIfNeeded: raw?.totalNodes ?? 0)
return .init(indexInTree: newIndexInTree)
}

func reversedBySibling(_ raw: RawSyntax?) -> SyntaxIndexInTree {
/// Assuming that this index points to the next sibling of `Raw`, reverse it
/// so that it points to the start of `Raw`.
func reversedBy(_ raw: RawSyntax?) -> SyntaxIndexInTree {
let newIndexInTree = self.indexInTree -
UInt32(truncatingIfNeeded: raw?.totalNodes ?? 0)
return .init(indexInTree: newIndexInTree)
Expand All @@ -104,12 +96,6 @@ struct SyntaxIndexInTree: Hashable {
return .init(indexInTree: newIndexInTree)
}

func advancedToEndOfChildren(_ raw: RawSyntax) -> SyntaxIndexInTree {
let newIndexInTree = self.indexInTree +
UInt32(truncatingIfNeeded: raw.totalNodes)
return .init(indexInTree: newIndexInTree)
}

init(indexInTree: UInt32) {
self.indexInTree = indexInTree
}
Expand All @@ -123,12 +109,12 @@ public struct SyntaxIdentifier: Hashable {
let indexInTree: SyntaxIndexInTree

func advancedBySibling(_ raw: RawSyntax?) -> SyntaxIdentifier {
let newIndexInTree = indexInTree.advancedBySibling(raw)
let newIndexInTree = indexInTree.advancedBy(raw)
return .init(rootId: self.rootId, indexInTree: newIndexInTree)
}

func reversedBySibling(_ raw: RawSyntax?) -> SyntaxIdentifier {
let newIndexInTree = self.indexInTree.reversedBySibling(raw)
let newIndexInTree = self.indexInTree.reversedBy(raw)
return .init(rootId: self.rootId, indexInTree: newIndexInTree)
}

Expand All @@ -137,11 +123,6 @@ public struct SyntaxIdentifier: Hashable {
return .init(rootId: self.rootId, indexInTree: newIndexInTree)
}

func advancedToEndOfChildren(_ raw: RawSyntax) -> SyntaxIdentifier {
let newIndexInTree = self.indexInTree.advancedToEndOfChildren(raw)
return .init(rootId: self.rootId, indexInTree: newIndexInTree)
}

static func newRoot() -> SyntaxIdentifier {
return .init(rootId: UInt32(truncatingIfNeeded: AtomicCounter.next()),
indexInTree: .zero)
Expand Down