Skip to content

Commit

Permalink
fix(autocomplete): fix arguments autocompletion (#53)
Browse files Browse the repository at this point in the history
fix arguments autocompletion
  • Loading branch information
mattallty committed May 29, 2017
1 parent 09eebf0 commit e21f846
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 20 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -4,3 +4,5 @@ coverage
.idea/workspace.xml
.idea/tasks.xml
.idea/watcherTasks.xml
docs
docs-src
19 changes: 14 additions & 5 deletions lib/autocomplete.js
Expand Up @@ -3,6 +3,7 @@
const tabtab = require('tabtab');
const parseArgs = require('micromist');
const Promise = require('bluebird');
const Option = require('./option');

class Autocomplete {

Expand Down Expand Up @@ -64,7 +65,7 @@ class Autocomplete {
const possibleCmd = arr.slice(0, index + 1).join(' ');
if (currentCommand && (currentCommand.name() === possibleCmd || currentCommand.alias() === possibleCmd)) {
realArgsCmdFound = true;
} else if (realArgsCmdFound) {
} else if (realArgsCmdFound && value.trim()) {
acc++;
}
return acc;
Expand Down Expand Up @@ -106,15 +107,21 @@ class Autocomplete {
const completion = this.getCompletion(arg);

if (typeof completion === 'function') {
return this._hanldleCompletionHandler(completion);
return this._hanldleCompletionHandler(completion, arg);
}

return Promise.resolve([]);
}

_hanldleCompletionHandler(handler) {
_hanldleCompletionHandler(handler, argOrOpt) {
const result = handler();
return Array.isArray(result) ? Promise.resolve(result) : result;
const type = argOrOpt instanceof Option ? 'option' : 'argument';
return Promise.resolve(result).then(res => {
if (!Array.isArray(res)) {
res = [res];
}
return res.map(r => r + `:Value for ${type} ${argOrOpt.synopsis()}`)
});
}

_getPossibleOptionNames(currentCommand, optionsAlreadyUsed, lastPartial, lastPartialIsOption) {
Expand Down Expand Up @@ -144,7 +151,7 @@ class Autocomplete {
// Promise completion
const completion = this.getCompletion(currentOption);
if (typeof completion === 'function') {
return this._hanldleCompletionHandler(completion);
return this._hanldleCompletionHandler(completion, currentOption);
}

return Promise.resolve([]);
Expand Down Expand Up @@ -178,6 +185,8 @@ class Autocomplete {
const cmd = args._.join(' ');
const currCommand = this._findCommand(cmd);



const realArgsCount = this._countArgs(currCommand, args);
const optionsAlreadyUsed = this._getOptionsAlreadyUsed(data.args);
const lastPartial = data.lastPartial;
Expand Down
40 changes: 25 additions & 15 deletions tests/autocomplete.js
Expand Up @@ -172,11 +172,11 @@ describe('Autocomplete', () => {
const response = this._complete.returnValues[0];
response.then(results => {
should(results).be.eql([
'store-1',
'store-2',
'store-3',
'store-4',
'store-5',
'store-1:Value for argument <from-store>',
'store-2:Value for argument <from-store>',
'store-3:Value for argument <from-store>',
'store-4:Value for argument <from-store>',
'store-5:Value for argument <from-store>',
'--number:Number of pizza',
'--discount:Discount offer',
'--pay-by:Pay by option',
Expand Down Expand Up @@ -205,10 +205,10 @@ describe('Autocomplete', () => {
"margherita:Value for argument <kind>",
"hawaiian:Value for argument <kind>",
"fredo:Value for argument <kind>",
"--number:Number of pizza",
"--discount:Discount offer",
"--pay-by:Pay by option",
"-e:Add extra ingredients"
'--number:Number of pizza',
'--discount:Discount offer',
'--pay-by:Pay by option',
'-e:Add extra ingredients'
]);
done();
}).catch(e => {
Expand All @@ -234,10 +234,10 @@ describe('Autocomplete', () => {
"margherita:Value for argument <kind>",
"hawaiian:Value for argument <kind>",
"fredo:Value for argument <kind>",
"--number:Number of pizza",
"--discount:Discount offer",
"--pay-by:Pay by option",
"-e:Add extra ingredients"
'--number:Number of pizza',
'--discount:Discount offer',
'--pay-by:Pay by option',
'-e:Add extra ingredients'
]);
done();
}).catch(e => {
Expand Down Expand Up @@ -377,7 +377,13 @@ describe('Autocomplete', () => {
should(this._complete.called).be.true();
const response = this._complete.returnValues[0];
response.then(results => {
should(results).containDeepOrdered(['#82792', '#71727', '#526Z52']);
should(results).eql([
'#82792:Value for argument <order-id>',
'#71727:Value for argument <order-id>',
'#526Z52:Value for argument <order-id>',
'--ask-change:Ask for other kind of pizza',
'--say-something:Say something to the manager'
]);
done();
}).catch(e => {
done(e);
Expand Down Expand Up @@ -421,7 +427,11 @@ describe('Autocomplete', () => {
should(this._complete.called).be.true();
const response = this._complete.returnValues[0];
response.then(results => {
should(results).eql(["margherita", "hawaiian", "fredo"]);
should(results).eql([
"margherita:Value for option --ask-change <other-kind-pizza>",
"hawaiian:Value for option --ask-change <other-kind-pizza>",
"fredo:Value for option --ask-change <other-kind-pizza>"
]);
done();
}).catch(e => {
done(e);
Expand Down

0 comments on commit e21f846

Please sign in to comment.