Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

repl: add autocomplete for filesystem modules #26648

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -1115,7 +1115,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