diff --git a/CHANGELOG.md b/CHANGELOG.md index e569c40..ddea798 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,9 @@ The format is based on [Keep a Changelog]. * Autoload the apheleia-goto-error command ([#215]). * Use `lisp-indent` as default formatter for `emacs-lisp-mode` ([#223]) * Use `hclfmt` for formatting hashicorp HCL files ([#231]) +* The `mix format` formatter will respect `.formatter.exs` files even + if they are present in a parent directory rather than the same + directory as the file being formatted ([#232]). ### Internal Changes * Refactored the organisation of the apheleia package for ease of @@ -104,6 +107,7 @@ The format is based on [Keep a Changelog]. [#215]: https://github.com/radian-software/apheleia/pull/215 [#223]: https://github.com/radian-software/apheleia/pull/223 [#231]: https://github.com/radian-software/apheleia/pull/231 +[#232]: https://github.com/radian-software/apheleia/issues/232 [#236]: https://github.com/radian-software/apheleia/pull/236 [#242]: https://github.com/radian-software/apheleia/pull/242 diff --git a/apheleia-formatters.el b/apheleia-formatters.el index d370fbc..8ee1adb 100644 --- a/apheleia-formatters.el +++ b/apheleia-formatters.el @@ -73,7 +73,7 @@ (lisp-indent . apheleia-indent-lisp-buffer) (ktlint . ("ktlint" "--log-level=none" "--stdin" "-F" "-")) (latexindent . ("latexindent" "--logfile=/dev/null")) - (mix-format . ("mix" "format" "-")) + (mix-format . ("apheleia-mix" "format" "-")) (nixfmt . ("nixfmt")) (ocamlformat . ("ocamlformat" "-" "--name" filepath "--enable-outside-detected-project")) diff --git a/scripts/formatters/apheleia-mix b/scripts/formatters/apheleia-mix new file mode 100755 index 0000000..d2038e1 --- /dev/null +++ b/scripts/formatters/apheleia-mix @@ -0,0 +1,22 @@ +#!/usr/bin/env bash + +# This function prints the name of the current directory if it +# contains a file or directory named after the first argument, or the +# parent directory if it contains such a file, or the parent's parent, +# and so on. If no such file is found it returns nonzero. +# https://unix.stackexchange.com/a/22215 +find_upwards() { + fname="$1" + + path="${PWD}" + while [[ -n "${path}" && ! -e "${path}/${fname}" ]]; do + path="${path%/*}" + done + [[ -n "${path}" ]] && echo "${path}" +} + +if dir="$(find_upwards .formatter.exs)"; then + cd -- "${dir}" || exit +fi + +exec mix "$@"