From 6a80875c0148de8015bdcb8c6d3620dc3466e805 Mon Sep 17 00:00:00 2001 From: Luke Barnard Date: Tue, 4 Jul 2017 16:24:59 +0100 Subject: [PATCH] Only match users by matchgin displayname or user ID prefixes Because tab completing "k" probably shouldn't give you "luke" Fixes https://github.com/vector-im/riot-web/issues/4495 --- src/autocomplete/QueryMatcher.js | 8 +++++++- src/autocomplete/UserProvider.js | 5 +++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/autocomplete/QueryMatcher.js b/src/autocomplete/QueryMatcher.js index 37ea169c29..f85d518fee 100644 --- a/src/autocomplete/QueryMatcher.js +++ b/src/autocomplete/QueryMatcher.js @@ -69,6 +69,12 @@ export default class QueryMatcher { if (this.options.shouldMatchWordsOnly === undefined) { this.options.shouldMatchWordsOnly = true; } + + // By default, match anywhere in the string being searched. If enabled, only return + // matches that are prefixed with the query. + if (this.options.shouldMatchPrefix === undefined) { + this.options.shouldMatchPrefix = false; + } } setObjects(objects: Array) { @@ -87,7 +93,7 @@ export default class QueryMatcher { resultKey = resultKey.replace(/[^\w]/g, ''); } const index = resultKey.indexOf(query); - if (index !== -1) { + if (index !== -1 && (!this.options.shouldMatchPrefix || index === 0)) { results.push({key, index}); } }); diff --git a/src/autocomplete/UserProvider.js b/src/autocomplete/UserProvider.js index b6be106a93..f1fa3a9236 100644 --- a/src/autocomplete/UserProvider.js +++ b/src/autocomplete/UserProvider.js @@ -37,10 +37,11 @@ export default class UserProvider extends AutocompleteProvider { constructor() { super(USER_REGEX, { - keys: ['name', 'userId'], + keys: ['name'], }); this.matcher = new FuzzyMatcher([], { - keys: ['name', 'userId'], + keys: ['name'], + shouldMatchPrefix: true, }); }