Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

repl: add completion preview #30907

Closed
wants to merge 12 commits into from
Prev Previous commit
Next Next commit
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.
  • Loading branch information
BridgeAR committed Dec 14, 2019
commit 42d3590aedd111ae14bdabcd256241f7709fca42
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