Skip to content

Commit 31b7eb1

Browse files
committed
Improve documentation of SyntaxProtocol.tracked and write entry in release notes
1 parent 4d28598 commit 31b7eb1

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

Release Notes/601.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
- Description: Allows retrieving the represented literal value when valid.
77
- Issue: https://github.com/apple/swift-syntax/issues/405
88
- Pull Request: https://github.com/apple/swift-syntax/pull/2605
9-
9+
1010
- `SyntaxProtocol` now has a method `ancestorOrSelf`.
1111
- Description: Returns the node or the first ancestor that satisfies `condition`.
1212
- Pull Request: https://github.com/swiftlang/swift-syntax/pull/2696
@@ -19,6 +19,11 @@
1919
- Description: This new library provides facilities for evaluating `#if` conditions and determining which regions of a syntax tree are active according to a given build configuration.
2020
- Pull Request: https://github.com/swiftlang/swift-syntax/pull/1816
2121

22+
- `SyntaxProtocol.tracked` / `SyntaxProtocol.originalNode(in:)`
23+
- 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.
24+
- Issue: rdar://112679655
25+
- Pull Request: https://github.com/apple/swift-syntax/pull/2118
26+
2227
## API Behavior Changes
2328

2429
- `SyntaxProtocol.trimmed` detaches the node
@@ -38,7 +43,7 @@
3843
- Description: Allows retrieving the radix value from the `literal.text`.
3944
- Issue: https://github.com/apple/swift-syntax/issues/405
4045
- Pull Request: https://github.com/apple/swift-syntax/pull/2605
41-
46+
4247
- `FixIt.Change` gained a new case `replaceChild(data:)`.
4348
- Description: The new case covers the replacement of a child node with another node.
4449
- Issue: https://github.com/swiftlang/swift-syntax/issues/2205

Sources/SwiftSyntax/SyntaxTracking.swift

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,18 +278,39 @@ class IndexInTreeFinder: SyntaxAnyVisitor {
278278
}
279279

280280
extension SyntaxProtocol {
281-
/// The same ``Syntax`` but with tracking enabled in the entire tree.
281+
/// Start tracking this syntax tree as the original tree for all trees derived
282+
/// from it.
282283
///
283-
/// All syntax nodes derived from this will be able to find their
284-
/// corresponding location in this original tree.
284+
/// All syntax nodes derived from the returned, tracked, tree will be able to
285+
/// find the corresponding node in the original tree by calling
286+
/// ``SyntaxProtocol/originalNode(in:)``.
287+
///
288+
/// Derived nodes are
289+
/// - Nodes that contain a node from this tree as a subtree.
290+
/// - Nodes that resulted from modification of a node in this tree (e.g.
291+
/// modification of a child node or insertion of an element into a
292+
/// collection).
293+
/// - Detached subtrees of this tree (see ``SyntaxProtocol/detached``).
294+
///
295+
/// Node tracking is not enabled by default because maintaining the mapping
296+
/// back to the tracked tree has a performance cost that.
297+
///
298+
/// A tree can only track a single original tree. I.e. it is not possible to
299+
/// create a node that has one child in tracked tree A and another child in
300+
/// tracked tree B. In practice, this should seldom pose an issue because the
301+
/// most common use case is to mark the tree obtained from a file on disk as
302+
/// the tracked tree and trees from separate source files will rarely be
303+
/// merged.
285304
///
305+
/// - SeeAlso: ``SyntaxProtocol/originalNode(in:)``
286306
/// - Complexity: O(number of ancestors)
287307
public var tracked: Self {
288308
return Syntax(self).tracked.cast(Self.self)
289309
}
290310

291311
/// If this syntax node is tracking `originalTree` and this node originated
292-
/// in that tree, return the corresponding corresponding node in `originalTree`.
312+
/// from that tree, return the corresponding corresponding node in
313+
/// `originalTree`.
293314
///
294315
/// The original node will have the same structure as this node but the parent
295316
/// might be different since it's anchored in `originalTree`.

0 commit comments

Comments
 (0)