Skip to content

Commit f187104

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 0b33a99 commit f187104

16 files changed

Lines changed: 176 additions & 26 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/Alt.purs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module Control.Alt
2-
( class Alt, alt, (<|>)
2+
( class Alt
3+
, alt
4+
, (<|>)
35
, module Data.Functor
46
) where
57

src/Control/Comonad.purs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module Control.Comonad
2-
( class Comonad, extract
2+
( class Comonad
3+
, extract
34
, module Control.Extend
45
, module Data.Functor
56
) where

src/Control/Extend.lua

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ return {
55
local result = {}
66
for i = 1, len do
77
local suffix = {}
8-
for j = i, len do
9-
suffix[j - i + 1] = xs[j]
10-
end
8+
for j = i, len do suffix[j - i + 1] = xs[j] end
119
result[i] = f(suffix)
1210
end
1311
return result

0 commit comments

Comments
 (0)