Skip to content

Commit 438e507

Browse files
committed
chore: add treefmt formatting (nix fmt) and format the tree
Wire treefmt via treefmt-nix: nixfmt, dhall format, purs-tidy (.tidyrc.json) and LuaFormatter for the FFI (.lua-format, kept over StyLua because it preserves the parentheses pslua's parser needs). `nix fmt` formats; the dev shell installs a content-based pre-commit hook and CI runs `nix fmt && git diff --exit-code` (content-based, since the in-place formatters bump mtime and would trip treefmt --fail-on-change). Lua lines budget 130 cols, matching the raised `luacheck --max-line-length`. The bulk of the diff is the first format pass.
1 parent 850429c commit 438e507

11 files changed

Lines changed: 366 additions & 76 deletions

File tree

.github/workflows/ci.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,7 @@ jobs:
2424
run: if [ -f scripts/test ]; then nix develop -c bash ./scripts/test; fi
2525

2626
- name: Luacheck
27-
run: nix develop -c luacheck --quiet --std lua51 --no-unused-args src/
27+
run: nix develop -c luacheck --quiet --std lua51 --no-unused-args --max-line-length 130 src/
28+
29+
- name: Format check
30+
run: nix fmt && git diff --exit-code

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
/.*
22
!/.gitignore
33
!/.github/
4+
!/.tidyrc.json
5+
!/.lua-format
46
/output/

.lua-format

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# LuaFormatter config for the hand-written FFI under src/.
2+
# 2-space indent. Keep simple functions on one line; column_limit sits a few
3+
# columns under luacheck's 130 limit because lua-format under-counts the leading
4+
# indent and trailing comma, so this keeps every emitted line within 130.
5+
indent_width: 2
6+
use_tab: false
7+
column_limit: 126
8+
continuation_indent_width: 2
9+
keep_simple_function_one_line: true
10+
keep_simple_control_block_one_line: true

.tidyrc.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"importSort": "source",
3+
"importWrap": "source",
4+
"indent": 2,
5+
"operatorsFile": null,
6+
"ribbon": 1,
7+
"typeArrowPlacement": "first",
8+
"unicode": "source",
9+
"width": 80
10+
}

AGENTS.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,21 @@ A PureScript→Lua FFI fork in the [`purescript-lua`](https://github.com/purescr
44

55
## Commands
66

7-
All commands run inside the nix dev shell:
8-
97
- Build: `nix develop -c ./scripts/build`
108
- Test (only if the fork has `scripts/test`): `nix develop -c bash ./scripts/test`
11-
- Lint: `nix develop -c luacheck --quiet --std lua51 --no-unused-args src/`
9+
- Lint: `nix develop -c luacheck --quiet --std lua51 --no-unused-args --max-line-length 130 src/`
10+
- Format: `nix fmt` (check: `nix fmt && git diff --exit-code`)
11+
12+
## Formatting
13+
14+
`nix fmt` runs treefmt (`treefmt.nix`): nixfmt for Nix, `dhall format`, purs-tidy
15+
for `*.purs` (config in `.tidyrc.json`), and LuaFormatter for the `*.lua` FFI
16+
(config in `.lua-format`). LuaFormatter is used over StyLua because it keeps the
17+
parentheses pslua's foreign-file parser requires. The Lua line budget is 130
18+
columns, matching the `luacheck --max-line-length` above. The check is
19+
content-based (`nix fmt && git diff --exit-code`) rather than `treefmt --ci`,
20+
since the in-place formatters bump mtime even when content is unchanged, which
21+
trips treefmt's `--fail-on-change`. CI and the pre-commit hook use it.
1222

1323
## Lua 5.1 target
1424

flake.lock

Lines changed: 22 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,33 @@
99
inputs.nixpkgs.follows = "nixpkgs";
1010
};
1111
pslua.url = "github:purescript-lua/purescript-lua";
12+
treefmt-nix = {
13+
url = "github:numtide/treefmt-nix";
14+
inputs.nixpkgs.follows = "nixpkgs";
15+
};
1216
};
1317

