nix flake: Add completion support

This commit is contained in:
Eelco Dolstra 2020-05-11 22:10:33 +02:00
parent 27d34ef770
commit 649c2db308
3 changed files with 27 additions and 10 deletions

View file

@ -38,6 +38,8 @@ struct EvalCommand : virtual StoreCommand, MixEvalArgs
ref<EvalState> getEvalState(); ref<EvalState> getEvalState();
std::shared_ptr<EvalState> evalState; std::shared_ptr<EvalState> evalState;
void completeFlakeRef(std::string_view prefix);
}; };
struct MixFlakeOptions : virtual Args struct MixFlakeOptions : virtual Args

View file

@ -28,7 +28,14 @@ public:
FlakeCommand() FlakeCommand()
{ {
expectArg("flake-url", &flakeUrl, true); expectArgs({
.label = "flake-url",
.optional = true,
.handler = {&flakeUrl},
.completer = {[&](size_t, std::string_view prefix) {
completeFlakeRef(prefix);
}}
});
} }
FlakeRef getFlakeRef() FlakeRef getFlakeRef()

View file

@ -109,8 +109,6 @@ Strings SourceExprCommand::getDefaultFlakeAttrPathPrefixes()
void SourceExprCommand::completeInstallable(std::string_view prefix) void SourceExprCommand::completeInstallable(std::string_view prefix)
{ {
completeDir(0, prefix);
if (file) return; // FIXME if (file) return; // FIXME
/* Look for flake output attributes that match the /* Look for flake output attributes that match the
@ -176,6 +174,23 @@ void SourceExprCommand::completeInstallable(std::string_view prefix)
warn(e.msg()); warn(e.msg());
} }
completeFlakeRef(prefix);
}
ref<EvalState> EvalCommand::getEvalState()
{
if (!evalState)
evalState = std::make_shared<EvalState>(searchPath, getStore());
return ref<EvalState>(evalState);
}
void EvalCommand::completeFlakeRef(std::string_view prefix)
{
if (prefix == "")
completions->insert(".");
completeDir(0, prefix);
/* Look for registry entries that match the prefix. */ /* Look for registry entries that match the prefix. */
for (auto & registry : fetchers::getRegistries(getStore())) { for (auto & registry : fetchers::getRegistries(getStore())) {
for (auto & entry : registry->entries) { for (auto & entry : registry->entries) {
@ -192,13 +207,6 @@ void SourceExprCommand::completeInstallable(std::string_view prefix)
} }
} }
ref<EvalState> EvalCommand::getEvalState()
{
if (!evalState)
evalState = std::make_shared<EvalState>(searchPath, getStore());
return ref<EvalState>(evalState);
}
Buildable Installable::toBuildable() Buildable Installable::toBuildable()
{ {
auto buildables = toBuildables(); auto buildables = toBuildables();