From dab9462d04326ccef2f7c20b19d4d14263cfa406 Mon Sep 17 00:00:00 2001 From: "rene.schakmann" Date: Thu, 22 Oct 2026 09:52:26 +0200 Subject: [PATCH] Make MessageTextView its own layout manager delegate Replaces the shared CodeHyphenationLayoutManagerDelegate with an NSLayoutManagerDelegate conformance on MessageTextView, as it's already the delegate for its gesture recognizers and pills. The layout manager only holds its delegate weakly and the text view owns the layout manager, so no shared instance or stored property is needed. The code block test now uses a
 without a nested , so it
actually covers the CodeBlock check rather than passing through the
InlineCode one.

Co-Authored-By: Claude Opus 5.5 
---
 ElementX.xcodeproj/project.pbxproj            |  8 ++---
 .../Sources/Other/Pills/MessageText.swift     | 33 ++++++++-----------
 ...Tests.swift => MessageTextViewTests.swift} | 16 ++++-----
 3 files changed, 26 insertions(+), 31 deletions(-)
 rename UnitTests/Sources/{CodeHyphenationLayoutManagerDelegateTests.swift => MessageTextViewTests.swift} (82%)

diff --git a/ElementX.xcodeproj/project.pbxproj b/ElementX.xcodeproj/project.pbxproj
index 7f2ca6b7a..3bddc9018 100644
--- a/ElementX.xcodeproj/project.pbxproj
+++ b/ElementX.xcodeproj/project.pbxproj
@@ -578,6 +578,7 @@
 		58F357A9D130A654ABCB1638 /* RageshakeConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8FC598338E7CF41107293AB5 /* RageshakeConfiguration.swift */; };
 		592A5B88471676D6F50B9798 /* SearchScreenCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A6B10AEA5983B4A2336387B /* SearchScreenCoordinator.swift */; };
 		5992EF10AA157EBD97D88910 /* AudioRecorderState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6569593FA36B22259E806A67 /* AudioRecorderState.swift */; };
+		59BDA5A5E09AEFA67C1B2828 /* MessageTextViewTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CEB871BDED6DBCE6554082E4 /* MessageTextViewTests.swift */; };
 		59C41313AED7566C3AC51163 /* RoomSummary.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29A953B6C0C431DBF4DD00B4 /* RoomSummary.swift */; };
 		59F940FCBE6BC343AECEF75E /* ImageCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E2245243369B99216C7D84E /* ImageCache.swift */; };
 		5A58C7C1837D3E5F2C3E8C8C /* SpaceScreenViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF8349149254469E24483FB0 /* SpaceScreenViewModel.swift */; };
@@ -1168,7 +1169,6 @@
 		B245583C63F8F90357B87FAE /* PostHog in Frameworks */ = {isa = PBXBuildFile; productRef = CCE5BF78B125320CBF3BB834 /* PostHog */; };
 		B2F8E01ABA1BA30265B4ECBE /* RoundedCornerShape.swift in Sources */ = {isa = PBXBuildFile; fileRef = 839E2C35DF3F9C7B54C3CE49 /* RoundedCornerShape.swift */; };
 		B2FDF69AC0C316F12142F91A /* RoomMembershipDetailsProxy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 12B09A94C519227264A41208 /* RoomMembershipDetailsProxy.swift */; };
-		B33D3AC21B609625574263E3 /* CodeHyphenationLayoutManagerDelegateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 309B378845D000076C71762C /* CodeHyphenationLayoutManagerDelegateTests.swift */; };
 		B3D652AA1654270742072FB3 /* DeveloperOptionsScreenViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86A6F283BC574FDB96ABBB07 /* DeveloperOptionsScreenViewModel.swift */; };
 		B3D8AA9988F8A000B162DCB5 /* TombstonedAvatarImage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 16D353E10A64172D863769BF /* TombstonedAvatarImage.swift */; };
 		B402708F8728DD0DB7C324E2 /* StartChatScreenViewModelProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 78910787F967CBC6042A101E /* StartChatScreenViewModelProtocol.swift */; };
@@ -2116,7 +2116,6 @@
 		307C3E5B9B189497D3250E23 /* AccessibleLongPress.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccessibleLongPress.swift; sourceTree = ""; };
 		30856520F3263D0E195710D7 /* TimelineMediaPreviewFileExportPicker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimelineMediaPreviewFileExportPicker.swift; sourceTree = ""; };
 		309AD8BAE6437C31BA7157BF /* ElementCallWidgetDriver.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ElementCallWidgetDriver.swift; sourceTree = ""; };
-		309B378845D000076C71762C /* CodeHyphenationLayoutManagerDelegateTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CodeHyphenationLayoutManagerDelegateTests.swift; sourceTree = ""; };
 		30EA681527A8FE65E4C8E9A9 /* ReportContentScreenViewModelTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReportContentScreenViewModelTests.swift; sourceTree = ""; };
 		30ED584467DB380E3CEFB1DB /* NotificationManagerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationManagerTests.swift; sourceTree = ""; };
 		314F1C79850BE46E8ABEAFCB /* ReadReceipt.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReadReceipt.swift; sourceTree = ""; };
@@ -3066,6 +3065,7 @@
 		CDA38C9B58693A49A3032552 /* DeclineAndBlockScreenCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeclineAndBlockScreenCoordinator.swift; sourceTree = ""; };
 		CDB3227C7A74B734924942E9 /* RoomSummaryProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RoomSummaryProvider.swift; sourceTree = ""; };
 		CDE3F3911FF7CC639BDE5844 /* nl */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = nl; path = nl.lproj/Localizable.strings; sourceTree = ""; };
