Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

repl tab completion: insert common prefix of multiple completions

  • Loading branch information...
commit 1aeaf8d2890a7b82e1ff598483a86615cd76227b 1 parent 5c1ffa1
@trentm trentm authored ry committed
Showing with 26 additions and 3 deletions.
  1. +26 −3 lib/readline.js
View
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--;
Please sign in to comment.
Something went wrong with that request. Please try again.