diff --git a/mobile/lib/features/channels/compose_bar/compose_bar_widget.dart b/mobile/lib/features/channels/compose_bar/compose_bar_widget.dart index 81c32ebcb3..ee6f578497 100644 --- a/mobile/lib/features/channels/compose_bar/compose_bar_widget.dart +++ b/mobile/lib/features/channels/compose_bar/compose_bar_widget.dart @@ -341,8 +341,9 @@ class ComposeBar extends HookConsumerWidget { final isModifyingText = useRef(false); final lastObservedEditingValue = useRef(controller.value); - // Insert a selected mention into the text field as a chip bound to it. - void insertMention(MentionCandidate candidate) { + // Insert a selected mention into the text field as a chip bound to it, + // followed by a space or by the closing punctuation that selected it. + void insertMention(MentionCandidate candidate, {String separator = ' '}) { ref .read(mentionHistoryProvider.notifier) .remember(channelId, candidate.pubkey); @@ -359,6 +360,7 @@ class ComposeBar extends HookConsumerWidget { start: start, end: cursor, candidate: candidate, + separator: separator, ); } finally { isModifyingText.value = false; @@ -422,24 +424,30 @@ class ComposeBar extends HookConsumerWidget { open = null; } - // Space after an exact, unique name selects it (section 5). + // Space or closing punctuation typed straight after an exact, unique + // name selects it (section 5); the typed character stays after the + // chip. Otherwise the character joins the query. final previousQuery = mentionQuery.value; if (open != null && previousQuery != null && previousQuery.trim().isNotEmpty && open.start == mentionStartIdx.value && - open.query == '$previousQuery ' && + open.query.length == previousQuery.length + 1 && + open.query.startsWith(previousQuery) && text.length == previousValue.text.length + 1) { - final chosen = _spaceMention( - ref, - channelId: channelId, - query: previousQuery, - opening: mentionOpening.value, - shown: shownMentions.value, - viewer: currentPubkey, - ); + final typed = open.query.substring(previousQuery.length); + final chosen = mentionTerminator(typed) + ? _spaceMention( + ref, + channelId: channelId, + query: previousQuery, + opening: mentionOpening.value, + shown: shownMentions.value, + viewer: currentPubkey, + ) + : null; if (chosen != null) { - insertMention(chosen); + insertMention(chosen, separator: typed); return; } } diff --git a/mobile/lib/features/channels/compose_bar/markdown_editing_controller.dart b/mobile/lib/features/channels/compose_bar/markdown_editing_controller.dart index f584e1c4b8..1197b65dd1 100644 --- a/mobile/lib/features/channels/compose_bar/markdown_editing_controller.dart +++ b/mobile/lib/features/channels/compose_bar/markdown_editing_controller.dart @@ -134,16 +134,18 @@ class _MarkdownEditingController extends TextEditingController { if (unchanged) notifyListeners(); } - /// Replaces the `@query` at `[start, end)` with `@Label ` bound to - /// [candidate], caret after the space. Mentions after it move with the - /// text in the same notification. + /// Replaces the `@query` at `[start, end)` with `@Label` and [separator] + /// (a space, or the closing punctuation that selected the name) bound to + /// [candidate], caret after the separator. Mentions after it move with + /// the text in the same notification. void insertMention({ required int start, required int end, required MentionCandidate candidate, + String separator = ' ', }) { final label = candidate.label; - final replacement = '@$label '; + final replacement = '@$label$separator'; final next = TextEditingValue( text: text.replaceRange(start, end, replacement), selection: TextSelection.collapsed(offset: start + replacement.length), diff --git a/mobile/lib/shared/mentions/mention_rules.dart b/mobile/lib/shared/mentions/mention_rules.dart index eeeb07a6b2..b3daf6ce20 100644 --- a/mobile/lib/shared/mentions/mention_rules.dart +++ b/mobile/lib/shared/mentions/mention_rules.dart @@ -246,6 +246,19 @@ String? exactMention(Iterable choices, String query) { return count == 1 ? found : null; } +final _mentionTerminators = RegExp(r'''^[ .,!?;:)\]}'’"”]$''', unicode: true); + +/// Section 5: whether [character], typed straight after a query, selects +/// its exact, unique name as Space does. Besides Space: closing punctuation, +/// a closing bracket for the ones a query opens after, or a closing quote, +/// so `@Bob,` and `@Bob's` mention Bob. The client keeps the character after +/// the mention in place of the space. Where Space would not select, the +/// character is typed into the query instead: `'` after `Pat O` while +/// `Pat O'Brien` is known stays in the query. Any other character, such as +/// a hyphen, is never a terminator. +bool mentionTerminator(String character) => + _mentionTerminators.hasMatch(character); + /// An open `@` query: `[start, end)` covers `@` and the query text. typedef MentionQuery = ({int start, int end, String query}); diff --git a/mobile/test/features/channels/compose_bar_test/exact_mention_tests.dart b/mobile/test/features/channels/compose_bar_test/exact_mention_tests.dart index df45dfcdf4..da57265926 100644 --- a/mobile/test/features/channels/compose_bar_test/exact_mention_tests.dart +++ b/mobile/test/features/channels/compose_bar_test/exact_mention_tests.dart @@ -423,4 +423,29 @@ void exactMentionTests() { ); expect(find.textContaining('is ambiguous'), findsOneWidget); }); + + testWidgets('closing punctuation never selects a shared name', ( + tester, + ) async { + await mountComposer( + tester, + members(), + (_, _, {mediaTags = const []}) async {}, + ); + final field = find.byType(TextField); + await tester.enterText(field, '@scout'); + await tester.pumpAndSettle(); + expect(scoutRows(), findsNWidgets(2)); + await tester.enterText(field, '@scout,'); + await tester.pumpAndSettle(); + expect(tester.widget(field).controller!.text, '@scout,'); + expect( + find.byKey(const ValueKey('composer-person-mention-chip')), + findsNothing, + ); + // Dispose before draining the chooser's pending search debounce, as the + // restore scenarios above do. + await tester.pumpWidget(const SizedBox.shrink()); + await tester.pump(mentionSearchDebounce * 2); + }); } diff --git a/mobile/test/features/channels/compose_bar_test/mention_chip_tests.dart b/mobile/test/features/channels/compose_bar_test/mention_chip_tests.dart index 316ce14b8f..4d95ad2209 100644 --- a/mobile/test/features/channels/compose_bar_test/mention_chip_tests.dart +++ b/mobile/test/features/channels/compose_bar_test/mention_chip_tests.dart @@ -216,6 +216,52 @@ void mentionChipTests() { await tester.pumpAndSettle(); expect(sent, [alice]); + // Closing punctuation typed straight after an exact, unique name + // selects it as Space does, and stays after the chip in place of the + // space (recommendation 7): `@bob,` is a bound Bob followed by `,`. + for (final mark in [ + ',', + '.', + ';', + ':', + '!', + '?', + ')', + ']', + '}', + "'", + '’', + '"', + '”', + ]) { + sent = null; + await tester.enterText(find.byType(TextField), 'ping @bob'); + await tester.pump(); + await tester.enterText(find.byType(TextField), 'ping @bob$mark'); + await tester.pumpAndSettle(); + expect(controller.text, 'ping @Bob$mark', reason: mark); + expect(controller.selection.baseOffset, 'ping @Bob$mark'.length); + expect(personChip, findsOneWidget, reason: mark); + await tester.tap(find.byIcon(BuzzIcons.arrowUp)); + await tester.pumpAndSettle(); + expect(sentText, 'ping @Bob$mark'); + expect(sent, [bob], reason: mark); + } + + // An inexact name (`@bo,`) and a character that is not a terminator + // (`@bob-`) keep the chooser's query: nothing is selected. + for (final text in ['@bo,', '@bob-']) { + await tester.enterText( + find.byType(TextField), + text.substring(0, text.length - 1), + ); + await tester.pump(); + await tester.enterText(find.byType(TextField), text); + await tester.pumpAndSettle(); + expect(controller.text, text); + expect(personChip, findsNothing, reason: text); + } + // Typed, unpicked names keep their existing fallback: an exact unique // member name notifies, after a bracket as after a space. sent = null; diff --git a/mobile/test/features/channels/compose_bar_test/mention_search_tests.dart b/mobile/test/features/channels/compose_bar_test/mention_search_tests.dart index c5e1b9a057..b89404f9ec 100644 --- a/mobile/test/features/channels/compose_bar_test/mention_search_tests.dart +++ b/mobile/test/features/channels/compose_bar_test/mention_search_tests.dart @@ -48,6 +48,8 @@ void mentionSearchTests() { 'the directory is searched only while a query could still name someone', (tester) async { final searched = []; + String? sentText; + List>? sentTags; await tester.pumpWidget( _buildComposeBar( uploadService: _testUploadService(nostr.Keys.generate().nsec), @@ -70,7 +72,10 @@ void mentionSearchTests() { person, ]; }, - onSend: (_, _, {mediaTags = const >[]}) async {}, + onSend: (text, _, {mediaTags = const >[]}) async { + sentText = text; + sentTags = mediaTags; + }, ), ); await _expandComposer(tester); @@ -106,6 +111,37 @@ void mentionSearchTests() { expect(row('Quinn'), findsOneWidget); expect(row('not in channel'), findsOneWidget); + // Closing punctuation after a person the directory found selects them + // as Space does once the search has finished (recommendation 7), and + // the send meets the outside-channel prompt as after a tapped pick. + await type(tester, '@qu', 'inn'); + expect(row('Quinn'), findsOneWidget); + await tester.enterText(field, '@quinn,'); + await tester.pumpAndSettle(); + expect(controller.text, '@Quinn,'); + expect( + find.byKey(const ValueKey('composer-person-mention-chip')), + findsOneWidget, + ); + expect(popover, findsNothing); + await tester.tap(find.byIcon(BuzzIcons.arrowUp)); + await tester.pumpAndSettle(); + expect(find.text('Mention people outside this channel?'), findsOneWidget); + // Send without inviting: Quinn is referenced, not notified. + await tester.tap( + find + .descendant( + of: find.byType(AlertDialog), + matching: find.byType(TextButton), + ) + .first, + ); + await tester.pumpAndSettle(); + expect(sentText, '@Quinn,'); + expect(sentTags, contains(orderedEquals(['mention', quinn.pubkey]))); + // The prompt took focus; the composer is a capsule again. + await _expandComposer(tester); + // Multi-word names are searched word by word: a second word that // continues no local name still goes to the directory, and one that // continues a found name keeps that person listed. Names fold accents