Skip to content

Commit e35d655

Browse files
Share Text Storage Delegates (#92)
### Description Makes the text view share the `MultiStorageDelegate` if it's already installed on the text storage object. This allows for multiple editors to completely share a storage object, receiving update events and sending them to the necessary objects without removing the other text view's objects from the shared delegate. ### Related Issues * N/A ### Checklist - [x] I read and understood the [contributing guide](https://github.com/CodeEditApp/CodeEdit/blob/main/CONTRIBUTING.md) as well as the [code of conduct](https://github.com/CodeEditApp/CodeEdit/blob/main/CODE_OF_CONDUCT.md) - [x] The issues this PR addresses are related to each other - [x] My changes generate no new warnings - [x] My code builds and runs on my machine - [x] My changes are all related to the related issue above - [x] I documented my code ### Screenshots
1 parent 2a6e7c7 commit e35d655

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

Sources/CodeEditTextView/TextView/TextView+SetText.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ extension TextView {
2020
public func setTextStorage(_ textStorage: NSTextStorage) {
2121
self.textStorage = textStorage
2222

23+
if let storageDelegate = textStorage.delegate as? MultiStorageDelegate {
24+
self.storageDelegate = storageDelegate
25+
}
26+
2327
subviews.forEach { view in
2428
view.removeFromSuperview()
2529
}

Sources/CodeEditTextView/TextView/TextView.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,11 @@ public class TextView: NSView, NSTextContent {
320320
super.init(frame: .zero)
321321

322322
self.emphasisManager = EmphasisManager(textView: self)
323-
self.storageDelegate = MultiStorageDelegate()
323+
if let storageDelegate = textStorage.delegate as? MultiStorageDelegate {
324+
self.storageDelegate = storageDelegate
325+
} else {
326+
self.storageDelegate = MultiStorageDelegate()
327+
}
324328

325329
wantsLayer = true
326330
postsFrameChangedNotifications = true

Tests/CodeEditTextViewTests/TextViewTests.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,28 @@ struct TextViewTests {
4040
// available in test module
4141
textView.layoutManager.lineStorage.validateInternalState()
4242
}
43+
44+
@Test
45+
func sharedTextStorage() {
46+
let storage = NSTextStorage(string: "Hello world")
47+
48+
let textView1 = TextView(string: "")
49+
textView1.frame = NSRect(x: 0, y: 0, width: 100, height: 100)
50+
textView1.layoutSubtreeIfNeeded()
51+
textView1.setTextStorage(storage)
52+
53+
let textView2 = TextView(string: "")
54+
textView2.frame = NSRect(x: 0, y: 0, width: 100, height: 100)
55+
textView2.layoutSubtreeIfNeeded()
56+
textView2.setTextStorage(storage)
57+
58+
// Expect both text views to receive edited events from the storage
59+
#expect(textView1.layoutManager.lineCount == 1)
60+
#expect(textView2.layoutManager.lineCount == 1)
61+
62+
storage.replaceCharacters(in: NSRange(location: 11, length: 0), with: "\nMore Lines\n")
63+
64+
#expect(textView1.layoutManager.lineCount == 3)
65+
#expect(textView2.layoutManager.lineCount == 3)
66+
}
4367
}

0 commit comments

Comments
 (0)