Skip to content

Commit 149412c

Browse files
antsmartiantargos
authored andcommitted
repl: add autocomplete for filesystem modules
PR-URL: #26648 Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 7c25dce commit 149412c

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
@@ -1118,7 +1118,30 @@ function complete(line, callback) {
11181118
}
11191119

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

1123+
let filePath = match[1];
1124+
let fileList;
1125+
filter = '';
1126+
1127+
try {
1128+
fileList = fs.readdirSync(filePath, { withFileTypes: true });
1129+
completionGroups.push(fileList.map((dirent) => dirent.name));
1130+
completeOn = '';
1131+
} catch {
1132+
try {
1133+
const baseName = path.basename(filePath);
1134+
filePath = path.dirname(filePath);
1135+
fileList = fs.readdirSync(filePath, { withFileTypes: true });
1136+
const filteredValue = fileList.filter((d) =>
1137+
d.name.startsWith(baseName))
1138+
.map((d) => d.name);
1139+
completionGroups.push(filteredValue);
1140+
completeOn = filePath;
1141+
} catch {}
1142+
}
1143+
1144+
completionGroupsLoaded();
11221145
// Handle variable member lookup.
11231146
// We support simple chained expressions like the following (no function
11241147
// 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)