14-
outputs = { self, nixpkgs, flake-utils, purescript-overlay, pslua }:
15-
flake-utils.lib.eachDefaultSystem (system:
18+
outputs =
19+
{
20+
self,
21+
nixpkgs,
22+
flake-utils,
23+
purescript-overlay,
24+
pslua,
25+
treefmt-nix,
26+
}:
27+
flake-utils.lib.eachDefaultSystem (
28+
system:
1629
let
1730
pkgs = import nixpkgs {
1831
inherit system;
1932
overlays = [ purescript-overlay.overlays.default ];
2033
};
21-
in {
34+
treefmtEval = treefmt-nix.lib.evalModule pkgs ./treefmt.nix;
35+
in
36+
{
37+
formatter = treefmtEval.config.build.wrapper;
38+
checks.formatting = treefmtEval.config.build.check self;
2239
devShell = pkgs.mkShell {
2340
buildInputs = with pkgs; [
2441
dhall
@@ -31,8 +48,26 @@
3148
spago-bin.spago-0_21_0
3249
treefmt
3350
];
51+
# Install a content-based pre-commit hook. It compares the working
52+
# tree diff before and after `nix fmt`, so it only objects to changes
53+
# the formatter itself introduces (not the developer's existing
54+
# unstaged work) and is not fooled by formatters that only bump mtime.
55+
# Rewritten each shell entry to stay in sync with this flake.
56+
shellHook = ''
57+
hook=.git/hooks/pre-commit
58+
if [ -d .git ]; then
59+
printf '%s\n' \
60+
'#!/usr/bin/env bash' \
61+
'before=$(git diff)' \
62+
'nix fmt >/dev/null 2>&1 || exit 0' \
63+
'[ "$before" = "$(git diff)" ] || { echo "nix fmt changed files; re-stage them, then commit." >&2; exit 1; }' \
64+
> "$hook"
65+
chmod +x "$hook"
66+
fi
67+
'';
3468
};
35-
});
69+
}
70+
);
3671

3772
# --- Flake Local Nix Configuration ----------------------------
3873
nixConfig = {

src/Control/Monad/ST/Internal.purs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ instance monadRecST :: MonadRec (ST r) where
6666
Done _ -> pure unit
6767
fromDone <$> read r
6868
where
69-
fromDone :: forall a b. Step a b -> b
70-
fromDone = unsafePartial \(Done b) -> b
69+
fromDone :: forall a b. Step a b -> b
70+
fromDone = unsafePartial \(Done b) -> b
7171

72-
isLooping = case _ of
73-
Loop _ -> true
74-
_ -> false
72+
isLooping = case _ of
73+
Loop _ -> true
74+
_ -> false
7575

7676
instance semigroupST :: Semigroup a => Semigroup (ST r a) where
7777
append = lift2 append
@@ -122,10 +122,12 @@ foreign import read :: forall a r. STRef r a -> ST r a
122122
-- | Update the value of a mutable reference by applying a function
123123
-- | to the current value, computing a new state value for the reference and
124124
-- | a return value.
125-
modify' :: forall r a b. (a -> { state :: a, value :: b }) -> STRef r a -> ST r b
125+
modify'
126+
:: forall r a b. (a -> { state :: a, value :: b }) -> STRef r a -> ST r b
126127
modify' = modifyImpl
127128

128-
foreign import modifyImpl :: forall r a b. (a -> { state :: a, value :: b }) -> STRef r a -> ST r b
129+
foreign import modifyImpl
130+
:: forall r a b. (a -> { state :: a, value :: b }) -> STRef r a -> ST r b
129131

130132
-- | Modify the value of a mutable reference by applying a function to the
131133
-- | current value. The modified value is returned.

src/Control/Monad/ST/Uncurried.lua

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ return {
1717

1818
mkSTFn9 = (function(fn) return function(a, b, c, d, e, f, g, h, i) return fn(a)(b)(c)(d)(e)(f)(g)(h)(i)() end end),
1919

20-
mkSTFn10 = (function(fn)
21-
return function(a, b, c, d, e, f, g, h, i, j) return fn(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)() end
22-
end),
20+
mkSTFn10 = (function(fn) return function(a, b, c, d, e, f, g, h, i, j) return fn(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)() end end),
2321

2422
runSTFn1 = (function(fn) return function(a) return function() return fn(a) end end end),
2523

@@ -38,9 +36,7 @@ return {
3836
runSTFn5 = (function(fn)
3937
return function(a)
4038
return function(b)
41-
return function(c)
42-
return function(d) return function(e) return function() return fn(a, b, c, d, e) end end end
43-
end
39+
return function(c) return function(d) return function(e) return function() return fn(a, b, c, d, e) end end end end
4440
end
4541
end
4642
end),
@@ -63,9 +59,7 @@ return {
6359
return function(c)
6460
return function(d)
6561
return function(e)
66-
return function(f)
67-
return function(g) return function() return fn(a, b, c, d, e, f, g) end end
68-
end
62+
return function(f) return function(g) return function() return fn(a, b, c, d, e, f, g) end end end
6963
end
7064
end
7165
end
@@ -121,9 +115,7 @@ return {
121115
return function(g)
122116
return function(h)
123117
return function(i)
124-
return function(j)
125-
return function() return fn(a, b, c, d, e, f, g, h, i, j) end
126-
end
118+
return function(j) return function() return fn(a, b, c, d, e, f, g, h, i, j) end end
127119
end
128120
end
129121
end

0 commit comments

Comments
 (0)