Skip to content

Commit b756624

Browse files
committed
Fix Crash At End Of File
1 parent b0688fa commit b756624

File tree

4 files changed

+42
-10
lines changed

4 files changed

+42
-10
lines changed

Example/CodeEditSourceEditorExample/CodeEditSourceEditorExample.xcodeproj/xcshareddata/xcschemes/CodeEditSourceEditorExample.xcscheme

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@
2929
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
3030
shouldUseLaunchSchemeArgsEnv = "YES"
3131
shouldAutocreateTestPlan = "YES">
32+
<Testables>
33+
<TestableReference
34+
skipped = "NO">
35+
<BuildableReference
36+
BuildableIdentifier = "primary"
37+
BlueprintIdentifier = "CodeEditSourceEditorTests"
38+
BuildableName = "CodeEditSourceEditorTests"
39+
BlueprintName = "CodeEditSourceEditorTests"
40+
ReferencedContainer = "container:../..">
41+
</BuildableReference>
42+
</TestableReference>
43+
</Testables>
3244
</TestAction>
3345
<LaunchAction
3446
buildConfiguration = "Debug"

Sources/CodeEditSourceEditor/Highlighting/StyledRangeContainer/StyledRangeStore/StyledRangeStore+Internals.swift renamed to Sources/CodeEditSourceEditor/Highlighting/StyledRangeContainer/StyledRangeStore/StyledRangeStore+Coalesce.swift

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,6 @@
77

88
import _RopeModule
99

10-
extension StyledRangeStore {
11-
/// Finds a Rope index, given a string offset.
12-
/// - Parameter offset: The offset to query for.
13-
/// - Returns: The index of the containing element in the rope.
14-
func findIndex(at offset: Int) -> (index: Index, remaining: Int) {
15-
_guts.find(at: offset, in: OffsetMetric(), preferEnd: false)
16-
}
17-
}
18-
1910
extension StyledRangeStore {
2011
/// Coalesce items before and after the given range.
2112
///
@@ -32,7 +23,7 @@ extension StyledRangeStore {
3223
}
3324

3425
index = findIndex(at: range.lowerBound).index
35-
if index > _guts.startIndex && _guts.count > 1 {
26+
if index > _guts.startIndex && index < _guts.endIndex && _guts.count > 1 {
3627
index = _guts.index(before: index)
3728
coalesceRunAfter(index: &index)
3829
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// StyledRangeStore+FindIndex.swift
3+
// CodeEditSourceEditor
4+
//
5+
// Created by Khan Winter on 1/6/25.
6+
//
7+
8+
extension StyledRangeStore {
9+
/// Finds a Rope index, given a string offset.
10+
/// - Parameter offset: The offset to query for.
11+
/// - Returns: The index of the containing element in the rope.
12+
func findIndex(at offset: Int) -> (index: Index, remaining: Int) {
13+
_guts.find(at: offset, in: OffsetMetric(), preferEnd: false)
14+
}
15+
}

Tests/CodeEditSourceEditorTests/Highlighting/StyledRangeStoreTests.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,20 @@ final class StyledRangeStoreTests: XCTestCase {
3636
XCTAssertEqual(store.count, 1, "Failed to coalesce")
3737
}
3838

39+
func test_storageRemoveSingleCharacterFromEnd() {
40+
let store = StyledRangeStore(documentLength: 10)
41+
store.set( // Test that we can delete a character associated with a single syntax run too
42+
runs: [
43+
.empty(length: 8),
44+
.init(length: 1, modifiers: [.abstract]),
45+
.init(length: 1, modifiers: [.declaration])],
46+
for: 0..<10
47+
)
48+
store.storageUpdated(replacedCharactersIn: 9..<10, withCount: 0)
49+
XCTAssertEqual(store.length, 9, "Failed to remove correct range")
50+
XCTAssertEqual(store.count, 2)
51+
}
52+
3953
func test_storageRemoveFromBeginning() {
4054
let store = StyledRangeStore(documentLength: 100)
4155
store.storageUpdated(replacedCharactersIn: 0..<15, withCount: 0)

0 commit comments

Comments
 (0)