Skip to content

[swiftSyntax] Make AbsolutePosition a value type #18706

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tools/SwiftSyntax/AbsolutePosition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

/// An absolute position in a source file as text - the absolute utf8Offset from
/// the start, line, and column.
public final class AbsolutePosition {
public struct AbsolutePosition {
public let utf8Offset: Int
public let line: Int
public let column: Int
Expand Down
15 changes: 12 additions & 3 deletions tools/SwiftSyntax/SyntaxData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ import Foundation
/// exposed to clients.
typealias NodeIdentifier = [Int]

/// Box a value type into a reference type
fileprivate class Box<T> {
let value: T

init(_ value: T) {
self.value = value
}
}

/// SyntaxData is the underlying storage for each Syntax node.
/// It's modelled as an array that stores and caches a SyntaxData for each raw
/// syntax node in its layout. It is up to the specific Syntax nodes to maintain
Expand All @@ -38,7 +47,7 @@ final class SyntaxData: Equatable {

let childCaches: [AtomicCache<SyntaxData>]

let positionCache: AtomicCache<AbsolutePosition>
private let positionCache: AtomicCache<Box<AbsolutePosition>>

fileprivate func calculatePosition() -> AbsolutePosition {
guard let parent = parent else {
Expand All @@ -62,7 +71,7 @@ final class SyntaxData: Equatable {

/// The position of the start of this node's leading trivia
var position: AbsolutePosition {
return positionCache.value { return calculatePosition() }
return positionCache.value({ return Box(calculatePosition()) }).value
}

/// The position of the start of this node's content, skipping its trivia
Expand Down Expand Up @@ -93,7 +102,7 @@ final class SyntaxData: Equatable {
self.indexInParent = indexInParent
self.parent = parent
self.childCaches = raw.layout.map { _ in AtomicCache<SyntaxData>() }
self.positionCache = AtomicCache<AbsolutePosition>()
self.positionCache = AtomicCache<Box<AbsolutePosition>>()
}

/// The index path from this node to the root. This can be used to uniquely
Expand Down