Skip to content

Commit 0adf987

Browse files
committed
autocomplete [nfc]: Add silent to MentionAutocompleteQuery
As it does in the web app, we'll want this bit to control the suggestion filtering: if the user wants a silent mention, then it won't make sense to offer wildcard mentions in the list (@ALL, @everyone, etc.).
1 parent 76b632d commit 0adf987

File tree

2 files changed

+32
-28
lines changed

2 files changed

+32
-28
lines changed

lib/model/autocomplete.dart

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ extension Autocomplete on ContentTextEditingController {
3434
if (match != null) {
3535
return AutocompleteIntent(
3636
syntaxStart: position,
37-
query: MentionAutocompleteQuery(match[1]!),
37+
query: MentionAutocompleteQuery(match[2]!, silent: match[1]! == '_'),
3838
textEditingValue: value);
3939
}
4040
}
@@ -57,7 +57,7 @@ final RegExp mentionAutocompleteMarkerRegex = (() {
5757

5858
return RegExp(
5959
beforeAtSign
60-
+ r'@_?'
60+
+ r'@(_?)' // capture, so we can distinguish silent mentions
6161
+ r'(|'
6262
// Reject on whitespace right after "@" or "@_". Emails can't start with
6363
// it, and full_name can't either (it's run through Python's `.strip()`).
@@ -271,11 +271,14 @@ class MentionAutocompleteView extends ChangeNotifier {
271271
}
272272

273273
class MentionAutocompleteQuery {
274-
MentionAutocompleteQuery(this.raw)
274+
MentionAutocompleteQuery(this.raw, {this.silent = false})
275275
: _lowercaseWords = raw.toLowerCase().split(' ');
276276

277277
final String raw;
278278

279+
/// Whether the user wants a silent mention (@_query, vs. @query).
280+
final bool silent;
281+
279282
final List<String> _lowercaseWords;
280283

281284
bool testUser(User user, AutocompleteDataCache cache) {
@@ -303,16 +306,16 @@ class MentionAutocompleteQuery {
303306

304307
@override
305308
String toString() {
306-
return '${objectRuntimeType(this, 'MentionAutocompleteQuery')}(raw: $raw})';
309+
return '${objectRuntimeType(this, 'MentionAutocompleteQuery')}(raw: $raw, silent: $silent})';
307310
}
308311

309312
@override
310313
bool operator ==(Object other) {
311-
return other is MentionAutocompleteQuery && other.raw == raw;
314+
return other is MentionAutocompleteQuery && other.raw == raw && other.silent == silent;
312315
}
313316

314317
@override
315-
int get hashCode => Object.hash('MentionAutocompleteQuery', raw);
318+
int get hashCode => Object.hash('MentionAutocompleteQuery', raw, silent);
316319
}
317320

318321
class AutocompleteDataCache {

test/model/autocomplete_test.dart

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ void main() {
8181
});
8282
}
8383

84-
MentionAutocompleteQuery queryOf(String raw) => MentionAutocompleteQuery(raw);
84+
MentionAutocompleteQuery queryOf(String raw) => MentionAutocompleteQuery(raw, silent: false);
85+
MentionAutocompleteQuery silentQueryOf(String raw) => MentionAutocompleteQuery(raw, silent: true);
8586

8687
doTest('', null);
8788
doTest('^', null);
@@ -120,36 +121,36 @@ void main() {
120121

121122
doTest('~@^_', queryOf('')); // Odd/unlikely, but should not crash
122123

123-
doTest('~@__^', queryOf('_'));
124+
doTest('~@__^', silentQueryOf('_'));
124125

125-
doTest('~@^abc^', queryOf('abc')); doTest('~@_^abc^', queryOf('abc'));
126-
doTest('~@a^bc^', queryOf('abc')); doTest('~@_a^bc^', queryOf('abc'));
127-
doTest('~@ab^c^', queryOf('abc')); doTest('~@_ab^c^', queryOf('abc'));
128-
doTest('~^@^', queryOf('')); doTest('~^@_^', queryOf(''));
126+
doTest('~@^abc^', queryOf('abc')); doTest('~@_^abc^', silentQueryOf('abc'));
127+
doTest('~@a^bc^', queryOf('abc')); doTest('~@_a^bc^', silentQueryOf('abc'));
128+
doTest('~@ab^c^', queryOf('abc')); doTest('~@_ab^c^', silentQueryOf('abc'));
129+
doTest('~^@^', queryOf('')); doTest('~^@_^', silentQueryOf(''));
129130
// but:
130131
doTest('^hello @chris^', null); doTest('^hello @_chris^', null);
131132

132-
doTest('~@abc^', queryOf('abc')); doTest('~@_abc^', queryOf('abc'));
133-
doTest(' ~@abc^', queryOf('abc')); doTest(' ~@_abc^', queryOf('abc'));
134-
doTest('(~@abc^', queryOf('abc')); doTest('(~@_abc^', queryOf('abc'));
135-
doTest('—~@abc^', queryOf('abc')); doTest('—~@_abc^', queryOf('abc'));
136-
doTest('"~@abc^', queryOf('abc')); doTest('"~@_abc^', queryOf('abc'));
137-
doTest('“~@abc^', queryOf('abc')); doTest('“~@_abc^', queryOf('abc'));
138-
doTest('。~@abc^', queryOf('abc')); doTest('。~@_abc^', queryOf('abc'));
139-
doTest('«~@abc^', queryOf('abc')); doTest('«~@_abc^', queryOf('abc'));
133+
doTest('~@abc^', queryOf('abc')); doTest('~@_abc^', silentQueryOf('abc'));
134+
doTest(' ~@abc^', queryOf('abc')); doTest(' ~@_abc^', silentQueryOf('abc'));
135+
doTest('(~@abc^', queryOf('abc')); doTest('(~@_abc^', silentQueryOf('abc'));
136+
doTest('—~@abc^', queryOf('abc')); doTest('—~@_abc^', silentQueryOf('abc'));
137+
doTest('"~@abc^', queryOf('abc')); doTest('"~@_abc^', silentQueryOf('abc'));
138+
doTest('“~@abc^', queryOf('abc')); doTest('“~@_abc^', silentQueryOf('abc'));
139+
doTest('。~@abc^', queryOf('abc')); doTest('。~@_abc^', silentQueryOf('abc'));
140+
doTest('«~@abc^', queryOf('abc')); doTest('«~@_abc^', silentQueryOf('abc'));
140141

141-
doTest('~@ab^c', queryOf('ab')); doTest('~@_ab^c', queryOf('ab'));
142-
doTest('~@a^bc', queryOf('a')); doTest('~@_a^bc', queryOf('a'));
143-
doTest('~@^abc', queryOf('')); doTest('~@_^abc', queryOf(''));
144-
doTest('~@^', queryOf('')); doTest('~@_^', queryOf(''));
142+
doTest('~@ab^c', queryOf('ab')); doTest('~@_ab^c', silentQueryOf('ab'));
143+
doTest('~@a^bc', queryOf('a')); doTest('~@_a^bc', silentQueryOf('a'));
144+
doTest('~@^abc', queryOf('')); doTest('~@_^abc', silentQueryOf(''));
145+
doTest('~@^', queryOf('')); doTest('~@_^', silentQueryOf(''));
145146

146-
doTest('Please ask ~@chris^', queryOf('chris')); doTest('Please ask ~@_chris^', queryOf('chris'));
147-
doTest('Please ask ~@chris bobbe^', queryOf('chris bobbe')); doTest('Please ask ~@_chris bobbe^', queryOf('chris bobbe'));
147+
doTest('Please ask ~@chris^', queryOf('chris')); doTest('Please ask ~@_chris^', silentQueryOf('chris'));
148+
doTest('Please ask ~@chris bobbe^', queryOf('chris bobbe')); doTest('Please ask ~@_chris bobbe^', silentQueryOf('chris bobbe'));
148149

149150
doTest('~@Rodion Romanovich Raskolnikov^', queryOf('Rodion Romanovich Raskolnikov'));
150-
doTest('~@_Rodion Romanovich Raskolniko^', queryOf('Rodion Romanovich Raskolniko'));
151+
doTest('~@_Rodion Romanovich Raskolniko^', silentQueryOf('Rodion Romanovich Raskolniko'));
151152
doTest('~@Родион Романович Раскольников^', queryOf('Родион Романович Раскольников'));
152-
doTest('~@_Родион Романович Раскольнико^', queryOf('Родион Романович Раскольнико'));
153+
doTest('~@_Родион Романович Раскольнико^', silentQueryOf('Родион Романович Раскольнико'));
153154
doTest('If @chris is around, please ask him.^', null); // @ sign is too far away from cursor
154155
doTest('If @_chris is around, please ask him.^', null); // @ sign is too far away from cursor
155156
});

0 commit comments

Comments
 (0)