Fixed error suggestion for new command syntax

Also renamed fetchCommands to getSuggestions.
master
Peery 7 years ago committed by lub
parent b6aa1ed45e
commit 56f9b0aa7b

@ -1,6 +1,7 @@
const fs = require('fs');
const commands = module.exports = {};
const msgRegex = /^!([a-zA-Z0-9]+) ([a-zA-Z0-9]+) ([a-zA-Z0-9]+) ?(.*)$/;
// Compute the edit distance between the two given strings
function getEditDistance(sourceString, targetString) {
@ -35,10 +36,10 @@ function getEditDistance(sourceString, targetString) {
return matrix[targetString.length][sourceString.length];
}
function fetchCommand(input, commands, threshhold){
function getSuggestions(input, list, threshhold){
let candidates = {};
let bestDistance = null;
for(let key in commands){
for(let key in list){
let distance = getEditDistance(input, key);
console.log("new it. key: "+key+" dist:"+distance);
if(distance >= threshhold) continue;
@ -55,7 +56,33 @@ function fetchCommand(input, commands, threshhold){
return candidates;
}
commands.fetchCommand = fetchCommand;
function suggestFix(msg, projects){
const shortcuts = msg.match(msgRegex);
const expansion = {};
expansion.project = findShortcut(shortcuts[1], projects);
if(expansion.project) {
expansion.project = expansion.project.toLowerCase();
} else {
return getSuggestions(shortcuts[1], projects);
}
expansion.service = findShortcut(shortcuts[2], projects[expansion.project]);
if(expansion.service) {
expansion.service = expansion.service.toLowerCase();
} else {
return getSuggestions(shortcuts[2], projects[expansion.project]);
}
expansion.subcommand = findShortcut(shortcuts[3], projects[expansion.project][expansion.service]);
if(expansion.subcommand) {
return null;
} else {
return getSuggestions(shortcuts[3], projects[expansion.project][expansion.service]);
}
}
commands.suggestFix = suggestFix;
commands.getEditDistance = getEditDistance;
const findShortcut = (shortcut, objects) => {
@ -80,9 +107,7 @@ const findShortcut = (shortcut, objects) => {
commands.expandCommand = (msg, projects) => {
return new Promise((resolve, reject) => {
const regex = /^!([a-zA-Z0-9]+) ([a-zA-Z0-9]+) ([a-zA-Z0-9]+) ?(.*)$/;
const shortcuts = msg.match(regex);
const shortcuts = msg.match(msgRegex);
const expansion = {};
expansion.project = findShortcut(shortcuts[1], projects);
@ -118,7 +143,7 @@ commands.expandCommand = (msg, projects) => {
};
commands.projects = {}
commands.projects = {};
fs.readdirSync('./projects').forEach((file) => {
let project = file.match(/^([a-z]*)\.js$/);
if(!project) {
@ -148,8 +173,8 @@ commands['imninja']['register'].usage = '<user localpart>';
commands['imninja']['register'].exec = (args) => {
//args is a <user input args string>.match with .regex
};
console.log(fetchCommand("inminja", commands)); // -> imninja
console.log(fetchCommand("34C21", commands, 4)); // -> {}*/
console.log(getSuggestions("inminja", commands)); // -> imninja
console.log(getSuggestions("34C21", commands, 4)); // -> {}*/
//console.log(getEditDistance("saturday", "sunday")); //should be 3
/*

@ -49,14 +49,14 @@ matrixClient.on('Room.timeline', function(event, room, resetTimeline) {
subcommand.exec(args, room, event);
}, (fail) => { // command not found
/* let tmp = commands.fetchCommand(project, commands.projects);
let tmp = commands.suggestFix(body, commands.projects);
let suggestions = "";
for(let i = 0; i < tmp.length; i++){
suggestions += "\n"+tmp[i];
}
matrixClient.sendNotice(room.roomId, 'Unrecognized command.\n' +
'Did you mean:'+suggestions);
*/
});
});