Files
buzz/mobile/lib/shared/mentions/bound_mentions.dart
Matt Toohey 2cbdabdae5 fix(mobile): bind picked mentions by position and never show a key in the composer
A picked chip is now bound to its recipient by its place in the draft, not
by its label. The editor carries each binding across every edit with a
selection-aware diff, so deleting the first of two identical chips removes
that recipient, text typed before a chip moves it, and a chip stays bound
until an edit reaches into it or a letter or digit is typed directly against
it. Punctuation, straight and curly apostrophes, a hyphen or a slash after a
chip keep it and its recipient (`@Bob's idea`, `@Bob-bot`, `@Bob/Alice`),
the same boundary rule buzz-app uses (recommendation 3).

Namesakes no longer put a public key in the composer. A picked chip whose
name another candidate shares reads the chooser's short label, `@Sam · a1b2`,
while the draft text stays `@Sam`; both Sams can be picked and each chip
keeps its own key. The sent content qualifies namesakes of different keys
as `@Sam (<key>)` so bubbles still tell them apart, except inside code,
and each picked Sam gets its own p tag (recommendation 4). The ambiguity
refusal for a typed, unpicked `@sam` is unchanged.

Drafts persist their bindings as positions (`mentions`), with the saved
label-keyed `mention_keys` still read and an unreadable record kept as a
tombstone that refuses to send while an `@` remains. The agent prefill and
failed-send recovery restore text and bindings in one update.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Signed-off-by: Matt Toohey <contact@matttoohey.com>
2026-11-19 15:06:59 +11:00

116 lines
4.5 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'dart:math' as math;
import 'package:flutter/services.dart';
/// Where a picked mention sits in a draft: [start] is its `@`, [end] is
/// exclusive. The text between is the literal `@Label` the composer shows
/// as one chip. The binding to a recipient lives beside the range, so two
/// namesakes can both read `@Sam` and still notify two different people.
typedef MentionRange = ({int start, int end});
final _nameCharacter = RegExp(r'^[\p{L}\p{M}\p{N}_]', unicode: true);
/// Whether a mention ending at [end] in [text] still ends at a name
/// boundary. A letter, mark, digit or underscore typed directly against it
/// continues the name (`@Bobby`), so the mention returns to prose and its
/// recipient goes; punctuation, quotes, hyphens, slashes and emoji keep both
/// (`@Bob's`, `@Bob’s`, `@Bob-bot`, `@Bob/Alice`). Same rule as buzz-app's
/// `mentionBoundary` in `src/features/messages/mention-draft.ts`.
bool mentionEndsAtBoundary(String text, int end) =>
end >= text.length || !_nameCharacter.hasMatch(text.substring(end));
/// The range of [oldText] an edit replaced to produce [newText].
///
/// Identical neighbouring text makes a plain diff ambiguous: deleting the
/// first of `@Sam @Sam` reads the same as deleting the second, yet the two
/// chips notify different people. The selections tell them apart: a
/// selected range the edit replaced, a backspace ending at the caret or a
/// forward delete starting there (the new caret says which), or an
/// insertion at the caret. Any other edit, such as an IME rewriting the word
/// behind the caret, falls back to the longest common prefix and suffix.
TextRange editReplacedRange(
String oldText,
String newText, {
required TextSelection oldSelection,
required TextSelection newSelection,
}) {
bool replaces(int start, int end) =>
start >= 0 &&
start <= end &&
end <= oldText.length &&
newText.length >= oldText.length - (end - start) &&
newText.startsWith(oldText.substring(0, start)) &&
newText.endsWith(oldText.substring(end));
if (oldSelection.isValid && oldSelection.end <= oldText.length) {
final start = oldSelection.start;
final end = oldSelection.end;
if (!oldSelection.isCollapsed) {
if (replaces(start, end)) return TextRange(start: start, end: end);
} else {
final removed = oldText.length - newText.length;
if (removed > 0) {
final caret = newSelection.isValid ? newSelection.baseOffset : -1;
final backspace = (start: start - removed, end: start);
final forward = (start: start, end: start + removed);
for (final candidate
in caret == start ? [forward, backspace] : [backspace, forward]) {
if (replaces(candidate.start, candidate.end)) {
return TextRange(start: candidate.start, end: candidate.end);
}
}
} else if (replaces(start, start)) {
return TextRange(start: start, end: start);
}
}
}
final limit = math.min(oldText.length, newText.length);
var prefix = 0;
while (prefix < limit &&
oldText.codeUnitAt(prefix) == newText.codeUnitAt(prefix)) {
prefix++;
}
var suffix = 0;
while (suffix < limit - prefix &&
oldText.codeUnitAt(oldText.length - 1 - suffix) ==
newText.codeUnitAt(newText.length - 1 - suffix)) {
suffix++;
}
return TextRange(start: prefix, end: oldText.length - suffix);
}
/// Carries [ranges], in text order, across an edit of the old text that
/// replaced [replaced] to produce [newText]. The result holds one entry per
/// input: its new range, or null when the edit reached into the mention or
/// left it no longer ending at a name boundary. Text typed before a mention
/// moves it; text typed at its end leaves it where it is.
List<MentionRange?> remapMentionRanges(
List<MentionRange> ranges, {
required TextRange replaced,
required int oldLength,
required String newText,
}) {
final delta = newText.length - oldLength;
return [
for (final range in ranges)
() {
final MentionRange moved;
if (range.end <= replaced.start) {
moved = range;
} else if (range.start >= replaced.end) {
moved = (start: range.start + delta, end: range.end + delta);
} else {
return null;
}
if (moved.start < 0 ||
moved.end > newText.length ||
moved.start >= moved.end ||
newText.codeUnitAt(moved.start) != 0x40 /* @ */ ||
!mentionEndsAtBoundary(newText, moved.end)) {
return null;
}
return moved;
}(),
];
}