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 <noreply@anthropic.com>
This commit is contained in:
rene.schakmann
2026-10-20 18:19:31 +02:00
committed by Mauro
parent b21eecec1b
commit 21dd4082f4
3 changed files with 87 additions and 0 deletions
+4
View File
@@ -1168,6 +1168,7 @@
B245583C63F8F90357B87FAE /* PostHog in Frameworks */ = {isa = PBXBuildFile; productRef = CCE5BF78B125320CBF3BB834 /* PostHog */; }; B245583C63F8F90357B87FAE /* PostHog in Frameworks */ = {isa = PBXBuildFile; productRef = CCE5BF78B125320CBF3BB834 /* PostHog */; };
B2F8E01ABA1BA30265B4ECBE /* RoundedCornerShape.swift in Sources */ = {isa = PBXBuildFile; fileRef = 839E2C35DF3F9C7B54C3CE49 /* RoundedCornerShape.swift */; }; B2F8E01ABA1BA30265B4ECBE /* RoundedCornerShape.swift in Sources */ = {isa = PBXBuildFile; fileRef = 839E2C35DF3F9C7B54C3CE49 /* RoundedCornerShape.swift */; };
B2FDF69AC0C316F12142F91A /* RoomMembershipDetailsProxy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 12B09A94C519227264A41208 /* RoomMembershipDetailsProxy.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 */; }; B3D652AA1654270742072FB3 /* DeveloperOptionsScreenViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86A6F283BC574FDB96ABBB07 /* DeveloperOptionsScreenViewModel.swift */; };
B3D8AA9988F8A000B162DCB5 /* TombstonedAvatarImage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 16D353E10A64172D863769BF /* TombstonedAvatarImage.swift */; }; B3D8AA9988F8A000B162DCB5 /* TombstonedAvatarImage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 16D353E10A64172D863769BF /* TombstonedAvatarImage.swift */; };
B402708F8728DD0DB7C324E2 /* StartChatScreenViewModelProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 78910787F967CBC6042A101E /* StartChatScreenViewModelProtocol.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 = "<group>"; }; 307C3E5B9B189497D3250E23 /* AccessibleLongPress.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccessibleLongPress.swift; sourceTree = "<group>"; };
30856520F3263D0E195710D7 /* TimelineMediaPreviewFileExportPicker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimelineMediaPreviewFileExportPicker.swift; sourceTree = "<group>"; }; 30856520F3263D0E195710D7 /* TimelineMediaPreviewFileExportPicker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimelineMediaPreviewFileExportPicker.swift; sourceTree = "<group>"; };
309AD8BAE6437C31BA7157BF /* ElementCallWidgetDriver.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ElementCallWidgetDriver.swift; sourceTree = "<group>"; }; 309AD8BAE6437C31BA7157BF /* ElementCallWidgetDriver.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ElementCallWidgetDriver.swift; sourceTree = "<group>"; };
309B378845D000076C71762C /* CodeHyphenationLayoutManagerDelegateTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CodeHyphenationLayoutManagerDelegateTests.swift; sourceTree = "<group>"; };
30EA681527A8FE65E4C8E9A9 /* ReportContentScreenViewModelTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReportContentScreenViewModelTests.swift; sourceTree = "<group>"; }; 30EA681527A8FE65E4C8E9A9 /* ReportContentScreenViewModelTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReportContentScreenViewModelTests.swift; sourceTree = "<group>"; };
30ED584467DB380E3CEFB1DB /* NotificationManagerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationManagerTests.swift; sourceTree = "<group>"; }; 30ED584467DB380E3CEFB1DB /* NotificationManagerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationManagerTests.swift; sourceTree = "<group>"; };
314F1C79850BE46E8ABEAFCB /* ReadReceipt.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReadReceipt.swift; sourceTree = "<group>"; }; 314F1C79850BE46E8ABEAFCB /* ReadReceipt.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReadReceipt.swift; sourceTree = "<group>"; };
@@ -5353,6 +5355,7 @@
0328F54E0C3AAEDDF3E05D9D /* ChatsTabFlowCoordinatorTests.swift */, 0328F54E0C3AAEDDF3E05D9D /* ChatsTabFlowCoordinatorTests.swift */,
80935ADC7ED867226225F965 /* ClassicAppAccountManagerTests.swift */, 80935ADC7ED867226225F965 /* ClassicAppAccountManagerTests.swift */,
4AC3F28DECDF8665E8EBC76E /* ClassicAppMediaLoaderTests.swift */, 4AC3F28DECDF8665E8EBC76E /* ClassicAppMediaLoaderTests.swift */,
309B378845D000076C71762C /* CodeHyphenationLayoutManagerDelegateTests.swift */,
D5EA0312A6262484AA393AC9 /* CompletionSuggestionServiceTests.swift */, D5EA0312A6262484AA393AC9 /* CompletionSuggestionServiceTests.swift */,
CA29952595B804DA221A0C1D /* ComposerToolbarViewModelTests.swift */, CA29952595B804DA221A0C1D /* ComposerToolbarViewModelTests.swift */,
C5AB83F3B94569DD1720EE97 /* ContentScannerServiceTests.swift */, C5AB83F3B94569DD1720EE97 /* ContentScannerServiceTests.swift */,
@@ -8522,6 +8525,7 @@
4BD5AB54A6982CF19F5CC7C4 /* ChatsTabFlowCoordinatorTests.swift in Sources */, 4BD5AB54A6982CF19F5CC7C4 /* ChatsTabFlowCoordinatorTests.swift in Sources */,
1F1BCEE81056FD9F344F3B0E /* ClassicAppAccountManagerTests.swift in Sources */, 1F1BCEE81056FD9F344F3B0E /* ClassicAppAccountManagerTests.swift in Sources */,
1866A34DBAD0F730058CB3C3 /* ClassicAppMediaLoaderTests.swift in Sources */, 1866A34DBAD0F730058CB3C3 /* ClassicAppMediaLoaderTests.swift in Sources */,
B33D3AC21B609625574263E3 /* CodeHyphenationLayoutManagerDelegateTests.swift in Sources */,
B5321A1F5B26A0F3EC54909E /* CollapsibleFlowLayoutTests.swift in Sources */, B5321A1F5B26A0F3EC54909E /* CollapsibleFlowLayoutTests.swift in Sources */,
3A164187907DA43B7858F9EC /* CompletionSuggestionServiceTests.swift in Sources */, 3A164187907DA43B7858F9EC /* CompletionSuggestionServiceTests.swift in Sources */,
0C932A5158C1D0604DFC5750 /* ComposerToolbarViewModelTests.swift in Sources */, 0C932A5158C1D0604DFC5750 /* ComposerToolbarViewModelTests.swift in Sources */,
@@ -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 { struct MessageText: UIViewRepresentable {
@Environment(\.openURL) private var openURLAction @Environment(\.openURL) private var openURLAction
@Environment(\.timelineContext) private var viewModel @Environment(\.timelineContext) private var viewModel
@@ -194,6 +210,8 @@ struct MessageText: UIViewRepresentable {
textView.textContainerInset = .zero textView.textContainerInset = .zero
textView.textContainer.lineFragmentPadding = 0 textView.textContainer.lineFragmentPadding = 0
textView.layoutManager.usesFontLeading = false 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 textView.backgroundColor = .clear
if let attributedText = makeAttributedText() { if let attributedText = makeAttributedText() {
textView.attributedText = attributedText textView.attributedText = attributedText
@@ -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 <code>d097cc7a2720e8a6e0714d291aa27a034bd20a0b</code> today")
let layoutManager = try #require(textStorage.layoutManagers.first)
let codeRange = (textStorage.string as NSString).range(of: "d097cc7a2720e8a6e0714d291aa27a034bd20a0b")
for index in codeRange.location..<NSMaxRange(codeRange) {
#expect(!delegate.layoutManager(layoutManager, shouldBreakLineByHyphenatingBeforeCharacterAt: index))
}
}
@Test
func codeBlockIsNotHyphenated() throws {
let textStorage = try makeTextStorage(html: "<pre><code>configurationmanagementinitialization</code></pre>")
let layoutManager = try #require(textStorage.layoutManagers.first)
let codeRange = (textStorage.string as NSString).range(of: "configurationmanagementinitialization")
for index in codeRange.location..<NSMaxRange(codeRange) {
#expect(!delegate.layoutManager(layoutManager, shouldBreakLineByHyphenatingBeforeCharacterAt: index))
}
}
@Test
func plainTextCanStillBeHyphenated() throws {
let textStorage = try makeTextStorage(html: "Internationalization <code>code</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>code</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
}
}