Improve the sorting applied to the sorting of autocomplete results

QueryMatcher: sort results based on the position of the query within the matching value. The closer to the beginning of the word, the higher the result apears.

UserProvider: perf improvement (slice early) and refactor onUserSpoke
This commit is contained in:
Luke Barnard 2017-07-04 13:53:06 +01:00
parent fb86534ba3
commit e688eca823
2 changed files with 24 additions and 10 deletions

View file

@ -80,13 +80,24 @@ export default class QueryMatcher {
if (this.options.shouldMatchWordsOnly) { if (this.options.shouldMatchWordsOnly) {
query = query.replace(/[^\w]/g, ''); query = query.replace(/[^\w]/g, '');
} }
const results = _sortedUniq(_sortBy(_flatMap(this.keyMap.keys, (key) => { const results = [];
this.keyMap.keys.forEach((key) => {
let resultKey = key.toLowerCase(); let resultKey = key.toLowerCase();
if (this.options.shouldMatchWordsOnly) { if (this.options.shouldMatchWordsOnly) {
resultKey = resultKey.replace(/[^\w]/g, ''); resultKey = resultKey.replace(/[^\w]/g, '');
} }
return resultKey.indexOf(query) !== -1 ? this.keyMap.objectMap[key] : []; const index = resultKey.indexOf(query);
}), (candidate) => this.keyMap.priorityMap.get(candidate))); if (index !== -1) {
return results; results.push({key, index});
}
});
return _sortedUniq(_flatMap(_sortBy(results, (candidate) => {
return candidate.index;
}).map((candidate) => {
// return an array of objects (those given to setObjects) that have the given
// key as a property.
return this.keyMap.objectMap[candidate.key];
})));
} }
} }

View file

@ -50,7 +50,7 @@ export default class UserProvider extends AutocompleteProvider {
let completions = []; let completions = [];
let {command, range} = this.getCurrentCommand(query, selection, force); let {command, range} = this.getCurrentCommand(query, selection, force);
if (command) { if (command) {
completions = this.matcher.match(command[0]).map(user => { completions = this.matcher.match(command[0]).slice(0, 4).map((user) => {
let displayName = (user.name || user.userId || '').replace(' (IRC)', ''); // FIXME when groups are done let displayName = (user.name || user.userId || '').replace(' (IRC)', ''); // FIXME when groups are done
let completion = displayName; let completion = displayName;
if (range.start === 0) { if (range.start === 0) {
@ -68,7 +68,7 @@ export default class UserProvider extends AutocompleteProvider {
), ),
range, range,
}; };
}).slice(0, 4); });
} }
return completions; return completions;
} }
@ -90,7 +90,9 @@ export default class UserProvider extends AutocompleteProvider {
if (member.userId !== currentUserId) return true; if (member.userId !== currentUserId) return true;
}); });
this.users = _sortBy(this.users, (user) => 1E20 - lastSpoken[user.userId] || 1E20); this.users = _sortBy(this.users, (completion) =>
1E20 - lastSpoken[completion.user.userId] || 1E20,
);
this.matcher.setObjects(this.users); this.matcher.setObjects(this.users);
} }
@ -98,9 +100,10 @@ export default class UserProvider extends AutocompleteProvider {
onUserSpoke(user: RoomMember) { onUserSpoke(user: RoomMember) {
if(user.userId === MatrixClientPeg.get().credentials.userId) return; if(user.userId === MatrixClientPeg.get().credentials.userId) return;
// Probably unsafe to compare by reference here? this.users = this.users.splice(
_pull(this.users, user); this.users.findIndex((user2) => user2.userId === user.userId), 1);
this.users.splice(0, 0, user); this.users = [user, ...this.users];
this.matcher.setObjects(this.users); this.matcher.setObjects(this.users);
} }