From d213695528e38a12c60a65ea23cf30ef70c8e75d Mon Sep 17 00:00:00 2001 From: Matt <85322+mattmassicotte@users.noreply.github.com> Date: Mon, 14 Oct 2024 16:37:14 -0400 Subject: [PATCH] Clairfy how scroll view monitoring works, and conditionalize it on init --- Sources/Neon/TextViewHighlighter.swift | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Sources/Neon/TextViewHighlighter.swift b/Sources/Neon/TextViewHighlighter.swift index 06982eb..71d0365 100644 --- a/Sources/Neon/TextViewHighlighter.swift +++ b/Sources/Neon/TextViewHighlighter.swift @@ -72,6 +72,9 @@ public final class TextViewHighlighter { private var lastVisibleRange = NSRange.zero #endif + /// Create a instance. + /// + /// This method will also invoke `observeEnclosingScrollView` if `textView` is within a scroll view. If not, you can invoke it directly after the view has been placed into a scroll view. public init( textView: TextView, configuration: Configuration @@ -128,7 +131,9 @@ public final class TextViewHighlighter { try textView.getTextStorage().delegate = storageDelegate - observeEnclosingScrollView() + if textView.enclosingScrollView != nil { + observeEnclosingScrollView() + } invalidate(.all) } @@ -145,6 +150,9 @@ public final class TextViewHighlighter { } extension TextViewHighlighter { + /// Begin monitoring for containing scroll view changes. + /// + /// This method sets up all the necessary monitoring so the highlighter can react to scrolling. It should be called only once the view heirarchy is fully established. public func observeEnclosingScrollView() { #if os(macOS) && !targetEnvironment(macCatalyst) guard let scrollView = textView.enclosingScrollView else {