★ wanayoo — archive 1999 https://github.com/nodejs/node/commit/b3f3ebf3b3Nouvelle recherche | Portail wanayoo
Skip to content
Permalink
Browse files
lib: repl multiline history support
PR-URL: #22153
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
antsmartian authored and targos committed Nov 3, 2018
1 parent 2331181 commit b3f3ebf3b35f8872f59f5926f5cb80362182cbcb
Showing with 34 additions and 0 deletions.
  1. +25 −0 lib/readline.js
  2. +2 −0 lib/repl.js
  3. +7 −0 test/parallel/test-repl-persistent-history.js
@@ -217,6 +217,7 @@ function Interface(input, output, completer, terminal) {

// Current line
this.line = '';
this.multiline = '';

this._setRawMode(true);
this.terminal = true;
@@ -327,6 +328,7 @@ Interface.prototype._addHistory = function() {
if (dupIndex !== -1) this.history.splice(dupIndex, 1);
}

this.multiline += this.line;
this.history.unshift(this.line);

// Only store so many
@@ -337,6 +339,29 @@ Interface.prototype._addHistory = function() {
return this.history[0];
};

// Called when a multiline is seen by the repl
Interface.prototype.undoHistory = function() {
if (this.terminal) {
this.history.shift();
}
};

// If it's a multiline code, then add history
// accordingly.
Interface.prototype.multilineHistory = function() {
// check if we got a multiline code
if (this.multiline !== '' && this.terminal) {
const dupIndex = this.history.indexOf(this.multiline);
if (dupIndex !== -1) this.history.splice(dupIndex, 1);
// Remove the last entered line as multiline
// already contains them.
this.history.shift();
this.history.unshift(this.multiline);
}

// clear the multiline buffer
this.multiline = '';
};

Interface.prototype._refreshLine = function() {
// line length
@@ -779,6 +779,7 @@ exports.start = function(prompt,

REPLServer.prototype.clearBufferedCommand = function clearBufferedCommand() {
this[kBufferedCommandSymbol] = '';
REPLServer.super_.prototype.multilineHistory.call(this);
};

REPLServer.prototype.close = function close() {
@@ -893,6 +894,7 @@ REPLServer.prototype.displayPrompt = function(preserveCursor) {
const len = this.lines.level.length ? this.lines.level.length - 1 : 0;
const levelInd = '..'.repeat(len);
prompt += levelInd + ' ';
REPLServer.super_.prototype.undoHistory.call(this);
}

// Do not overwrite `_initialPrompt` here
@@ -111,6 +111,13 @@ const tests = [
test: [UP],
expected: [prompt, replFailedRead, prompt, replDisabled, prompt]
},
{ // Tests multiline history
env: {},
test: ['{', '}', UP, CLEAR],
expected: [prompt, '{', '... ', '}', '{}\n',
prompt, `${prompt}{}`, prompt],
clean: false
},
{
before: function before() {
if (common.isWindows) {

0 comments on commit b3f3ebf

Please sign in to comment.