repl: add autocomplete for filesystem modules

PR-URL: https://github.com/nodejs/node/pull/26648
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
Anto Aravinth 2019-04-25 20:14:47 +05:30
parent 377939eef8
commit d69f004657
5 changed files with 65 additions and 0 deletions

View File

@ -1118,7 +1118,30 @@ function complete(line, callback) {
}
completionGroupsLoaded();
} else if (match = line.match(/fs\.\s*[a-z][a-zA-Z]+\(\s*["'](.*)/)) {
let filePath = match[1];
let fileList;
filter = '';
try {
fileList = fs.readdirSync(filePath, { withFileTypes: true });
completionGroups.push(fileList.map((dirent) => dirent.name));
completeOn = '';
} catch {
try {
const baseName = path.basename(filePath);
filePath = path.dirname(filePath);
fileList = fs.readdirSync(filePath, { withFileTypes: true });
const filteredValue = fileList.filter((d) =>
d.name.startsWith(baseName))
.map((d) => d.name);
completionGroups.push(filteredValue);
completeOn = filePath;
} catch {}
}
completionGroupsLoaded();
// Handle variable member lookup.
// We support simple chained expressions like the following (no function
// calls, etc.). That is for simplicity and also because we *eval* that

View File

@ -0,0 +1 @@
This is hidden

View File

@ -0,0 +1 @@
Random txt

View File

@ -0,0 +1 @@
console.log("hello world");

View File

@ -397,6 +397,45 @@ testMe.complete('obj.', common.mustCall((error, data) => {
assert(data[0].includes('obj.key'));
}));
// Tab completion for files/directories
{
putIn.run(['.clear']);
process.chdir(__dirname);
const readFileSync = 'fs.readFileSync("';
const fixturePath = `${readFileSync}../fixtures/test-repl-tab-completion`;
if (!common.isWindows) {
testMe.complete(fixturePath, common.mustCall((err, data) => {
assert.strictEqual(err, null);
assert.ok(data[0][0].includes('.hiddenfiles'));
assert.ok(data[0][1].includes('hellorandom.txt'));
assert.ok(data[0][2].includes('helloworld.js'));
}));
testMe.complete(`${fixturePath}/hello`,
common.mustCall((err, data) => {
assert.strictEqual(err, null);
assert.ok(data[0][0].includes('hellorandom.txt'));
assert.ok(data[0][1].includes('helloworld.js'));
})
);
testMe.complete(`${fixturePath}/.h`,
common.mustCall((err, data) => {
assert.strictEqual(err, null);
assert.ok(data[0][0].includes('.hiddenfiles'));
})
);
testMe.complete(`${readFileSync}./xxxRandom/random`,
common.mustCall((err, data) => {
assert.strictEqual(err, null);
assert.strictEqual(data[0].length, 0);
})
);
}
}
[
Array,
Buffer,