mirror of
https://github.com/nodejs/node.git
synced 2024-11-21 10:59:27 +00:00
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:
parent
377939eef8
commit
d69f004657
23
lib/repl.js
23
lib/repl.js
@ -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
test/fixtures/test-repl-tab-completion/.hiddenfiles
vendored
Normal file
1
test/fixtures/test-repl-tab-completion/.hiddenfiles
vendored
Normal file
@ -0,0 +1 @@
|
||||
This is hidden
|
1
test/fixtures/test-repl-tab-completion/hellorandom.txt
vendored
Normal file
1
test/fixtures/test-repl-tab-completion/hellorandom.txt
vendored
Normal file
@ -0,0 +1 @@
|
||||
Random txt
|
1
test/fixtures/test-repl-tab-completion/helloworld.js
vendored
Normal file
1
test/fixtures/test-repl-tab-completion/helloworld.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
console.log("hello world");
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user