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
This commit is contained in:
Luke Barnard 2017-07-04 16:24:59 +01:00
parent 2e95f5871c
commit 6a80875c01
2 changed files with 10 additions and 3 deletions

View file

@ -69,6 +69,12 @@ export default class QueryMatcher {
if (this.options.shouldMatchWordsOnly === undefined) { if (this.options.shouldMatchWordsOnly === undefined) {
this.options.shouldMatchWordsOnly = true; 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<Object>) { setObjects(objects: Array<Object>) {
@ -87,7 +93,7 @@ export default class QueryMatcher {
resultKey = resultKey.replace(/[^\w]/g, ''); resultKey = resultKey.replace(/[^\w]/g, '');
} }
const index = resultKey.indexOf(query); const index = resultKey.indexOf(query);
if (index !== -1) { if (index !== -1 && (!this.options.shouldMatchPrefix || index === 0)) {
results.push({key, index}); results.push({key, index});
} }
}); });

View file

@ -37,10 +37,11 @@ export default class UserProvider extends AutocompleteProvider {
constructor() { constructor() {
super(USER_REGEX, { super(USER_REGEX, {
keys: ['name', 'userId'], keys: ['name'],
}); });
this.matcher = new FuzzyMatcher([], { this.matcher = new FuzzyMatcher([], {
keys: ['name', 'userId'], keys: ['name'],
shouldMatchPrefix: true,
}); });
} }