@@ -41,6 +41,27 @@ final class HighlighterTests: XCTestCase {
4141 func attributesFor( _ capture: CaptureName ? ) -> [ NSAttributedString . Key : Any ] { [ : ] }
4242 }
4343
44+ class SentryStorageDelegate : NSObject , NSTextStorageDelegate {
45+ var editedIndices : IndexSet = IndexSet ( )
46+
47+ func textStorage(
48+ _ textStorage: NSTextStorage ,
49+ didProcessEditing editedMask: NSTextStorageEditActions ,
50+ range editedRange: NSRange ,
51+ changeInLength delta: Int ) {
52+ editedIndices. insert ( integersIn: editedRange)
53+ }
54+ }
55+
56+ var attributeProvider : MockAttributeProvider !
57+ var textView : TextView !
58+
59+ override func setUp( ) {
60+ attributeProvider = MockAttributeProvider ( )
61+ textView = Mock . textView ( )
62+ textView. frame = NSRect ( x: 0 , y: 0 , width: 1000 , height: 1000 )
63+ }
64+
4465 @MainActor
4566 func test_canceledHighlightsAreInvalidated( ) {
4667 var didQueryOnce = false
@@ -75,23 +96,8 @@ final class HighlighterTests: XCTestCase {
7596
7697 @MainActor
7798 func test_highlightsDoNotInvalidateEntireTextView( ) {
78- class SentryStorageDelegate : NSObject , NSTextStorageDelegate {
79- var editedIndices : IndexSet = IndexSet ( )
80-
81- func textStorage(
82- _ textStorage: NSTextStorage ,
83- didProcessEditing editedMask: NSTextStorageEditActions ,
84- range editedRange: NSRange ,
85- changeInLength delta: Int ) {
86- editedIndices. insert ( integersIn: editedRange)
87- }
88- }
89-
9099 let highlightProvider = TreeSitterClient ( )
91100 highlightProvider. forceSyncOperation = true
92- let attributeProvider = MockAttributeProvider ( )
93- let textView = Mock . textView ( )
94- textView. frame = NSRect ( x: 0 , y: 0 , width: 1000 , height: 1000 )
95101 textView. setText ( " func helloWorld() { \n \t print( \" Hello World! \" ) \n } " )
96102
97103 let highlighter = Mock . highlighter (
@@ -226,4 +232,49 @@ final class HighlighterTests: XCTestCase {
226232 XCTAssertTrue ( thirdSet. allSatisfy ( { $0. queryCount >= 1 } ) , " Not all in third batch were queried " )
227233 }
228234 }
235+
236+ // This test isn't testing much highlighter functionality. However, we've seen crashes and other errors after normal
237+ // editing that were caused by the highlighter and would only have been caught by an integration test like this.
238+ @MainActor
239+ func test_editFile( ) {
240+ let highlightProvider = TreeSitterClient ( )
241+ highlightProvider. forceSyncOperation = true
242+ textView. setText ( " func helloWorld() { \n \t print( \" Hello World! \" ) \n } " ) // 44 chars
243+
244+ let highlighter = Mock . highlighter (
245+ textView: textView,
246+ highlightProvider: highlightProvider,
247+ attributeProvider: attributeProvider
248+ )
249+ textView. addStorageDelegate ( highlighter)
250+ highlighter. setLanguage ( language: . swift)
251+ highlighter. invalidate ( )
252+
253+ // Delete Characters
254+ textView. replaceCharacters ( in: [ NSRange ( location: 43 , length: 1 ) ] , with: " " )
255+ textView. replaceCharacters ( in: [ NSRange ( location: 0 , length: 4 ) ] , with: " " )
256+ textView. replaceCharacters ( in: [ NSRange ( location: 6 , length: 5 ) ] , with: " " )
257+ textView. replaceCharacters ( in: [ NSRange ( location: 25 , length: 5 ) ] , with: " " )
258+
259+ XCTAssertEqual ( textView. string, " hello() { \n \t print( \" Hello ! \" ) \n " )
260+
261+ // Insert Characters
262+ textView. replaceCharacters ( in: [ NSRange ( location: 29 , length: 0 ) ] , with: " } " )
263+ textView. replaceCharacters (
264+ in: [ NSRange ( location: 25 , length: 0 ) , NSRange ( location: 6 , length: 0 ) ] ,
265+ with: " World "
266+ )
267+ // emulate typing with a cursor
268+ textView. selectionManager. setSelectedRange ( NSRange ( location: 0 , length: 0 ) )
269+ textView. insertText ( " f " )
270+ textView. insertText ( " u " )
271+ textView. insertText ( " n " )
272+ textView. insertText ( " c " )
273+ XCTAssertEqual ( textView. string, " func helloWorld() { \n \t print( \" Hello World! \" ) \n } " )
274+
275+ // Replace contents
276+ textView. replaceCharacters ( in: textView. documentRange, with: " " )
277+ textView. insertText ( " func helloWorld() { \n \t print( \" Hello World! \" ) \n } " )
278+ XCTAssertEqual ( textView. string, " func helloWorld() { \n \t print( \" Hello World! \" ) \n } " )
279+ }
229280}
0 commit comments