1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-27 10:32:58 +02:00
joplin/CliClient/app/autocompletion.js

204 lines
5.7 KiB
JavaScript
Raw Normal View History

const { app } = require('./app.js');
const Note = require('lib/models/Note.js');
const Folder = require('lib/models/Folder.js');
const Tag = require('lib/models/Tag.js');
const { cliUtils } = require('./cli-utils.js');
const yargParser = require('yargs-parser');
const 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();
const words = getArguments(line);
2019-10-09 21:35:13 +02:00
// If there is only one word and it is not already a command name then you
// should look for commands it could be
2017-12-14 05:25:18 +02:00
if (words.length == 1) {
if (names.indexOf(words[0]) === -1) {
const x = names.filter(n => n.indexOf(words[0]) === 0);
if (x.length === 1) {
2019-09-19 23:51:18 +02:00
return `${x[0]} `;
}
return x.length > 0 ? x.map(a => `${a} `) : line;
2017-12-14 05:25:18 +02:00
} else {
return line;
}
}
2019-10-09 21:35:13 +02:00
// There is more than one word and it is a command
2017-12-14 05:25:18 +02:00
const metadata = (await app().commandMetadata())[words[0]];
2019-10-09 21:35:13 +02:00
// If for some reason this command does not have any associated metadata
// just don't autocomplete. However, this should not happen.
2017-12-14 05:25:18 +02:00
if (metadata === undefined) {
return line;
}
if (words[0] === 'tag' && words[1] === 'notetags') {
metadata.usage = 'tag <tag-command> <note>';
}
2019-10-09 21:35:13 +02:00
// complete an option
const next = words.length > 1 ? words[words.length - 1] : '';
const l = [];
if (next[0] === '-') {
for (let i = 0; i < metadata.options.length; i++) {
const options = metadata.options[i][0].split(' ');
2019-10-09 21:35:13 +02:00
// if there are multiple options then they will be separated by comma and
// 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;
}
2019-10-09 21:35:13 +02:00
// First two elements are the flag and the third is the description
// Only autocomplete long
if (options.length > 1 && options[1].indexOf(next) === 0) {
l.push(options[1]);
} else if (options[0].indexOf(next) === 0) {
2018-01-18 22:29:13 +02:00
l.push(options[0]);
}
2017-12-14 05:25:18 +02:00
}
if (l.length === 0) {
return line;
}
const ret = l.map(a => toCommandLine(a));
2019-09-19 23:51:18 +02:00
ret.prefix = `${toCommandLine(words.slice(0, -1))} `;
return ret;
2017-12-14 05:25:18 +02:00
}
2019-10-09 21:35:13 +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
const positionalArgs = words.filter(a => a.indexOf('-') !== 0).length - 1;
const cmdUsage = yargParser(metadata.usage)['_'];
2017-12-14 05:25:18 +02:00
cmdUsage.splice(0, 1);
2017-12-14 05:13:43 +02:00
if (cmdUsage.length >= positionalArgs) {
let argName = cmdUsage[positionalArgs - 1];
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') {
2019-09-19 23:51:18 +02:00
const notes = currentFolder ? await Note.previews(currentFolder.id, { titlePattern: `${next}*` }) : [];
l.push(...notes.map(n => n.title));
}
2017-12-14 05:13:43 +02:00
if (argName == 'notebook') {
2019-09-19 23:51:18 +02:00
const folders = await Folder.search({ titlePattern: `${next}*` });
l.push(...folders.map(n => n.title));
}
2017-12-14 05:13:43 +02:00
if (argName == 'item') {
2019-09-19 23:51:18 +02:00
const notes = currentFolder ? await Note.previews(currentFolder.id, { titlePattern: `${next}*` }) : [];
const folders = await Folder.search({ titlePattern: `${next}*` });
l.push(...notes.map(n => n.title), folders.map(n => n.title));
}
if (argName == 'tag') {
const tags = await Tag.search({ titlePattern: `${next}*` });
l.push(...tags.map(n => n.title));
}
if (argName == 'file') {
const files = await fs.readdir('.');
l.push(...files);
}
2017-12-14 05:13:43 +02:00
if (argName == 'tag-command') {
const c = filterList(['add', 'remove', 'list', 'notetags'], next);
l.push(...c);
}
if (argName == 'todo-command') {
const c = filterList(['toggle', 'clear'], next);
l.push(...c);
}
}
if (l.length === 1) {
return toCommandLine([...words.slice(0, -1), l[0]]);
} else if (l.length > 1) {
const ret = l.map(a => toCommandLine(a));
2019-09-19 23:51:18 +02:00
ret.prefix = `${toCommandLine(words.slice(0, -1))} `;
return ret;
2017-12-14 05:25:18 +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
}
function toCommandLine(args) {
if (Array.isArray(args)) {
return args
.map(function(a) {
if (a.indexOf('"') !== -1 || a.indexOf(' ') !== -1) {
2019-09-19 23:51:18 +02:00
return `'${a}'`;
} else if (a.indexOf('\'') !== -1) {
2019-09-19 23:51:18 +02:00
return `"${a}"`;
} else {
return a;
}
})
.join(' ');
} else {
if (args.indexOf('"') !== -1 || args.indexOf(' ') !== -1) {
2019-09-19 23:51:18 +02:00
return `'${args}' `;
} else if (args.indexOf('\'') !== -1) {
2019-09-19 23:51:18 +02:00
return `"${args}" `;
} else {
2019-09-19 23:51:18 +02:00
return `${args} `;
}
}
}
function getArguments(line) {
let inSingleQuotes = false;
let inDoubleQuotes = false;
let currentWord = '';
const parsed = [];
for (let i = 0; i < line.length; i++) {
if (line[i] === '"') {
if (inDoubleQuotes) {
inDoubleQuotes = false;
2019-10-09 21:35:13 +02:00
// maybe push word to parsed?
// currentWord += '"';
} else {
inDoubleQuotes = true;
2019-10-09 21:35:13 +02:00
// currentWord += '"';
}
} else if (line[i] === '\'') {
if (inSingleQuotes) {
inSingleQuotes = false;
2019-10-09 21:35:13 +02:00
// maybe push word to parsed?
// currentWord += "'";
} else {
inSingleQuotes = true;
2019-10-09 21:35:13 +02:00
// 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) {
const output = [];
2017-12-14 05:25:18 +02:00
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 };