This repository was archived by the owner on Sep 11, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 819
Resolve emoji autocomplete not being temporally consistent #8086
Merged
turt2live
merged 13 commits into
matrix-org:develop
from
commonlawfeature:bug/element-web/issues/19302
Apr 14, 2022
+77
−1
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
837f916
Adds a test to demonstrate the issue with emoji autocomplete reported…
02566d6
Trim trailing `:` when checking for autocompletes for emoji.
ac09e71
Move all references to the emoji delimiter character to reference a c…
7cd2a97
Revert "Move all references to the emoji delimiter character to refer…
da10ca3
Rename variable.
3f8a4fe
Make the test file a .js file.
997a5bf
Update quotes to match style and make a valid stubbed room.
6ebabf1
Fix variable name and test reporting.
df18446
Merge branch 'develop' into bug/element-web/issues/19302
0317419
Use str.replace with a regex.
10d2e3f
Merge branch 'develop' into bug/element-web/issues/19302
220cb0e
Use an improved regex that does not have have to iterate through the …
3989a1f
Revert "Use an improved regex that does not have have to iterate thro…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 <[email protected]> | ||
|
||
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" | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
Copyright 2022 Ryan Browne <[email protected]> | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import EmojiProvider from '../../src/autocomplete/EmojiProvider'; | ||
import { mkStubRoom } from '../test-utils/test-utils'; | ||
|
||
const EMOJI_SHORTCODES = [ | ||
":+1", | ||
":heart", | ||
":grinning", | ||
":hand", | ||
":man", | ||
":sweat", | ||
":monkey", | ||
":boat", | ||
":mailbox", | ||
":cop", | ||
":bow", | ||
":kiss", | ||
":golf", | ||
]; | ||
|
||
// Some emoji shortcodes are too short and do not actually trigger autocompletion until the ending `:`. | ||
// This means that we cannot compare their autocompletion before and after the ending `:` and have | ||
// to simply assert that the final completion with the colon is the exact emoji. | ||
const TOO_SHORT_EMOJI_SHORTCODE = [ | ||
{ emojiShortcode: ":o", expectedEmoji: "⭕️" }, | ||
]; | ||
|
||
describe('EmojiProvider', function() { | ||
const testRoom = mkStubRoom(undefined, undefined, undefined); | ||
|
||
it.each(EMOJI_SHORTCODES)('Returns consistent results after final colon %s', async function(emojiShortcode) { | ||
const ep = new EmojiProvider(testRoom); | ||
const range = { "beginning": true, "start": 0, "end": 3 }; | ||
const completionsBeforeColon = await ep.getCompletions(emojiShortcode, range); | ||
const completionsAfterColon = await ep.getCompletions(emojiShortcode + ':', range); | ||
|
||
const firstCompletionWithoutColon = completionsBeforeColon[0].completion; | ||
const firstCompletionWithColon = completionsAfterColon[0].completion; | ||
|
||
expect(firstCompletionWithoutColon).toEqual(firstCompletionWithColon); | ||
}); | ||
|
||
it.each( | ||
TOO_SHORT_EMOJI_SHORTCODE, | ||
)('Returns correct results after final colon $emojiShortcode', async ({ emojiShortcode, expectedEmoji }) => { | ||
const ep = new EmojiProvider(testRoom); | ||
const range = { "beginning": true, "start": 0, "end": 3 }; | ||
const completions = await ep.getCompletions(emojiShortcode + ':', range); | ||
|
||
expect(completions[0].completion).toEqual(expectedEmoji); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.