Skip to content

Commit

Permalink
wip precompute ranks fully
Browse files Browse the repository at this point in the history
  • Loading branch information
gnprice committed Dec 7, 2024
1 parent 831ec10 commit 4ee0675
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
6 changes: 3 additions & 3 deletions lib/model/autocomplete.dart
Original file line number Diff line number Diff line change
Expand Up @@ -758,12 +758,12 @@ sealed class ComposeAutocompleteResult extends AutocompleteResult {}

/// An emoji chosen in an autocomplete interaction, via [EmojiAutocompleteView].
class EmojiAutocompleteResult extends ComposeAutocompleteResult {
EmojiAutocompleteResult(this.matchQuality, this.candidate);
EmojiAutocompleteResult(this.rank, this.candidate);

/// The quality of the emoji's match to the query.
/// A measure of the result's quality in the context of the query.
///
/// Used internally by [EmojiAutocompleteView] for ranking the results.
final EmojiMatchQuality matchQuality;
final int rank;

final EmojiCandidate candidate;
}
Expand Down
12 changes: 6 additions & 6 deletions lib/model/emoji.dart
Original file line number Diff line number Diff line change
Expand Up @@ -379,12 +379,13 @@ class EmojiAutocompleteView extends AutocompleteView<EmojiAutocompleteQuery, Emo
candidates: store.allEmojiCandidates(), results: unsorted)) {
return null;
}
return bucketSort(unsorted, _rankResult, numBuckets: _numResultRanks);
return bucketSort(unsorted, (r) => r.rank, numBuckets: _numResultRanks);
}

EmojiAutocompleteResult? _testCandidate(EmojiAutocompleteQuery query, EmojiCandidate candidate) {
final match = query.match(candidate);
return match == null ? null : EmojiAutocompleteResult(match, candidate);
if (match == null) return null;
return EmojiAutocompleteResult(_rankResult(match, candidate), candidate);
}

static const _numResultRanks = 9;
Expand All @@ -411,11 +412,10 @@ class EmojiAutocompleteView extends AutocompleteView<EmojiAutocompleteQuery, Emo
// only if the latter is also a match for the query. That mostly works,
// because emoji with the same name will mostly both match or both not;
// but it breaks if the Unicode emoji was a literal match.
static int _rankResult(EmojiAutocompleteResult result) {
if (result.matchQuality == EmojiMatchQuality.exact) {
static int _rankResult(EmojiMatchQuality matchQuality, EmojiCandidate candidate) {
if (matchQuality == EmojiMatchQuality.exact) {
return 0;
}
final candidate = result.candidate;
final isPopular = _isPopularEmoji(candidate);
final isCustomEmoji = switch (candidate.emojiType) {
// The web implementation calls this condition `is_realm_emoji`,
Expand All @@ -424,7 +424,7 @@ class EmojiAutocompleteView extends AutocompleteView<EmojiAutocompleteQuery, Emo
ReactionType.realmEmoji || ReactionType.zulipExtraEmoji => true,
ReactionType.unicodeEmoji => false,
};
return switch (result.matchQuality) {
return switch (matchQuality) {
EmojiMatchQuality.exact => throw Error(), // handled above
EmojiMatchQuality.prefix => isPopular ? 1 : isCustomEmoji ? 3 : 5,
EmojiMatchQuality.wordAligned => isPopular ? 2 : isCustomEmoji ? 4 : 6,
Expand Down

0 comments on commit 4ee0675

Please sign in to comment.