Skip to content

Commit

Permalink
repl,readline: refactor for simplicity
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
BridgeAR authored and targos committed Apr 28, 2020
1 parent 6eda28c commit 8be0031
Show file tree
Hide file tree
Showing 2 changed files with 179 additions and 181 deletions.
64 changes: 33 additions & 31 deletions lib/readline.js
Expand Up @@ -506,41 +506,43 @@ Interface.prototype._tabComplete = function(lastKeypressWasTab) {
return;
}

const completions = rv[0];
const completeOn = rv[1]; // The text that was completed
if (completions && completions.length) {
// Apply/show completions.
if (lastKeypressWasTab) {
self._writeToOutput('\r\n');
const width = completions.reduce(function completionReducer(a, b) {
return a.length > b.length ? a : b;
}).length + 2; // 2 space padding
let maxColumns = MathFloor(self.columns / width);
if (!maxColumns || maxColumns === Infinity) {
maxColumns = 1;
}
let group = [];
for (let i = 0; i < completions.length; i++) {
const c = completions[i];
if (c === '') {
handleGroup(self, group, width, maxColumns);
group = [];
} else {
group.push(c);
}
}
handleGroup(self, group, width, maxColumns);
}
// Result and the text that was completed.
const [completions, completeOn] = rv;

if (!completions || completions.length === 0) {
return;
}

// If there is a common prefix to all matches, then apply that portion.
const f = completions.filter((e) => e);
const prefix = commonPrefix(f);
if (prefix.length > completeOn.length) {
self._insertString(prefix.slice(completeOn.length));
// Apply/show completions.
if (lastKeypressWasTab) {
self._writeToOutput('\r\n');
const width = completions.reduce((a, b) => {
return a.length > b.length ? a : b;
}).length + 2; // 2 space padding
let maxColumns = MathFloor(self.columns / width);
if (!maxColumns || maxColumns === Infinity) {
maxColumns = 1;
}
let group = [];
for (const c of completions) {
if (c === '') {
handleGroup(self, group, width, maxColumns);
group = [];
} else {
group.push(c);
}
}
handleGroup(self, group, width, maxColumns);
}

self._refreshLine();
// If there is a common prefix to all matches, then apply that portion.
const f = completions.filter((e) => e);
const prefix = commonPrefix(f);
if (prefix.length > completeOn.length) {
self._insertString(prefix.slice(completeOn.length));
}

self._refreshLine();
});
};

Expand Down

0 comments on commit 8be0031

Please sign in to comment.