Browse Source

repl tab completion: insert common prefix of multiple completions

v0.7.4-release
Trent Mick 15 years ago
committed by Ryan Dahl
parent
commit
1aeaf8d289
  1. 29
      lib/readline.js

29
lib/readline.js

@ -160,7 +160,6 @@ Interface.prototype._tabComplete = function () {
self._insertString(completions[0].slice(completeOn.length));
self._refreshLine();
} else {
//TODO: If there is a common prefix to all matches (e.g. Python `sys.exi<Tab>`) then apply that portion
self.output.write("\r\n");
var width = completions.reduce(function(a, b) {
return a.length > b.length ? a : b;
@ -191,8 +190,8 @@ Interface.prototype._tabComplete = function () {
self.output.write('\r\n');
}
var group = [], c;
for (var i = 0; i < completions.length; i++) {
var group = [], c, i;
for (i = 0; i < completions.length; i++) {
c = completions[i];
if (c === "") {
handleGroup(group);
@ -202,11 +201,35 @@ Interface.prototype._tabComplete = function () {
}
}
handleGroup(group);
// If there is a common prefix to all matches, then apply that
// portion.
var prefix = commonPrefix(
completions.filter(function(e) { if (e) return e }));
if (prefix.length > completeOn.length) {
self._insertString(prefix.slice(completeOn.length));
}
self._refreshLine();
}
}
};
function commonPrefix(strings) {
if (!strings || strings.length == 0) {
return "";
}
var sorted = strings.slice().sort();
var min = sorted[0];
var max = sorted[sorted.length - 1];
for (var i = 0; i < min.length; i++) {
if (min[i] != max[i]) {
return min.slice(0, i);
}
}
return min;
}
Interface.prototype._historyNext = function () {
if (this.historyIndex > 0) {
this.historyIndex--;

Loading…
Cancel
Save