Skip to content

Commit

Permalink
Remove transformTargetToCurrent, improvements to RangeTarget
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmassicotte committed Jan 18, 2024
1 parent 0be9cc8 commit 68f441d
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 39 deletions.
10 changes: 10 additions & 0 deletions .swiftpm/xcode/xcshareddata/xcschemes/Neon.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "RangeStateTests"
BuildableName = "RangeStateTests"
BlueprintName = "RangeStateTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
Expand Down
3 changes: 1 addition & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/ChimeHQ/Rearrange",
"state" : {
"revision" : "0fb658e721c68495f6340c211cc6d4719e6b52d8",
"version" : "1.6.0"
"revision" : "3b154b59176ce40839071e7830c83410a0139d86"
}
},
{
Expand Down
3 changes: 2 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/ChimeHQ/SwiftTreeSitter", revision: "87ed52a71d4ad6b5e6a11185b42f6f74eb5b47da"),
.package(url: "https://github.com/ChimeHQ/Rearrange", from: "1.6.0"),
.package(url: "https://github.com/ChimeHQ/Rearrange", revision: "3b154b59176ce40839071e7830c83410a0139d86"),
],
targets: [
.target(name: "ConcurrencyCompatibility"),
.target(name: "RangeState", dependencies: ["ConcurrencyCompatibility", "Rearrange"]),
.testTarget(name: "RangeStateTests", dependencies: ["RangeState"]),
.target(
name: "Neon",
dependencies: [
Expand Down
26 changes: 3 additions & 23 deletions Sources/RangeState/RangeProcessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ extension RangeProcessor {
}
}

/// Array of any mutations that are scheduled to be applied.
///
/// You can use this property to transform Range, IndexSet, and RangeTarget values to match the current content.
public var pendingMutations: [RangeMutation] {
pendingEvents.compactMap {
switch $0 {
Expand Down Expand Up @@ -221,29 +224,6 @@ extension RangeProcessor {
completeContentChanged(mutation)
}

public func transformTargetToCurrent(_ target: RangeTarget) -> RangeTarget {
switch target {
case .all:
return .all
case var .range(range):
for mutation in pendingMutations {
guard let newRange = mutation.transform(range: range) else {
return .empty
}

range = newRange
}

return .range(range)
case var .set(set):
for mutation in pendingMutations {
set = mutation.transform(set: set)
}

return .set(set)
}
}

public func continueFillingIfNeeded() {
if hasPendingChanges {
return
Expand Down
53 changes: 43 additions & 10 deletions Sources/RangeState/RangeTarget.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
import Foundation

import Rearrange

public enum RangeTarget: Hashable, Sendable {
case set(IndexSet)
case range(NSRange)
case all

public static let empty = RangeTarget.set(IndexSet())

public init(_ set: IndexSet) {
self = .set(set)
}

public init(_ range: NSRange) {
self = .range(range)
}

public init(_ ranges: [NSRange]) {
self = .set(IndexSet(ranges: ranges))
}

public func indexSet(with length: Int) -> IndexSet {
let set: IndexSet

Expand All @@ -21,30 +35,49 @@ public enum RangeTarget: Hashable, Sendable {

return set
}
}

extension RangeTarget {
public func union(_ other: RangeTarget) -> RangeTarget {
switch (self, other) {
case let (.set(lhs), .set(rhs)):
return RangeTarget.set(lhs.union(rhs))
case (.set(var set), .set(let rhs)):
set.formUnion(rhs)
return RangeTarget(set)
case (.all, _):
return RangeTarget.all
case (_, .all):
return RangeTarget.all
case let (.set(lhs), .range(rhs)):
let set = lhs.union(IndexSet(integersIn: rhs))
case (.set(var set), .range(let range)):
set.insert(range: range)

return RangeTarget.set(set)
return RangeTarget(set)
case let (.range(lhs), .set(rhs)):
let set = rhs.union(IndexSet(integersIn: lhs))

return RangeTarget.set(set)
return RangeTarget(set)
case let (.range(lhs), .range(rhs)):
var set = IndexSet()
return RangeTarget([lhs, rhs])
}
}

public func apply(mutations: [RangeMutation]) -> RangeTarget {
switch self {
case .all:
return .all
case var .range(range):
for mutation in mutations {
guard let newRange = range.apply(mutation) else {
return .empty
}

range = newRange
}

set.insert(range: lhs)
set.insert(range: rhs)
return .range(range)
case var .set(set):
set.applying(mutations)

return RangeTarget.set(set)
return .set(set)
}
}
}
5 changes: 2 additions & 3 deletions Sources/TreeSitterClient/TreeSitterClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,12 @@ extension TreeSitterClient {
}

private func handleInvalidation(_ set: IndexSet, sublayers: Bool) {
let target = rangeProcessor.transformTargetToCurrent(.set(set))
let transformedSet = target.indexSet(with: configuration.lengthProvider())
let transformedSet = set.apply(rangeProcessor.pendingMutations)

configuration.invalidationHandler(transformedSet)

if sublayers {
sublayerValidator.invalidate(target)
sublayerValidator.invalidate(.set(transformedSet))
}
}

Expand Down
19 changes: 19 additions & 0 deletions Tests/RangeStateTests/RangeTargetTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import XCTest

import RangeState

final class RangeTargetTests: XCTestCase {
func testUnion() {
let range = RangeTarget(NSRange(0..<10))
let set = RangeTarget(IndexSet(integersIn: 10..<20))

XCTAssertEqual(RangeTarget.all.union(.all), .all)
XCTAssertEqual(RangeTarget.all.union(range), .all)
XCTAssertEqual(RangeTarget.all.union(set), .all)
XCTAssertEqual(range.union(.all), .all)
XCTAssertEqual(set.union(.all), .all)

XCTAssertEqual(range.union(set), .set(IndexSet(integersIn: 0..<20)))
XCTAssertEqual(set.union(range), .set(IndexSet(integersIn: 0..<20)))
}
}

0 comments on commit 68f441d

Please sign in to comment.