Skip to content

Commit 4cab621

Browse files
committed
Improve documentation of SyntaxProtocol.tracked and write entry in release notes
1 parent bd24f84 commit 4cab621

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

Release Notes/510.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
- `DiagnosticSeverity` and `PluginMessage.Diagnostic.Severity` now have new case named `remark`
2121
- Description: Remarks are used by the Swift compiler and other tools to describe some aspect of translation that doesn't reflect correctness, but may be useful for the user. Remarks have been added to the diagnostic severity enums to align with the Swift compiler.
2222
- Pull Request: https://github.com/apple/swift-syntax/pull/2143
23+
- `SyntaxProtocol.tracked` / `SyntaxProtocol.originalNode(in:)`
24+
- 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.
25+
- Issue: rdar://112679655
26+
- Pull Request: https://github.com/apple/swift-syntax/pull/2118
2327

2428
## API Behavior Changes
2529

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)