@@ -36,9 +36,7 @@ class Highlighter: NSObject {
3636
3737 /// The range of the entire document
3838 private var entireTextRange : Range < Int > {
39- get {
40- return 0 ..< ( textView. textContentStorage. textStorage? . length ?? 0 )
41- }
39+ return 0 ..< ( textView. textContentStorage. textStorage? . length ?? 0 )
4240 }
4341
4442 /// The set of visible indexes in tht text view
@@ -56,7 +54,7 @@ class Highlighter: NSObject {
5654 // MARK: - TreeSitter Client
5755
5856 /// Calculates invalidated ranges given an edit.
59- private var treeSitterClient : TreeSitterClient
57+ private var treeSitterClient : TreeSitterClient ?
6058
6159 // MARK: - Init
6260
@@ -66,7 +64,7 @@ class Highlighter: NSObject {
6664 /// - treeSitterClient: The tree-sitter client to handle tree updates and highlight queries.
6765 /// - theme: The theme to use for highlights.
6866 init ( textView: STTextView ,
69- treeSitterClient: TreeSitterClient ,
67+ treeSitterClient: TreeSitterClient ? ,
7068 theme: EditorTheme ,
7169 attributeProvider: ThemeAttributesProviding ) {
7270 self . textView = textView
@@ -76,7 +74,7 @@ class Highlighter: NSObject {
7674
7775 super. init ( )
7876
79- treeSitterClient. setText ( text: textView. string)
77+ treeSitterClient? . setText ( text: textView. string)
8078
8179 guard textView. textContentStorage. textStorage != nil else {
8280 assertionFailure ( " Text view does not have a textStorage " )
@@ -102,16 +100,16 @@ class Highlighter: NSObject {
102100
103101 /// Invalidates all text in the textview. Useful for updating themes.
104102 func invalidate( ) {
105- if !treeSitterClient. hasSetText {
106- treeSitterClient. setText ( text: textView. string)
103+ if !( treeSitterClient? . hasSetText ?? true ) {
104+ treeSitterClient? . setText ( text: textView. string)
107105 }
108106 invalidate ( range: entireTextRange)
109107 }
110108
111109 /// Sets the language and causes a re-highlight of the entire text.
112110 /// - Parameter language: The language to update to.
113111 func setLanguage( language: CodeLanguage ) throws {
114- try treeSitterClient. setLanguage ( codeLanguage: language, text: textView. string)
112+ try treeSitterClient? . setLanguage ( codeLanguage: language, text: textView. string)
115113 invalidate ( )
116114 }
117115
@@ -163,14 +161,23 @@ private extension Highlighter {
163161 let range = Range ( nsRange) !
164162 pendingSet. insert ( integersIn: range)
165163
166- treeSitterClient. queryColorsFor ( range: nsRange) { [ weak self] highlightRanges in
164+ treeSitterClient? . queryColorsFor ( range: nsRange) { [ weak self] highlightRanges in
167165 guard let attributeProvider = self ? . attributeProvider,
168166 let textView = self ? . textView else { return }
169167 self ? . pendingSet. remove ( integersIn: range)
170168 self ? . validSet. formUnion ( IndexSet ( integersIn: range) )
171- highlightRanges. forEach { highlight in
172- textView. addAttributes ( attributeProvider. attributesFor ( highlight. capture) , range: highlight. range, updateLayout: false )
169+ if !( self ? . visibleSet ?? . init( ) ) . contains ( integersIn: range) {
170+ return
171+ }
172+
173+ textView. textContentStorage. textStorage? . beginEditing ( )
174+ for highlight in highlightRanges {
175+ textView. textContentStorage. textStorage? . setAttributes (
176+ attributeProvider. attributesFor ( highlight. capture) ,
177+ range: highlight. range
178+ )
173179 }
180+ textView. textContentStorage. textStorage? . endEditing ( )
174181 }
175182 }
176183
@@ -191,7 +198,6 @@ private extension Highlighter {
191198
192199}
193200
194-
195201// MARK: - Visible Content Updates
196202
197203private extension Highlighter {
@@ -212,7 +218,8 @@ private extension Highlighter {
212218// MARK: - NSTextStorageDelegate
213219
214220extension Highlighter : NSTextStorageDelegate {
215- /// Processes an edited range in the text. Will query tree-sitter for any updated indices and re-highlight only the ranges that need it.
221+ /// Processes an edited range in the text.
222+ /// Will query tree-sitter for any updated indices and re-highlight only the ranges that need it.
216223 func textStorage( _ textStorage: NSTextStorage ,
217224 didProcessEditing editedMask: NSTextStorageEditActions ,
218225 range editedRange: NSRange ,
@@ -230,7 +237,7 @@ extension Highlighter: NSTextStorageDelegate {
230237 return
231238 }
232239
233- treeSitterClient. applyEdit ( edit,
240+ treeSitterClient? . applyEdit ( edit,
234241 text: textStorage. string) { [ weak self] invalidatedIndexSet in
235242 let indexSet = invalidatedIndexSet
236243 . union ( IndexSet ( integersIn: Range ( editedRange) !) )
0 commit comments