From 27d34ef770356f86823ca832f278e72bb0a07982 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Mon, 11 May 2020 22:04:13 +0200 Subject: [PATCH] When completing flakerefs, only return directories --- src/libutil/args.cc | 14 ++++++++++++-- src/libutil/args.hh | 2 ++ src/nix/installables.cc | 2 +- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/libutil/args.cc b/src/libutil/args.cc index ee0c99c2a..f6740e076 100644 --- a/src/libutil/args.cc +++ b/src/libutil/args.cc @@ -220,17 +220,27 @@ Args::Flag Args::Flag::mkHashTypeFlag(std::string && longName, HashType * ht) }; } -void completePath(size_t, std::string_view prefix) +static void completePath(std::string_view prefix, int flags) { pathCompletions = true; glob_t globbuf; - if (glob((std::string(prefix) + "*").c_str(), GLOB_NOESCAPE | GLOB_TILDE, nullptr, &globbuf) == 0) { + if (glob((std::string(prefix) + "*").c_str(), GLOB_NOESCAPE | GLOB_TILDE | flags, nullptr, &globbuf) == 0) { for (size_t i = 0; i < globbuf.gl_pathc; ++i) completions->insert(globbuf.gl_pathv[i]); globfree(&globbuf); } } +void completePath(size_t, std::string_view prefix) +{ + completePath(prefix, 0); +} + +void completeDir(size_t, std::string_view prefix) +{ + completePath(prefix, GLOB_ONLYDIR); +} + Strings argvToStrings(int argc, char * * argv) { Strings args; diff --git a/src/libutil/args.hh b/src/libutil/args.hh index 250244162..405ec3d47 100644 --- a/src/libutil/args.hh +++ b/src/libutil/args.hh @@ -277,4 +277,6 @@ std::optional needsCompletion(std::string_view s); void completePath(size_t, std::string_view prefix); +void completeDir(size_t, std::string_view prefix); + } diff --git a/src/nix/installables.cc b/src/nix/installables.cc index ade8f83f9..5ba48d69a 100644 --- a/src/nix/installables.cc +++ b/src/nix/installables.cc @@ -109,7 +109,7 @@ Strings SourceExprCommand::getDefaultFlakeAttrPathPrefixes() void SourceExprCommand::completeInstallable(std::string_view prefix) { - completePath(0, prefix); + completeDir(0, prefix); if (file) return; // FIXME