From 26163ca2658e3aa1cc49cd246e44c3cdce11089a Mon Sep 17 00:00:00 2001 From: Winter Date: Sat, 16 Aug 2025 13:28:09 -0400 Subject: [PATCH] add treefmt config and docs Since Zed can't easily use our wrapped Prettier (see previous commit), and we're now using three formatters, let's just add one command to format all of our code correctly. Signed-off-by: Winter --- .prettierignore | 13 ------------- docs/contributing.md | 6 ++++++ flake.nix | 37 +++++++++++++++++++++++++++++++------ 3 files changed, 37 insertions(+), 19 deletions(-) delete mode 100644 .prettierignore diff --git a/.prettierignore b/.prettierignore deleted file mode 100644 index 1246c578..00000000 --- a/.prettierignore +++ /dev/null @@ -1,13 +0,0 @@ -flake.lock - -# for now, we don't want these formatted, but let's consider it in the future? -*.json -*.md -*.yml -*.yaml -*.jsonc -*.json - -# causes Go template plugin errors: https://github.com/NiklasPor/prettier-plugin-go-template/issues/120 -appview/pages/templates/layouts/repobase.html -appview/pages/templates/repo/tags.html diff --git a/docs/contributing.md b/docs/contributing.md index adf23efc..6d98f79d 100644 --- a/docs/contributing.md +++ b/docs/contributing.md @@ -55,6 +55,12 @@ fussed about this. - Avoid noisy commit messages like "wip" or "final fix"—rewrite history before submitting if necessary. +## code formatting + +We use a variety of tools to format our code, and multiplex them with +[`treefmt`](https://treefmt.com): all you need to do to format your changes +is run `nix run .#fmt` (or just `treefmt` if you're in the devshell). + ## proposals for bigger changes Small fixes like typos, minor bugs, or trivial refactors can be diff --git a/flake.nix b/flake.nix index a6fd2de6..acd92bd8 100644 --- a/flake.nix +++ b/flake.nix @@ -107,13 +107,34 @@ pkgsCross-gnu64-pkgsStatic-knot-unwrapped = crossPackages.knot-unwrapped; pkgsCross-gnu64-pkgsStatic-spindle = crossPackages.spindle; - prettier-wrapper = pkgs.runCommandLocal "prettier-wrapper" {nativeBuildInputs = [pkgs.makeWrapper];} '' - mkdir -p "$out/bin" - makeWrapper ${pkgs.prettier}/bin/prettier "$out/bin/prettier" --add-flags "--plugin=${pkgs.prettier-plugin-go-template}/lib/node_modules/prettier-plugin-go-template/lib/index.js" - ''; + treefmt-wrapper = pkgs.treefmt.withConfig { + settings.formatter = { + alejandra = { + command = pkgs.lib.getExe pkgs.alejandra; + includes = ["*.nix"]; + }; + + gofmt = { + command = pkgs.lib.getExe' pkgs.go "gofmt"; + options = ["-w"]; + includes = ["*.go"]; + }; + + prettier = let + wrapper = pkgs.runCommandLocal "prettier-wrapper" {nativeBuildInputs = [pkgs.makeWrapper];} '' + makeWrapper ${pkgs.prettier}/bin/prettier "$out" --add-flags "--plugin=${pkgs.prettier-plugin-go-template}/lib/node_modules/prettier-plugin-go-template/lib/index.js" + ''; + in { + command = wrapper; + options = ["-w"]; + includes = ["*.html"]; + # causes Go template plugin errors: https://github.com/NiklasPor/prettier-plugin-go-template/issues/120 + excludes = ["appview/pages/templates/layouts/repobase.html" "appview/pages/templates/repo/tags.html"]; + }; + }; + }; }); defaultPackage = forAllSystems (system: self.packages.${system}.appview); - formatter = forAllSystems (system: nixpkgsFor.${system}.alejandra); devShells = forAllSystems (system: let pkgs = nixpkgsFor.${system}; packages' = self.packages.${system}; @@ -134,7 +155,7 @@ pkgs.redis pkgs.coreutils # for those of us who are on systems that use busybox (alpine) packages'.lexgen - packages'.prettier-wrapper + packages'.treefmt-wrapper ]; shellHook = '' mkdir -p appview/pages/static @@ -164,6 +185,10 @@ ${pkgs.tailwindcss}/bin/tailwindcss -w -i input.css -o ./appview/pages/static/tw.css ''; in { + fmt = { + type = "app"; + program = pkgs.lib.getExe packages'.treefmt-wrapper; + }; watch-appview = { type = "app"; program = toString (pkgs.writeShellScript "watch-appview" '' -- 2.51.2