Skip to content

Commit 01102c4

Browse files
committed
repl: add autocomplete for filesystem modules
1 parent 964174e commit 01102c4

File tree

5 files changed

+65
-0
lines changed

5 files changed

+65
-0
lines changed

lib/repl.js

+23
Original file line numberDiff line numberDiff line change
@@ -1115,7 +1115,30 @@ function complete(line, callback) {
11151115
}
11161116

11171117
completionGroupsLoaded();
1118+
} else if (match = line.match(/fs\.\s*[a-z][a-zA-Z]+\(\s*["'](.*)/)) {
11181119

1120+
let filePath = match[1];
1121+
let fileList;
1122+
filter = '';
1123+
1124+
try {
1125+
fileList = fs.readdirSync(filePath, { withFileTypes: true });
1126+
completionGroups.push(fileList.map((dirent) => dirent.name));
1127+
completeOn = '';
1128+
} catch {
1129+
try {
1130+
const baseName = path.basename(filePath);
1131+
filePath = path.dirname(filePath);
1132+
fileList = fs.readdirSync(filePath, { withFileTypes: true });
1133+
const filteredValue = fileList.filter((d) =>
1134+
d.name.startsWith(baseName))
1135+
.map((d) => d.name);
1136+
completionGroups.push(filteredValue);
1137+
completeOn = filePath;
1138+
} catch {}
1139+
}
1140+
1141+
completionGroupsLoaded();
11191142
// Handle variable member lookup.
11201143
// We support simple chained expressions like the following (no function
11211144
// calls, etc.). That is for simplicity and also because we *eval* that
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is hidden
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Random txt
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("hello world");

test/parallel/test-repl-tab-complete.js

+39
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,45 @@ testMe.complete('obj.', common.mustCall((error, data) => {
397397
assert(data[0].includes('obj.key'));
398398
}));
399399

400+
// Tab completion for files/directories
401+
{
402+
putIn.run(['.clear']);
403+
process.chdir(__dirname);
404+
405+
const readFileSync = 'fs.readFileSync("';
406+
const fixturePath = `${readFileSync}../fixtures/test-repl-tab-completion`;
407+
if (!common.isWindows) {
408+
testMe.complete(fixturePath, common.mustCall((err, data) => {
409+
assert.strictEqual(err, null);
410+
assert.ok(data[0][0].includes('.hiddenfiles'));
411+
assert.ok(data[0][1].includes('hellorandom.txt'));
412+
assert.ok(data[0][2].includes('helloworld.js'));
413+
}));
414+
415+
testMe.complete(`${fixturePath}/hello`,
416+
common.mustCall((err, data) => {
417+
assert.strictEqual(err, null);
418+
assert.ok(data[0][0].includes('hellorandom.txt'));
419+
assert.ok(data[0][1].includes('helloworld.js'));
420+
})
421+
);
422+
423+
testMe.complete(`${fixturePath}/.h`,
424+
common.mustCall((err, data) => {
425+
assert.strictEqual(err, null);
426+
assert.ok(data[0][0].includes('.hiddenfiles'));
427+
})
428+
);
429+
430+
testMe.complete(`${readFileSync}./xxxRandom/random`,
431+
common.mustCall((err, data) => {
432+
assert.strictEqual(err, null);
433+
assert.strictEqual(data[0].length, 0);
434+
})
435+
);
436+
}
437+
}
438+
400439
[
401440
Array,
402441
Buffer,

0 commit comments

Comments
 (0)