Skip to content

Commit

Permalink
repl: complete if all matches share prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
dtzWill committed Jun 26, 2018
1 parent 8c7325b commit af4f014
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/nix/repl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,24 @@ 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) {
auto checkAllHaveSameAt = [&](size_t pos) {
auto &first = *possible.begin();
for (auto &p : possible) {
if (p.size() <= pos || p[pos] != first[pos])
return false;
}
return true;
};
size_t start = strlen(s);
size_t len = 0;
while (checkAllHaveSameAt(start + len)) ++len;
if (len > 0) {
*match = 1;
auto *res = strdup(std::string(*possible.begin(), start, len).c_str());
if (!res) throw Error("allocation failure");
return res;
}
}

*match = 0;
Expand Down

0 comments on commit af4f014

Please sign in to comment.