Skip to content

Commit

Permalink
repl: complete if all matches share prefix
Browse files Browse the repository at this point in the history
There's gotta be a nicer way to do this
(without resorting to making a tree or something)
but this seems to do the job.
  • Loading branch information
dtzWill committed Jun 26, 2018
1 parent b2bae0d commit 9b98e3d
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/nix/repl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,32 @@ static char * completionCallback(char * s, int *match) {
auto *res = strdup(possible.begin()->c_str() + strlen(s));
if (!res) throw Error("allocation failure");
return res;
} else if (possible.size() > 1) {
size_t startLen = strlen(s);
size_t len = startLen;
while (true) {
bool same = true;
try {
char c = possible.begin()->at(len);
for (auto &p : possible) {
if (p.at(len) != c) {
same = false;
break;
}
}
} catch (std::out_of_range &) {
same = false;
}
if (!same)
break;
++len;
}
if (len > startLen) {
*match = 1;
auto *res = strdup(std::string(*possible.begin(), startLen, len-startLen).c_str());
if (!res) throw Error("allocation failure");
return res;
}
}

*match = 0;
Expand Down

0 comments on commit 9b98e3d

Please sign in to comment.