From 70cb2ffaccb91eff6f4afe2552d0784e279f1fe9 Mon Sep 17 00:00:00 2001 From: Maximilian Bosch Date: Sat, 3 Jul 2021 14:19:10 +0200 Subject: [PATCH 1/2] libcmd/installables: implement completion for Nix expressions passed via `-f` --- src/libcmd/installables.cc | 44 ++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/src/libcmd/installables.cc b/src/libcmd/installables.cc index fe52912cf..49f063334 100644 --- a/src/libcmd/installables.cc +++ b/src/libcmd/installables.cc @@ -171,14 +171,44 @@ Strings SourceExprCommand::getDefaultFlakeAttrPathPrefixes() void SourceExprCommand::completeInstallable(std::string_view prefix) { - if (file) return; // FIXME + if (file) { + evalSettings.pureEval = false; + auto state = getEvalState(); + Expr *e = state->parseExprFromFile( + resolveExprPath(state->checkSourcePath(lookupFileArg(*state, *file))) + ); - completeFlakeRefWithFragment( - getEvalState(), - lockFlags, - getDefaultFlakeAttrPathPrefixes(), - getDefaultFlakeAttrPaths(), - prefix); + Value root; + state->eval(e, root); + + auto autoArgs = getAutoArgs(*state); + + std::string prefix_ = std::string(prefix); + auto sep = prefix_.rfind('.'); + if (sep != std::string::npos) { + prefix_.erase(sep); + } else { + prefix_ = ""; + } + + Value &v1(*findAlongAttrPath(*state, prefix_, *autoArgs, root).first); + state->forceValue(v1); + Value v2; + state->autoCallFunction(*autoArgs, v1, v2); + + if (v2.type() == nAttrs) { + for (auto & i : *v2.attrs) { + completions->add(i.name); + } + } + } else { + completeFlakeRefWithFragment( + getEvalState(), + lockFlags, + getDefaultFlakeAttrPathPrefixes(), + getDefaultFlakeAttrPaths(), + prefix); + } } void completeFlakeRefWithFragment( From 3c5f69bb60f858f471121adaf172edb47628188e Mon Sep 17 00:00:00 2001 From: Maximilian Bosch Date: Mon, 5 Jul 2021 21:37:33 +0200 Subject: [PATCH 2/2] completeInstallable: also match for already typed prefixes --- src/libcmd/installables.cc | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/libcmd/installables.cc b/src/libcmd/installables.cc index 49f063334..5f263061b 100644 --- a/src/libcmd/installables.cc +++ b/src/libcmd/installables.cc @@ -185,9 +185,12 @@ void SourceExprCommand::completeInstallable(std::string_view prefix) std::string prefix_ = std::string(prefix); auto sep = prefix_.rfind('.'); + std::string searchWord; if (sep != std::string::npos) { - prefix_.erase(sep); + searchWord = prefix_.substr(sep, std::string::npos); + prefix_ = prefix_.substr(0, sep); } else { + searchWord = prefix_; prefix_ = ""; } @@ -198,7 +201,10 @@ void SourceExprCommand::completeInstallable(std::string_view prefix) if (v2.type() == nAttrs) { for (auto & i : *v2.attrs) { - completions->add(i.name); + std::string name = i.name; + if (name.find(searchWord) == 0) { + completions->add(i.name); + } } } } else {