Skip to content

Commit

Permalink
feat(completion): Enhance package.json completion to support last word
Browse files Browse the repository at this point in the history
  • Loading branch information
mklabs committed May 1, 2016
1 parent 65e6654 commit ce794d4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
49 changes: 36 additions & 13 deletions lib/complete.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,32 +79,53 @@ class Complete extends EventEmitter {

if (!this.completePackage(env)) {
debug('No tabtab configuration in package.json, will emit events.');
process.nextTick(() => {
// Keeps emitting event only if previous one is not being listened to.
// Emits in series: first, prev and name.
var event = (first || env.prev || name).trim();
this.send(event, env, this.recv.bind(this))
|| this.send(env.prev.trim(), env, this.recv.bind(this))
|| this.send(name.trim(), env, this.recv.bind(this));
});

// Keeps emitting event only if previous one is not being listened to.
// Emits in series: first, prev and name.
var event = (first || env.prev || name).trim();
this.send(event, env, this.recv.bind(this))
|| this.send(env.prev.trim(), env, this.recv.bind(this))
|| this.send(name.trim(), env, this.recv.bind(this));
}
}

completePackage(env) {
completePackage(env, stop) {
var config = this.resolve('tabtab');
if (!config) return;

var pkgname = config[this.options.name];

var prop = env.last || env.prev;
var last = (env.last || env.prev).trim();
// last = last.replace(/^-+/, '');
var prop = last || this.options.name;
if (!prop) return false;

var command = config[prop];
if (!command) {
if (pkgname) return this.recv(null, pkgname, env);
if (stop) {
let first = env.line.split(' ')[0];
let results = config[first];
if (!results) return false;
return this.recv(null, results, env);
}

// Keeps looking up for completion only if previous one have not returned
// any results.
var command = config[prop];
var completions = this.recv(null, command, env);

if (!completions) {
if (last && !stop) {
let reg = new RegExp('\\s*' + last + '\\s*$');
let line = env.line.replace(reg, '');
completions = this.completePackage(this.parseEnv({
env: {
COMP_LINE: line,
COMP_WORD: env.words,
COMP_POINT: env.point
}
}), true);
}
}

return true
}

Expand All @@ -122,6 +143,8 @@ class Complete extends EventEmitter {
// completions - The Array of String completion results to write to stdout
// env - Env object as parsed by parseEnv
recv(err, completions, env) {
if (!completions) return;

env = env || this.env;

if (err) return this.emit('error', err);
Expand Down
2 changes: 1 addition & 1 deletion test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('CLI', () => {
tabtab('install')
// Prompt first answer, output to stdout
.prompt(/Where do you want/, '\n')
.expect('compctl ')
.expect('complete ')
.end(done);
});

Expand Down

0 comments on commit ce794d4

Please sign in to comment.