Skip to content

Commit 8be0031

Browse files
BridgeARtargos
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 6eda28c commit 8be0031

File tree

2 files changed

+179
-181
lines changed

2 files changed

+179
-181
lines changed

lib/readline.js

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -506,41 +506,43 @@ Interface.prototype._tabComplete = function(lastKeypressWasTab) {
506506
return;
507507
}
508508

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

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

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

0 commit comments

Comments
 (0)