Skip to content

Commit a434fe4

Browse files
committed
repl: consider removeHistoryDuplicates when saving multiline history
1 parent 96fb156 commit a434fe4

3 files changed

Lines changed: 16 additions & 11 deletions

File tree

lib/internal/readline/interface.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -936,14 +936,17 @@ class Interface extends InterfaceConstructor {
936936
[kMoveDownOrHistoryNext]() {
937937
const { cols, rows } = this.getCursorPos();
938938
const splitLine = StringPrototypeSplit(this.line, '\n');
939+
if (!this.historyIndex && rows === splitLine.length) {
940+
return;
941+
}
939942
// Go to the next history only if the cursor is in the first line of the multiline input.
940943
// Otherwise treat the "arrow down" as a movement to the next row.
941944
if (splitLine.length > 1 && rows < splitLine.length - 1) {
942945
const currentLine = splitLine[rows];
943946
const nextLine = splitLine[rows + 1];
944947
const amountToMove = (cols > nextLine.length) ?
945-
+cols + nextLine.length - 1 :
946-
+currentLine.length + 1;
948+
currentLine.length - cols + nextLine.length + 1 :
949+
currentLine.length + 1;
947950
// Go to the same position on the next line, or the end of the next line
948951
// If the current position does not exist in the next line.
949952
this[kMoveCursor](amountToMove);
@@ -963,9 +966,7 @@ class Interface extends InterfaceConstructor {
963966
[kHistoryNext]() {
964967
if (this.historyIndex >= 0) {
965968
this[kBeforeEdit](this.line, this.cursor);
966-
const isLineMultiline = StringPrototypeIndexOf(this.line, '\n') !== -1;
967-
968-
const search = isLineMultiline ? '' : this[kSubstringSearch] || '';
969+
const search = this[kSubstringSearch] || '';
969970
let index = this.historyIndex - 1;
970971
while (
971972
index >= 0 &&
@@ -987,6 +988,9 @@ class Interface extends InterfaceConstructor {
987988

988989
[kMoveUpOrHistoryPrev]() {
989990
const { cols, rows } = this.getCursorPos();
991+
if (this.historyIndex === this.history.length && rows) {
992+
return;
993+
}
990994
const splitLine = StringPrototypeSplit(this.line, '\n');
991995
// Go to the previous history only if the cursor is in the first line of the multiline input.
992996
// Otherwise treat the "arrow up" as a movement to the previous row.
@@ -1007,9 +1011,7 @@ class Interface extends InterfaceConstructor {
10071011
[kHistoryPrev]() {
10081012
if (this.historyIndex < this.history.length && this.history.length) {
10091013
this[kBeforeEdit](this.line, this.cursor);
1010-
const isLineMultiline = StringPrototypeIndexOf(this.line, '\n') !== -1;
1011-
1012-
const search = isLineMultiline ? '' : this[kSubstringSearch] || '';
1014+
const search = this[kSubstringSearch] || '';
10131015
let index = this.historyIndex + 1;
10141016
while (
10151017
index < this.history.length &&
@@ -1122,7 +1124,8 @@ class Interface extends InterfaceConstructor {
11221124
!key.meta &&
11231125
!key.shift
11241126
) {
1125-
if (this[kSubstringSearch] === null) {
1127+
const isLineMultiline = StringPrototypeIndexOf(this.line, '\n') !== -1;
1128+
if (this[kSubstringSearch] === null && !isLineMultiline) {
11261129
this[kSubstringSearch] = StringPrototypeSlice(
11271130
this.line,
11281131
0,

lib/repl.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -966,7 +966,10 @@ function REPLServer(prompt,
966966
ArrayPrototypePop(lines);
967967
// And replace them with the single command split by '\r'
968968
ArrayPrototypePush(lines, cmd);
969-
ArrayPrototypeUnshift(self.history, ArrayPrototypeJoin(lines, '\r'));
969+
const newHistoryLine = ArrayPrototypeJoin(lines, '\r');
970+
if (self.history[0] !== newHistoryLine) {
971+
ArrayPrototypeUnshift(self.history, newHistoryLine);
972+
}
970973
}
971974

972975
if (e) {

test/parallel/test-repl-history-navigation.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,6 @@ function runTest() {
814814
try {
815815
assert.strictEqual(output, expected[i]);
816816
} catch (e) {
817-
console.log({ output, expected: expected[i] });
818817
console.error(`Failed test # ${numtests - tests.length}`);
819818
console.error('Last outputs: ' + inspect(lastChunks, {
820819
breakLength: 5, colors: true

0 commit comments

Comments
 (0)