Merge c6a747ac13b4ab6518ae4e1647e54c16a54d69de into 04995f68e470a18d1fc99f86fa326c2a87bab4f2

This commit is contained in:
Taksh Kothari 2026-10-30 13:16:52 +05:30 committed by GitHub
commit 887a1db6e0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 29 additions and 6 deletions

View File

@ -41,7 +41,9 @@ final class MessageFormattingEngine {
enum Patterns {
static let hashtag = SafeRegex.compile("#([a-zA-Z0-9_]+)")
static let mention = SafeRegex.compile("@([\\p{L}0-9_]+(?:#[a-fA-F0-9]{4})?)")
// Include \p{M} so NFD nicknames (base letter + combining mark) still
// parse as one mention token; NFC forms are covered by \p{L} alone.
static let mention = SafeRegex.compile("@([\\p{L}\\p{M}0-9_]+(?:#[a-fA-F0-9]{4})?)")
/// Trailing partial `@nickname` at the composer cursor — autocomplete only.
static let mentionAutocomplete = SafeRegex.compile("@([\\p{L}0-9_]*)$")

View File

@ -114,6 +114,9 @@ final class ChatComposerCoordinator {
}
func parseMentions(from content: String) -> [String] {
// Canonicalize before matching so NFD typed text and NFC peer tokens
// share one codepoint sequence (regex also accepts combining marks).
let content = content.precomposedStringWithCanonicalMapping
let regex = ChatViewModel.Patterns.mention
let nsContent = content as NSString
let matches = regex.matches(
@ -123,16 +126,22 @@ final class ChatComposerCoordinator {
)
let peerNicknames = context.meshPeerNicknames()
var validTokens = Set(peerNicknames.values)
validTokens.insert(context.nickname)
validTokens.insert(context.nickname + "#" + String(context.myPeerID.id.prefix(4)))
// Key by NFC so a typed combining-accent form still resolves to the
// stored (precomposed) nickname token used on the wire.
var tokensByCanonical = [String: String]()
for token in peerNicknames.values {
tokensByCanonical[token.normalizedNickname] = token
}
tokensByCanonical[context.nickname.normalizedNickname] = context.nickname
let selfSuffix = context.nickname + "#" + String(context.myPeerID.id.prefix(4))
tokensByCanonical[selfSuffix.normalizedNickname] = selfSuffix
var mentions: [String] = []
for match in matches {
guard let range = Range(match.range(at: 1), in: content) else { continue }
let mentionedName = String(content[range])
if validTokens.contains(mentionedName) {
mentions.append(mentionedName)
if let canonical = tokensByCanonical[mentionedName.normalizedNickname] {
mentions.append(canonical)
}
}

View File

@ -245,4 +245,16 @@ struct ChatComposerCoordinatorContextTests {
)
#expect(Set(mentions) == ["alice", "me", "me#0011"])
}
@Test @MainActor
func parseMentions_matchesUnicodeEquivalentNicknames() {
let context = MockChatComposerContext()
let coordinator = ChatComposerCoordinator(context: context)
let precomposed = "caf\u{00E9}"
let decomposed = "cafe\u{0301}"
context.meshNicknamesByPeerID = [PeerID(str: "1111111111111111"): precomposed]
let mentions = coordinator.parseMentions(from: "hi @\(decomposed)")
#expect(mentions == [precomposed])
}
}