mirror of
https://github.com/nodejs/node.git
synced 2025-12-28 16:07:39 +00:00
repl,readline: clean up code
This simplifies code that was more complicated than it had to be and removes code that should never be reached. PR-URL: https://github.com/nodejs/node/pull/31288 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
This commit is contained in:
parent
5baae143c7
commit
fe058188a1
@ -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)
|
||||
@ -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];
|
||||
}
|
||||
|
||||
@ -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]) {
|
||||
|
||||
@ -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) {
|
||||
@ -1119,31 +1105,32 @@ 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`
|
||||
@ -1151,10 +1138,6 @@ function emitKeypressEvents(stream, iface = {}) {
|
||||
stream[ESCAPE_DECODER] = emitKeys(stream);
|
||||
stream[ESCAPE_DECODER].next();
|
||||
throw err;
|
||||
} finally {
|
||||
if (iface) {
|
||||
iface.isCompletionEnabled = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
12
lib/repl.js
12
lib/repl.js
@ -1362,15 +1362,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]);
|
||||
if (completeOn && result.length !== 0) {
|
||||
result = [commonPrefix(result)];
|
||||
}
|
||||
|
||||
const isNotEmpty = (v) => v.length > 0;
|
||||
const trimCompleteOnPrefix = (v) => v.substring(prefixLength);
|
||||
const data = completions.filter(isNotEmpty).map(trimCompleteOnPrefix);
|
||||
|
||||
callback(null, [[`${completeOn}${commonPrefix(data)}`], completeOn]);
|
||||
callback(null, [result, completeOn]);
|
||||
};
|
||||
|
||||
REPLServer.prototype.defineCommand = function(keyword, cmd) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user