aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorEelco Dolstra <edolstra@gmail.com>2021-12-22 17:55:05 +0100
committerGitHub <noreply@github.com>2021-12-22 17:55:05 +0100
commitb666a2ca8c99a35ebab07ef86f4f6e01c278a30f (patch)
tree667e5793204ad6e179211d06eac8a7545575e90a /src
parentddd78391547be5f34f4042fd48124c0ced1f33b2 (diff)
parentc4a03bc4ae7ac89dfca3ada27d1a7946d568d4d3 (diff)
Merge pull request #5820 from ncfavier/completion-nospace
Don't insert spaces when completing attribute paths
Diffstat (limited to 'src')
-rw-r--r--src/libcmd/installables.cc12
-rw-r--r--src/libutil/args.cc4
-rw-r--r--src/libutil/args.hh8
-rw-r--r--src/nix/main.cc9
4 files changed, 25 insertions, 8 deletions
diff --git a/src/libcmd/installables.cc b/src/libcmd/installables.cc
index ef200b1d2..6c20bb7b1 100644
--- a/src/libcmd/installables.cc
+++ b/src/libcmd/installables.cc
@@ -191,7 +191,7 @@ void SourceExprCommand::completeInstallable(std::string_view prefix)
auto sep = prefix_.rfind('.');
std::string searchWord;
if (sep != std::string::npos) {
- searchWord = prefix_.substr(sep, std::string::npos);
+ searchWord = prefix_.substr(sep + 1, std::string::npos);
prefix_ = prefix_.substr(0, sep);
} else {
searchWord = prefix_;
@@ -203,6 +203,8 @@ void SourceExprCommand::completeInstallable(std::string_view prefix)
Value v2;
state->autoCallFunction(*autoArgs, v1, v2);
+ completionType = ctAttrs;
+
if (v2.type() == nAttrs) {
for (auto & i : *v2.attrs) {
std::string name = i.name;
@@ -232,7 +234,9 @@ void completeFlakeRefWithFragment(
prefix. */
try {
auto hash = prefix.find('#');
- if (hash != std::string::npos) {
+ if (hash == std::string::npos) {
+ completeFlakeRef(evalState->store, prefix);
+ } else {
auto fragment = prefix.substr(hash + 1);
auto flakeRefS = std::string(prefix.substr(0, hash));
// FIXME: do tilde expansion.
@@ -248,6 +252,8 @@ void completeFlakeRefWithFragment(
flake. */
attrPathPrefixes.push_back("");
+ completionType = ctAttrs;
+
for (auto & attrPathPrefixS : attrPathPrefixes) {
auto attrPathPrefix = parseAttrPath(*evalState, attrPathPrefixS);
auto attrPathS = attrPathPrefixS + std::string(fragment);
@@ -285,8 +291,6 @@ void completeFlakeRefWithFragment(
} catch (Error & e) {
warn(e.msg());
}
-
- completeFlakeRef(evalState->store, prefix);
}
void completeFlakeRef(ref<Store> store, std::string_view prefix)
diff --git a/src/libutil/args.cc b/src/libutil/args.cc
index 9df279faf..a739d6b4e 100644
--- a/src/libutil/args.cc
+++ b/src/libutil/args.cc
@@ -39,7 +39,7 @@ void Completions::add(std::string completion, std::string description)
bool Completion::operator<(const Completion & other) const
{ return completion < other.completion || (completion == other.completion && description < other.description); }
-bool pathCompletions = false;
+CompletionType completionType = ctNormal;
std::shared_ptr<Completions> completions;
std::string completionMarker = "___COMPLETE___";
@@ -277,7 +277,7 @@ Args::Flag Args::Flag::mkHashTypeOptFlag(std::string && longName, std::optional<
static void _completePath(std::string_view prefix, bool onlyDirs)
{
- pathCompletions = true;
+ completionType = ctFilenames;
glob_t globbuf;
int flags = GLOB_NOESCAPE | GLOB_TILDE;
#ifdef GLOB_ONLYDIR
diff --git a/src/libutil/args.hh b/src/libutil/args.hh
index 7521b3065..76b1cfe92 100644
--- a/src/libutil/args.hh
+++ b/src/libutil/args.hh
@@ -237,7 +237,13 @@ public:
void add(std::string completion, std::string description = "");
};
extern std::shared_ptr<Completions> completions;
-extern bool pathCompletions;
+
+enum CompletionType {
+ ctNormal,
+ ctFilenames,
+ ctAttrs
+};
+extern CompletionType completionType;
std::optional<std::string> needsCompletion(std::string_view s);
diff --git a/src/nix/main.cc b/src/nix/main.cc
index 60b0aa410..759118fd5 100644
--- a/src/nix/main.cc
+++ b/src/nix/main.cc
@@ -310,7 +310,14 @@ void mainWrapped(int argc, char * * argv)
Finally printCompletions([&]()
{
if (completions) {
- std::cout << (pathCompletions ? "filenames\n" : "no-filenames\n");
+ switch (completionType) {
+ case ctNormal:
+ std::cout << "normal\n"; break;
+ case ctFilenames:
+ std::cout << "filenames\n"; break;
+ case ctAttrs:
+ std::cout << "attrs\n"; break;
+ }
for (auto & s : *completions)
std::cout << s.completion << "\t" << s.description << "\n";
}