2022-03-11 15:57:28 +02:00
|
|
|
#include "command.hh"
|
2023-02-05 19:16:17 +02:00
|
|
|
#include "installable-value.hh"
|
2022-03-11 15:57:28 +02:00
|
|
|
#include "run.hh"
|
|
|
|
|
|
|
|
using namespace nix;
|
|
|
|
|
|
|
|
struct CmdFmt : SourceExprCommand {
|
|
|
|
std::vector<std::string> args;
|
|
|
|
|
|
|
|
CmdFmt() { expectArgs({.label = "args", .handler = {&args}}); }
|
|
|
|
|
|
|
|
std::string description() override {
|
|
|
|
return "reformat your code in the standard style";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string doc() override {
|
|
|
|
return
|
|
|
|
#include "fmt.md"
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
Category category() override { return catSecondary; }
|
|
|
|
|
|
|
|
Strings getDefaultFlakeAttrPaths() override {
|
|
|
|
return Strings{"formatter." + settings.thisSystem.get()};
|
|
|
|
}
|
|
|
|
|
|
|
|
Strings getDefaultFlakeAttrPathPrefixes() override { return Strings{}; }
|
|
|
|
|
2022-04-28 15:24:17 +03:00
|
|
|
void run(ref<Store> store) override
|
|
|
|
{
|
2022-03-11 15:57:28 +02:00
|
|
|
auto evalState = getEvalState();
|
|
|
|
auto evalStore = getEvalStore();
|
|
|
|
|
2023-02-05 19:16:17 +02:00
|
|
|
auto installable_ = parseInstallable(store, ".");
|
|
|
|
auto & installable = InstallableValue::require(*installable_);
|
|
|
|
auto app = installable.toApp(*evalState).resolve(evalStore, store);
|
2022-03-11 15:57:28 +02:00
|
|
|
|
|
|
|
Strings programArgs{app.program};
|
|
|
|
|
|
|
|
// Propagate arguments from the CLI
|
|
|
|
if (args.empty()) {
|
|
|
|
// Format the current flake out of the box
|
|
|
|
programArgs.push_back(".");
|
|
|
|
} else {
|
|
|
|
// User wants more power, let them decide which paths to include/exclude
|
|
|
|
for (auto &i : args) {
|
|
|
|
programArgs.push_back(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-01 16:35:21 +02:00
|
|
|
runProgramInStore(store, UseSearchPath::DontUse, app.program, programArgs);
|
2022-03-11 15:57:28 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
static auto r2 = registerCommand<CmdFmt>("fmt");
|