+		CEB871BDED6DBCE6554082E4 /* MessageTextViewTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageTextViewTests.swift; sourceTree = ""; };
 		CEE20623EB4A9B88FB29F2BA /* fr */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = fr; path = fr.lproj/SAS.strings; sourceTree = ""; };
 		CEE41494C837AA403A06A5D9 /* UnitTests.xctestplan */ = {isa = PBXFileReference; path = UnitTests.xctestplan; sourceTree = ""; };
 		CF19027E7FFA5E63D148873A /* CreateRoomScreenViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CreateRoomScreenViewModel.swift; sourceTree = ""; };
@@ -5355,7 +5355,6 @@
 				0328F54E0C3AAEDDF3E05D9D /* ChatsTabFlowCoordinatorTests.swift */,
 				80935ADC7ED867226225F965 /* ClassicAppAccountManagerTests.swift */,
 				4AC3F28DECDF8665E8EBC76E /* ClassicAppMediaLoaderTests.swift */,
-				309B378845D000076C71762C /* CodeHyphenationLayoutManagerDelegateTests.swift */,
 				D5EA0312A6262484AA393AC9 /* CompletionSuggestionServiceTests.swift */,
 				CA29952595B804DA221A0C1D /* ComposerToolbarViewModelTests.swift */,
 				C5AB83F3B94569DD1720EE97 /* ContentScannerServiceTests.swift */,
@@ -5396,6 +5395,7 @@
 				AE203026B9AD3DB412439866 /* MediaUploadingPreprocessorTests.swift */,
 				03FABD73FD8086EFAB699F42 /* MediaUploadPreviewScreenViewModelTests.swift */,
 				6F6E6EDC4BBF962B2ED595A4 /* MessageForwardingScreenViewModelTests.swift */,
+				CEB871BDED6DBCE6554082E4 /* MessageTextViewTests.swift */,
 				53D4EE9E5F3E5BF651869242 /* NativeCallStackTests.swift */,
 				F875D71347DC81EAE7687446 /* NavigationRootCoordinatorTests.swift */,
 				78913D6E120D46138E97C107 /* NavigationSplitCoordinatorTests.swift */,
@@ -8525,7 +8525,6 @@
 				4BD5AB54A6982CF19F5CC7C4 /* ChatsTabFlowCoordinatorTests.swift in Sources */,
 				1F1BCEE81056FD9F344F3B0E /* ClassicAppAccountManagerTests.swift in Sources */,
 				1866A34DBAD0F730058CB3C3 /* ClassicAppMediaLoaderTests.swift in Sources */,
-				B33D3AC21B609625574263E3 /* CodeHyphenationLayoutManagerDelegateTests.swift in Sources */,
 				B5321A1F5B26A0F3EC54909E /* CollapsibleFlowLayoutTests.swift in Sources */,
 				3A164187907DA43B7858F9EC /* CompletionSuggestionServiceTests.swift in Sources */,
 				0C932A5158C1D0604DFC5750 /* ComposerToolbarViewModelTests.swift in Sources */,
@@ -8573,6 +8572,7 @@
 				B9A8C34A00D03094C0CF56F3 /* MediaUploadPreviewScreenViewModelTests.swift in Sources */,
 				23701DE32ACD6FD40AA992C3 /* MediaUploadingPreprocessorTests.swift in Sources */,
 				F777C6FEE7D106136E2ED2B2 /* MessageForwardingScreenViewModelTests.swift in Sources */,
+				59BDA5A5E09AEFA67C1B2828 /* MessageTextViewTests.swift in Sources */,
 				1146E9EDCF8344F7D6E0D553 /* MockCoder.swift in Sources */,
 				DC68E866D6E664B0D2B06E74 /* MockImageCache.swift in Sources */,
 				3F791C4E0F7B373DB217EC90 /* NSItemProviderTests.swift in Sources */,
diff --git a/ElementX/Sources/Other/Pills/MessageText.swift b/ElementX/Sources/Other/Pills/MessageText.swift
index e54934191..39ce0c87f 100644
--- a/ElementX/Sources/Other/Pills/MessageText.swift
+++ b/ElementX/Sources/Other/Pills/MessageText.swift
@@ -9,7 +9,7 @@
 import MatrixRustSDK
 import SwiftUI
 
