repl tab completion: insert common prefix of multiple completions

This commit is contained in:
Trent Mick 2010-08-17 16:53:27 -07:00 коммит произвёл Ryan Dahl
Родитель 5c1ffa165f
Коммит 1aeaf8d289
1 изменённых файлов: 26 добавлений и 3 удалений

Просмотреть файл

@ -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--;