Skip to content

Commit afc5752

Browse files
Fix Style Store Crash (#345)
### Description Fixes a crash when the styled range store has empty arrays and still tries to remove values. ### Related Issues N/A ### Checklist - [x] I read and understood the [contributing guide](https://github.com/CodeEditApp/CodeEdit/blob/main/CONTRIBUTING.md) as well as the [code of conduct](https://github.com/CodeEditApp/CodeEdit/blob/main/CODE_OF_CONDUCT.md) - [x] The issues this PR addresses are related to each other - [x] My changes generate no new warnings - [x] My code builds and runs on my machine - [x] My changes are all related to the related issue above - [x] I documented my code ### Screenshots N/A
1 parent 8a47aa4 commit afc5752

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

Sources/CodeEditSourceEditor/Highlighting/StyledRangeContainer/StyledRangeContainer.swift

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,31 @@ class StyledRangeContainer {
4545
}
4646
}
4747

48+
enum RunState {
49+
case empty
50+
case value(RangeStoreRun<StyleElement>)
51+
case exhausted
52+
53+
var isExhausted: Bool {
54+
if case .exhausted = self { return true }
55+
return false
56+
}
57+
58+
var hasValue: Bool {
59+
if case .value = self { return true }
60+
return false
61+
}
62+
63+
var length: Int {
64+
switch self {
65+
case .empty, .exhausted:
66+
return 0
67+
case .value(let run):
68+
return run.length
69+
}
70+
}
71+
}
72+
4873
var _storage: [ProviderID: RangeStore<StyleElement>] = [:]
4974
weak var delegate: StyledRangeContainerDelegate?
5075

@@ -118,7 +143,9 @@ class StyledRangeContainer {
118143
}
119144
}
120145

121-
allRuns[minRunIdx].removeLast()
146+
if !allRuns[minRunIdx].isEmpty {
147+
allRuns[minRunIdx].removeLast()
148+
}
122149

123150
runs.append(minRun)
124151
minValue = allRuns.compactMap { $0.last }.enumerated().min(by: { $0.1.length < $1.1.length })

Sources/CodeEditSourceEditor/SourceEditor/SourceEditor.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ public struct SourceEditor: NSViewControllerRepresentable {
149149
controller.setCursorPositions(cursorPositions)
150150
}
151151

152-
if let scrollPosition = state.scrollPosition, scrollPosition != state.scrollPosition {
152+
let scrollView = controller.scrollView
153+
if let scrollPosition = state.scrollPosition, scrollPosition != scrollView?.contentView.bounds.origin {
153154
controller.scrollView.scroll(controller.scrollView.contentView, to: scrollPosition)
154155
controller.scrollView.reflectScrolledClipView(controller.scrollView.contentView)
155156
controller.gutterView.needsDisplay = true

0 commit comments

Comments
 (0)