Skip to content

Commit 6cfa327

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 5ebedb0 commit 6cfa327

10 files changed

Lines changed: 149 additions & 15 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/Test/Assert.lua

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
return {
2-
assertImpl = (function(message)
3-
return function(success) return function() if not success then error(message) end end end
4-
end),
2+
assertImpl = (function(message) return function(success) return function() if not success then error(message) end end end end),
53
checkThrows = (function(fn)
64
return function()
75
local success = pcall(fn)

src/Test/Assert.purs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,15 @@ assertEqual'
8484
=> String
8585
-> { actual :: a, expected :: a }
8686
-> Effect Unit
87-
assertEqual' userMessage {actual, expected} = do
87+
assertEqual' userMessage { actual, expected } = do
8888
unless result $ error message
8989
assert' message result
9090
where
9191
message = (if userMessage == "" then "" else userMessage <> "\n")
92-
<> "Expected: " <> show expected
93-
<> "\nActual: " <> show actual
92+
<> "Expected: "
93+
<> show expected
94+
<> "\nActual: "
95+
<> show actual
9496
result = actual == expected
9597

9698
-- | Throws a runtime exception when the value is `false`.

treefmt.nix

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{ pkgs, ... }:
2+
{
3+
projectRootFile = "flake.nix";
4+
5+
# Nix — RFC 166 formatter.
6+
programs.nixfmt.enable = true;
7+
8+
# Dhall — spago.dhall / packages.dhall layout.
9+
programs.dhall.enable = true;
10+
11+
# PureScript — purs-tidy is not a first-class treefmt program, so wire it via
12+
# the generic mechanism. It picks up `.tidyrc.json` from the project root.
13+
settings.formatter.purs-tidy = {
14+
command = "${pkgs.purs-tidy}/bin/purs-tidy";
15+
options = [ "format-in-place" ];
16+
includes = [ "*.purs" ];
17+
};
18+
19+
# Lua FFI — LuaFormatter keeps the parentheses pslua's foreign-file parser
20+
# requires (unlike StyLua, which strips them). Config in `.lua-format`.
21+
settings.formatter.lua-format = {
22+
command = "${pkgs.luaformatter}/bin/lua-format";
23+
options = [
24+
"-i"
25+
"-c"
26+
".lua-format"
27+
];
28+
includes = [ "*.lua" ];
29+
};
30+
31+
# Never format generated output or vendored trees.
32+
settings.global.excludes = [
33+
"dist/*"
34+
"output/*"
35+
".spago/*"
36+
"node_modules/*"
37+
"*.lock"
38+
"flake.lock"
39+
"spago.lock"
40+
".tidyrc.json"
41+
".lua-format"
42+
];
43+
}

0 commit comments

Comments
 (0)