Skip to content

Commit

Permalink
repl tab completion: insert common prefix of multiple completions
Browse files Browse the repository at this point in the history
  • Loading branch information
trentm authored and ry committed Aug 19, 2010
1 parent 5c1ffa1 commit 1aeaf8d
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions lib/readline.js
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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--;
Expand Down

0 comments on commit 1aeaf8d

Please sign in to comment.