Skip to content

Commit

Permalink
repl: improve require() autocompletion
Browse files Browse the repository at this point in the history
Currently REPL supports autocompletion for core modules and those found
in node_modules.  This commit adds tab completion for modules relative
to the current directory.

PR-URL: #14409
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: Khaidi Chu <i@2333.moe>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
aqrln authored and MylesBorins committed Feb 12, 2018
1 parent 35f2508 commit b7c4e52
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,18 @@ function complete(line, callback) {
filter = match[1];
var dir, files, f, name, base, ext, abs, subfiles, s;
group = [];
var paths = module.paths.concat(Module.globalPaths);
let paths = [];

if (completeOn === '.') {
group = ['./', '../'];
} else if (completeOn === '..') {
group = ['../'];
} else if (/^\.\.?\//.test(completeOn)) {
paths = [process.cwd()];
} else {
paths = module.paths.concat(Module.globalPaths);
}

for (i = 0; i < paths.length; i++) {
dir = path.resolve(paths[i], subdir);
try {
Expand Down
50 changes: 50 additions & 0 deletions test/parallel/test-repl-tab-complete.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,56 @@ testMe.complete('require(\'n', common.mustCall(function(error, data) {
}));
}

// Test tab completion for require() relative to the current directory
{
putIn.run(['.clear']);

const cwd = process.cwd();
process.chdir(__dirname);

['require(\'.', 'require(".'].forEach((input) => {
testMe.complete(input, common.mustCall((err, data) => {
assert.strictEqual(err, null);
assert.strictEqual(data.length, 2);
assert.strictEqual(data[1], '.');
assert.strictEqual(data[0].length, 2);
assert.ok(data[0].includes('./'));
assert.ok(data[0].includes('../'));
}));
});

['require(\'..', 'require("..'].forEach((input) => {
testMe.complete(input, common.mustCall((err, data) => {
assert.strictEqual(err, null);
assert.deepStrictEqual(data, [['../'], '..']);
}));
});

['./', './test-'].forEach((path) => {
[`require('${path}`, `require("${path}`].forEach((input) => {
testMe.complete(input, common.mustCall((err, data) => {
assert.strictEqual(err, null);
assert.strictEqual(data.length, 2);
assert.strictEqual(data[1], path);
assert.ok(data[0].includes('./test-repl-tab-complete'));
}));
});
});

['../parallel/', '../parallel/test-'].forEach((path) => {
[`require('${path}`, `require("${path}`].forEach((input) => {
testMe.complete(input, common.mustCall((err, data) => {
assert.strictEqual(err, null);
assert.strictEqual(data.length, 2);
assert.strictEqual(data[1], path);
assert.ok(data[0].includes('../parallel/test-repl-tab-complete'));
}));
});
});

process.chdir(cwd);
}

// Make sure tab completion works on context properties
putIn.run(['.clear']);

Expand Down

0 comments on commit b7c4e52

Please sign in to comment.