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 authored and Michaël Zasso committed 2019-04-29 16:57:34 +02:00
1 parent 7c25dce7ba
commit 149412ca02
5 files changed
+65

No files matched your search

+23
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
+1
View File
@@ -0,0 +1 @@
This is hidden
@@ -0,0 +1 @@
Random txt
+1
View File
@@ -0,0 +1 @@
console.log("hello world");
+39
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,