Skip to content

Commit 1fa046c

Browse files
committed
Add Multi-Run Method, Fix Tests, Update Tests
1 parent f313e7c commit 1fa046c

File tree

4 files changed

+23
-13
lines changed

4 files changed

+23
-13
lines changed

Sources/CodeEditSourceEditor/Extensions/Range+Length.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ import Foundation
1010
extension Range where Bound == Int {
1111
var length: Bound { upperBound - lowerBound }
1212

13+
/// The final index covered by this range. If the range has 0 length (upper bound = lower bound) it returns the
14+
/// single value represented by the range (lower bound)
15+
var lastIndex: Bound { upperBound == lowerBound ? upperBound : upperBound - 1 }
16+
1317
init(lowerBound: Int, length: Int) {
1418
self = lowerBound..<(lowerBound + length)
1519
}

Sources/CodeEditSourceEditor/Highlighting/StyledRangeContainer/StyledRangeStore/StyledRangeStore+Internals.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ extension StyledRangeStore {
2626
///
2727
/// - Parameter range: The range of the item to coalesce around.
2828
func coalesceNearby(range: Range<Int>) {
29-
var index = findIndex(at: range.endIndex).index
29+
var index = findIndex(at: range.lastIndex).index
3030
if index < _guts.endIndex && _guts.index(after: index) != _guts.endIndex {
3131
coalesceRunAfter(index: &index)
3232
}

Sources/CodeEditSourceEditor/Highlighting/StyledRangeContainer/StyledRangeStore/StyledRangeStore.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,17 @@ final class StyledRangeStore {
5959
func set(capture: CaptureName, modifiers: Set<CaptureModifiers>, for range: Range<Int>) {
6060
assert(range.lowerBound >= 0, "Negative lowerBound")
6161
assert(range.upperBound <= _guts.count(in: OffsetMetric()), "upperBound outside valid range")
62+
set(runs: [Run(length: range.length, capture: capture, modifiers: modifiers)], for: range)
63+
}
6264

63-
let run = StyledRun(length: range.length, capture: capture, modifiers: modifiers)
64-
_guts.replaceSubrange(range, in: OffsetMetric(), with: [run])
65+
func set(runs: [Run], for range: Range<Int>) {
66+
_guts.replaceSubrange(
67+
range,
68+
in: OffsetMetric(),
69+
with: runs.map { StyledRun(length: $0.length, capture: $0.capture, modifiers: $0.modifiers) }
70+
)
6571

6672
coalesceNearby(range: range)
67-
6873
cache = nil
6974
}
7075
}

Tests/CodeEditSourceEditorTests/Highlighting/StyledRangeStoreTests.swift

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -176,22 +176,23 @@ final class StyledRangeStoreTests: XCTestCase {
176176
func test_setMultipleRunsAndStorageUpdate() {
177177
let store = StyledRangeStore(documentLength: 100)
178178

179-
store.set(capture: .comment, modifiers: [.builtin], for: 5..<15)
180-
store.set(capture: .keyword, modifiers: [], for: 20..<30)
181-
store.set(capture: .string, modifiers: [.builtin], for: 35..<40)
182-
store.set(capture: .function, modifiers: [], for: 45..<50)
183-
store.set(capture: .variable, modifiers: [], for: 60..<70)
179+
var lengths = [5, 10, 5, 10, 5, 5, 5, 5, 10, 10, 30]
180+
var captures: [CaptureName?] = [nil, .comment, nil, .keyword, nil, .string, nil, .function, nil, .variable, nil]
181+
var modifiers: [Set<CaptureModifiers>] = [[], [.builtin], [], [], [], [.builtin], [], [], [], [], []]
182+
183+
store.set(
184+
runs: zip(zip(lengths, captures), modifiers).map {
185+
StyledRangeStore.Run(length: $0.0, capture: $0.1, modifiers: $1)
186+
},
187+
for: 0..<100
188+
)
184189

185190
XCTAssertEqual(store.length, 100)
186191

187192
var runs = store.runs(in: 0..<100)
188193
XCTAssertEqual(runs.count, 11)
189194
XCTAssertEqual(runs.reduce(0, { $0 + $1.length }), 100)
190195

191-
var lengths = [5, 10, 5, 10, 5, 5, 5, 5, 10, 10, 30]
192-
var captures: [CaptureName?] = [nil, .comment, nil, .keyword, nil, .string, nil, .function, nil, .variable, nil]
193-
var modifiers: [Set<CaptureModifiers>] = [[], [.builtin], [], [], [], [.builtin], [], [], [], [], []]
194-
195196
runs.enumerated().forEach {
196197
XCTAssertEqual(
197198
$0.element.length,

0 commit comments

Comments
 (0)