Skip to content

Commit 926ecaf

Browse files
committed
autocomplete: Identify when the user intends a channel link autocomplete
For this commit we temporarily intercept the query at the AutocompleteField widget, to avoid invoking the widgets that are still unimplemented. That lets us defer those widgets' logic to a separate later commit.
1 parent 26f0b49 commit 926ecaf

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

lib/model/autocomplete.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ extension ComposeContentAutocomplete on ComposeContentController {
4848
} else if (charAtPos == ':') {
4949
final match = _emojiIntentRegex.matchAsPrefix(textUntilCursor, pos);
5050
if (match == null) continue;
51+
} else if (charAtPos == '#') {
52+
final match = _channelLinkIntentRegex.matchAsPrefix(textUntilCursor, pos);
53+
if (match == null) continue;
5154
} else {
5255
continue;
5356
}
@@ -66,6 +69,10 @@ extension ComposeContentAutocomplete on ComposeContentController {
6669
final match = _emojiIntentRegex.matchAsPrefix(textUntilCursor, pos);
6770
if (match == null) continue;
6871
query = EmojiAutocompleteQuery(match[1]!);
72+
} else if (charAtPos == '#') {
73+
final match = _channelLinkIntentRegex.matchAsPrefix(textUntilCursor, pos);
74+
if (match == null) continue;
75+
query = ChannelLinkAutocompleteQuery(match[1] ?? match[2]!);
6976
} else {
7077
continue;
7178
}
@@ -165,6 +172,16 @@ final RegExp _emojiIntentRegex = (() {
165172
+ r')$');
166173
})();
167174

175+
final RegExp _channelLinkIntentRegex = () {
176+
// Similar reasoning as in _mentionIntentRegex.
177+
const before = r'(?<=^|\s|\p{Punctuation})';
178+
179+
// TODO: organize with comments for better readability
180+
// TODO(upstream): maybe use duplicate-named capture groups for better readability?
181+
// https://github.com/dart-lang/sdk/issues/61337
182+
return RegExp(unicode: true, before + r'#(?:\*\*(?!\s)((?:(?!\*\*).)*)|(?!\*\*|\s)(.*))$');
183+
}();
184+
168185
/// The text controller's recognition that the user might want autocomplete UI.
169186
class AutocompleteIntent<QueryT extends AutocompleteQuery> {
170187
AutocompleteIntent({

lib/widgets/autocomplete.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ class _AutocompleteFieldState<QueryT extends AutocompleteQuery, ResultT extends
4545
}
4646

4747
void _handleControllerChange() {
48-
final newQuery = widget.autocompleteIntent()?.query;
48+
var newQuery = widget.autocompleteIntent()?.query;
49+
if (newQuery is ChannelLinkAutocompleteQuery) newQuery = null; // TODO(#124)
4950
// First, tear down the old view-model if necessary.
5051
if (_viewModel != null
5152
&& (newQuery == null

0 commit comments

Comments
 (0)