Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 94 additions & 12 deletions lib/internal/readline/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ const {
SafeStringIterator,
StringPrototypeCodePointAt,
StringPrototypeEndsWith,
StringPrototypeIndexOf,
StringPrototypeRepeat,
StringPrototypeReplaceAll,
StringPrototypeSlice,
StringPrototypeSplit,
StringPrototypeStartsWith,
StringPrototypeTrim,
Symbol,
Expand Down Expand Up @@ -99,7 +102,9 @@ const kDeleteWordLeft = Symbol('_deleteWordLeft');
const kDeleteWordRight = Symbol('_deleteWordRight');
const kGetDisplayPos = Symbol('_getDisplayPos');
const kHistoryNext = Symbol('_historyNext');
const kMoveDownOrHistoryNext = Symbol('_moveDownOrHistoryNext');
const kHistoryPrev = Symbol('_historyPrev');
const kMoveUpOrHistoryPrev = Symbol('_moveUpOrHistoryPrev');
const kInsertString = Symbol('_insertString');
const kLine = Symbol('_line');
const kLine_buffer = Symbol('_line_buffer');
Expand Down Expand Up @@ -449,8 +454,15 @@ class Interface extends InterfaceConstructor {

// If the trimmed line is empty then return the line
if (StringPrototypeTrim(this.line).length === 0) return this.line;

if (this.history.length === 0 || this.history[0] !== this.line) {
const normalizedLine = StringPrototypeReplaceAll(this.line, '\n', '\r');
Comment thread
BridgeAR marked this conversation as resolved.
Outdated

if (this.history.length === 0 || this.history[0] !== normalizedLine) {
if (this.lastCommandErrored && this.historyIndex === 0) {
// If the last command errored, remove it from history.
// The user is issuing a new command starting from the errored command,
// Hopefully with the fix
ArrayPrototypeShift(this.history);
}
Comment thread
BridgeAR marked this conversation as resolved.
Outdated
if (this.removeHistoryDuplicates) {
// Remove older history line if identical to new one
const dupIndex = ArrayPrototypeIndexOf(this.history, this.line);
Expand Down Expand Up @@ -498,8 +510,22 @@ class Interface extends InterfaceConstructor {
// erase data
clearScreenDown(this.output);

// Write the prompt and the current buffer content.
this[kWriteToOutput](line);
// Check if this is a multiline entry
const lines = StringPrototypeSplit(this.line, '\n');
Comment thread
BridgeAR marked this conversation as resolved.
Outdated
const isMultiline = lines.length > 1;

if (!isMultiline) {
// Write the prompt and the current buffer content.
this[kWriteToOutput](line);
} else {
// Write first line with normal prompt
this[kWriteToOutput](this[kPrompt] + lines[0]);

// For continuation lines, add the "|" prefix
for (let i = 1; i < lines.length; i++) {
this[kWriteToOutput]('\n| ' + lines[i]);
}
}

// Force terminal to allocate a new line
if (lineCols === 0) {
Expand Down Expand Up @@ -928,6 +954,29 @@ class Interface extends InterfaceConstructor {
this[kRefreshLine]();
}

[kMoveDownOrHistoryNext]() {
const { cols, rows } = this.getCursorPos();
const splitLine = StringPrototypeSplit(this.line, '\n');
if (!this.historyIndex && rows === splitLine.length) {
return;
}
// Go to the next history only if the cursor is in the first line of the multiline input.
// Otherwise treat the "arrow down" as a movement to the next row.
if (splitLine.length > 1 && rows < splitLine.length - 1) {
const currentLine = splitLine[rows];
const nextLine = splitLine[rows + 1];
const amountToMove = (cols > nextLine.length + 1) ?
currentLine.length - cols + nextLine.length + 3 :
currentLine.length + 1;
Comment thread
BridgeAR marked this conversation as resolved.
Outdated
// Go to the same position on the next line, or the end of the next line
// If the current position does not exist in the next line.
this[kMoveCursor](amountToMove);
return;
}

this[kHistoryNext]();
}

// TODO(BridgeAR): Add underscores to the search part and a red background in
// case no match is found. This should only be the visual part and not the
// actual line content!
Expand All @@ -950,14 +999,36 @@ class Interface extends InterfaceConstructor {
if (index === -1) {
this.line = search;
} else {
this.line = this.history[index];
this.line = StringPrototypeReplaceAll(this.history[index], '\r', '\n');
}
this.historyIndex = index;
this.cursor = this.line.length; // Set cursor to end of line.
this[kRefreshLine]();
}
}

[kMoveUpOrHistoryPrev]() {
const { cols, rows } = this.getCursorPos();
if (this.historyIndex === this.history.length && rows) {
return;
}
const splitLine = StringPrototypeSplit(this.line, '\n');
// Go to the previous history only if the cursor is in the first line of the multiline input.
// Otherwise treat the "arrow up" as a movement to the previous row.
if (splitLine.length > 1 && rows > 0) {
const previousLine = splitLine[rows - 1];
const amountToMove = (cols > previousLine.length + 1) ?
-cols + 1 :
-previousLine.length - 1;
// Go to the same position on the previous line, or the end of the previous line
// If the current position does not exist in the previous line.
this[kMoveCursor](amountToMove);
return;
}

this[kHistoryPrev]();
}

[kHistoryPrev]() {
if (this.historyIndex < this.history.length && this.history.length) {
this[kBeforeEdit](this.line, this.cursor);
Expand All @@ -973,7 +1044,7 @@ class Interface extends InterfaceConstructor {
if (index === this.history.length) {
this.line = search;
} else {
this.line = this.history[index];
this.line = StringPrototypeReplaceAll(this.history[index], '\r', '\n');
}
this.historyIndex = index;
this.cursor = this.line.length; // Set cursor to end of line.
Expand Down Expand Up @@ -1024,9 +1095,19 @@ class Interface extends InterfaceConstructor {
* }}
*/
getCursorPos() {
const strBeforeCursor =
this[kPrompt] + StringPrototypeSlice(this.line, 0, this.cursor);
return this[kGetDisplayPos](strBeforeCursor);
// Handle multiline input by accounting for "| " prefixes on continuation lines
const lineUptoCursor = StringPrototypeSlice(this.line, 0, this.cursor);
const lines = StringPrototypeSplit(lineUptoCursor, '\n');

// First line uses normal prompt, subsequent lines use "| " (2 chars)
let displayText = this[kPrompt] + lines[0];

// Add continuation lines with their prefixes
for (let i = 1; i < lines.length; i++) {
displayText += '\n| ' + lines[i];
}

return this[kGetDisplayPos](displayText);
Comment thread
BridgeAR marked this conversation as resolved.
Outdated
}

// This function moves cursor dx places to the right
Expand Down Expand Up @@ -1074,7 +1155,8 @@ class Interface extends InterfaceConstructor {
!key.meta &&
!key.shift
) {
if (this[kSubstringSearch] === null) {
const isLineMultiline = StringPrototypeIndexOf(this.line, '\n') !== -1;
if (this[kSubstringSearch] === null && !isLineMultiline) {
this[kSubstringSearch] = StringPrototypeSlice(
this.line,
0,
Expand Down Expand Up @@ -1308,11 +1390,11 @@ class Interface extends InterfaceConstructor {
break;

case 'up':
this[kHistoryPrev]();
this[kMoveUpOrHistoryPrev]();
break;

case 'down':
this[kHistoryNext]();
this[kMoveDownOrHistoryNext]();
break;

case 'tab':
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/repl/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ function setupHistory(repl, historyPath, ready) {
}

if (data) {
repl.history = RegExpPrototypeSymbolSplit(/[\n\r]+/, data, repl.historySize);
repl.history = RegExpPrototypeSymbolSplit(/[\n]+/, data, repl.historySize);
Comment thread
BridgeAR marked this conversation as resolved.
Outdated
} else {
repl.history = [];
}
Expand Down
22 changes: 22 additions & 0 deletions lib/internal/repl/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,15 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) {

let escaped = null;

let justExecutedMultilineCommand = false;

function getPreviewPos() {
const displayPos = repl._getDisplayPos(`${repl.getPrompt()}${repl.line}`);
// If the line is a multi line
if (StringPrototypeIndexOf(repl.line, '\n') !== -1) {
// This is to consider the additional "| " that prefixes the line
displayPos.cols += 2;
Comment thread
BridgeAR marked this conversation as resolved.
Outdated
}
const cursorPos = repl.line.length !== repl.cursor ?
repl.getCursorPos() :
displayPos;
Expand All @@ -177,6 +184,11 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) {
clearLine(repl.output);
moveCursor(repl.output, 0, -rows);
inputPreview = null;
// If pressing enter on some history line,
// The next preview should not be generated
if ((key.name === 'return' || key.name === 'enter') && !key.meta && repl.historyIndex !== -1) {
justExecutedMultilineCommand = true;
}
Comment thread
BridgeAR marked this conversation as resolved.
Outdated
}
if (completionPreview !== null) {
// Prevent cursor moves if not necessary!
Expand Down Expand Up @@ -373,6 +385,11 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) {
return;
}

if (justExecutedMultilineCommand) {
justExecutedMultilineCommand = false;
return;
}

hasCompletions = false;

// Add the autocompletion preview.
Expand Down Expand Up @@ -444,9 +461,14 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) {

const { cursorPos, displayPos } = getPreviewPos();
const rows = displayPos.rows - cursorPos.rows;
// Moves one line below all the user lines
moveCursor(repl.output, 0, rows);
// Writes the preview there
repl.output.write(`\n${result}`);

// Go back to the horizontal position of the cursor
cursorTo(repl.output, cursorPos.cols);
// Go back to the vertical position of the cursor
moveCursor(repl.output, 0, -rows - 1);
};

Expand Down
29 changes: 28 additions & 1 deletion lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ const {
StringPrototypeCodePointAt,
StringPrototypeEndsWith,
StringPrototypeIncludes,
StringPrototypeIndexOf,
StringPrototypeRepeat,
StringPrototypeReplaceAll,
StringPrototypeSlice,
StringPrototypeSplit,
StringPrototypeStartsWith,
Expand Down Expand Up @@ -949,7 +951,32 @@ function REPLServer(prompt,
self.displayPrompt();
return;
}
}

// In the next two if blocks, we do not use os.EOL instead of '\n'
// because on Windows it is '\r\n'
if (cmd && StringPrototypeIndexOf(cmd, '\n') !== -1) { // If you are editing a multiline command
Comment thread
BridgeAR marked this conversation as resolved.
Outdated
self.history[0] = StringPrototypeReplaceAll(cmd, '\n', '\r');
} else if (self[kBufferedCommandSymbol]) { // If a new multiline command was entered
// Remove the first N lines from the self.history array
// where N is the number of lines in the buffered command

const lines = StringPrototypeSplit(self[kBufferedCommandSymbol], '\n');
self.history = ArrayPrototypeSlice(self.history, lines.length);
ArrayPrototypePop(lines);
// And replace them with the single command split by '\r'
ArrayPrototypePush(lines, cmd);
Comment thread
BridgeAR marked this conversation as resolved.
Outdated
const newHistoryLine = ArrayPrototypeJoin(lines, '\r');
if (self.history[0] !== newHistoryLine) {
Comment thread
BridgeAR marked this conversation as resolved.
ArrayPrototypeUnshift(self.history, newHistoryLine);
}
}

if (e) {
self._domain.emit('error', e.err || e);
self.lastCommandErrored = true;
} else {
self.lastCommandErrored = false;
Comment thread
BridgeAR marked this conversation as resolved.
Outdated
}

// Clear buffer if no SyntaxErrors
Expand Down Expand Up @@ -1189,7 +1216,7 @@ REPLServer.prototype.resetContext = function() {
REPLServer.prototype.displayPrompt = function(preserveCursor) {
let prompt = this._initialPrompt;
if (this[kBufferedCommandSymbol].length) {
prompt = '...';
prompt = '|';
const len = this.lines.level.length ? this.lines.level.length - 1 : 0;
const levelInd = StringPrototypeRepeat('..', len);
prompt += levelInd + ' ';
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/.node_repl_history-multiline
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var d = [ { a: 1, b: 2, }, { a: 3, b: 4, c: [{ a: 1, b: 2 }, { a: 3, b: 4, } ] }]
const c = [ { a: 1, b: 2, }]
`const b = [ 1, 2, 3, 4,]`
a = `I am a multiline stringI can be as long as I want`
Expand Down
Loading
Loading