From 72c1cf9288554c37bfcaba9291df9ea87be1bb45 Mon Sep 17 00:00:00 2001 From: Luke Barnard Date: Wed, 2 Aug 2017 10:09:00 +0100 Subject: [PATCH] When sorting completions, use matched string, not entire query Otherwise the results vary depending on where you start autocompleting in your message. We only care about the matched string. --- src/autocomplete/EmojiProvider.js | 10 +++++----- src/autocomplete/RoomProvider.js | 5 +++-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/autocomplete/EmojiProvider.js b/src/autocomplete/EmojiProvider.js index d0257de3e8..16e0347a5b 100644 --- a/src/autocomplete/EmojiProvider.js +++ b/src/autocomplete/EmojiProvider.js @@ -115,12 +115,12 @@ export default class EmojiProvider extends AutocompleteProvider { completions = completions.concat(this.nameMatcher.match(matchedString)); const sorters = []; - // First, sort by score (Infinity if query not in shortname) - sorters.push((c) => score(query, c.shortname)); - // If the query is not empty, sort by length of shortname. Example: - // query = ":bookmark" + // First, sort by score (Infinity if matchedString not in shortname) + sorters.push((c) => score(matchedString, c.shortname)); + // If the matchedString is not empty, sort by length of shortname. Example: + // matchedString = ":bookmark" // completions = [":bookmark:", ":bookmark_tabs:", ...] - if (query.length > 1) { + if (matchedString.length > 1) { sorters.push((c) => c.shortname.length); } // Finally, sort by original ordering diff --git a/src/autocomplete/RoomProvider.js b/src/autocomplete/RoomProvider.js index 5a8fd0a0b5..3749e7e693 100644 --- a/src/autocomplete/RoomProvider.js +++ b/src/autocomplete/RoomProvider.js @@ -63,9 +63,10 @@ export default class RoomProvider extends AutocompleteProvider { displayedAlias: getDisplayAliasForRoom(room), }; })); - completions = this.matcher.match(command[0]); + const matchedString = command[0]; + completions = this.matcher.match(matchedString); completions = _sortBy(completions, [ - (c) => score(query, c.displayedAlias), + (c) => score(matchedString, c.displayedAlias), (c) => c.displayedAlias.length, ]).map((room) => { const displayAlias = getDisplayAliasForRoom(room.room) || room.roomId;