Skip to content

Commit 54b1add

Browse files
committed
autocomplete [nfc]: Split intent-finding loop to within/before selection
This puts much less control flow inside the `@` case. That will help simplify things as we add more kinds of autocomplete beyond @-mentions.
1 parent 6b3d426 commit 54b1add

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

lib/model/autocomplete.dart

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,33 @@ extension ComposeContentAutocomplete on ComposeContentController {
3232
}
3333

3434
final textUntilCursor = text.substring(0, selection.end);
35-
for (int pos = selection.end - 1; pos >= earliest; pos--) {
35+
int pos;
36+
for (pos = selection.end - 1; pos > selection.start; pos--) {
3637
final charAtPos = textUntilCursor[pos];
3738
if (charAtPos == '@') {
3839
final match = _mentionIntentRegex.matchAsPrefix(textUntilCursor, pos);
39-
if (match == null) {
40-
continue;
41-
}
42-
if (selection.start < pos) {
43-
// See comment about [TextSelection.isCollapsed] above.
44-
return null;
45-
}
46-
return AutocompleteIntent(
47-
syntaxStart: pos,
48-
query: MentionAutocompleteQuery(match[2]!, silent: match[1]! == '_'),
49-
textEditingValue: value);
40+
if (match == null) continue;
41+
} else {
42+
continue;
5043
}
44+
// See comment about [TextSelection.isCollapsed] above.
45+
return null;
46+
}
47+
48+
for (; pos >= earliest; pos--) {
49+
final charAtPos = textUntilCursor[pos];
50+
final ComposeAutocompleteQuery query;
51+
if (charAtPos == '@') {
52+
final match = _mentionIntentRegex.matchAsPrefix(textUntilCursor, pos);
53+
if (match == null) continue;
54+
query = MentionAutocompleteQuery(match[2]!, silent: match[1]! == '_');
55+
} else {
56+
continue;
57+
}
58+
return AutocompleteIntent(syntaxStart: pos, textEditingValue: value,
59+
query: query);
5160
}
61+
5262
return null;
5363
}
5464
}

0 commit comments

Comments
 (0)