Skip to content

Commit

Permalink
repl,readline: clean up code
Browse files Browse the repository at this point in the history
This simplifies code that was more complicated than it had to be
and removes code that should never be reached.

PR-URL: nodejs#31288
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
BridgeAR authored and targos committed Apr 25, 2020
1 parent d1ea9e1 commit 3eb21f9
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 43 deletions.
6 changes: 1 addition & 5 deletions lib/internal/readline/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@ if (internalBinding('config').hasIntl) {
(code >= 0x1b000 && code <= 0x1b001) ||
// Enclosed Ideographic Supplement
(code >= 0x1f200 && code <= 0x1f251) ||
// Miscellaneous Symbols and Pictographs 0x1f300 - 0x1f5ff
// Emoticons 0x1f600 - 0x1f64f
// Miscellaneous Symbols and Pictographs .. Emoticons
(code >= 0x1f300 && code <= 0x1f64f) ||
// CJK Unified Ideographs Extension B .. Tertiary Ideographic Plane
(code >= 0x20000 && code <= 0x3fffd)
Expand Down Expand Up @@ -459,9 +458,6 @@ function* emitKeys(stream) {

// This runs in O(n log n).
function commonPrefix(strings) {
if (!strings || strings.length === 0) {
return '';
}
if (strings.length === 1) {
return strings[0];
}
Expand Down
6 changes: 2 additions & 4 deletions lib/internal/repl/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,10 +320,8 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) {
// Enter should automatically add the suffix to the current line as long as
// escape was not pressed. We might even remove the preview in case any
// cursor movement is triggered.
if (typeof repl.completer === 'function') {
const insertPreview = false;
showCompletionPreview(repl.line, insertPreview);
}
const insertPreview = false;
showCompletionPreview(repl.line, insertPreview);

// Do not preview if the command is buffered.
if (repl[bufferSymbol]) {
Expand Down
37 changes: 10 additions & 27 deletions lib/readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -735,27 +735,13 @@ Interface.prototype._getDisplayPos = function(str) {
return { cols, rows };
};


// Returns current cursor's position and line
Interface.prototype.getCursorPos = function() {
const columns = this.columns;
const strBeforeCursor = this._prompt + this.line.substring(0, this.cursor);
const dispPos = this._getDisplayPos(strBeforeCursor);
let cols = dispPos.cols;
let rows = dispPos.rows;
// If the cursor is on a full-width character which steps over the line,
// move the cursor to the beginning of the next line.
if (cols + 1 === columns &&
this.cursor < this.line.length &&
getStringWidth(this.line[this.cursor]) > 1) {
rows++;
cols = 0;
}
return { cols, rows };
return this._getDisplayPos(strBeforeCursor);
};
Interface.prototype._getCursorPos = Interface.prototype.getCursorPos;


// This function moves cursor dx places to the right
// (-dx for left) and refreshes the line if it is needed.
Interface.prototype._moveCursor = function(dx) {
Expand Down Expand Up @@ -1119,42 +1105,39 @@ function emitKeypressEvents(stream, iface = {}) {
stream[ESCAPE_DECODER] = emitKeys(stream);
stream[ESCAPE_DECODER].next();

const escapeCodeTimeout = () => stream[ESCAPE_DECODER].next('');
const triggerEscape = () => stream[ESCAPE_DECODER].next('');
const { escapeCodeTimeout = ESCAPE_CODE_TIMEOUT } = iface;
let timeoutId;

function onData(b) {
function onData(input) {
if (stream.listenerCount('keypress') > 0) {
const string = stream[KEYPRESS_DECODER].write(b);
const string = stream[KEYPRESS_DECODER].write(input);
if (string) {
clearTimeout(timeoutId);

// This supports characters of length 2.
iface._sawKeyPress = charLengthAt(string, 0) === string.length;
const escapeTimeout = iface.escapeCodeTimeout || ESCAPE_CODE_TIMEOUT;
iface.isCompletionEnabled = false;

let length = 0;
for (const character of string) {
length += character.length;
if (character === '\t' && length !== string.length) {
iface.isCompletionEnabled = false;
if (length === string.length) {
iface.isCompletionEnabled = true;
}

try {
stream[ESCAPE_DECODER].next(character);
// Escape letter at the tail position
if (character === kEscape && length === string.length) {
timeoutId = setTimeout(escapeCodeTimeout, escapeTimeout);
if (length === string.length && character === kEscape) {
timeoutId = setTimeout(triggerEscape, escapeCodeTimeout);
}
} catch (err) {
// If the generator throws (it could happen in the `keypress`
// event), we need to restart it.
stream[ESCAPE_DECODER] = emitKeys(stream);
stream[ESCAPE_DECODER].next();
throw err;
} finally {
if (iface) {
iface.isCompletionEnabled = true;
}
}
}
}
Expand Down
12 changes: 5 additions & 7 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -1365,15 +1365,13 @@ REPLServer.prototype.completeOnEditorMode = (callback) => (err, results) => {
if (err) return callback(err);

const [completions, completeOn = ''] = results;
const prefixLength = completeOn.length;
let result = completions.filter((v) => v);

if (prefixLength === 0) return callback(null, [[], completeOn]);

const isNotEmpty = (v) => v.length > 0;
const trimCompleteOnPrefix = (v) => v.substring(prefixLength);
const data = completions.filter(isNotEmpty).map(trimCompleteOnPrefix);
if (completeOn && result.length !== 0) {
result = [commonPrefix(result)];
}

callback(null, [[`${completeOn}${commonPrefix(data)}`], completeOn]);
callback(null, [result, completeOn]);
};

REPLServer.prototype.defineCommand = function(keyword, cmd) {
Expand Down

0 comments on commit 3eb21f9

Please sign in to comment.