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

Commit 9fbe456

Browse files
diosneylance
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 18ae74c commit 9fbe456

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
@@ -698,6 +699,10 @@ function filteredOwnPropertyNames(obj) {
698699
return Object.getOwnPropertyNames(obj).filter(intFilter);
699700
}
700701

702+
REPLServer.prototype.complete = function() {
703+
this.completer.apply(this, arguments);
704+
};
705+
701706
// Provide a list of completions for the given leading text. This is
702707
// given to the readline interface for handling tab completion.
703708
//
@@ -708,7 +713,7 @@ function filteredOwnPropertyNames(obj) {
708713
//
709714
// Warning: This eval's code like "foo.bar.baz", so it will run property
710715
// getter code.
711-
REPLServer.prototype.complete = function(line, callback) {
716+
function complete(line, callback) {
712717
// There may be local variables to evaluate, try a nested REPL
713718
if (this.bufferedCommand !== undefined && this.bufferedCommand.length) {
714719
// Get a new array of inputed lines
@@ -967,7 +972,7 @@ REPLServer.prototype.complete = function(line, callback) {
967972

968973
callback(null, [completions || [], completeOn]);
969974
}
970-
};
975+
}
971976

972977

973978
/**

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

Lines changed: 66 additions & 1 deletion

0 commit comments

Comments
 (0)