Skip to content

Commit

Permalink
repl: add autocomplete for filesystem modules
Browse files Browse the repository at this point in the history
PR-URL: #26648
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
  • Loading branch information
antsmartian authored and targos committed Apr 29, 2019
1 parent 7c25dce commit 149412c
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/test-repl-tab-completion/.hiddenfiles
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is hidden
1 change: 1 addition & 0 deletions test/fixtures/test-repl-tab-completion/hellorandom.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Random txt
1 change: 1 addition & 0 deletions test/fixtures/test-repl-tab-completion/helloworld.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("hello world");
39 changes: 39 additions & 0 deletions test/parallel/test-repl-tab-complete.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 149412c

Please sign in to comment.