2017-12-14 05:13:43 +02:00
|
|
|
var { app } = require('./app.js');
|
2018-01-05 19:40:57 +02:00
|
|
|
var Note = require('lib/models/Note.js');
|
|
|
|
var Folder = require('lib/models/Folder.js');
|
|
|
|
var Tag = require('lib/models/Tag.js');
|
2017-12-14 05:13:43 +02:00
|
|
|
var { cliUtils } = require('./cli-utils.js');
|
|
|
|
var yargParser = require('yargs-parser');
|
2018-01-28 15:59:58 +02:00
|
|
|
var fs = require('fs-extra');
|
2017-12-14 05:13:43 +02:00
|
|
|
|
|
|
|
async function handleAutocompletionPromise(line) {
|
2017-12-14 05:25:18 +02:00
|
|
|
// Auto-complete the command name
|
|
|
|
const names = await app().commandNames();
|
2017-12-23 03:48:56 +02:00
|
|
|
let words = getArguments(line);
|
2017-12-14 05:25:18 +02:00
|
|
|
//If there is only one word and it is not already a command name then you
|
|
|
|
//should look for commmands it could be
|
|
|
|
if (words.length == 1) {
|
|
|
|
if (names.indexOf(words[0]) === -1) {
|
|
|
|
let x = names.filter((n) => n.indexOf(words[0]) === 0);
|
2017-12-14 15:53:49 +02:00
|
|
|
if (x.length === 1) {
|
|
|
|
return x[0] + ' ';
|
|
|
|
}
|
2017-12-14 05:25:18 +02:00
|
|
|
return x.length > 0 ? x.map((a) => a + ' ') : line;
|
|
|
|
} else {
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//There is more than one word and it is a command
|
|
|
|
const metadata = (await app().commandMetadata())[words[0]];
|
|
|
|
//If for some reason this command does not have any associated metadata
|
|
|
|
//just don't autocomplete. However, this should not happen.
|
|
|
|
if (metadata === undefined) {
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
//complete an option
|
|
|
|
let next = words.length > 1 ? words[words.length - 1] : '';
|
|
|
|
let l = [];
|
2017-12-14 15:53:49 +02:00
|
|
|
if (next[0] === '-') {
|
|
|
|
for (let i = 0; i<metadata.options.length; i++) {
|
|
|
|
const options = metadata.options[i][0].split(' ');
|
2018-05-05 16:22:14 +02:00
|
|
|
//if there are multiple options then they will be separated by comma and
|
2017-12-14 15:53:49 +02:00
|
|
|
//space. The comma should be removed
|
|
|
|
if (options[0][options[0].length - 1] === ',') {
|
|
|
|
options[0] = options[0].slice(0, -1);
|
|
|
|
}
|
|
|
|
if (words.includes(options[0]) || words.includes(options[1])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
//First two elements are the flag and the third is the description
|
|
|
|
//Only autocomplete long
|
|
|
|
if (options.length > 1 && options[1].indexOf(next) === 0) {
|
2017-12-24 20:27:25 +02:00
|
|
|
l.push(options[1]);
|
2017-12-14 15:53:49 +02:00
|
|
|
} else if (options[0].indexOf(next) === 0) {
|
2018-01-18 22:29:13 +02:00
|
|
|
l.push(options[0]);
|
2017-12-23 03:48:56 +02:00
|
|
|
}
|
2017-12-14 05:25:18 +02:00
|
|
|
}
|
2017-12-24 20:27:25 +02:00
|
|
|
if (l.length === 0) {
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
let ret = l.map(a=>toCommandLine(a));
|
|
|
|
ret.prefix = toCommandLine(words.slice(0, -1)) + ' ';
|
|
|
|
return ret;
|
2017-12-14 05:25:18 +02:00
|
|
|
}
|
|
|
|
//Complete an argument
|
|
|
|
//Determine the number of positional arguments by counting the number of
|
|
|
|
//words that don't start with a - less one for the command name
|
2017-12-23 03:48:56 +02:00
|
|
|
const positionalArgs = words.filter((a)=>a.indexOf('-') !== 0).length - 1;
|
2017-12-14 15:53:49 +02:00
|
|
|
|
2017-12-14 05:25:18 +02:00
|
|
|
let cmdUsage = yargParser(metadata.usage)['_'];
|
|
|
|
cmdUsage.splice(0, 1);
|
2017-12-14 05:13:43 +02:00
|
|
|
|
2017-12-23 03:48:56 +02:00
|
|
|
if (cmdUsage.length >= positionalArgs) {
|
2017-12-14 05:13:43 +02:00
|
|
|
|
2017-12-23 03:48:56 +02:00
|
|
|
let argName = cmdUsage[positionalArgs - 1];
|
2017-12-14 15:53:49 +02:00
|
|
|
argName = cliUtils.parseCommandArg(argName).name;
|
2017-12-14 05:13:43 +02:00
|
|
|
|
2018-01-28 20:19:56 +02:00
|
|
|
const currentFolder = app().currentFolder();
|
|
|
|
|
|
|
|
if (argName == 'note' || argName == 'note-pattern') {
|
|
|
|
const notes = currentFolder ? await Note.previews(currentFolder.id, { titlePattern: next + '*' }) : [];
|
2017-12-24 20:27:25 +02:00
|
|
|
l.push(...notes.map((n) => n.title));
|
2017-12-14 15:53:49 +02:00
|
|
|
}
|
2017-12-14 05:13:43 +02:00
|
|
|
|
2017-12-14 15:53:49 +02:00
|
|
|
if (argName == 'notebook') {
|
|
|
|
const folders = await Folder.search({ titlePattern: next + '*' });
|
2017-12-24 20:27:25 +02:00
|
|
|
l.push(...folders.map((n) => n.title));
|
2017-12-14 15:53:49 +02:00
|
|
|
}
|
2017-12-14 05:13:43 +02:00
|
|
|
|
2018-01-28 16:12:54 +02:00
|
|
|
if (argName == 'item') {
|
2018-01-28 20:19:56 +02:00
|
|
|
const notes = currentFolder ? await Note.previews(currentFolder.id, { titlePattern: next + '*' }) : [];
|
2018-01-28 16:12:54 +02:00
|
|
|
const folders = await Folder.search({ titlePattern: next + '*' });
|
|
|
|
l.push(...notes.map((n) => n.title), folders.map((n) => n.title));
|
|
|
|
}
|
|
|
|
|
2017-12-14 15:53:49 +02:00
|
|
|
if (argName == 'tag') {
|
|
|
|
let tags = await Tag.search({ titlePattern: next + '*' });
|
2017-12-24 20:27:25 +02:00
|
|
|
l.push(...tags.map((n) => n.title));
|
2018-01-28 16:12:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (argName == 'file') {
|
|
|
|
let files = await fs.readdir('.');
|
|
|
|
l.push(...files);
|
|
|
|
}
|
2017-12-14 05:13:43 +02:00
|
|
|
|
2017-12-14 15:53:49 +02:00
|
|
|
if (argName == 'tag-command') {
|
|
|
|
let c = filterList(['add', 'remove', 'list'], next);
|
2017-12-24 20:27:25 +02:00
|
|
|
l.push(...c);
|
2017-12-14 15:53:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (argName == 'todo-command') {
|
|
|
|
let c = filterList(['toggle', 'clear'], next);
|
2017-12-24 20:27:25 +02:00
|
|
|
l.push(...c);
|
2017-12-14 15:53:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (l.length === 1) {
|
2017-12-24 20:27:25 +02:00
|
|
|
return toCommandLine([...words.slice(0, -1), l[0]]);
|
2017-12-14 15:53:49 +02:00
|
|
|
} else if (l.length > 1) {
|
2017-12-24 20:27:25 +02:00
|
|
|
let ret = l.map(a=>toCommandLine(a));
|
|
|
|
ret.prefix = toCommandLine(words.slice(0, -1)) + ' ';
|
|
|
|
return ret;
|
2017-12-14 05:25:18 +02:00
|
|
|
}
|
2017-12-14 15:53:49 +02:00
|
|
|
return line;
|
2017-12-14 05:13:43 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
function handleAutocompletion(str, callback) {
|
2017-12-14 05:25:18 +02:00
|
|
|
handleAutocompletionPromise(str).then(function(res) {
|
|
|
|
callback(undefined, res);
|
|
|
|
});
|
2017-12-14 05:13:43 +02:00
|
|
|
}
|
2017-12-23 03:48:56 +02:00
|
|
|
function toCommandLine(args) {
|
2017-12-24 20:27:25 +02:00
|
|
|
if (Array.isArray(args)) {
|
|
|
|
return args.map(function(a) {
|
|
|
|
if(a.indexOf('"') !== -1 || a.indexOf(' ') !== -1) {
|
|
|
|
return "'" + a + "'";
|
|
|
|
} else if (a.indexOf("'") !== -1) {
|
|
|
|
return '"' + a + '"';
|
|
|
|
} else {
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
}).join(' ');
|
|
|
|
} else {
|
|
|
|
if(args.indexOf('"') !== -1 || args.indexOf(' ') !== -1) {
|
|
|
|
return "'" + args + "' ";
|
|
|
|
} else if (args.indexOf("'") !== -1) {
|
|
|
|
return '"' + args + '" ';
|
2017-12-23 03:48:56 +02:00
|
|
|
} else {
|
2017-12-24 20:27:25 +02:00
|
|
|
return args + ' ';
|
2017-12-23 03:48:56 +02:00
|
|
|
}
|
2017-12-24 20:27:25 +02:00
|
|
|
}
|
2017-12-23 03:48:56 +02:00
|
|
|
}
|
|
|
|
function getArguments(line) {
|
|
|
|
let inSingleQuotes = false;
|
|
|
|
let inDoubleQuotes = false;
|
|
|
|
let currentWord = '';
|
|
|
|
let parsed = [];
|
|
|
|
for(let i = 0; i<line.length; i++) {
|
|
|
|
if(line[i] === '"') {
|
|
|
|
if(inDoubleQuotes) {
|
|
|
|
inDoubleQuotes = false;
|
|
|
|
//maybe push word to parsed?
|
|
|
|
//currentWord += '"';
|
|
|
|
} else {
|
|
|
|
inDoubleQuotes = true;
|
|
|
|
//currentWord += '"';
|
|
|
|
}
|
|
|
|
} else if(line[i] === "'") {
|
|
|
|
if(inSingleQuotes) {
|
|
|
|
inSingleQuotes = false;
|
|
|
|
//maybe push word to parsed?
|
|
|
|
//currentWord += "'";
|
|
|
|
} else {
|
|
|
|
inSingleQuotes = true;
|
|
|
|
//currentWord += "'";
|
|
|
|
}
|
|
|
|
} else if (/\s/.test(line[i]) &&
|
|
|
|
!(inDoubleQuotes || inSingleQuotes)) {
|
|
|
|
if (currentWord !== '') {
|
|
|
|
parsed.push(currentWord);
|
|
|
|
currentWord = '';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
currentWord += line[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!(inSingleQuotes || inDoubleQuotes) && /\s/.test(line[line.length - 1])) {
|
|
|
|
parsed.push('');
|
|
|
|
} else {
|
|
|
|
parsed.push(currentWord);
|
|
|
|
}
|
|
|
|
return parsed;
|
|
|
|
}
|
2017-12-14 05:13:43 +02:00
|
|
|
function filterList(list, next) {
|
2017-12-14 05:25:18 +02:00
|
|
|
let output = [];
|
|
|
|
for (let i = 0; i < list.length; i++) {
|
|
|
|
if (list[i].indexOf(next) !== 0) continue;
|
|
|
|
output.push(list[i]);
|
|
|
|
}
|
|
|
|
return output;
|
2017-12-14 05:13:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = { handleAutocompletion };
|