Skip to content

Commit 489827b

Browse files
committed
Improve documentation of SyntaxProtocol.tracked and write entry in release notes
1 parent a26a5bb commit 489827b

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

Release Notes/510.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
- `SyntaxCollection.index(at:)`
99
- Description: Returns the index of the n-th element in a `SyntaxCollection`. This computation is in O(n) and `SyntaxCollection` is not subscriptable by an integer.
1010
- Pull Request: https://github.com/apple/swift-syntax/pull/2014
11+
- `SyntaxProtocol.tracked` / `SyntaxProtocol.originalNode(in:)`
12+
- Description: `tracked` enables node tracking of a tree. For every tree derived from a tracked tree, `originalNode(in:)` returns the original node in the tracked tree. This allows clients to e.g. get the original location of a node in a source file after a tree has been modified.
13+
- Issue: rdar://112679655
14+
- Pull Request: https://github.com/apple/swift-syntax/pull/2118
15+
1116

1217
## API Behavior Changes
1318

Sources/SwiftSyntax/SyntaxTracking.swift

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ extension SyntaxData {
226226
fileprivate var tracked: SyntaxData {
227227
if let parent {
228228
let parentTracked = parent.tracked
229-
return parentTracked.child(at: self.indexInParent, parent: Syntax(parentTracked))!
229+
return parentTracked.child(at: self.indexInParent)!
230230
} else {
231231
let result = SyntaxData.forRoot(self.raw, rawNodeArena: self.raw.arena)
232232
let syntaxTracking = SyntaxTracking(
@@ -267,18 +267,39 @@ class IndexInTreeFinder: SyntaxAnyVisitor {
267267
}
268268

269269
extension SyntaxProtocol {
270-
/// The same `SyntaxData` but with tracking enabled in the entire tree.
270+
/// Start tracking this syntax tree as the original tree for all trees derived
271+
/// from it.
271272
///
272-
/// All syntax nodes derived from this will be able to find their
273-
/// corresponding location in this original tree.
273+
/// All syntax nodes derived from the returned, tracked, tree will be able to
274+
/// find the corresponding node in the original tree by calling
275+
/// ``SyntaxProtocol/originalNode(in:)``.
276+
///
277+
/// Derived nodes are
278+
/// - Nodes that contain a node from this tree as a subtree.
279+
/// - Nodes that resulted from modification of a node in this tree (e.g.
280+
/// modification of a child node or insertion of an element into a
281+
/// collection).
282+
/// - Detached subtrees of this tree (see ``SyntaxProtocol/detached``).
283+
///
284+
/// Node tracking is not enabled by default because maintaining the mapping
285+
/// back to the tracked tree has a performance cost that.
286+
///
287+
/// A tree can only track a single original tree. I.e. it is not possible to
288+
/// create a node that has one child in tracked tree A and another child in
289+
/// tracked tree B. In practice, this should seldom pose an issue because the
290+
/// most common use case is to mark the tree obtained from a file on disk as
291+
/// the tracked tree and trees from separate source files will rarely be
292+
/// merged.
274293
///
294+
/// - SeeAlso: ``SyntaxProtocol/originalNode(in:)``
275295
/// - Complexity: O(number of ancestors)
276296
public var tracked: Self {
277297
return Syntax(self.data.tracked).cast(Self.self)
278298
}
279299

280300
/// If this syntax node is tracking `originalTree` and this node originated
281-
/// in that tree, return the corresponding corresponding node in `originalTree`.
301+
/// from that tree, return the corresponding corresponding node in
302+
/// `originalTree`.
282303
///
283304
/// The original node will have the same structure as this node but the parent
284305
/// might be different since it's anchored in `originalTree`.

0 commit comments

Comments
 (0)