Skip to content

Commit 3906e14

Browse files
BridgeARMylesBorins
authored andcommitted
repl,readline: refactor for simplicity
This just refactors code without changing the behavior. Especially the REPL code is difficult to read and deeply indented. This reduces the indentation to improve that. PR-URL: #30907 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent f6f298e commit 3906e14

File tree

2 files changed

+179
-181
lines changed

2 files changed

+179
-181
lines changed

lib/readline.js

+33-31
Original file line numberDiff line numberDiff line change
@@ -505,41 +505,43 @@ Interface.prototype._tabComplete = function(lastKeypressWasTab) {
505505
return;
506506
}
507507

508-
const completions = rv[0];
509-
const completeOn = rv[1]; // The text that was completed
510-
if (completions && completions.length) {
511-
// Apply/show completions.
512-
if (lastKeypressWasTab) {
513-
self._writeToOutput('\r\n');
514-
const width = completions.reduce(function completionReducer(a, b) {
515-
return a.length > b.length ? a : b;
516-
}).length + 2; // 2 space padding
517-
let maxColumns = MathFloor(self.columns / width);
518-
if (!maxColumns || maxColumns === Infinity) {
519-
maxColumns = 1;
520-
}
521-
let group = [];
522-
for (let i = 0; i < completions.length; i++) {
523-
const c = completions[i];
524-
if (c === '') {
525-
handleGroup(self, group, width, maxColumns);
526-
group = [];
527-
} else {
528-
group.push(c);
529-
}
530-
}
531-
handleGroup(self, group, width, maxColumns);
532-
}
508+
// Result and the text that was completed.
509+
const [completions, completeOn] = rv;
510+
511+
if (!completions || completions.length === 0) {
512+
return;
513+
}
533514

534-
// If there is a common prefix to all matches, then apply that portion.
535-
const f = completions.filter((e) => e);
536-
const prefix = commonPrefix(f);
537-
if (prefix.length > completeOn.length) {
538-
self._insertString(prefix.slice(completeOn.length));
515+
// Apply/show completions.
516+
if (lastKeypressWasTab) {
517+
self._writeToOutput('\r\n');
518+
const width = completions.reduce((a, b) => {
519+
return a.length > b.length ? a : b;
520+
}).length + 2; // 2 space padding
521+
let maxColumns = MathFloor(self.columns / width);
522+
if (!maxColumns || maxColumns === Infinity) {
523+
maxColumns = 1;
539524
}
525+
let group = [];
526+
for (const c of completions) {
527+
if (c === '') {
528+
handleGroup(self, group, width, maxColumns);
529+
group = [];
530+
} else {
531+
group.push(c);
532+
}
533+
}
534+
handleGroup(self, group, width, maxColumns);
535+
}
540536

541-
self._refreshLine();
537+
// If there is a common prefix to all matches, then apply that portion.
538+
const f = completions.filter((e) => e);
539+
const prefix = commonPrefix(f);
540+
if (prefix.length > completeOn.length) {
541+
self._insertString(prefix.slice(completeOn.length));
542542
}
543+
544+
self._refreshLine();
543545
});
544546
};
545547

0 commit comments

Comments
 (0)