Skip to content

Commit 55f2c2f

Browse files
Introduce the concept of snapshotting content
1 parent 36aa61d commit 55f2c2f

File tree

4 files changed

+51
-12
lines changed

4 files changed

+51
-12
lines changed

Sources/SwiftTreeSitter/Parser.swift

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ extension Parser {
9797

9898
extension Parser {
9999
public typealias ReadBlock = (Int, Point) -> Data?
100+
public typealias DataSnapshotProvider = @Sendable (Int, Point) -> Data?
100101

101102
public func parse(_ string: String) -> MutableTree? {
102103
guard let data = string.data(using: encoding) else { return nil }
@@ -144,15 +145,18 @@ extension Parser {
144145
parse(tree: tree?.tree, string: string, limit: limit, chunkSize: chunkSize)
145146
}
146147

147-
public static func readFunction(for string: String, limit: Int? = nil, chunkSize: Int = 2048) -> Parser.ReadBlock {
148-
let usableLimit = limit ?? string.utf16.count
149-
let encoding = String.nativeUTF16Encoding
150-
151-
return { (start, _) -> Data? in
152-
return string.data(at: start,
153-
limit: usableLimit,
154-
using: encoding,
155-
chunkSize: chunkSize)
156-
}
157-
}
148+
/// Form a function that captures an immutable view into the data of a `String`.
149+
public static func readFunction(for string: String, limit: Int? = nil, chunkSize: Int = 2048) -> Parser.DataSnapshotProvider {
150+
let usableLimit = limit ?? string.utf16.count
151+
let encoding = String.nativeUTF16Encoding
152+
153+
return { (start, _) -> Data? in
154+
return string.data(
155+
at: start,
156+
limit: usableLimit,
157+
using: encoding,
158+
chunkSize: chunkSize
159+
)
160+
}
161+
}
158162
}

Sources/SwiftTreeSitter/Predicate.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public enum Predicate: Hashable, Sendable {
7979

8080
extension Predicate {
8181
public typealias TextProvider = (NSRange, Range<Point>) -> String?
82+
public typealias TextSnapshotProvider = @Sendable (NSRange, Range<Point>) -> String?
8283
public typealias GroupMembershipProvider = (String, NSRange, Range<Point>) -> Bool
8384

8485
public struct Context {

Sources/SwiftTreeSitter/String+TextProvider.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ extension String {
1414
}
1515

1616
public var predicateTextProvider: Predicate.TextProvider {
17-
return { (nsRange, _) in
17+
predicateTextSnapshotProvider
18+
}
19+
20+
public var predicateTextSnapshotProvider: Predicate.TextSnapshotProvider {
21+
{ (nsRange, _) in
1822
guard let range = Range<String.Index>(nsRange, in: self) else {
1923
return nil
2024
}

Sources/SwiftTreeSitterLayer/LanguageLayer.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,36 @@ public final class LanguageLayer {
3535
)
3636
}
3737
}
38+
39+
public struct ContentSnapshot: Sendable {
40+
public let readHandler: Parser.DataSnapshotProvider
41+
public let textProvider: SwiftTreeSitter.Predicate.TextSnapshotProvider
42+
43+
public init(
44+
readHandler: @escaping @Sendable (Int, Point) -> Data?,
45+
textProvider: @escaping @Sendable (NSRange, Range<Point>) -> String?
46+
) {
47+
self.readHandler = readHandler
48+
self.textProvider = textProvider
49+
}
50+
51+
public init(string: String, limit: Int) {
52+
let read = Parser.readFunction(for: string, limit: limit)
53+
54+
self.init(
55+
readHandler: read,
56+
textProvider: string.predicateTextSnapshotProvider
57+
)
58+
}
59+
60+
public init(string: String) {
61+
self.init(string: string, limit: string.utf16.count)
62+
}
63+
64+
public var content: LanguageLayer.Content {
65+
.init(readHandler: readHandler, textProvider: textProvider)
66+
}
67+
}
3868

3969
public struct Configuration {
4070
public let languageProvider: LanguageProvider

0 commit comments

Comments
 (0)