From 1d6f8217847607e1017b0e8c510b5692ce228757 Mon Sep 17 00:00:00 2001 From: Terje Larsen Date: Fri, 12 Jan 2024 20:10:45 +0100 Subject: [PATCH] Add support for `treefmt` (#280) This formatter is formatting via [treefmt](https://numtide.github.io/treefmt). This is useful if the project is configuring formatters for the project and you don't have to replicate this logic within Emacs, but instead utilize treefmt directly. To do that you can set the `apheleia-formatter` to `treefmt` within the projects `.dir-locals.el`. I noticed there are tests for this, since treefmt could technically format anything, I went with treefmt formatting rust code. It also relies on having a `treefmt.toml` in the project folder, so had to add that as well in the tests. --- CHANGELOG.md | 1 + apheleia-formatters.el | 1 + test/formatters/installers/treefmt.bash | 11 +++++++++++ test/formatters/samplecode/treefmt/in.rs | 12 ++++++++++++ test/formatters/samplecode/treefmt/out.rs | 9 +++++++++ 5 files changed, 34 insertions(+) create mode 100644 test/formatters/installers/treefmt.bash create mode 100644 test/formatters/samplecode/treefmt/in.rs create mode 100644 test/formatters/samplecode/treefmt/out.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 0c02196..d0e7a56 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,7 @@ The format is based on [Keep a Changelog]. clojurescript, edn files. ([#271]) * Stylua is used now in `lua-ts-mode` as well as just `lua-mode`, by default ([#275]). +* [`treefmt`](https://numtide.github.io/treefmt) for project configured formatters ([#280]). ### Bugs fixed * Apheleia sometimes failed to determine indent level from Emacs diff --git a/apheleia-formatters.el b/apheleia-formatters.el index c96c4c5..a9c08b4 100644 --- a/apheleia-formatters.el +++ b/apheleia-formatters.el @@ -185,6 +185,7 @@ (stylua . ("stylua" "-")) (rustfmt . ("rustfmt" "--quiet" "--emit" "stdout")) (terraform . ("terraform" "fmt" "-")) + (treefmt . ("treefmt" "--stdin" filepath)) (xmllint . ("xmllint" "--format" "-")) (yapf . ("yapf")) (yq-csv . ("yq" "--prettyPrint" "--no-colors" diff --git a/test/formatters/installers/treefmt.bash b/test/formatters/installers/treefmt.bash new file mode 100644 index 0000000..ac42f01 --- /dev/null +++ b/test/formatters/installers/treefmt.bash @@ -0,0 +1,11 @@ +ver="$(latest_release numtide/treefmt)" + +wget "https://github.com/numtide/treefmt/releases/download/${ver}/treefmt-x86_64-unknown-linux-gnu.tar.gz" -O - | tar -C /usr/local/bin -xz +chmod +x /usr/local/bin/treefmt + +apt-get install -y rustfmt +cat <> /tmp/treefmt.toml +[formatter.rust] +command = "rustfmt" +includes = ["*.rs"] +EOT diff --git a/test/formatters/samplecode/treefmt/in.rs b/test/formatters/samplecode/treefmt/in.rs new file mode 100644 index 0000000..e09c014 --- /dev/null +++ b/test/formatters/samplecode/treefmt/in.rs @@ -0,0 +1,12 @@ +fn foo() { + println!("a"); +} + + + +fn bar() { + println!("b"); + + + println!("c"); +} diff --git a/test/formatters/samplecode/treefmt/out.rs b/test/formatters/samplecode/treefmt/out.rs new file mode 100644 index 0000000..28db64c --- /dev/null +++ b/test/formatters/samplecode/treefmt/out.rs @@ -0,0 +1,9 @@ +fn foo() { + println!("a"); +} + +fn bar() { + println!("b"); + + println!("c"); +}