From 149412ca02874cdca34aba86495ac8bb383127c7 Mon Sep 17 00:00:00 2001 From: Anto Aravinth Date: Thu, 25 Apr 2019 20:14:47 +0530 Subject: [PATCH] repl: add autocomplete for filesystem modules PR-URL: https://github.com/nodejs/node/pull/26648 Reviewed-By: Ruben Bridgewater --- lib/repl.js | 23 +++++++++++ .../test-repl-tab-completion/.hiddenfiles | 1 + .../test-repl-tab-completion/hellorandom.txt | 1 + .../test-repl-tab-completion/helloworld.js | 1 + test/parallel/test-repl-tab-complete.js | 39 +++++++++++++++++++ 5 files changed, 65 insertions(+) create mode 100644 test/fixtures/test-repl-tab-completion/.hiddenfiles create mode 100644 test/fixtures/test-repl-tab-completion/hellorandom.txt create mode 100644 test/fixtures/test-repl-tab-completion/helloworld.js diff --git a/lib/repl.js b/lib/repl.js index a7eb22e396e847..d439d205a40e0c 100644 --- a/lib/repl.js +++ b/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 diff --git a/test/fixtures/test-repl-tab-completion/.hiddenfiles b/test/fixtures/test-repl-tab-completion/.hiddenfiles new file mode 100644 index 00000000000000..8943faefedd9e6 --- /dev/null +++ b/test/fixtures/test-repl-tab-completion/.hiddenfiles @@ -0,0 +1 @@ +This is hidden \ No newline at end of file diff --git a/test/fixtures/test-repl-tab-completion/hellorandom.txt b/test/fixtures/test-repl-tab-completion/hellorandom.txt new file mode 100644 index 00000000000000..35268b4f156814 --- /dev/null +++ b/test/fixtures/test-repl-tab-completion/hellorandom.txt @@ -0,0 +1 @@ +Random txt \ No newline at end of file diff --git a/test/fixtures/test-repl-tab-completion/helloworld.js b/test/fixtures/test-repl-tab-completion/helloworld.js new file mode 100644 index 00000000000000..0f504264f5b1af --- /dev/null +++ b/test/fixtures/test-repl-tab-completion/helloworld.js @@ -0,0 +1 @@ +console.log("hello world"); \ No newline at end of file diff --git a/test/parallel/test-repl-tab-complete.js b/test/parallel/test-repl-tab-complete.js index 02cd06c9534bc5..364d880e8cc479 100644 --- a/test/parallel/test-repl-tab-complete.js +++ b/test/parallel/test-repl-tab-complete.js @@ -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,