repl tab completion: insert common prefix of multiple completions
This commit is contained in:
parent
5c1ffa165f
commit
1aeaf8d289
@ -160,7 +160,6 @@ Interface.prototype._tabComplete = function () {
|
|||||||
self._insertString(completions[0].slice(completeOn.length));
|
self._insertString(completions[0].slice(completeOn.length));
|
||||||
self._refreshLine();
|
self._refreshLine();
|
||||||
} else {
|
} 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");
|
self.output.write("\r\n");
|
||||||
var width = completions.reduce(function(a, b) {
|
var width = completions.reduce(function(a, b) {
|
||||||
return a.length > b.length ? a : b;
|
return a.length > b.length ? a : b;
|
||||||
@ -191,8 +190,8 @@ Interface.prototype._tabComplete = function () {
|
|||||||
self.output.write('\r\n');
|
self.output.write('\r\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
var group = [], c;
|
var group = [], c, i;
|
||||||
for (var i = 0; i < completions.length; i++) {
|
for (i = 0; i < completions.length; i++) {
|
||||||
c = completions[i];
|
c = completions[i];
|
||||||
if (c === "") {
|
if (c === "") {
|
||||||
handleGroup(group);
|
handleGroup(group);
|
||||||
@ -202,11 +201,35 @@ Interface.prototype._tabComplete = function () {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
handleGroup(group);
|
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();
|
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 () {
|
Interface.prototype._historyNext = function () {
|
||||||
if (this.historyIndex > 0) {
|
if (this.historyIndex > 0) {
|
||||||
this.historyIndex--;
|
this.historyIndex--;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user