Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit f6a76ed

Browse files
committed
Fuzzy matching in User and Room providers
1 parent 442291c commit f6a76ed

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

src/autocomplete/RoomProvider.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@ import Q from 'q';
33
import MatrixClientPeg from '../MatrixClientPeg';
44
import Fuse from 'fuse.js';
55

6-
const ROOM_REGEX = /(?=#)[^\s]*/g;
6+
const ROOM_REGEX = /(?=#)([^\s]*)/g;
77

88
let instance = null;
99

1010
export default class RoomProvider extends AutocompleteProvider {
1111
constructor() {
1212
super();
13+
this.fuse = new Fuse([], {
14+
keys: ['name', 'roomId', 'aliases']
15+
});
1316
}
1417

1518
getCompletions(query: String) {
@@ -18,12 +21,20 @@ export default class RoomProvider extends AutocompleteProvider {
1821
const matches = query.match(ROOM_REGEX);
1922
const command = matches && matches[0];
2023
if(command) {
21-
completions = client.getRooms().map(room => {
24+
// the only reason we need to do this is because Fuse only matches on properties
25+
this.fuse.set(client.getRooms().filter(room => !!room).map(room => {
26+
return {
27+
name: room.name,
28+
roomId: room.roomId,
29+
aliases: room.getAliases()
30+
};
31+
}));
32+
completions = this.fuse.search(command).map(room => {
2233
return {
2334
title: room.name,
2435
subtitle: room.roomId
2536
};
26-
});
37+
}).slice(0, 4);;
2738
}
2839
return Q.when(completions);
2940
}

src/autocomplete/UserProvider.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import AutocompleteProvider from './AutocompleteProvider';
22
import Q from 'q';
3-
import MatrixClientPeg from '../MatrixClientPeg';
3+
import Fuse from 'fuse.js';
44

55
const ROOM_REGEX = /@[^\s]*/g;
66

@@ -10,19 +10,23 @@ export default class UserProvider extends AutocompleteProvider {
1010
constructor() {
1111
super();
1212
this.users = [];
13+
this.fuse = new Fuse([], {
14+
keys: ['displayName', 'userId']
15+
})
1316
}
1417

1518
getCompletions(query: String) {
1619
let completions = [];
17-
const matches = query.match(ROOM_REGEX);
18-
if(!!matches) {
19-
const command = matches[0];
20-
completions = this.users.map(user => {
20+
let matches = query.match(ROOM_REGEX);
21+
let command = matches && matches[0];
22+
if(command) {
23+
this.fuse.set(this.users);
24+
completions = this.fuse.search(command).map(user => {
2125
return {
2226
title: user.displayName || user.userId,
2327
description: user.userId
2428
};
25-
});
29+
}).slice(0, 4);
2630
}
2731
return Q.when(completions);
2832
}
@@ -32,7 +36,6 @@ export default class UserProvider extends AutocompleteProvider {
3236
}
3337

3438
setUserList(users) {
35-
console.log('setUserList');
3639
this.users = users;
3740
}
3841

0 commit comments

Comments
 (0)