about summary refs log tree commit diff
path: root/src/nix
diff options
context:
space:
mode:
authorWill Dietz <w@wdtz.org>2018-06-26T18·06-0500
committerWill Dietz <w@wdtz.org>2018-10-29T13·45-0500
commit9f998096d23eab433f47d8dc1ad5eb1e2ff4b667 (patch)
tree8dbcd0e3facc09252387116c3dcb98caf89ea9dd /src/nix
parent3d974d31facefe8eaf59af56b0187e6a63fdd0cc (diff)
repl: complete if all matches share prefix
Diffstat (limited to 'src/nix')
-rw-r--r--src/nix/repl.cc18
1 files changed, 18 insertions, 0 deletions
diff --git a/src/nix/repl.cc b/src/nix/repl.cc
index 7ce370770218..838aa64ae29a 100644
--- a/src/nix/repl.cc
+++ b/src/nix/repl.cc
@@ -132,6 +132,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;