mirror of
https://github.com/laurent22/joplin.git
synced 2025-03-26 21:12:59 +02:00
Improve autocompletion
1. Removed autocomplete menu because it lists the entire line, not just what is being autocompleted. 2. Autocomplete positional args first unless cursor is at - then autocomplete long options 3. Don't autocomplete an options that is already present. 4. Other fixes
This commit is contained in:
parent
3bf9d01f0a
commit
39c73e1649
@ -14,6 +14,9 @@ async function handleAutocompletionPromise(line) {
|
||||
if (words.length == 1) {
|
||||
if (names.indexOf(words[0]) === -1) {
|
||||
let x = names.filter((n) => n.indexOf(words[0]) === 0);
|
||||
if (x.length === 1) {
|
||||
return x[0] + ' ';
|
||||
}
|
||||
return x.length > 0 ? x.map((a) => a + ' ') : line;
|
||||
} else {
|
||||
return line;
|
||||
@ -29,62 +32,70 @@ async function handleAutocompletionPromise(line) {
|
||||
//complete an option
|
||||
let next = words.length > 1 ? words[words.length - 1] : '';
|
||||
let l = [];
|
||||
for (let i = 0; i<metadata.options.length; i++) {
|
||||
const options = metadata.options[i][0].split(' ');
|
||||
//if there are multiple options then they will be seperated 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;
|
||||
}
|
||||
//First two elements are the flag and the third is the description
|
||||
if (options[0].indexOf(next) === 0) {
|
||||
l.push(line.slice(0, line.lastIndexOf(next)) + options[0] + ' ');
|
||||
}
|
||||
if (options.length > 1 && options[1].indexOf(next) === 0) {
|
||||
l.push(line.slice(0, line.lastIndexOf(next)) + options[1] + ' ');
|
||||
if (next[0] === '-') {
|
||||
for (let i = 0; i<metadata.options.length; i++) {
|
||||
const options = metadata.options[i][0].split(' ');
|
||||
//if there are multiple options then they will be seperated 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;
|
||||
}
|
||||
//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(line.slice(0, line.lastIndexOf(next)) + options[1] + ' ');
|
||||
} else if (options[0].indexOf(next) === 0) {
|
||||
l.push(line.slice(0, line.lastIndexOf(next)) + options[0] + ' ');
|
||||
}
|
||||
}
|
||||
}
|
||||
//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 && a !== '').length - 1;
|
||||
|
||||
|
||||
let cmdUsage = yargParser(metadata.usage)['_'];
|
||||
cmdUsage.splice(0, 1);
|
||||
|
||||
if (cmdUsage.length <= positionalArgs) return line;
|
||||
if (cmdUsage.length > positionalArgs) {
|
||||
|
||||
let argName = cmdUsage[positionalArgs];
|
||||
argName = cliUtils.parseCommandArg(argName).name;
|
||||
|
||||
if (argName == 'note' || argName == 'note-pattern' && app().currentFolder()) {
|
||||
const notes = await Note.previews(app().currentFolder().id, { titlePattern: next + '*' });
|
||||
l = l.concat(notes.map((n) => line.slice(0, line.lastIndexOf(next)) + n.title + ' '));
|
||||
}
|
||||
let argName = cmdUsage[positionalArgs];
|
||||
argName = cliUtils.parseCommandArg(argName).name;
|
||||
|
||||
if (argName == 'notebook') {
|
||||
const folders = await Folder.search({ titlePattern: next + '*' });
|
||||
l = l.concat(folders.map((n) => line.slice(0, line.lastIndexOf(next)) + n.title + ' '));
|
||||
}
|
||||
if (argName == 'note' || argName == 'note-pattern' && app().currentFolder()) {
|
||||
const notes = await Note.previews(app().currentFolder().id, { titlePattern: next + '*' });
|
||||
l = l.concat(notes.map((n) => line.slice(0, line.lastIndexOf(next)) + n.title + ' '));
|
||||
}
|
||||
|
||||
if (argName == 'tag') {
|
||||
let tags = await Tag.search({ titlePattern: next + '*' });
|
||||
l = l.concat(tags.map((n) => line.slice(0, line.lastIndexOf(next)) + n.title + ' '));
|
||||
}
|
||||
if (argName == 'notebook') {
|
||||
const folders = await Folder.search({ titlePattern: next + '*' });
|
||||
l = l.concat(folders.map((n) => line.slice(0, line.lastIndexOf(next)) + n.title + ' '));
|
||||
}
|
||||
|
||||
if (argName == 'tag-command') {
|
||||
let c = filterList(['add', 'remove', 'list'], next);
|
||||
l = l.concat(c.map((n) => line.slice(0, line.lastIndexOf(next)) + n + ' '))
|
||||
}
|
||||
if (argName == 'tag') {
|
||||
let tags = await Tag.search({ titlePattern: next + '*' });
|
||||
l = l.concat(tags.map((n) => line.slice(0, line.lastIndexOf(next)) + n.title + ' '));
|
||||
}
|
||||
|
||||
if (argName == 'todo-command') {
|
||||
let c = filterList(['toggle', 'clear'], next);
|
||||
l = l.concat(c.map((n) => line.slice(0, line.lastIndexOf(next)) + n + ' '))
|
||||
if (argName == 'tag-command') {
|
||||
let c = filterList(['add', 'remove', 'list'], next);
|
||||
l = l.concat(c.map((n) => line.slice(0, line.lastIndexOf(next)) + n + ' '))
|
||||
}
|
||||
|
||||
if (argName == 'todo-command') {
|
||||
let c = filterList(['toggle', 'clear'], next);
|
||||
l = l.concat(c.map((n) => line.slice(0, line.lastIndexOf(next)) + n + ' '))
|
||||
}
|
||||
}
|
||||
return l.length > 0 ? l : line;
|
||||
if (l.length === 1) {
|
||||
return l[0];
|
||||
} else if (l.length > 1) {
|
||||
return l;
|
||||
}
|
||||
return line;
|
||||
|
||||
}
|
||||
function handleAutocompletion(str, callback) {
|
||||
|
@ -111,7 +111,6 @@ class StatusBarWidget extends BaseWidget {
|
||||
history: this.history,
|
||||
default: this.promptState_.initialText,
|
||||
autoComplete: handleAutocompletion,
|
||||
autoCompleteMenu: true,
|
||||
autoCompleteHint : true,
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user