1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
// MARK: - Index
/// The data for an index in a syntax children collection that is not the end
/// index. See ``SyntaxChildrenIndex`` for the representation of the end index.
struct SyntaxChildrenIndexData: Hashable, Comparable, Sendable {
/// The UTF-8 offset of the item at this index in the source file
/// See `AbsoluteSyntaxPosition.offset`
let offset: UInt32
/// The index of the node in the parent.
/// See `AbsoluteSyntaxPosition.indexInParent`
let indexInParent: UInt32
/// Unique value for a node within its own tree.
/// See ``SyntaxIdentifier/indexInTree``
let indexInTree: SyntaxIdentifier.SyntaxIndexInTree
static func < (
lhs: SyntaxChildrenIndexData,
rhs: SyntaxChildrenIndexData
) -> Bool {
return lhs.indexInParent < rhs.indexInParent
}
fileprivate init(
offset: UInt32,
indexInParent: UInt32,
indexInTree: SyntaxIdentifier.SyntaxIndexInTree
) {
self.offset = offset
self.indexInParent = indexInParent
self.indexInTree = indexInTree
}
init(_ absoluteSyntaxInfo: AbsoluteSyntaxInfo) {
self.offset = absoluteSyntaxInfo.offset
self.indexInParent = absoluteSyntaxInfo.indexInParent
self.indexInTree = absoluteSyntaxInfo.nodeId.indexInTree
}
}
/// An index in a syntax children collection.
public struct SyntaxChildrenIndex: Hashable, Comparable, ExpressibleByNilLiteral, Sendable {
/// Construct the `endIndex` of a ``SyntaxChildren`` collection.
public init(nilLiteral: ()) {
self.data = nil
}
// Performance considerations:
// It is faster to use a special end index than computing a materialized index
// that stores past the end of the collection if forward iteration is the only
// thing that's needed.
/// `nil` represents the end index and `.some` represents a materialized index
/// that points into a collection.
/// It is faster to use `Optional` here rather than making
/// `SyntaxChildrenIndex` an enum because the optional value can be
/// force-unwrapped when we know that an index is not the end index, saving a
/// switch case comparison.
let data: SyntaxChildrenIndexData?
fileprivate init(
offset: UInt32,
indexInParent: UInt32,
indexInTree: SyntaxIdentifier.SyntaxIndexInTree
) {
self.data = SyntaxChildrenIndexData(
offset: offset,
indexInParent: indexInParent,
indexInTree: indexInTree
)
}
internal init(_ absoluteSyntaxInfo: AbsoluteSyntaxInfo) {
self.data = SyntaxChildrenIndexData(absoluteSyntaxInfo)
}
/// Returns `true` if `lhs` occurs before `rhs`.
public static func < (lhs: SyntaxChildrenIndex, rhs: SyntaxChildrenIndex) -> Bool {
switch (lhs.data, rhs.data) {
case (.some(let lhsData), .some(let rhsData)):
return lhsData < rhsData
case (.some(_), .none):
return true
case (.none, .some(_)):
return false
case (.none, .none):
return false
}
}
}
fileprivate extension AbsoluteSyntaxInfo {
/// Construct `AbsoluteSyntaxInfo` from the given index data and a `rootId`.
init(index: SyntaxChildrenIndexData, rootId: UInt) {
let position = AbsoluteSyntaxPosition(
offset: index.offset,
indexInParent: index.indexInParent
)
let identifier = SyntaxIdentifier(
rootId: rootId,
indexInTree: index.indexInTree
)
self.init(position: position, nodeId: identifier)
}
}
// MARK: - Collections
/// Collection that contains the child nodes of a raw node (including missing
/// nodes), along with their associated `AbsoluteSyntaxInfo`.
struct RawSyntaxChildren: BidirectionalCollection, Sendable {
typealias Element = (raw: RawSyntax?, syntaxInfo: AbsoluteSyntaxInfo)
typealias Index = SyntaxChildrenIndex
struct Iterator: IteratorProtocol {
let collection: RawSyntaxChildren
var nextIndex: SyntaxChildrenIndex
init(collection: RawSyntaxChildren) {
self.collection = collection
self.nextIndex = collection.startIndex
}
mutating func next() -> RawSyntaxChildren.Element? {
guard nextIndex != collection.endIndex else {
return nil
}
// Re-use the fetched child to compute the next index, eliminating one
// access to the underlying collection
let child = collection[nextIndex]
nextIndex = collection.index(nextIndex, advancedBy: child.raw)
return child
}
}
/// The node whose children shall be accessed
private let parent: RawSyntax
private var parentLayoutView: RawSyntaxLayoutView {
return parent.layoutView!
}
/// The rootId of the tree the child nodes belong to
private let rootId: UInt
/// The number of children in `parent`. Cached to avoid reaching into `parent` for every index
/// advancement
private let numberOfChildren: Int
let startIndex: SyntaxChildrenIndex
var endIndex: SyntaxChildrenIndex {
return nil
}
func makeIterator() -> Iterator {
return Iterator(collection: self)
}
/// Advance the given index by the given ``RawSyntax`` node.
func index(_ index: Index, advancedBy node: RawSyntax?) -> Index {
// We can assume a non-end index since advancing the end index is undefined
// behaviour.
let index = index.data!
if index.indexInParent + 1 < numberOfChildren {
// Compute the next materialized index
let nodeLength = UInt32(node?.totalLength.utf8Length ?? 0)
let advancedIndexInTree = index.indexInTree.advancedBy(node)
return SyntaxChildrenIndex(
offset: index.offset + nodeLength,
indexInParent: index.indexInParent + 1,
indexInTree: advancedIndexInTree
)
} else {
// We have reached the end of the collection. Return the end index.
return nil
}
}
func index(after index: SyntaxChildrenIndex) -> SyntaxChildrenIndex {
guard let indexData = index.data else {
preconditionFailure("Cannot get the index after the end index")
}
let node = parent.layoutView!.children[Int(indexData.indexInParent)]
return self.index(index, advancedBy: node)
}
func index(before index: SyntaxChildrenIndex) -> SyntaxChildrenIndex {
if let index = index.data {
// We are reversing a non-end index.
let previousNode = parent.layoutView!.children[Int(index.indexInParent - 1)]
let previousNodeLength = UInt32(previousNode?.totalLength.utf8Length ?? 0)
let reversedIndexInTree = index.indexInTree.reversedBy(previousNode)
return SyntaxChildrenIndex(
offset: index.offset - previousNodeLength,
indexInParent: index.indexInParent - 1,
indexInTree: reversedIndexInTree
)
} else {
// We need to reverse the end index. For this we need to compute a
// materialized version of the end index that points one past the end of
// the collection. After we have that materialized index, we can reverse
// it using the above logic.
// If the start index is nil, then the collection is empty and we are
// reversing before the start index. That is undefined behaviour, so we
// can assume a non-end index.
let startIndex = self.startIndex.data!
// Compute a materialized index.
let offset = startIndex.offset + UInt32(parent.totalLength.utf8Length)
let indexInParent = startIndex.indexInParent + UInt32(parentLayoutView.children.count)
let indexInTree = startIndex.indexInTree.indexInTree + UInt32(parent.totalNodes) - 1
let syntaxIndexInTree = SyntaxIdentifier.SyntaxIndexInTree(indexInTree: indexInTree)
let materialized = SyntaxChildrenIndex(
offset: offset,
indexInParent: indexInParent,
indexInTree: syntaxIndexInTree
)
// Reverse index based on the above logic
return self.index(before: materialized)
}
}
func distance(from start: SyntaxChildrenIndex, to end: SyntaxChildrenIndex) -> Int {
switch (start.data, end.data) {
case (.some(let start), .some(let end)):
return Int(end.indexInParent - start.indexInParent)
case (.some(let start), .none):
return parentLayoutView.children.count - Int(start.indexInParent)
case (.none, .some(let end)):
return Int(end.indexInParent) - parentLayoutView.children.count
case (.none, .none):
return 0
}
}
subscript(index: SyntaxChildrenIndex) -> (raw: RawSyntax?, syntaxInfo: AbsoluteSyntaxInfo) {
// Accessing the end index is undefined. So we can assume a non-end index.
let index = index.data!
let child = parent.layoutView!.children[Int(index.indexInParent)]
let info = AbsoluteSyntaxInfo(index: index, rootId: rootId)
return (child, info)
}
init(_ parent: AbsoluteRawSyntax) {
self.parent = parent.raw
self.rootId = parent.info.nodeId.rootId
switch parent.raw.view {
case .layout(let layoutView):
self.numberOfChildren = layoutView.children.count
case .token:
self.numberOfChildren = 0
}
if self.numberOfChildren == 0 {
self.startIndex = nil
} else {
let startPosition = parent.info.advancedToFirstChild()
self.startIndex = SyntaxChildrenIndex(startPosition)
}
}
init(_ base: __shared Syntax) {
self.init(base.absoluteRaw)
}
}
/// Collection that contains the `present` child nodes of an
/// `AbsoluteRawSyntax` node.
struct NonNilRawSyntaxChildren: BidirectionalCollection, Sendable {
typealias Element = AbsoluteRawSyntax
typealias Index = SyntaxChildrenIndex
struct Iterator: IteratorProtocol {
var iterator: RawSyntaxChildren.Iterator
let viewMode: SyntaxTreeViewMode
init(allChildren: RawSyntaxChildren, viewMode: SyntaxTreeViewMode) {
self.iterator = allChildren.makeIterator()
self.viewMode = viewMode
}
mutating func next() -> AbsoluteRawSyntax? {
// Advance the underlying RawSyntaxChildren.Iterator until we find a
// present node.
while true {
guard let (node, info) = self.iterator.next() else {
return nil
}
if let node, viewMode.shouldTraverse(node: node) {
return AbsoluteRawSyntax(raw: node, info: info)
}
}
}
}
let viewMode: SyntaxTreeViewMode
/// The underlying collection which contains all children. The present
/// children are filtered from this collection.
private var allChildren: RawSyntaxChildren
let startIndex: SyntaxChildrenIndex
var endIndex: SyntaxChildrenIndex {
return allChildren.endIndex
}
func makeIterator() -> Iterator {
return Iterator(allChildren: allChildren, viewMode: viewMode)
}
/// Advances the index to the next present node in the given collection. If
/// the node at the given index is already present, it is not advanced.
/// If no present node exists in the given collection after the index, the
/// collection's `endIndex` is returned.
private static func presentIndex(
after index: SyntaxChildrenIndex,
in children: RawSyntaxChildren,
viewMode: SyntaxTreeViewMode
) -> SyntaxChildrenIndex {
var advancedIndex = index
while true {
if advancedIndex != children.endIndex {
let node = children[advancedIndex].raw
if let node = node, viewMode.shouldTraverse(node: node) {
// Found a present node. Return its index.
return advancedIndex
}
// Continue advancing
advancedIndex = children.index(advancedIndex, advancedBy: node)
} else {
// Reached the end of the collection. Don't advance further.
return advancedIndex
}
}
}
/// Reverses the index to the previous present node in the given collection.
/// If the node at the given index is already present, it is not reversed.
/// Behavior is undefined if no present index exists before the given index.
private static func presentIndex(
before index: SyntaxChildrenIndex,
in children: RawSyntaxChildren,
viewMode: SyntaxTreeViewMode
) -> SyntaxChildrenIndex {
var reversedIndex = index
while true {
if reversedIndex < children.endIndex,
let node = children[reversedIndex].raw,
viewMode.shouldTraverse(node: node)
{
return reversedIndex
}
// Reversing any further would result in undefined behaviour of
// index(before:)
precondition(
reversedIndex != children.startIndex,
"presentIndex(before:) must not be called if there is no " + "present index before the given one"
)
reversedIndex = children.index(before: reversedIndex)
}
}
func index(after index: SyntaxChildrenIndex) -> SyntaxChildrenIndex {
let nextIndex = allChildren.index(after: index)
return Self.presentIndex(
after: nextIndex,
in: allChildren,
viewMode: viewMode
)
}
func index(before index: SyntaxChildrenIndex) -> SyntaxChildrenIndex {
// presentIndex(before:) must have a valid previous present node. By
// contract of the index(before:) function we are not called on the start
// index. The start index points to the first present node. Hence there is
// a present node before us.
return Self.presentIndex(
before: allChildren.index(before: index),
in: allChildren,
viewMode: viewMode
)
}
subscript(position: SyntaxChildrenIndex) -> AbsoluteRawSyntax {
let (node, info) = allChildren[position]
// Valid indices always point to present nodes. Thus safe to force unwrap.
return AbsoluteRawSyntax(raw: node!, info: info)
}
init(_ parent: AbsoluteRawSyntax, viewMode: SyntaxTreeViewMode) {
let allChildren = RawSyntaxChildren(parent)
self.allChildren = allChildren
self.startIndex = Self.presentIndex(
after: allChildren.startIndex,
in: allChildren,
viewMode: viewMode
)
self.viewMode = viewMode
}
/// - Note: Inline so we don't retain `Syntax.Info` when creating `NonNilRawSyntaxChildren` from a `Syntax`.
@inline(__always)
init(_ node: Syntax, viewMode: SyntaxTreeViewMode) {
self.init(node.absoluteRaw, viewMode: viewMode)
}
}
/// Collection that contains the present child ``Syntax`` nodes of the given node.
public struct SyntaxChildren: BidirectionalCollection, Sendable {
/// ``SyntaxChildren`` is indexed by ``SyntaxChildrenIndex``.
public typealias Index = SyntaxChildrenIndex
/// ``SyntaxChildren`` contains ``Syntax`` nodes.
public typealias Element = Syntax
/// The collection that contains the raw child nodes. ``Syntax`` nodes are
/// generated from these.
private let rawChildren: NonNilRawSyntaxChildren
/// The parent node of the children. Used to build the ``Syntax`` nodes.
private let parent: Syntax
/// The index of the first child in this collection.
public var startIndex: SyntaxChildrenIndex { return rawChildren.startIndex }
/// The index that’s one after the last element in the collection.
public var endIndex: SyntaxChildrenIndex { return rawChildren.endIndex }
/// The index for the child that’s after the child at `index`.
public func index(after index: SyntaxChildrenIndex) -> SyntaxChildrenIndex {
return rawChildren.index(after: index)
}
/// The index for the child that’s before the child at `index`.
public func index(before index: SyntaxChildrenIndex) -> SyntaxChildrenIndex {
return rawChildren.index(before: index)
}
/// The syntax node at the given `index`
public subscript(index: SyntaxChildrenIndex) -> Syntax {
let child = rawChildren[index]
return Syntax(child, parent: parent)
}
internal init(_ node: Syntax, viewMode: SyntaxTreeViewMode) {
self.rawChildren = NonNilRawSyntaxChildren(node, viewMode: viewMode)
self.parent = node
}
/// Return the index of `node` within this collection.
///
/// If `node` is not part of this collection, returns `nil`.
public func index(of node: some SyntaxProtocol) -> SyntaxChildrenIndex? {
return index(of: Syntax(node))
}
/// Return the index of `node` within this collection.
///
/// If `node` is not part of this collection, returns `nil`.
///
/// - Note: This method is functionally equivalent to the one that takes
/// ``SyntaxProtocol``. It is provided because otherwise `Collection.index(of:)`
/// is chosen, which is marked as deprecated and renamed to `firstIndex(of:)`.
public func index(of node: Syntax) -> SyntaxChildrenIndex? {
guard node.parent == parent else {
return nil
}
return node.indexInParent
}
}
|