All files / lib/internal/repl utils.js

97.47% Statements 734/753
93.71% Branches 194/207
100% Functions 25/25
97.47% Lines 734/753

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 7542x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x   2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 168x 168x 168x 168x 168x 168x 168x 168x 163x 163x 163x 163x 163x 163x 163x 163x 163x 163x 163x 163x 163x 163x 163x 163x 163x 163x 163x 163x 163x 163x 163x 163x 163x 163x 163x 163x 1827x 1827x 1827x 1827x 163x 159x 159x 159x 5x 5x 159x 159x 11x 11x 11x 11x 11x 11x 11x 11x 159x 159x 159x 163x 163x 163x 163x 163x 163x 163x 163x 163x 163x 163x 163x 163x 163x 163x 163x 168x 2x 202x 202x 202x 140x 140x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 321x 321x 321x 321x 321x 321x 62x 62x 71x 71x 71x 71x 62x 62x 7195x 84x 84x 84x 84x 84x 84x 84x 84x 7195x 54x 54x 54x 54x 13x 13x 13x 13x 13x 54x 54x 54x 54x 54x 54x 54x     54x 13x 13x 13x 54x 44x 5x 5x 5x 44x 39x 39x 39x 3x 3x 44x 54x 54x 7195x 7185x 7185x 62x 62x 62x 414x 414x 414x 414x 414x 413x 413x     413x 413x       413x 413x 413x 413x 413x 247x 247x 166x 166x 166x 166x 166x 166x 166x 166x 384x 89x 89x 77x 77x 77x 384x 10x 10x 10x 67x 67x 67x 206x 413x 413x 413x 413x 413x 16x 16x 16x 67x 67x 67x 67x 413x 413x 414x 414x 62x 62x 9x 8x 8x 8x 8x 8x 8x 9x 9x 62x 62x 62x 526x 526x 526x 526x 150x 150x 150x 526x 526x 526x 526x 526x 526x 526x 526x       526x 526x 72x 72x 72x 72x 526x 396x 396x 41x 41x 41x 454x 391x 454x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x   52x 52x 52x 52x 63x 11x 11x 11x 526x 526x 526x 62x 62x 7508x 7508x 6537x 6537x 971x 971x 971x 971x 6164x 567x 567x 404x 404x 404x 404x 404x 404x 404x 404x 5965x     404x 404x 526x 393x 393x 133x 133x 133x 133x 133x 526x 43x 43x 90x 500x       90x 90x 500x 526x 4x 4x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 500x 526x 19x 19x 19x 4439x 4439x 4439x 4420x 4420x 19x 19x 86x 86x 86x 86x 500x 7x 7x 86x 224x 526x 526x 526x 526x 526x 526x 526x 526x 526x 404x 404x 404x 404x 5965x 7508x 7508x 51x 51x 404x 404x 5965x 122x 122x 404x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 345x 345x 345x 62x 62x 62x 62x 62x 62x 62x 291x 291x 291x 291x 291x 10x 10x 10x 62x 62x 62x 62x 62x 62x 263x 263x 263x 62x 62x 62x 202x 2x 202x 202x 202x 7x 7x 195x 195x 195x 195x 195x 195x 195x 195x 195x 195x 195x 195x 195x 195x 195x 195x 78x 63x 63x 21x 6x 6x 6x 6x 6x 15x 78x 195x 195x 24x 24x 24x 24x 24x 24x 195x 195x 20x 20x 2x 2x 2x 18x 20x 15x 10x 10x 20x 1x 1x 18x 20x 40x 40x 40x 2x 2x 2x 38x 38x 40x 35x 35x 31x 31x 35x 40x 3x 3x 38x 40x 24x 24x 40x 14x 9x 9x 9x 9x 14x 14x 14x 14x 14x 14x 14x     14x 14x 40x 4x 20x 195x 195x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 10x 10x 10x 25x 9x 9x 9x 25x 25x 25x 25x 20x 20x 25x 5x 5x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 195x 195x 15x 15x 15x 15x 15x 5x 2x 2x 2x 2x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 2x 2x 2x 2x 5x 5x 15x 15x 15x 15x 15x 15x 195x 195x 7205x 7180x 5x 5x 5x 5x 5x 7205x 10x 25x 15x 2x 2x 2x 2x 15x 2x 2x 2x 2x 2x 13x 11x 11x 11x 11x 11x 3x 3x 11x 8x 8x 8x 7203x 7205x 195x 195x 202x 2x 2x 2x 2x 2x 2x 2x 2x 2x  
'use strict';
 
