From 21dd4082f4d7780782028b10e78d7f9ef42e6c6d Mon Sep 17 00:00:00 2001 From: "rene.schakmann" Date: Tue, 20 Oct 2026 18:19:31 +0200 Subject: [PATCH] Stop hyphenating code when it wraps in the timeline When a word in inline code (or a code block) is too long for the line, TextKit breaks it with an inserted hyphen, so commit hashes, UUIDs and identifiers render with characters that aren't part of the code. Disabling hyphenation through the paragraph style has no effect on this fallback, so MessageText now sets an NSLayoutManagerDelegate that refuses to hyphenate before any character marked as code. Code wraps by character instead while regular message text keeps its existing behaviour. The delegate is a shared instance rather than a stored property on MessageTextView: UITextView(usingTextLayoutManager:) skips the subclass's stored property initialisers, so such a property would be nil. Fixes #5725 Co-Authored-By: Claude Opus 5.5 --- ElementX.xcodeproj/project.pbxproj | 4 ++ .../Sources/Other/Pills/MessageText.swift | 18 +++++ ...yphenationLayoutManagerDelegateTests.swift | 65 +++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 UnitTests/Sources/CodeHyphenationLayoutManagerDelegateTests.swift diff --git a/ElementX.xcodeproj/project.pbxproj b/ElementX.xcodeproj/project.pbxproj index e40b73c1a..7f2ca6b7a 100644 --- a/ElementX.xcodeproj/project.pbxproj +++ b/ElementX.xcodeproj/project.pbxproj @@ -1168,6 +1168,7 @@ 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 */; }; @@ -2115,6 +2116,7 @@ 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 = ""; }; @@ -5353,6 +5355,7 @@ 0328F54E0C3AAEDDF3E05D9D /* ChatsTabFlowCoordinatorTests.swift */, 80935ADC7ED867226225F965 /* ClassicAppAccountManagerTests.swift */, 4AC3F28DECDF8665E8EBC76E /* ClassicAppMediaLoaderTests.swift */, + 309B378845D000076C71762C /* CodeHyphenationLayoutManagerDelegateTests.swift */, D5EA0312A6262484AA393AC9 /* CompletionSuggestionServiceTests.swift */, CA29952595B804DA221A0C1D /* ComposerToolbarViewModelTests.swift */, C5AB83F3B94569DD1720EE97 /* ContentScannerServiceTests.swift */, @@ -8522,6 +8525,7 @@ 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 */, diff --git a/ElementX/Sources/Other/Pills/MessageText.swift b/ElementX/Sources/Other/Pills/MessageText.swift index 8a4c7dbec..e54934191 100644 --- a/ElementX/Sources/Other/Pills/MessageText.swift +++ b/ElementX/Sources/Other/Pills/MessageText.swift @@ -70,6 +70,22 @@ 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 @@ -194,6 +210,8 @@ 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.backgroundColor = .clear if let attributedText = makeAttributedText() { textView.attributedText = attributedText diff --git a/UnitTests/Sources/CodeHyphenationLayoutManagerDelegateTests.swift b/UnitTests/Sources/CodeHyphenationLayoutManagerDelegateTests.swift new file mode 100644 index 000000000..767386b83 --- /dev/null +++ b/UnitTests/Sources/CodeHyphenationLayoutManagerDelegateTests.swift @@ -0,0 +1,65 @@ +// +// Copyright 2026 Element Creations Ltd. +// +// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial. +// Please see LICENSE files in the repository root for full details. +// + +@testable import ElementX +import Testing +import UIKit + +struct CodeHyphenationLayoutManagerDelegateTests { + private let attributedStringBuilder = AttributedStringBuilder(mentionBuilder: MentionBuilder()) + private let delegate = CodeHyphenationLayoutManagerDelegate() + + @Test + func inlineCodeIsNotHyphenated() throws { + let textStorage = try makeTextStorage(html: "Merged at d097cc7a2720e8a6e0714d291aa27a034bd20a0b today") + let layoutManager = try #require(textStorage.layoutManagers.first) + + let codeRange = (textStorage.string as NSString).range(of: "d097cc7a2720e8a6e0714d291aa27a034bd20a0b") + for index in codeRange.location..configurationmanagementinitialization") + let layoutManager = try #require(textStorage.layoutManagers.first) + + let codeRange = (textStorage.string as NSString).range(of: "configurationmanagementinitialization") + for index in codeRange.location..code incomprehensibilities") + let layoutManager = try #require(textStorage.layoutManagers.first) + let string = textStorage.string as NSString + + #expect(delegate.layoutManager(layoutManager, shouldBreakLineByHyphenatingBeforeCharacterAt: string.range(of: "Internationalization").location + 5)) + #expect(delegate.layoutManager(layoutManager, shouldBreakLineByHyphenatingBeforeCharacterAt: string.range(of: "incomprehensibilities").location + 5)) + } + + @Test + func outOfBoundsIndexIsIgnored() throws { + let textStorage = try makeTextStorage(html: "code") + let layoutManager = try #require(textStorage.layoutManagers.first) + + #expect(delegate.layoutManager(layoutManager, shouldBreakLineByHyphenatingBeforeCharacterAt: textStorage.length)) + } + + // MARK: - Helpers + + /// The layout manager only holds a weak reference to its text storage, so the storage is what must be kept alive. + private func makeTextStorage(html: String) throws -> NSTextStorage { + let attributedString = try #require(attributedStringBuilder.fromHTML(html)) + let textStorage = try NSTextStorage(attributedString: NSAttributedString(attributedString, including: \.elementX)) + textStorage.addLayoutManager(NSLayoutManager()) + return textStorage + } +}