From 1da1b2b345ccf32220a2628622ee8b170d9d521a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Na=C3=AFm=20Favier?= Date: Wed, 22 Dec 2021 12:37:59 +0100 Subject: [PATCH 1/2] Don't insert spaces when completing attribute paths --- misc/bash/completion.sh | 4 +++- misc/fish/completion.fish | 1 - src/libcmd/installables.cc | 10 +++++++--- src/libutil/args.cc | 4 ++-- src/libutil/args.hh | 8 +++++++- src/nix/main.cc | 9 ++++++++- 6 files changed, 27 insertions(+), 9 deletions(-) diff --git a/misc/bash/completion.sh b/misc/bash/completion.sh index bea2a40bc..3eb44dfa6 100644 --- a/misc/bash/completion.sh +++ b/misc/bash/completion.sh @@ -7,8 +7,10 @@ function _complete_nix { local completion=${line%% *} if [[ -z $have_type ]]; then have_type=1 - if [[ $completion = filenames ]]; then + if [[ $completion == filenames ]]; then compopt -o filenames + elif [[ $completion == attrs ]]; then + compopt -o nospace fi else COMPREPLY+=("$completion") diff --git a/misc/fish/completion.fish b/misc/fish/completion.fish index bedbefaf8..c6b8ef16a 100644 --- a/misc/fish/completion.fish +++ b/misc/fish/completion.fish @@ -19,7 +19,6 @@ end function _nix_accepts_files set -l response (_nix_complete) - # First line is either filenames or no-filenames. test $response[1] = 'filenames' end diff --git a/src/libcmd/installables.cc b/src/libcmd/installables.cc index ef200b1d2..5f118b8d0 100644 --- a/src/libcmd/installables.cc +++ b/src/libcmd/installables.cc @@ -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, 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; 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; -extern bool pathCompletions; + +enum CompletionType { + ctNormal, + ctFilenames, + ctAttrs +}; +extern CompletionType completionType; std::optional 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"; } From c4a03bc4ae7ac89dfca3ada27d1a7946d568d4d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Na=C3=AFm=20Favier?= Date: Wed, 22 Dec 2021 16:37:58 +0100 Subject: [PATCH 2/2] Fix attr path completion after a dot --- src/libcmd/installables.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcmd/installables.cc b/src/libcmd/installables.cc index 5f118b8d0..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_;