const {
  ArrayPrototypeFilter,
  ArrayPrototypeIncludes,
  ArrayPrototypeMap,
  Boolean,
  FunctionPrototypeBind,
  MathMin,
  RegExpPrototypeTest,
  SafeSet,
  SafeStringIterator,
  StringPrototypeEndsWith,
  StringPrototypeIndexOf,
  StringPrototypeLastIndexOf,
  StringPrototypeReplace,
  StringPrototypeSlice,
  StringPrototypeStartsWith,
  StringPrototypeToLowerCase,
  StringPrototypeTrim,
  Symbol,
} = primordials;
 
const { tokTypes: tt, Parser: AcornParser } =
  require('internal/deps/acorn/acorn/dist/acorn');
 
const { sendInspectorCommand } = require('internal/util/inspector');
 
const {
  ERR_INSPECTOR_NOT_AVAILABLE
} = require('internal/errors').codes;
 
const {
  clearLine,
  clearScreenDown,
  cursorTo,
  moveCursor,
} = require('internal/readline/callbacks');
 
const {
  commonPrefix,
  kSubstringSearch,
} = require('internal/readline/utils');
 
const {
  getStringWidth,
  inspect,
} = require('internal/util/inspect');
 
let debug = require('internal/util/debuglog').debuglog('repl', (fn) => {
  debug = fn;
});
 
const previewOptions = {
  colors: false,
  depth: 1,
  showHidden: false
};
 
const REPL_MODE_STRICT = Symbol('repl-strict');
 
