repl: add support for custom completions · nodejs/node@9fbe456

@@ -32,7 +32,7 @@ testMe.complete('console.lo', common.mustCall(function(error, data) {

3232

assert.deepStrictEqual(data, [['console.log'], 'console.lo']);

3333

}));

343435-

// Tab Complete will return globaly scoped variables

35+

// Tab Complete will return globally scoped variables

3636

putIn.run(['};']);

3737

testMe.complete('inner.o', common.mustCall(function(error, data) {

3838

assert.deepStrictEqual(data, works);

@@ -283,3 +283,68 @@ if (typeof Intl === 'object') {

283283

testNonGlobal.complete('I', common.mustCall((error, data) => {

284284

assert.deepStrictEqual(data, builtins);

285285

}));

286+287+

// To test custom completer function.

288+

// Sync mode.

289+

const customCompletions = 'aaa aa1 aa2 bbb bb1 bb2 bb3 ccc ddd eee'.split(' ');

290+

const testCustomCompleterSyncMode = repl.start({

291+

prompt: '',

292+

input: putIn,

293+

output: putIn,

294+

completer: function completerSyncMode(line) {

295+

const hits = customCompletions.filter((c) => {

296+

return c.indexOf(line) === 0;

297+

});

298+

// Show all completions if none found.

299+

return [hits.length ? hits : customCompletions, line];

300+

}

301+

});

302+303+

// On empty line should output all the custom completions

304+

// without complete anything.

305+

testCustomCompleterSyncMode.complete('', common.mustCall((error, data) => {

306+

assert.deepStrictEqual(data, [

307+

customCompletions,

308+

''

309+

]);

310+

}));

311+312+

// On `a` should output `aaa aa1 aa2` and complete until `aa`.

313+

testCustomCompleterSyncMode.complete('a', common.mustCall((error, data) => {

314+

assert.deepStrictEqual(data, [

315+

'aaa aa1 aa2'.split(' '),

316+

'a'

317+

]);

318+

}));

319+320+

// To test custom completer function.

321+

// Async mode.

322+

const testCustomCompleterAsyncMode = repl.start({

323+

prompt: '',

324+

input: putIn,

325+

output: putIn,

326+

completer: function completerAsyncMode(line, callback) {

327+

const hits = customCompletions.filter((c) => {

328+

return c.indexOf(line) === 0;

329+

});

330+

// Show all completions if none found.

331+

callback(null, [hits.length ? hits : customCompletions, line]);

332+

}

333+

});

334+335+

// On empty line should output all the custom completions

336+

// without complete anything.

337+

testCustomCompleterAsyncMode.complete('', common.mustCall((error, data) => {

338+

assert.deepStrictEqual(data, [

339+

customCompletions,

340+

''

341+

]);

342+

}));

343+344+

// On `a` should output `aaa aa1 aa2` and complete until `aa`.

345+

testCustomCompleterAsyncMode.complete('a', common.mustCall((error, data) => {

346+

assert.deepStrictEqual(data, [

347+

'aaa aa1 aa2'.split(' '),

348+

'a'

349+

]);

350+

}));