Skip to content

Commit 23fa1bf

Browse files
Invalidate Correct Edited Range (#279)
### Description In the highlight state, invalidates the correct range when performing an edit. Sometimes tree-sitter doesn't invalidate the ranges that were edited, so we need to add that edited range in. This ensures that a non-empty range is always invalidated. ### Related Issues - Related pr: #273 ### 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 https://github.com/user-attachments/assets/9e4d9b35-1e2d-44b8-9c2e-ffbfde69c8e4 Previously: https://github.com/user-attachments/assets/4b572c31-0320-4f90-ac52-6f911713bc75
1 parent 21619c4 commit 23fa1bf

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

Sources/CodeEditSourceEditor/Highlighting/HighlighProviding/HighlightProviderState.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,9 @@ extension HighlightProviderState {
139139
highlightProvider?.applyEdit(textView: textView, range: range, delta: delta) { [weak self] result in
140140
switch result {
141141
case .success(let invalidSet):
142+
let modifiedRange = NSRange(location: range.location, length: range.length + delta)
142143
// Make sure we add in the edited range too
143-
self?.invalidate(invalidSet.union(IndexSet(integersIn: range)))
144+
self?.invalidate(invalidSet.union(IndexSet(integersIn: modifiedRange)))
144145
case .failure(let error):
145146
if case HighlightProvidingError.operationCancelled = error {
146147
self?.invalidate(IndexSet(integersIn: range))

Tests/CodeEditSourceEditorTests/Highlighting/HighlightProviderStateTest.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,13 @@ final class HighlightProviderStateTest: XCTestCase {
116116
language: .swift
117117
)
118118

119+
// These reflect values like `NSTextStorage` outputs, and differ from ranges used in other tests.
119120
let mockEdits: [(NSRange, Int)] = [
120-
(NSRange(location: 0, length: 10), 10), // Inserted 10
121-
(NSRange(location: 5, length: 0), -2), // Deleted 2 at 5
122-
(NSRange(location: 0, length: 2), 3), // Replaced 0-2 with 3
121+
(NSRange(location: 0, length: 0), 10), // Inserted 10
122+
(NSRange(location: 3, length: 2), -2), // Deleted 2 at 5
123+
(NSRange(location: 0, length: 2), 1), // Replaced 0-2 with 3
123124
(NSRange(location: 9, length: 1), 1),
124-
(NSRange(location: 0, length: 0), -10)
125+
(NSRange(location: 0, length: 10), -10)
125126
]
126127

127128
for edit in mockEdits {

0 commit comments

Comments
 (0)