-final class MessageTextView: UITextView, PillAttachmentViewProviderDelegate, UIGestureRecognizerDelegate {
+final class MessageTextView: UITextView, PillAttachmentViewProviderDelegate, UIGestureRecognizerDelegate, NSLayoutManagerDelegate {
     var timelineContext: TimelineViewModel.Context?
     var updateClosure: (() -> Void)?
     private var pillViews = NSHashTable.weakObjects()
@@ -42,6 +42,18 @@ final class MessageTextView: UITextView, PillAttachmentViewProviderDelegate, UIG
         }
     }
     
+    /// Stops TextKit from hyphenating code when a word doesn't fit on a line, wrapping it by
+    /// character instead. Code is meant to be verbatim, so an inserted hyphen would corrupt it.
+    nonisolated func layoutManager(_ layoutManager: NSLayoutManager, shouldBreakLineByHyphenatingBeforeCharacterAt charIndex: Int) -> Bool {
+        guard let textStorage = layoutManager.textStorage, charIndex < textStorage.length else {
+            return true
+        }
+        
+        let isCode = textStorage.attribute(.InlineCode, at: charIndex, effectiveRange: nil) != nil
+            || textStorage.attribute(.CodeBlock, at: charIndex, effectiveRange: nil) != nil
+        return !isCode
+    }
+    
     func registerPillView(_ pillView: UIView) {
         pillViews.add(pillView)
     }
@@ -70,22 +82,6 @@ private final nonisolated class TransparentTextAttachment: NSTextAttachment {
     }
 }
 
-/// Stops TextKit from hyphenating code when a word doesn't fit on a line, wrapping it by
-/// character instead. Code is meant to be verbatim, so an inserted hyphen would corrupt it.
-final nonisolated class CodeHyphenationLayoutManagerDelegate: NSObject, NSLayoutManagerDelegate, Sendable {
-    static let shared = CodeHyphenationLayoutManagerDelegate()
-    
-    func layoutManager(_ layoutManager: NSLayoutManager, shouldBreakLineByHyphenatingBeforeCharacterAt charIndex: Int) -> Bool {
-        guard let textStorage = layoutManager.textStorage, charIndex < textStorage.length else {
-            return true
-        }
-        
-        let isCode = textStorage.attribute(.InlineCode, at: charIndex, effectiveRange: nil) != nil
-            || textStorage.attribute(.CodeBlock, at: charIndex, effectiveRange: nil) != nil
-        return !isCode
-    }
-}
-
 struct MessageText: UIViewRepresentable {
     @Environment(\.openURL) private var openURLAction
     @Environment(\.timelineContext) private var viewModel
@@ -210,8 +206,7 @@ struct MessageText: UIViewRepresentable {
         textView.textContainerInset = .zero
         textView.textContainer.lineFragmentPadding = 0
         textView.layoutManager.usesFontLeading = false
-        // A shared instance as the layout manager only keeps a weak reference to its delegate.
-        textView.layoutManager.delegate = CodeHyphenationLayoutManagerDelegate.shared
+        textView.layoutManager.delegate = textView
         textView.backgroundColor = .clear
         if let attributedText = makeAttributedText() {
             textView.attributedText = attributedText
diff --git a/UnitTests/Sources/CodeHyphenationLayoutManagerDelegateTests.swift b/UnitTests/Sources/MessageTextViewTests.swift
similarity index 82%
rename from UnitTests/Sources/CodeHyphenationLayoutManagerDelegateTests.swift
rename to UnitTests/Sources/MessageTextViewTests.swift
index 767386b83..fadd669f7 100644
--- a/UnitTests/Sources/CodeHyphenationLayoutManagerDelegateTests.swift
+++ b/UnitTests/Sources/MessageTextViewTests.swift
@@ -9,9 +9,9 @@
 import Testing
 import UIKit
 
-struct CodeHyphenationLayoutManagerDelegateTests {
+struct MessageTextViewTests {
     private let attributedStringBuilder = AttributedStringBuilder(mentionBuilder: MentionBuilder())
-    private let delegate = CodeHyphenationLayoutManagerDelegate()
+    private let textView = MessageTextView(usingTextLayoutManager: false)
     
     @Test
     func inlineCodeIsNotHyphenated() throws {
@@ -20,18 +20,18 @@ struct CodeHyphenationLayoutManagerDelegateTests {
         
         let codeRange = (textStorage.string as NSString).range(of: "d097cc7a2720e8a6e0714d291aa27a034bd20a0b")
         for index in codeRange.location..configurationmanagementinitialization
") + let textStorage = try makeTextStorage(html: "
configurationmanagementinitialization
") let layoutManager = try #require(textStorage.layoutManagers.first) let codeRange = (textStorage.string as NSString).range(of: "configurationmanagementinitialization") for index in codeRange.location..code
") let layoutManager = try #require(textStorage.layoutManagers.first) - #expect(delegate.layoutManager(layoutManager, shouldBreakLineByHyphenatingBeforeCharacterAt: textStorage.length)) + #expect(textView.layoutManager(layoutManager, shouldBreakLineByHyphenatingBeforeCharacterAt: textStorage.length)) } // MARK: - Helpers