From 98b8064fb804002a7bd3fb738a008645c6e4e54b Mon Sep 17 00:00:00 2001 From: Khan Winter <35942988+thecoolwinter@users.noreply.github.com> Date: Wed, 23 Apr 2025 11:13:41 -0500 Subject: [PATCH 1/2] Share Text Storage Delegates --- .../TextView/TextView+SetText.swift | 5 ++++ .../CodeEditTextView/TextView/TextView.swift | 6 ++++- .../CodeEditTextViewTests/TextViewTests.swift | 24 +++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/Sources/CodeEditTextView/TextView/TextView+SetText.swift b/Sources/CodeEditTextView/TextView/TextView+SetText.swift index a3f064857..8e70f7dad 100644 --- a/Sources/CodeEditTextView/TextView/TextView+SetText.swift +++ b/Sources/CodeEditTextView/TextView/TextView+SetText.swift @@ -20,6 +20,11 @@ extension TextView { public func setTextStorage(_ textStorage: NSTextStorage) { self.textStorage = textStorage + if let storageDelegate = textStorage.delegate as? MultiStorageDelegate { + self.storageDelegate = nil + self.storageDelegate = storageDelegate + } + subviews.forEach { view in view.removeFromSuperview() } diff --git a/Sources/CodeEditTextView/TextView/TextView.swift b/Sources/CodeEditTextView/TextView/TextView.swift index 9b0337f9e..4c9cf7c31 100644 --- a/Sources/CodeEditTextView/TextView/TextView.swift +++ b/Sources/CodeEditTextView/TextView/TextView.swift @@ -320,7 +320,11 @@ public class TextView: NSView, NSTextContent { super.init(frame: .zero) self.emphasisManager = EmphasisManager(textView: self) - self.storageDelegate = MultiStorageDelegate() + if let storageDelegate = textStorage.delegate as? MultiStorageDelegate { + self.storageDelegate = storageDelegate + } else { + self.storageDelegate = MultiStorageDelegate() + } wantsLayer = true postsFrameChangedNotifications = true diff --git a/Tests/CodeEditTextViewTests/TextViewTests.swift b/Tests/CodeEditTextViewTests/TextViewTests.swift index da5056d27..d8ac192b1 100644 --- a/Tests/CodeEditTextViewTests/TextViewTests.swift +++ b/Tests/CodeEditTextViewTests/TextViewTests.swift @@ -40,4 +40,28 @@ struct TextViewTests { // available in test module textView.layoutManager.lineStorage.validateInternalState() } + + @Test + func sharedTextStorage() { + let storage = NSTextStorage(string: "Hello world") + + let textView1 = TextView(string: "") + textView1.frame = NSRect(x: 0, y: 0, width: 100, height: 100) + textView1.layoutSubtreeIfNeeded() + textView1.setTextStorage(storage) + + let textView2 = TextView(string: "") + textView2.frame = NSRect(x: 0, y: 0, width: 100, height: 100) + textView2.layoutSubtreeIfNeeded() + textView2.setTextStorage(storage) + + // Expect both text views to receive edited events from the storage + #expect(textView1.layoutManager.lineCount == 1) + #expect(textView2.layoutManager.lineCount == 1) + + storage.replaceCharacters(in: NSRange(location: 11, length: 0), with: "\nMore Lines\n") + + #expect(textView1.layoutManager.lineCount == 3) + #expect(textView2.layoutManager.lineCount == 3) + } } From f5d54c5b83650eb3ffa61f5508181c366702e9d8 Mon Sep 17 00:00:00 2001 From: Khan Winter <35942988+thecoolwinter@users.noreply.github.com> Date: Thu, 24 Apr 2025 15:14:39 -0500 Subject: [PATCH 2/2] Update TextView+SetText.swift --- Sources/CodeEditTextView/TextView/TextView+SetText.swift | 1 - 1 file changed, 1 deletion(-) diff --git a/Sources/CodeEditTextView/TextView/TextView+SetText.swift b/Sources/CodeEditTextView/TextView/TextView+SetText.swift index 8e70f7dad..6ba66916a 100644 --- a/Sources/CodeEditTextView/TextView/TextView+SetText.swift +++ b/Sources/CodeEditTextView/TextView/TextView+SetText.swift @@ -21,7 +21,6 @@ extension TextView { self.textStorage = textStorage if let storageDelegate = textStorage.delegate as? MultiStorageDelegate { - self.storageDelegate = nil self.storageDelegate = storageDelegate }