diff --git a/.gitignore b/.gitignore index ea39995..0a3cf34 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,5 @@ coverage .idea/workspace.xml .idea/tasks.xml .idea/watcherTasks.xml +docs +docs-src diff --git a/lib/autocomplete.js b/lib/autocomplete.js index d2fa48b..8aedc6a 100644 --- a/lib/autocomplete.js +++ b/lib/autocomplete.js @@ -3,6 +3,7 @@ const tabtab = require('tabtab'); const parseArgs = require('micromist'); const Promise = require('bluebird'); +const Option = require('./option'); class Autocomplete { @@ -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; @@ -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) { @@ -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([]); @@ -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; diff --git a/tests/autocomplete.js b/tests/autocomplete.js index 7ecbb83..995ed22 100644 --- a/tests/autocomplete.js +++ b/tests/autocomplete.js @@ -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 ', + 'store-2:Value for argument ', + 'store-3:Value for argument ', + 'store-4:Value for argument ', + 'store-5:Value for argument ', '--number:Number of pizza', '--discount:Discount offer', '--pay-by:Pay by option', @@ -205,10 +205,10 @@ describe('Autocomplete', () => { "margherita:Value for argument ", "hawaiian:Value for argument ", "fredo:Value for argument ", - "--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 => { @@ -234,10 +234,10 @@ describe('Autocomplete', () => { "margherita:Value for argument ", "hawaiian:Value for argument ", "fredo:Value for argument ", - "--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 => { @@ -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 ', + '#71727:Value for argument ', + '#526Z52:Value for argument ', + '--ask-change:Ask for other kind of pizza', + '--say-something:Say something to the manager' + ]); done(); }).catch(e => { done(e); @@ -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 ", + "hawaiian:Value for option --ask-change ", + "fredo:Value for option --ask-change " + ]); done(); }).catch(e => { done(e);