use fuzzy to do search; but still do sorting

This commit is contained in:
Iuri Matias 2018-10-16 08:01:55 -04:00 committed by Pascal Precht
parent 3c563ae317
commit 0379f6bc35
No known key found for this signature in database
GPG Key ID: 0EE28D8D6FD85D7D
4 changed files with 27 additions and 35 deletions

View File

@ -26,51 +26,31 @@ class Suggestions {
getSuggestions(cmd) { getSuggestions(cmd) {
cmd = cmd.toLowerCase() cmd = cmd.toLowerCase()
if (cmd === 'web3' || cmd === 'web3.') { suggestions.push({value: 'web3.eth', command_type: "web3 object", description: "module for interacting with the Ethereum network"})
return [ suggestions.push({value: 'web3.net', command_type: "web3 object", description: "module for interacting with network properties"})
{value: 'web3.eth', command_type: "web3 object", description: "module for interacting with the Ethereum network"}, suggestions.push({value: 'web3.shh', command_type: "web3 object", description: "module for interacting with the whisper protocol"})
{value: 'web3.net', command_type: "web3 object", description: "module for interacting with network properties"}, suggestions.push({value: 'web3.bzz', command_type: "web3 object", description: "module for interacting with the swarm network"})
{value: 'web3.shh', command_type: "web3 object", description: "module for interacting with the whisper protocol"}, suggestions.push({value: 'web3.eth.getAccounts()', command_type: "web3 object", description: "get list of accounts"})
{value: 'web3.bzz', command_type: "web3 object", description: "module for interacting with the swarm network"},
{value: 'web3.eth.getAccounts()', command_type: "web3 object", description: "get list of accounts"}
]
}
let suggestions = [] let suggestions = []
for (let contractName in this.contracts) { for (let contractName in this.contracts) {
let contract = this.contracts[contractName] let contract = this.contracts[contractName]
if (contract.className.toLowerCase().indexOf(cmd) >= 0 || contract.deployedAddress.indexOf(cmd) >= 0 || cmd.indexOf('contract') >= 0) { suggestions.push({value: contract.className, command_type: "web3 object", description: "contract deployed at " + contract.deployedAddress});
suggestions.push({value: contract.className, command_type: "web3 object", description: "contract deployed at " + contract.deployedAddress});
}
if ('profile'.indexOf(cmd) >= 0) { suggestions.push({value: "profile " + contract.className, command_type: "embark command", description: "profile " + contract.className + " contract"});
suggestions.push({value: "profile " + contract.className, command_type: "embark command", description: "profile " + contract.className + " contract"});
}
} }
// TODO: make this registered through an API instead // TODO: make this registered through an API instead
if ('help'.indexOf(cmd) >= 0) { suggestions.push({value: 'help', command_type: "embark command", description: "displays quick list of some of the available embark commands"});
suggestions.push({value: 'help', command_type: "embark command", description: "displays quick list of some of the available embark commands"}); suggestions.push({value: 'versions', command_type: "embark command", description: "display versions in use for libraries and tools like web3 and solc"});
} suggestions.push({value: 'ipfs', command_type: "javascript object", description: "instantiated js-ipfs object configured to the current environment (available if ipfs is enabled)"});
if ('versions'.indexOf(cmd) >= 0) { suggestions.push({value: 'swarm', command_type: "javascript object", description: "instantiated swarm-api object configured to the current environment (available if swarm is enabled)"});
suggestions.push({value: 'versions', command_type: "embark command", description: "display versions in use for libraries and tools like web3 and solc"}); suggestions.push({value: 'web3', command_type: "javascript object", description: "instantiated web3.js object configured to the current environment"});
} suggestions.push({value: 'EmbarkJS', command_type: "javascript object", description: "EmbarkJS static functions for Storage, Messages, Names, etc."});
if ('ipfs'.indexOf(cmd) >= 0) {
suggestions.push({value: 'ipfs', command_type: "javascript object", description: "instantiated js-ipfs object configured to the current environment (available if ipfs is enabled)"});
}
if ('swarm'.indexOf(cmd) >= 0) {
suggestions.push({value: 'swarm', command_type: "javascript object", description: "instantiated swarm-api object configured to the current environment (available if swarm is enabled)"});
}
if (cmd == 'w' || cmd === 'we' || cmd === 'web') {
suggestions.push({value: 'web3', command_type: "javascript object", description: "instantiated web3.js object configured to the current environment"});
}
if ('embarkjs'.indexOf(cmd) >= 0) {
suggestions.push({value: 'EmbarkJS', command_type: "javascript object", description: "EmbarkJS static functions for Storage, Messages, Names, etc."});
}
// sort first the ones that match the command at the beginning of the string, then prefer smaller commands first // sort first the ones that match the command at the beginning of the string, then prefer smaller commands first
return suggestions.sort((x,y) => { return utils.fuzzySearch(cmd, suggestions, (result) => { return result.value + " " + result.description }).map((x) => x.original).sort((x,y) => {
let diff = x.value.indexOf(cmd) - y.value.indexOf(cmd) let diff = x.value.indexOf(cmd) - y.value.indexOf(cmd)
if (diff !== 0) return diff; if (diff !== 0) return diff;
return x.value.length - y.value.length; return x.value.length - y.value.length;

View File

@ -519,6 +519,11 @@ function copyToClipboard(text) {
clipboardy.writeSync(text); clipboardy.writeSync(text);
} }
function fuzzySearch(text, list, filter) {
const fuzzy = require('fuzzy');
return fuzzy.filter(text, list, {extract: (filter || function () {})})
}
module.exports = { module.exports = {
joinPath, joinPath,
dirname, dirname,
@ -564,5 +569,6 @@ module.exports = {
errorMessage, errorMessage,
timer, timer,
fileTreeSort, fileTreeSort,
copyToClipboard copyToClipboard,
fuzzySearch
}; };

5
package-lock.json generated
View File

@ -6610,6 +6610,11 @@
"resolved": "https://registry.npmjs.org/functional-red-black-tree/-/functional-red-black-tree-1.0.1.tgz", "resolved": "https://registry.npmjs.org/functional-red-black-tree/-/functional-red-black-tree-1.0.1.tgz",
"integrity": "sha1-GwqzvVU7Kg1jmdKcDj6gslIHgyc=" "integrity": "sha1-GwqzvVU7Kg1jmdKcDj6gslIHgyc="
}, },
"fuzzy": {
"version": "0.1.3",
"resolved": "https://registry.npmjs.org/fuzzy/-/fuzzy-0.1.3.tgz",
"integrity": "sha1-THbsL/CsGjap3M+aAN+GIweNTtg="
},
"ganache-cli": { "ganache-cli": {
"version": "6.1.8", "version": "6.1.8",
"resolved": "https://registry.npmjs.org/ganache-cli/-/ganache-cli-6.1.8.tgz", "resolved": "https://registry.npmjs.org/ganache-cli/-/ganache-cli-6.1.8.tgz",

View File

@ -67,6 +67,7 @@
"flatted": "0.2.3", "flatted": "0.2.3",
"follow-redirects": "1.5.7", "follow-redirects": "1.5.7",
"fs-extra": "2.1.2", "fs-extra": "2.1.2",
"fuzzy": "0.1.3",
"ganache-cli": "6.1.8", "ganache-cli": "6.1.8",
"glob": "7.1.3", "glob": "7.1.3",
"globule": "1.2.1", "globule": "1.2.1",