diff --git a/maintainers/flake-module.nix b/maintainers/flake-module.nix index de71be0d7..2dbeaf889 100644 --- a/maintainers/flake-module.nix +++ b/maintainers/flake-module.nix @@ -605,8 +605,8 @@ ''^tests/functional/flakes/prefetch\.sh$'' ''^tests/functional/flakes/run\.sh$'' ''^tests/functional/flakes/show\.sh$'' - ''^tests/functional/fmt\.sh$'' - ''^tests/functional/fmt\.simple\.sh$'' + ''^tests/functional/formatter\.sh$'' + ''^tests/functional/formatter\.simple\.sh$'' ''^tests/functional/gc-auto\.sh$'' ''^tests/functional/gc-concurrent\.builder\.sh$'' ''^tests/functional/gc-concurrent\.sh$'' diff --git a/src/nix/fmt.cc b/src/nix/fmt.cc deleted file mode 100644 index dc270fb8c..000000000 --- a/src/nix/fmt.cc +++ /dev/null @@ -1,55 +0,0 @@ -#include "nix/cmd/command.hh" -#include "nix/cmd/installable-value.hh" -#include "nix/expr/eval.hh" -#include "run.hh" - -using namespace nix; - -struct CmdFmt : SourceExprCommand { - std::vector 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{}; } - - void run(ref store) override - { - auto evalState = getEvalState(); - auto evalStore = getEvalStore(); - - auto installable_ = parseInstallable(store, "."); - auto & installable = InstallableValue::require(*installable_); - auto app = installable.toApp(*evalState).resolve(evalStore, store); - - Strings programArgs{app.program}; - - // Propagate arguments from the CLI - for (auto &i : args) { - programArgs.push_back(i); - } - - // Release our references to eval caches to ensure they are persisted to disk, because - // we are about to exec out of this process without running C++ destructors. - evalState->evalCaches.clear(); - - execProgramInStore(store, UseLookupPath::DontUse, app.program, programArgs); - }; -}; - -static auto r2 = registerCommand("fmt"); diff --git a/src/nix/fmt.md b/src/nix/formatter-run.md similarity index 90% rename from src/nix/fmt.md rename to src/nix/formatter-run.md index b4693eb65..a01b52a9d 100644 --- a/src/nix/fmt.md +++ b/src/nix/formatter-run.md @@ -2,7 +2,7 @@ R""( # Description -`nix fmt` calls the formatter specified in the flake. +`nix fmt` (an alias for `nix formatter run`) calls the formatter specified in the flake. Flags can be forwarded to the formatter by using `--` followed by the flags. diff --git a/src/nix/formatter.cc b/src/nix/formatter.cc new file mode 100644 index 000000000..80ce64d28 --- /dev/null +++ b/src/nix/formatter.cc @@ -0,0 +1,98 @@ +#include "nix/cmd/command.hh" +#include "nix/cmd/installable-value.hh" +#include "nix/expr/eval.hh" +#include "run.hh" + +using namespace nix; + +struct CmdFormatter : NixMultiCommand +{ + CmdFormatter() + : NixMultiCommand("formatter", RegisterCommand::getCommandsFor({"formatter"})) + { + } + + std::string description() override + { + return "build or run the formatter"; + } + + Category category() override + { + return catSecondary; + } +}; + +static auto rCmdFormatter = registerCommand("formatter"); + +struct CmdFormatterRun : SourceExprCommand +{ + std::vector args; + + CmdFormatterRun() + { + expectArgs({.label = "args", .handler = {&args}}); + } + + std::string description() override + { + return "reformat your code in the standard style"; + } + + std::string doc() override + { + return +#include "formatter-run.md" + ; + } + + Category category() override + { + return catSecondary; + } + + Strings getDefaultFlakeAttrPaths() override + { + return Strings{"formatter." + settings.thisSystem.get()}; + } + + Strings getDefaultFlakeAttrPathPrefixes() override + { + return Strings{}; + } + + void run(ref store) override + { + auto evalState = getEvalState(); + auto evalStore = getEvalStore(); + + auto installable_ = parseInstallable(store, "."); + auto & installable = InstallableValue::require(*installable_); + auto app = installable.toApp(*evalState).resolve(evalStore, store); + + Strings programArgs{app.program}; + + // Propagate arguments from the CLI + for (auto & i : args) { + programArgs.push_back(i); + } + + // Release our references to eval caches to ensure they are persisted to disk, because + // we are about to exec out of this process without running C++ destructors. + evalState->evalCaches.clear(); + + execProgramInStore(store, UseLookupPath::DontUse, app.program, programArgs); + }; +}; + +static auto rFormatterRun = registerCommand2({"formatter", "run"}); + +struct CmdFmt : CmdFormatterRun +{ + void run(ref store) override + { + CmdFormatterRun::run(store); + } +}; + +static auto rFmt = registerCommand("fmt"); diff --git a/src/nix/meson.build b/src/nix/meson.build index 901021330..11c30914b 100644 --- a/src/nix/meson.build +++ b/src/nix/meson.build @@ -78,7 +78,7 @@ nix_sources = [config_priv_h] + files( 'env.cc', 'eval.cc', 'flake.cc', - 'fmt.cc', + 'formatter.cc', 'hash.cc', 'log.cc', 'ls.cc', diff --git a/tests/functional/fmt.sh b/tests/functional/formatter.sh similarity index 100% rename from tests/functional/fmt.sh rename to tests/functional/formatter.sh diff --git a/tests/functional/fmt.simple.sh b/tests/functional/formatter.simple.sh similarity index 100% rename from tests/functional/fmt.simple.sh rename to tests/functional/formatter.simple.sh diff --git a/tests/functional/meson.build b/tests/functional/meson.build index f2d6a64ea..b2005d9d9 100644 --- a/tests/functional/meson.build +++ b/tests/functional/meson.build @@ -132,7 +132,7 @@ suites = [ 'nix-copy-ssh-ng.sh', 'post-hook.sh', 'function-trace.sh', - 'fmt.sh', + 'formatter.sh', 'eval-store.sh', 'why-depends.sh', 'derivation-json.sh',