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 <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
  • Loading branch information
BridgeAR authored and MylesBorins committed Dec 17, 2019
1 parent f6f298e commit 3906e14
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
Original file line number Diff line number Diff line change
Expand Up @@ -505,41 +505,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
Loading

0 comments on commit 3906e14

Please sign in to comment.