Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f8e8059

Browse filesBrowse files
BridgeARMylesBorins
authored andcommitted
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: #31288 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 3881189 commit f8e8059
Copy full SHA for f8e8059

File tree

Expand file treeCollapse file tree

4 files changed

+18
-43
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

4 files changed

+18
-43
lines changed
Open diff view settings
Collapse file

‎lib/internal/readline/utils.js‎

Copy file name to clipboardExpand all lines: lib/internal/readline/utils.js
+1-5Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,7 @@ if (internalBinding('config').hasIntl) {
136136
(code >= 0x1b000 && code <= 0x1b001) ||
137137
// Enclosed Ideographic Supplement
138138
(code >= 0x1f200 && code <= 0x1f251) ||
139-
// Miscellaneous Symbols and Pictographs 0x1f300 - 0x1f5ff
140-
// Emoticons 0x1f600 - 0x1f64f
139+
// Miscellaneous Symbols and Pictographs .. Emoticons
141140
(code >= 0x1f300 && code <= 0x1f64f) ||
142141
// CJK Unified Ideographs Extension B .. Tertiary Ideographic Plane
143142
(code >= 0x20000 && code <= 0x3fffd)
@@ -459,9 +458,6 @@ function* emitKeys(stream) {
459458

460459
// This runs in O(n log n).
461460
function commonPrefix(strings) {
462-
if (!strings || strings.length === 0) {
463-
return '';
464-
}
465461
if (strings.length === 1) {
466462
return strings[0];
467463
}
Collapse file

‎lib/internal/repl/utils.js‎

Copy file name to clipboardExpand all lines: lib/internal/repl/utils.js
+2-4Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -320,10 +320,8 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) {
320320
// Enter should automatically add the suffix to the current line as long as
321321
// escape was not pressed. We might even remove the preview in case any
322322
// cursor movement is triggered.
323-
if (typeof repl.completer === 'function') {
324-
const insertPreview = false;
325-
showCompletionPreview(repl.line, insertPreview);
326-
}
323+
const insertPreview = false;
324+
showCompletionPreview(repl.line, insertPreview);
327325

328326
// Do not preview if the command is buffered.
329327
if (repl[bufferSymbol]) {
Collapse file

‎lib/readline.js‎

Copy file name to clipboardExpand all lines: lib/readline.js
+10-27Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -735,27 +735,13 @@ Interface.prototype._getDisplayPos = function(str) {
735735
return { cols, rows };
736736
};
737737

738-
739738
// Returns current cursor's position and line
740739
Interface.prototype.getCursorPos = function() {
741-
const columns = this.columns;
742740
const strBeforeCursor = this._prompt + this.line.substring(0, this.cursor);
743-
const dispPos = this._getDisplayPos(strBeforeCursor);
744-
let cols = dispPos.cols;
745-
let rows = dispPos.rows;
746-
// If the cursor is on a full-width character which steps over the line,
747-
// move the cursor to the beginning of the next line.
748-
if (cols + 1 === columns &&
749-
this.cursor < this.line.length &&
750-
getStringWidth(this.line[this.cursor]) > 1) {
751-
rows++;
752-
cols = 0;
753-
}
754-
return { cols, rows };
741+
return this._getDisplayPos(strBeforeCursor);
755742
};
756743
Interface.prototype._getCursorPos = Interface.prototype.getCursorPos;
757744

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

1122-
const escapeCodeTimeout = () => stream[ESCAPE_DECODER].next('');
1108+
const triggerEscape = () => stream[ESCAPE_DECODER].next('');
1109+
const { escapeCodeTimeout = ESCAPE_CODE_TIMEOUT } = iface;
11231110
let timeoutId;
11241111

1125-
function onData(b) {
1112+
function onData(input) {
11261113
if (stream.listenerCount('keypress') > 0) {
1127-
const string = stream[KEYPRESS_DECODER].write(b);
1114+
const string = stream[KEYPRESS_DECODER].write(input);
11281115
if (string) {
11291116
clearTimeout(timeoutId);
11301117

11311118
// This supports characters of length 2.
11321119
iface._sawKeyPress = charLengthAt(string, 0) === string.length;
1133-
const escapeTimeout = iface.escapeCodeTimeout || ESCAPE_CODE_TIMEOUT;
1120+
iface.isCompletionEnabled = false;
11341121

11351122
let length = 0;
11361123
for (const character of string) {
11371124
length += character.length;
1138-
if (character === '\t' && length !== string.length) {
1139-
iface.isCompletionEnabled = false;
1125+
if (length === string.length) {
1126+
iface.isCompletionEnabled = true;
11401127
}
11411128

11421129
try {
11431130
stream[ESCAPE_DECODER].next(character);
11441131
// Escape letter at the tail position
1145-
if (character === kEscape && length === string.length) {
1146-
timeoutId = setTimeout(escapeCodeTimeout, escapeTimeout);
1132+
if (length === string.length && character === kEscape) {
1133+
timeoutId = setTimeout(triggerEscape, escapeCodeTimeout);
11471134
}
11481135
} catch (err) {
11491136
// If the generator throws (it could happen in the `keypress`
11501137
// event), we need to restart it.
11511138
stream[ESCAPE_DECODER] = emitKeys(stream);
11521139
stream[ESCAPE_DECODER].next();
11531140
throw err;
1154-
} finally {
1155-
if (iface) {
1156-
iface.isCompletionEnabled = true;
1157-
}
11581141
}
11591142
}
11601143
}
Collapse file

‎lib/repl.js‎

Copy file name to clipboardExpand all lines: lib/repl.js
+5-7Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,15 +1364,13 @@ REPLServer.prototype.completeOnEditorMode = (callback) => (err, results) => {
13641364
if (err) return callback(err);
13651365

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

1369-
if (prefixLength === 0) return callback(null, [[], completeOn]);
1370-
1371-
const isNotEmpty = (v) => v.length > 0;
1372-
const trimCompleteOnPrefix = (v) => v.substring(prefixLength);
1373-
const data = completions.filter(isNotEmpty).map(trimCompleteOnPrefix);
1369+
if (completeOn && result.length !== 0) {
1370+
result = [commonPrefix(result)];
1371+
}
13741372

1375-
callback(null, [[`${completeOn}${commonPrefix(data)}`], completeOn]);
1373+
callback(null, [result, completeOn]);
13761374
};
13771375

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

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.