// If the error is that we've unexpectedly ended the input,
// then let the user try to recover by adding more input.
// Note: `e` (the original exception) is not used by the current implementation,
// but may be needed in the future.
function isRecoverableError(e, code) {
  // For similar reasons as `defaultEval`, wrap expressions starting with a
  // curly brace with parenthesis.  Note: only the open parenthesis is added
  // here as the point is to test for potentially valid but incomplete
  // expressions.
  if (RegExpPrototypeTest(/^\s*\{/, code) &&
      isRecoverableError(e, `(${code}`))
    return true;
 
  let recoverable = false;
 
  // Determine if the point of any error raised is at the end of the input.
  // There are two cases to consider:
  //
  //   1.  Any error raised after we have encountered the 'eof' token.
  //       This prevents us from declaring partial tokens (like '2e') as
  //       recoverable.
  //
  //   2.  Three cases where tokens can legally span lines.  This is
  //       template, comment, and strings with a backslash at the end of
  //       the line, indicating a continuation.  Note that we need to look
  //       for the specific errors of 'unterminated' kind (not, for example,
  //       a syntax error in a ${} expression in a template), and the only
  //       way to do that currently is to look at the message.  Should Acorn
  //       change these messages in the future, this will lead to a test
  //       failure, indicating that this code needs to be updated.
  //
  const RecoverableParser = AcornParser
    .extend(
      (Parser) => {
        return class extends Parser {
          // eslint-disable-next-line no-useless-constructor
          constructor(options, input, startPos) {
            super(options, input, startPos);
          }
          nextToken() {
            super.nextToken();
            if (this.type === tt.eof)
              recoverable = true;
          }
          raise(pos, message) {
            switch (message) {
              case 'Unterminated template':
              case 'Unterminated comment':
                recoverable = true;
                break;
 
              case 'Unterminated string constant': {
                const token = StringPrototypeSlice(this.input,
                                                   this.lastTokStart, this.pos);
                // See https://www.ecma-international.org/ecma-262/#sec-line-terminators
                if (RegExpPrototypeTest(/\\(?:\r\n?|\n|\u2028|\u2029)$/,
                                        token)) {
                  recoverable = true;
                }
              }
            }
            super.raise(pos, message);
          }
        };
      }
    );
 
  // Try to parse the code with acorn.  If the parse fails, ignore the acorn
  // error and return the recoverable status.
  try {
    RecoverableParser.parse(code, { ecmaVersion: 'latest' });
 
    // Odd case: the underlying JS engine (V8, Chakra) rejected this input
    // but Acorn detected no issue.  Presume that additional text won't
    // address this issue.
    return false;
  } catch {
    return recoverable;
  }
}
 
function setupPreview(repl, contextSymbol, bufferSymbol, active) {
  // Simple terminals can't handle previews.
  if (process.env.TERM === 'dumb' || !active) {
    return { showPreview() {}, clearPreview() {} };
  }
 
  let inputPreview = null;
  let lastInputPreview = '';
 
  let previewCompletionCounter = 0;
  let completionPreview = null;
 
  let hasCompletions = false;
 
  let wrapped = false;
 
  let escaped = null;
 
  function getPreviewPos() {
    const displayPos = repl._getDisplayPos(`${repl.getPrompt()}${repl.line}`);
    const cursorPos = repl.line.length !== repl.cursor ?
      repl.getCursorPos() :
      displayPos;
    return { displayPos, cursorPos };
  }
 
  function isCursorAtInputEnd() {
    const { cursorPos, displayPos } = getPreviewPos();
    return cursorPos.rows === displayPos.rows &&
           cursorPos.cols === displayPos.cols;
  }
 
  const clearPreview = (key) => {
    if (inputPreview !== null) {
      const { displayPos, cursorPos } = getPreviewPos();
      const rows = displayPos.rows - cursorPos.rows + 1;
      moveCursor(repl.output, 0, rows);
      clearLine(repl.output);
      moveCursor(repl.output, 0, -rows);
      lastInputPreview = inputPreview;
      inputPreview = null;
    }
    if (completionPreview !== null) {
      // Prevent cursor moves if not necessary!
      const move = repl.line.length !== repl.cursor;
      let pos, rows;
      if (move) {
        pos = getPreviewPos();
        cursorTo(repl.output, pos.displayPos.cols);
        rows = pos.displayPos.rows - pos.cursorPos.rows;
        moveCursor(repl.output, 0, rows);
      }
      const totalLine = `${repl.getPrompt()}${repl.line}${completionPreview}`;
      const newPos = repl._getDisplayPos(totalLine);
      // Minimize work for the terminal. It is enough to clear the right part of
      // the current line in case the preview is visible on a single line.
      if (newPos.rows === 0 || (pos && pos.displayPos.rows === newPos.rows)) {
        clearLine(repl.output, 1);
      } else {
        clearScreenDown(repl.output);
      }
      if (move) {
        cursorTo(repl.output, pos.cursorPos.cols);
        moveCursor(repl.output, 0, -rows);
      }
      if (!key.ctrl && !key.shift) {
        if (key.name === 'escape') {
          if (escaped === null && key.meta) {
            escaped = repl.line;
          }
        } else if ((key.name === 'return' || key.name === 'enter') &&
                   !key.meta &&
                   escaped !== repl.line &&
                   isCursorAtInputEnd()) {
          repl._insertString(completionPreview);
        }
      }
      completionPreview = null;
    }
    if (escaped !== repl.line) {
      escaped = null;
    }
  };
 
  function showCompletionPreview(line, insertPreview) {
    previewCompletionCounter++;
 
    const count = previewCompletionCounter;
 
    repl.completer(line, (error, data) => {
      // Tab completion might be async and the result might already be outdated.
      if (count !== previewCompletionCounter) {
        return;
      }
 
      if (error) {
        debug('Error while generating completion preview', error);
        return;
      }
 
      // Result and the text that was completed.
      const { 0: rawCompletions, 1: completeOn } = data;
 
      if (!rawCompletions || rawCompletions.length === 0) {
        return;
      }
 
      hasCompletions = true;
 
      // If there is a common prefix to all matches, then apply that portion.
      const completions = ArrayPrototypeFilter(rawCompletions, Boolean);
      const prefix = commonPrefix(completions);
 
      // No common prefix found.
      if (prefix.length <= completeOn.length) {
        return;
      }
 
      const suffix = StringPrototypeSlice(prefix, completeOn.length);
 
      if (insertPreview) {
        repl._insertString(suffix);
        return;
      }
 
      completionPreview = suffix;
 
      const result = repl.useColors ?
        `\u001b[90m${suffix}\u001b[39m` :
        ` // ${suffix}`;
 
      const { cursorPos, displayPos } = getPreviewPos();
      if (repl.line.length !== repl.cursor) {
        cursorTo(repl.output, displayPos.cols);
        moveCursor(repl.output, 0, displayPos.rows - cursorPos.rows);
      }
      repl.output.write(result);
      cursorTo(repl.output, cursorPos.cols);
      const totalLine = `${repl.getPrompt()}${repl.line}${suffix}`;
      const newPos = repl._getDisplayPos(totalLine);
      const rows = newPos.rows - cursorPos.rows - (newPos.cols === 0 ? 1 : 0);
      moveCursor(repl.output, 0, -rows);
    });
  }
 
  function isInStrictMode(repl) {
    return repl.replMode === REPL_MODE_STRICT || ArrayPrototypeIncludes(
      ArrayPrototypeMap(process.execArgv,
                        (e) => StringPrototypeReplace(
                          StringPrototypeToLowerCase(e),
                          /_/g,
                          '-'
                        )),
      '--use-strict');
  }
 
  // This returns a code preview for arbitrary input code.
  function getInputPreview(input, callback) {
    // For similar reasons as `defaultEval`, wrap expressions starting with a
    // curly brace with parenthesis.
    if (StringPrototypeStartsWith(input, '{') &&
        !StringPrototypeEndsWith(input, ';') && !wrapped) {
      input = `(${input})`;
      wrapped = true;
    }
    sendInspectorCommand((session) => {
      session.post('Runtime.evaluate', {
        expression: input,
        throwOnSideEffect: true,
        timeout: 333,
        contextId: repl[contextSymbol],
      }, (error, preview) => {
        if (error) {
          callback(error);
          return;
        }
        const { result } = preview;
        if (result.value !== undefined) {
          callback(null, inspect(result.value, previewOptions));
        // Ignore EvalErrors, SyntaxErrors and ReferenceErrors. It is not clear
        // where they came from and if they are recoverable or not. Other errors
        // may be inspected.
        } else if (preview.exceptionDetails &&
                   (result.className === 'EvalError' ||
                    result.className === 'SyntaxError' ||
                    // Report ReferenceError in case the strict mode is active
                    // for input that has no completions.
                    (result.className === 'ReferenceError' &&
                     (hasCompletions || !isInStrictMode(repl))))) {
          callback(null, null);
        } else if (result.objectId) {
          // The writer options might change and have influence on the inspect
          // output. The user might change e.g., `showProxy`, `getters` or
          // `showHidden`. Use `inspect` instead of `JSON.stringify` to keep
          // `Infinity` and similar intact.
          const inspectOptions = inspect({
            ...repl.writer.options,
            colors: false,
            depth: 1,
            compact: true,
            breakLength: Infinity
          }, previewOptions);
          session.post('Runtime.callFunctionOn', {
            functionDeclaration:
              `(v) =>
                    Reflect
                    .getOwnPropertyDescriptor(globalThis, 'util')
                    .get().inspect(v, ${inspectOptions})`,
            objectId: result.objectId,
            arguments: [result]
          }, (error, preview) => {
            if (error) {
              callback(error);
            } else {
              callback(null, preview.result.value);
            }
          });
        } else {
          // Either not serializable or undefined.
          callback(null, result.unserializableValue || result.type);
        }
      });
    }, () => callback(new ERR_INSPECTOR_NOT_AVAILABLE()));
  }
 
  const showPreview = () => {
    // Prevent duplicated previews after a refresh.
    if (inputPreview !== null || !repl.isCompletionEnabled) {
      return;
    }
 
    const line = StringPrototypeTrim(repl.line);
 
    // Do not preview in case the line only contains whitespace.
    if (line === '') {
      return;
    }
 
    hasCompletions = false;
 
    // Add the autocompletion preview.
    const insertPreview = false;
    showCompletionPreview(repl.line, insertPreview);
 
    // Do not preview if the command is buffered.
    if (repl[bufferSymbol]) {
      return;
    }
 
    const inputPreviewCallback = (error, inspected) => {
      if (inspected == null) {
        return;
      }
 
      wrapped = false;
 
      // Ignore the output if the value is identical to the current line and the
      // former preview is not identical to this preview.
      if (line === inspected && lastInputPreview !== inspected) {
        return;
      }
 
      if (error) {
        debug('Error while generating preview', error);
        return;
      }
      // Do not preview `undefined` if colors are deactivated or explicitly
      // requested.
      if (inspected === 'undefined' &&
          (!repl.useColors || repl.ignoreUndefined)) {
        return;
      }
 
      inputPreview = inspected;
 
      // Limit the output to maximum 250 characters. Otherwise it becomes a)
      // difficult to read and b) non terminal REPLs would visualize the whole
      // output.
      let maxColumns = MathMin(repl.columns, 250);
 
      // Support unicode characters of width other than one by checking the
      // actual width.
      if (inspected.length * 2 >= maxColumns &&
          getStringWidth(inspected) > maxColumns) {
        maxColumns -= 4 + (repl.useColors ? 0 : 3);
        let res = '';
        for (const char of new SafeStringIterator(inspected)) {
          maxColumns -= getStringWidth(char);
          if (maxColumns < 0)
            break;
          res += char;
        }
        inspected = `${res}...`;
      }
 
      // Line breaks are very rare and probably only occur in case of error
      // messages with line breaks.
      const lineBreakPos = StringPrototypeIndexOf(inspected, '\n');
      if (lineBreakPos !== -1) {
        inspected = `${StringPrototypeSlice(inspected, 0, lineBreakPos)}`;
      }
 
      const result = repl.useColors ?
        `\u001b[90m${inspected}\u001b[39m` :
        `// ${inspected}`;
 
      const { cursorPos, displayPos } = getPreviewPos();
      const rows = displayPos.rows - cursorPos.rows;
      moveCursor(repl.output, 0, rows);
      repl.output.write(`\n${result}`);
      cursorTo(repl.output, cursorPos.cols);
      moveCursor(repl.output, 0, -rows - 1);
    };
 
    let previewLine = line;
 
    if (completionPreview !== null &&
        isCursorAtInputEnd() &&
        escaped !== repl.line) {
      previewLine += completionPreview;
    }
 
    getInputPreview(previewLine, inputPreviewCallback);
    if (wrapped) {
      getInputPreview(previewLine, inputPreviewCallback);
    }
    wrapped = false;
  };
 
  // -------------------------------------------------------------------------//
  // Replace multiple interface functions. This is required to fully support  //
  // previews without changing readlines behavior.                            //
  // -------------------------------------------------------------------------//
 
  // Refresh prints the whole screen again and the preview will be removed
  // during that procedure. Print the preview again. This also makes sure
  // the preview is always correct after resizing the terminal window.
  const originalRefresh = FunctionPrototypeBind(repl._refreshLine, repl);
  repl._refreshLine = () => {
    inputPreview = null;
    originalRefresh();
    showPreview();
  };
 
  let insertCompletionPreview = true;
  // Insert the longest common suffix of the current input in case the user
  // moves to the right while already being at the current input end.
  const originalMoveCursor = FunctionPrototypeBind(repl._moveCursor, repl);
  repl._moveCursor = (dx) => {
    const currentCursor = repl.cursor;
    originalMoveCursor(dx);
    if (currentCursor + dx > repl.line.length &&
        typeof repl.completer === 'function' &&
        insertCompletionPreview) {
      const insertPreview = true;
      showCompletionPreview(repl.line, insertPreview);
    }
  };
 
  // This is the only function that interferes with the completion insertion.
  // Monkey patch it to prevent inserting the completion when it shouldn't be.
  const originalClearLine = FunctionPrototypeBind(repl.clearLine, repl);
  repl.clearLine = () => {
    insertCompletionPreview = false;
    originalClearLine();
    insertCompletionPreview = true;
  };
 
  return { showPreview, clearPreview };
}
 
function setupReverseSearch(repl) {
  // Simple terminals can't use reverse search.
  if (process.env.TERM === 'dumb') {
    return { reverseSearch() { return false; } };
  }
 
  const alreadyMatched = new SafeSet();
  const labels = {
    r: 'bck-i-search: ',
    s: 'fwd-i-search: '
  };
  let isInReverseSearch = false;
  let historyIndex = -1;
  let input = '';
  let cursor = -1;
  let dir = 'r';
  let lastMatch = -1;
  let lastCursor = -1;
  let promptPos;
 
  function checkAndSetDirectionKey(keyName) {
    if (!labels[keyName]) {
      return false;
    }
    if (dir !== keyName) {
      // Reset the already matched set in case the direction is changed. That
      // way it's possible to find those entries again.
      alreadyMatched.clear();
      dir = keyName;
    }
    return true;
  }
 
  function goToNextHistoryIndex() {
    // Ignore this entry for further searches and continue to the next
    // history entry.
    alreadyMatched.add(repl.history[historyIndex]);
    historyIndex += dir === 'r' ? 1 : -1;
    cursor = -1;
  }
 
  function search() {
    // Just print an empty line in case the user removed the search parameter.
    if (input === '') {
      print(repl.line, `${labels[dir]}_`);
      return;
    }
    // Fix the bounds in case the direction has changed in the meanwhile.
    if (dir === 'r') {
      if (historyIndex < 0) {
        historyIndex = 0;
      }
    } else if (historyIndex >= repl.history.length) {
      historyIndex = repl.history.length - 1;
    }
    // Check the history entries until a match is found.
    while (historyIndex >= 0 && historyIndex < repl.history.length) {
      let entry = repl.history[historyIndex];
      // Visualize all potential matches only once.
      if (alreadyMatched.has(entry)) {
        historyIndex += dir === 'r' ? 1 : -1;
        continue;
      }
      // Match the next entry either from the start or from the end, depending
      // on the current direction.
      if (dir === 'r') {
        // Update the cursor in case it's necessary.
        if (cursor === -1) {
          cursor = entry.length;
        }
        cursor = StringPrototypeLastIndexOf(entry, input, cursor - 1);
      } else {
        cursor = StringPrototypeIndexOf(entry, input, cursor + 1);
      }
      // Match not found.
      if (cursor === -1) {
        goToNextHistoryIndex();
      // Match found.
      } else {
        if (repl.useColors) {
          const start = StringPrototypeSlice(entry, 0, cursor);
          const end = StringPrototypeSlice(entry, cursor + input.length);
          entry = `${start}\x1B[4m${input}\x1B[24m${end}`;
        }
        print(entry, `${labels[dir]}${input}_`, cursor);
        lastMatch = historyIndex;
        lastCursor = cursor;
        // Explicitly go to the next history item in case no further matches are
        // possible with the current entry.
        if ((dir === 'r' && cursor === 0) ||
            (dir === 's' && entry.length === cursor + input.length)) {
          goToNextHistoryIndex();
        }
        return;
      }
    }
    print(repl.line, `failed-${labels[dir]}${input}_`);
  }
 
  function print(outputLine, inputLine, cursor = repl.cursor) {
    // TODO(BridgeAR): Resizing the terminal window hides the overlay. To fix
    // that, readline must be aware of this information. It's probably best to
    // add a couple of properties to readline that allow to do the following:
    // 1. Add arbitrary data to the end of the current line while not counting
    //    towards the line. This would be useful for the completion previews.
    // 2. Add arbitrary extra lines that do not count towards the regular line.
    //    This would be useful for both, the input preview and the reverse
    //    search. It might be combined with the first part?
    // 3. Add arbitrary input that is "on top" of the current line. That is
    //    useful for the reverse search.
    // 4. To trigger the line refresh, functions should be used to pass through
    //    the information. Alternatively, getters and setters could be used.
    //    That might even be more elegant.
    // The data would then be accounted for when calling `_refreshLine()`.
    // This function would then look similar to:
    //   repl.overlay(outputLine);
    //   repl.addTrailingLine(inputLine);
    //   repl.setCursor(cursor);
    // More potential improvements: use something similar to stream.cork().
    // Multiple cursor moves on the same tick could be prevented in case all
    // writes from the same tick are combined and the cursor is moved at the
    // tick end instead of after each operation.
    let rows = 0;
    if (lastMatch !== -1) {
      const line = StringPrototypeSlice(repl.history[lastMatch], 0, lastCursor);
      rows = repl._getDisplayPos(`${repl.getPrompt()}${line}`).rows;
      cursorTo(repl.output, promptPos.cols);
    } else if (isInReverseSearch && repl.line !== '') {
      rows = repl.getCursorPos().rows;
      cursorTo(repl.output, promptPos.cols);
    }
    if (rows !== 0)
      moveCursor(repl.output, 0, -rows);
 
    if (isInReverseSearch) {
      clearScreenDown(repl.output);
      repl.output.write(`${outputLine}\n${inputLine}`);
    } else {
      repl.output.write(`\n${inputLine}`);
    }
 
    lastMatch = -1;
 
    // To know exactly how many rows we have to move the cursor back we need the
    // cursor rows, the output rows and the input rows.
    const prompt = repl.getPrompt();
    const cursorLine = prompt + StringPrototypeSlice(outputLine, 0, cursor);
    const cursorPos = repl._getDisplayPos(cursorLine);
    const outputPos = repl._getDisplayPos(`${prompt}${outputLine}`);
    const inputPos = repl._getDisplayPos(inputLine);
    const inputRows = inputPos.rows - (inputPos.cols === 0 ? 1 : 0);
 
    rows = -1 - inputRows - (outputPos.rows - cursorPos.rows);
 
    moveCursor(repl.output, 0, rows);
    cursorTo(repl.output, cursorPos.cols);
  }
 
  function reset(string) {
    isInReverseSearch = string !== undefined;
 
    // In case the reverse search ends and a history entry is found, reset the
    // line to the found entry.
    if (!isInReverseSearch) {
      if (lastMatch !== -1) {
        repl.line = repl.history[lastMatch];
        repl.cursor = lastCursor;
        repl.historyIndex = lastMatch;
      }
 
      lastMatch = -1;
 
      // Clear screen and write the current repl.line before exiting.
      cursorTo(repl.output, promptPos.cols);
      moveCursor(repl.output, 0, promptPos.rows);
      clearScreenDown(repl.output);
      if (repl.line !== '') {
        repl.output.write(repl.line);
        if (repl.line.length !== repl.cursor) {
          const { cols, rows } = repl.getCursorPos();
          cursorTo(repl.output, cols);
          moveCursor(repl.output, 0, rows);
        }
      }
    }
 
    input = string || '';
    cursor = -1;
    historyIndex = repl.historyIndex;
    alreadyMatched.clear();
  }
 
  function reverseSearch(string, key) {
    if (!isInReverseSearch) {
      if (key.ctrl && checkAndSetDirectionKey(key.name)) {
        historyIndex = repl.historyIndex;
        promptPos = repl._getDisplayPos(`${repl.getPrompt()}`);
        print(repl.line, `${labels[dir]}_`);
        isInReverseSearch = true;
      }
    } else if (key.ctrl && checkAndSetDirectionKey(key.name)) {
      search();
    } else if (key.name === 'backspace' ||
        (key.ctrl && (key.name === 'h' || key.name === 'w'))) {
      reset(StringPrototypeSlice(input, 0, input.length - 1));
      search();
      // Special handle <ctrl> + c and escape. Those should only cancel the
      // reverse search. The original line is visible afterwards again.
    } else if ((key.ctrl && key.name === 'c') || key.name === 'escape') {
      lastMatch = -1;
      reset();
      return true;
      // End search in case either enter is pressed or if any non-reverse-search
      // key (combination) is pressed.
    } else if (key.ctrl ||
               key.meta ||
               key.name === 'return' ||
               key.name === 'enter' ||
               typeof string !== 'string' ||
               string === '') {
      reset();
      repl[kSubstringSearch] = '';
    } else {
      reset(`${input}${string}`);
      search();
    }
    return isInReverseSearch;
  }
 
  return { reverseSearch };
}
 
module.exports = {
  REPL_MODE_SLOPPY: Symbol('repl-sloppy'),
  REPL_MODE_STRICT,
  isRecoverableError,
  kStandaloneREPL: Symbol('kStandaloneREPL'),
  setupPreview,
  setupReverseSearch
};