Skip to content

Commit 964bf15

Browse files
ahoppennkcsgexi
authored andcommitted
[swiftSyntax] Make SourceLength a struct
1 parent 1c2d02b commit 964bf15

File tree

3 files changed

+16
-15
lines changed

3 files changed

+16
-15
lines changed

tools/SwiftSyntax/RawSyntax.swift

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ extension ByteTreeUserInfoKey {
3636
ByteTreeUserInfoKey(rawValue: "SwiftSyntax.RawSyntax.OmittedNodeLookup")
3737
}
3838

39+
/// Box a value type into a reference type
40+
class Box<T> {
41+
let value: T
42+
43+
init(_ value: T) {
44+
self.value = value
45+
}
46+
}
47+
3948
/// A ID that uniquely identifies a syntax node and stays stable across multiple
4049
/// incremental parses
4150
public struct SyntaxNodeId: Hashable, Codable {
@@ -84,11 +93,11 @@ struct RawSyntax: Codable {
8493
/// incremental parses
8594
let id: SyntaxNodeId
8695

87-
var _contentLength = AtomicCache<SourceLength>()
96+
var _contentLength = AtomicCache<Box<SourceLength>>()
8897

8998
/// The length of this node excluding its leading and trailing trivia
9099
var contentLength: SourceLength {
91-
return _contentLength.value() {
100+
return _contentLength.value({
92101
switch data {
93102
case .node(kind: _, layout: let layout):
94103
let firstElementIndex = layout.firstIndex(where: { $0 != nil })
@@ -109,11 +118,11 @@ struct RawSyntax: Codable {
109118
contentLength += element.trailingTriviaLength
110119
}
111120
}
112-
return contentLength
121+
return Box(contentLength)
113122
case .token(kind: let kind, leadingTrivia: _, trailingTrivia: _):
114-
return SourceLength(of: kind.text)
123+
return Box(SourceLength(of: kind.text))
115124
}
116-
}
125+
}).value
117126
}
118127

119128
init(kind: SyntaxKind, layout: [RawSyntax?], presence: SourcePresence,

tools/SwiftSyntax/SourceLength.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
/// The length a syntax node spans in the source code. From any AbsolutePosition
1414
/// you reach a node's end location by either adding its UTF-8 length or by
1515
/// inserting `lines` newlines and then moving `columns` columns to the right.
16-
public final class SourceLength {
16+
public struct SourceLength {
1717
public let newlines: Int
1818
public let columnsAtLastLine: Int
1919
public let utf8Length: Int
@@ -90,4 +90,4 @@ extension AbsolutePosition {
9090
public static func +=(lhs: inout AbsolutePosition, rhs: SourceLength) {
9191
lhs = lhs + rhs
9292
}
93-
}
93+
}

tools/SwiftSyntax/SyntaxData.swift

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,6 @@ import Foundation
1818
/// exposed to clients.
1919
typealias NodeIdentifier = [Int]
2020

21-
/// Box a value type into a reference type
22-
fileprivate class Box<T> {
23-
let value: T
24-
25-
init(_ value: T) {
26-
self.value = value
27-
}
28-
}
2921

3022
/// SyntaxData is the underlying storage for each Syntax node.
3123
/// It's modelled as an array that stores and caches a SyntaxData for each raw

0 commit comments

Comments
 (0)