Skip to content

Commit ee1b916

Browse files
committed
Merge branch 'fix/range-storebugs' into jump-to-definition
2 parents f204605 + 3be125b commit ee1b916

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

Sources/CodeEditSourceEditor/Highlighting/Highlighter.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ class Highlighter: NSObject {
154154
/// - Parameter providers: All providers to use.
155155
public func setProviders(_ providers: [HighlightProviding]) {
156156
guard let textView else { return }
157+
self.styleContainer.updateStorageLength(newLength: textView.textStorage.length)
157158

158159
let existingIds: [ObjectIdentifier] = self.highlightProviders
159160
.compactMap { $0.highlightProvider }

Sources/CodeEditSourceEditor/Highlighting/StyledRangeContainer/StyledRangeContainer.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,25 @@ class StyledRangeContainer {
111111
}
112112

113113
extension StyledRangeContainer: HighlightProviderStateDelegate {
114+
func updateStorageLength(newLength: Int) {
115+
for key in _storage.keys {
116+
guard var value = _storage[key] else { continue }
117+
var store = value.store
118+
let length = store.length
119+
if length != newLength {
120+
let missingCharacters = newLength - length
121+
if missingCharacters < 0 {
122+
store.storageUpdated(replacedCharactersIn: (length + missingCharacters)..<length, withCount: 0)
123+
} else {
124+
store.storageUpdated(replacedCharactersIn: length..<length, withCount: missingCharacters)
125+
}
126+
}
127+
128+
value.store = store
129+
_storage[key] = value
130+
}
131+
}
132+
114133
/// Applies a highlight result from a highlight provider to the storage container.
115134
/// - Parameters:
116135
/// - provider: The provider sending the highlights.

Sources/CodeEditSourceEditor/RangeStore/RangeStore.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ struct RangeStore<Element: RangeStoreElement>: Sendable {
8484
/// - runs: The runs to insert.
8585
/// - range: The range to replace.
8686
mutating func set(runs: [Run], for range: Range<Int>) {
87-
let gutsRange = 0..<_guts.count(in: OffsetMetric())
87+
let gutsRange = 0..<length
8888
if range.clamped(to: gutsRange) != range {
8989
let upperBound = range.clamped(to: gutsRange).upperBound
9090
let missingCharacters = range.upperBound - upperBound

0 commit comments

Comments
 (0)