Skip to content

Commit 121cb2b

Browse files
committed
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 8a1c40b commit 121cb2b

File tree

7 files changed

+39
-9
lines changed

7 files changed

+39
-9
lines changed

maintainers/flake-module.nix

+2-2
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.md renamed to src/nix/formatter-run.md

+1-1
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/fmt.cc renamed to src/nix/formatter.cc

+34-4
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,38 @@
55

66
using namespace nix;
77

8-
struct CmdFmt : SourceExprCommand {
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 {
929
std::vector<std::string> args;
1030

11-
CmdFmt() { expectArgs({.label = "args", .handler = {&args}}); }
31+
CmdFormatterRun() { expectArgs({.label = "args", .handler = {&args}}); }
1232

1333
std::string description() override {
1434
return "reformat your code in the standard style";
1535
}
1636

1737
std::string doc() override {
1838
return
19-
#include "fmt.md"
39+
#include "formatter-run.md"
2040
;
2141
}
2242

@@ -52,4 +72,14 @@ struct CmdFmt : SourceExprCommand {
5272
};
5373
};
5474

55-
static auto r2 = registerCommand<CmdFmt>("fmt");
75+
static auto rFormatterRun = registerCommand2<CmdFormatterRun>({"formatter", "run"});
76+
77+
struct CmdFmt : CmdFormatterRun
78+
{
79+
void run(ref<Store> store) override
80+
{
81+
CmdFormatterRun::run(store);
82+
}
83+
};
84+
85+
static auto rFmt = registerCommand<CmdFmt>("fmt");

src/nix/meson.build

+1-1
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

+1-1
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)