@@ -155,168 +155,6 @@ extension Highlighter: NSTextStorageDelegate {
155155 changeInLength delta: Int
156156 ) {
157157 guard editedMask. contains ( . editedCharacters) else { return }
158-
159- <<<<<<< Updated upstream
160- queryHighlights ( for: rangesToQuery)
161- }
162-
163- /// Highlights the given ranges
164- /// - Parameter ranges: The ranges to request highlights for.
165- func queryHighlights( for rangesToHighlight: [ NSRange ] ) {
166- guard let textView else { return }
167-
168- DispatchQueue . dispatchMainIfNot {
169- for range in rangesToHighlight {
170- self . highlightProvider? . queryHighlightsFor ( textView: textView, range: range) { [ weak self] highlights in
171- assert ( Thread . isMainThread, " Highlighted ranges called on non-main thread. " )
172- self ? . applyHighlightResult ( highlights, rangeToHighlight: range)
173- }
174- }
175- }
176- }
177-
178- /// Applies a highlight query result to the text view.
179- /// - Parameters:
180- /// - results: The result of a highlight query.
181- /// - rangeToHighlight: The range to apply the highlight to.
182- private func applyHighlightResult( _ results: Result < [ HighlightRange ] , Error > , rangeToHighlight: NSRange ) {
183- pendingSet. remove ( integersIn: rangeToHighlight)
184-
185- switch results {
186- case let . failure( error) :
187- if case HighlightProvidingError . operationCancelled = error {
188- invalidate ( range: rangeToHighlight)
189- } else {
190- Self . logger. error ( " Failed to query highlight range: \( error) " )
191- }
192- case let . success( results) :
193- guard let attributeProvider = self . attributeProvider,
194- visibleSet. intersects ( integersIn: rangeToHighlight) else {
195- return
196- }
197- validSet. formUnion ( IndexSet ( integersIn: rangeToHighlight) )
198-
199- // Loop through each highlight and modify the textStorage accordingly.
200- textView? . textStorage. beginEditing ( )
201-
202- // Create a set of indexes that were not highlighted.
203- var ignoredIndexes = IndexSet ( integersIn: rangeToHighlight)
204-
205- // Apply all highlights that need color
206- for highlight in results
207- where textView? . documentRange. upperBound ?? 0 > highlight. range. upperBound {
208- textView? . textStorage. setAttributes (
209- attributeProvider. attributesFor ( highlight. capture) ,
210- range: highlight. range
211- )
212-
213- // Remove highlighted indexes from the "ignored" indexes.
214- ignoredIndexes. remove ( integersIn: highlight. range)
215- }
216-
217- // For any indices left over, we need to apply normal attributes to them
218- // This fixes the case where characters are changed to have a non-text color, and then are skipped when
219- // they need to be changed back.
220- for ignoredRange in ignoredIndexes. rangeView
221- where textView? . documentRange. upperBound ?? 0 > ignoredRange. upperBound {
222- textView? . textStorage. setAttributes ( attributeProvider. attributesFor ( nil ) , range: NSRange ( ignoredRange) )
223- }
224-
225- textView? . textStorage. endEditing ( )
226- textView? . layoutManager. invalidateLayoutForRange ( rangeToHighlight)
227- }
228- }
229-
230- /// Gets the next `NSRange` to highlight based on the invalid set, visible set, and pending set.
231- /// - Returns: An `NSRange` to highlight if it could be fetched.
232- func getNextRange( ) -> NSRange ? {
233- let set : IndexSet = IndexSet ( integersIn: textView? . documentRange ?? . zero) // All text
234- . subtracting ( validSet) // Subtract valid = Invalid set
235- . intersection ( visibleSet) // Only visible indexes
236- . subtracting ( pendingSet) // Don't include pending indexes
237-
238- guard let range = set. rangeView. first else {
239- return nil
240- }
241-
242- // Chunk the ranges in sets of rangeChunkLimit characters.
243- return NSRange (
244- location: range. lowerBound,
245- length: min ( rangeChunkLimit, range. upperBound - range. lowerBound)
246- )
247- }
248- }
249-
250- // MARK: - Visible Content Updates
251-
252- private extension Highlighter {
253- private func updateVisibleSet( textView: TextView ) {
254- if let newVisibleRange = textView. visibleTextRange {
255- visibleSet = IndexSet ( integersIn: newVisibleRange)
256- }
257- }
258-
259- /// Updates the view to highlight newly visible text when the textview is scrolled or bounds change.
260- @objc func visibleTextChanged( _ notification: Notification ) {
261- let textView : TextView
262- if let clipView = notification. object as? NSClipView ,
263- let documentView = clipView. enclosingScrollView? . documentView as? TextView {
264- textView = documentView
265- } else if let scrollView = notification. object as? NSScrollView ,
266- let documentView = scrollView. documentView as? TextView {
267- textView = documentView
268- } else {
269- return
270- }
271-
272- updateVisibleSet ( textView: textView)
273-
274- // Any indices that are both *not* valid and in the visible text range should be invalidated
275- let newlyInvalidSet = visibleSet. subtracting ( validSet)
276-
277- for range in newlyInvalidSet. rangeView. map ( { NSRange ( $0) } ) {
278- invalidate ( range: range)
279- }
280- }
281- }
282-
283- // MARK: - Editing
284-
285- extension Highlighter {
286- func storageDidEdit( editedRange: NSRange , delta: Int ) {
287- guard let textView else { return }
288-
289- let range = NSRange ( location: editedRange. location, length: editedRange. length - delta)
290- if delta > 0 {
291- visibleSet. insert ( range: editedRange)
292- }
293-
294- updateVisibleSet ( textView: textView)
295-
296- highlightProvider? . applyEdit ( textView: textView, range: range, delta: delta) { [ weak self] result in
297- switch result {
298- case let . success( invalidIndexSet) :
299- let indexSet = invalidIndexSet. union ( IndexSet ( integersIn: editedRange) )
300-
301- for range in indexSet. rangeView {
302- self ? . invalidate ( range: NSRange ( range) )
303- }
304- case let . failure( error) :
305- if case HighlightProvidingError . operationCancelled = error {
306- self ? . invalidate ( range: range)
307- return
308- } else {
309- Self . logger. error ( " Failed to apply edit. Query returned with error: \( error) " )
310- }
311- }
312- }
313- }
314-
315- func storageWillEdit( editedRange: NSRange ) {
316- guard let textView else { return }
317- highlightProvider? . willApplyEdit ( textView: textView, range: editedRange)
318- =======
319158// self.storageWillEdit(editedRange: editedRange)
320- >>>>>>> Stashed changes
321159 }
322160}
0 commit comments