Skip to content

Commit 7133303

Browse files
jflyMic92
authored andcommitted
refactor: create a new nix formatter run command alias for nix fmt
This refactor shouldn't change much except add a new `nix formatter run` command. This creates space for the new `nix formatter build` command, which I'll be introducing in the next commit.
1 parent e22b92e commit 7133303

File tree

8 files changed

+103
-60
lines changed

8 files changed

+103
-60
lines changed

maintainers/flake-module.nix

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,8 +605,8 @@
605605
''^tests/functional/flakes/prefetch\.sh$''
606606
''^tests/functional/flakes/run\.sh$''
607607
''^tests/functional/flakes/show\.sh$''
608-
''^tests/functional/fmt\.sh$''
609-
''^tests/functional/fmt\.simple\.sh$''
608+
''^tests/functional/formatter\.sh$''
609+
''^tests/functional/formatter\.simple\.sh$''
610610
''^tests/functional/gc-auto\.sh$''
611611
''^tests/functional/gc-concurrent\.builder\.sh$''
612612
''^tests/functional/gc-concurrent\.sh$''

src/nix/fmt.cc

Lines changed: 0 additions & 55 deletions
This file was deleted.

src/nix/fmt.md renamed to src/nix/formatter-run.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ R""(
22

33
# Description
44

5-
`nix fmt` calls the formatter specified in the flake.
5+
`nix fmt` (an alias for `nix formatter run`) calls the formatter specified in the flake.
66

77
Flags can be forwarded to the formatter by using `--` followed by the flags.
88

src/nix/formatter.cc

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#include "nix/cmd/command.hh"
2+
#include "nix/cmd/installable-value.hh"
3+
#include "nix/expr/eval.hh"
4+
#include "run.hh"
5+
6+
using namespace nix;
7+
8+
struct CmdFormatter : NixMultiCommand
9+
{
10+
CmdFormatter()
11+
: NixMultiCommand("formatter", RegisterCommand::getCommandsFor({"formatter"}))
12+
{
13+
}
14+
15+
std::string description() override
16+
{
17+
return "build or run the formatter";
18+
}
19+
20+
Category category() override
21+
{
22+
return catSecondary;
23+
}
24+
};
25+
26+
static auto rCmdFormatter = registerCommand<CmdFormatter>("formatter");
27+
28+
struct CmdFormatterRun : SourceExprCommand
29+
{
30+
std::vector<std::string> args;
31+
32+
CmdFormatterRun()
33+
{
34+
expectArgs({.label = "args", .handler = {&args}});
35+
}
36+
37+
std::string description() override
38+
{
39+
return "reformat your code in the standard style";
40+
}
41+
42+
std::string doc() override
43+
{
44+
return
45+
#include "formatter-run.md"
46+
;
47+
}
48+
49+
Category category() override
50+
{
51+
return catSecondary;
52+
}
53+
54+
Strings getDefaultFlakeAttrPaths() override
55+
{
56+
return Strings{"formatter." + settings.thisSystem.get()};
57+
}
58+
59+
Strings getDefaultFlakeAttrPathPrefixes() override
60+
{
61+
return Strings{};
62+
}
63+
64+
void run(ref<Store> store) override
65+
{
66+
auto evalState = getEvalState();
67+
auto evalStore = getEvalStore();
68+
69+
auto installable_ = parseInstallable(store, ".");
70+
auto & installable = InstallableValue::require(*installable_);
71+
auto app = installable.toApp(*evalState).resolve(evalStore, store);
72+
73+
Strings programArgs{app.program};
74+
75+
// Propagate arguments from the CLI
76+
for (auto & i : args) {
77+
programArgs.push_back(i);
78+
}
79+
80+
// Release our references to eval caches to ensure they are persisted to disk, because
81+
// we are about to exec out of this process without running C++ destructors.
82+
evalState->evalCaches.clear();
83+
84+
execProgramInStore(store, UseLookupPath::DontUse, app.program, programArgs);
85+
};
86+
};
87+
88+
static auto rFormatterRun = registerCommand2<CmdFormatterRun>({"formatter", "run"});
89+
90+
struct CmdFmt : CmdFormatterRun
91+
{
92+
void run(ref<Store> store) override
93+
{
94+
CmdFormatterRun::run(store);
95+
}
96+
};
97+
98+
static auto rFmt = registerCommand<CmdFmt>("fmt");

src/nix/meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ nix_sources = [config_priv_h] + files(
7878
'env.cc',
7979
'eval.cc',
8080
'flake.cc',
81-
'fmt.cc',
81+
'formatter.cc',
8282
'hash.cc',
8383
'log.cc',
8484
'ls.cc',
File renamed without changes.
File renamed without changes.

tests/functional/meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ suites = [
132132
'nix-copy-ssh-ng.sh',
133133
'post-hook.sh',
134134
'function-trace.sh',
135-
'fmt.sh',
135+
'formatter.sh',
136136
'eval-store.sh',
137137
'why-depends.sh',
138138
'derivation-json.sh',

0 commit comments

Comments
 (0)