libcmd/installables: implement completion for Nix expressions passed via -f

This commit is contained in:
Maximilian Bosch 2021-07-03 14:19:10 +02:00
parent 69eb65403a
commit 70cb2ffacc
No known key found for this signature in database
GPG key ID: 091DBF4D1FC46B8E

View file

@ -171,8 +171,37 @@ Strings SourceExprCommand::getDefaultFlakeAttrPathPrefixes()
void SourceExprCommand::completeInstallable(std::string_view prefix) 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)))
);
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( completeFlakeRefWithFragment(
getEvalState(), getEvalState(),
lockFlags, lockFlags,
@ -180,6 +209,7 @@ void SourceExprCommand::completeInstallable(std::string_view prefix)
getDefaultFlakeAttrPaths(), getDefaultFlakeAttrPaths(),
prefix); prefix);
} }
}
void completeFlakeRefWithFragment( void completeFlakeRefWithFragment(
ref<EvalState> evalState, ref<EvalState> evalState,