Resolve emoji autocomplete not being temporally consistent (#8086)

* Adds a test to demonstrate the issue with emoji autocomplete reported in https://github.com/vector-im/element-web/issues/19302.

Signed-off-by: Ryan Browne <code@commonlawfeature.com>

* Trim trailing `:` when checking for autocompletes for emoji.

Closes https://github.com/vector-im/element-web/issues/19302

Signed-off-by: Ryan Browne <code@commonlawfeature.com>

* Move all references to the emoji delimiter character to reference a constant.

Signed-off-by: Ryan Browne <code@commonlawfeature.com>

* Revert "Move all references to the emoji delimiter character to reference a constant."

This reverts commit ac09e71e4c6151e35d21f612c9b329ead2a381f1.

Signed-off-by: Ryan Browne <code@commonlawfeature.com>

* Rename variable.

Signed-off-by: Ryan Browne <code@commonlawfeature.com>

* Make the test file a .js file.

Signed-off-by: Ryan Browne <code@commonlawfeature.com>

* Update quotes to match style and make a valid stubbed room.

Signed-off-by: Ryan Browne <code@commonlawfeature.com>

* Fix variable name and test reporting.

Signed-off-by: Ryan Browne <code@commonlawfeature.com>

* Use str.replace with a regex.

Signed-off-by: Ryan Browne <code@commonlawfeature.com>

* Use an improved regex that does not have have to iterate through the entire string, and can just backtrack at most the last 2 characters.

Signed-off-by: Ryan Browne <code@commonlawfeature.com>

* Revert "Use an improved regex that does not have have to iterate through the
entire string, and can just backtrack at most the last 2 characters."

This regex is very efficient, but requires a specific form of the emoji
shortcode that it is not clear is within our control. This is a restriction
that is not required by the technicalities of solving the bug this PR is
attempting to fix. (It requires that an emoji shortcode end with a colon.)

This reverts commit 220cb0efb8de247158c11daf9170464a57cc3af2.

Signed-off-by: Ryan Browne <code@commonlawfeature.com>

Co-authored-by: Ryan Browne <code@commonlawfeature.com>
This commit is contained in:
commonlawfeature 2022-04-14 13:31:50 -07:00 committed by GitHub
parent c9880feb04
commit 08a2d81d6b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 77 additions and 1 deletions

View file

@ -3,6 +3,7 @@ Copyright 2016 Aviral Dasgupta
Copyright 2017 Vector Creations Ltd
Copyright 2017, 2018 New Vector Ltd
Copyright 2019 The Matrix.org Foundation C.I.C.
Copyright 2022 Ryan Browne <code@commonlawfeature.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@ -62,6 +63,13 @@ function score(query, space) {
}
}
function colonsTrimmed(str: string): string {
// Trim off leading and potentially trailing `:` to correctly match the emoji data as they exist in emojibase.
// Notes: The regex is pinned to the start and end of the string so that we can use the lazy-capturing `*?` matcher.
// It needs to be lazy so that the trailing `:` is not captured in the replacement group, if it exists.
return str.replace(/^:(.*?):?$/, "$1");
}
export default class EmojiProvider extends AutocompleteProvider {
matcher: QueryMatcher<ISortedEmoji>;
nameMatcher: QueryMatcher<ISortedEmoji>;
@ -108,8 +116,9 @@ export default class EmojiProvider extends AutocompleteProvider {
// then sort by score (Infinity if matchedString not in shortcode)
sorters.push(c => score(matchedString, c.emoji.shortcodes[0]));
// then sort by max score of all shortcodes, trim off the `:`
const trimmedMatch = colonsTrimmed(matchedString);
sorters.push(c => Math.min(
...c.emoji.shortcodes.map(s => score(matchedString.substring(1), s)),
...c.emoji.shortcodes.map(s => score(trimmedMatch, s)),
));
// If the matchedString is not empty, sort by length of shortcode. Example:
// matchedString = ":bookmark"