From 56f9b0aa7b4df58ce012531bea1a8c896046ed5b Mon Sep 17 00:00:00 2001 From: Peery Date: Tue, 17 Oct 2017 23:43:50 +0200 Subject: [PATCH] Fixed error suggestion for new command syntax Also renamed fetchCommands to getSuggestions. --- commands.js | 43 ++++++++++++++++++++++++++++++++++--------- index.js | 4 ++-- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/commands.js b/commands.js index 387aff3..eb29e3c 100644 --- a/commands.js +++ b/commands.js @@ -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 = ''; commands['imninja']['register'].exec = (args) => { //args is a .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 /* diff --git a/index.js b/index.js index a90703a..3d28ab3 100644 --- a/index.js +++ b/index.js @@ -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); -*/ + }); });