repl: add support for custom completions · nodejs/node@b3164ae · GitHub
Skip to content

Commit b3164ae

Browse files
diosneycjihrig
authored andcommitted
repl: add support for custom completions
Allow user code to override the default `complete()` function from `readline.Interface`. See: https://nodejs.org/api/readline.html#readline_use_of_the_completer_function Ref: nodejs/node-v0.x-archive#8484 PR-URL: #7527 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Lance Ball <lball@redhat.com>
1 parent c967af8 commit b3164ae

3 files changed

Lines changed: 80 additions & 7 deletions

File tree

doc/api/repl.md

Lines changed: 3 additions & 0 deletions

lib/repl.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -386,14 +386,15 @@ function REPLServer(prompt,
386386
self.bufferedCommand = '';
387387
self.lines.level = [];
388388

389-
function complete(text, callback) {
390-
self.complete(text, callback);
391-
}
389+
// Figure out which "complete" function to use.
390+
self.completer = (typeof options.completer === 'function')
391+
? options.completer
392+
: complete;
392393

393394
Interface.call(this, {
394395
input: self.inputStream,
395396
output: self.outputStream,
396-
completer: complete,
397+
completer: self.completer,
397398
terminal: options.terminal,
398399
historySize: options.historySize,
399400
prompt
@@ -706,6 +707,10 @@ function filteredOwnPropertyNames(obj) {
706707
return Object.getOwnPropertyNames(obj).filter(intFilter);
707708
}
708709

710+
REPLServer.prototype.complete = function() {
711+
this.completer.apply(this, arguments);
712+
};
713+
709714
// Provide a list of completions for the given leading text. This is
710715
// given to the readline interface for handling tab completion.
711716
//
@@ -716,7 +721,7 @@ function filteredOwnPropertyNames(obj) {
716721
//
717722
// Warning: This eval's code like "foo.bar.baz", so it will run property
718723
// getter code.
719-
REPLServer.prototype.complete = function(line, callback) {
724+
function complete(line, callback) {
720725
// There may be local variables to evaluate, try a nested REPL
721726
if (this.bufferedCommand !== undefined && this.bufferedCommand.length) {
722727
// Get a new array of inputed lines
@@ -975,7 +980,7 @@ REPLServer.prototype.complete = function(line, callback) {
975980

976981
callback(null, [completions || [], completeOn]);
977982
}
978-
};
983+
}
979984

980985

981986
/**

test/parallel/test-repl-tab-complete.js

Lines changed: 66 additions & 1 deletion

0 commit comments

Comments
 (0)