[#232] Format from elixir project dir (#239)

Make it so that if there is a `.formatter.exs` file somewhere in the
parent directories, then Apheleia will run `mix format` from there
instead of the current directory.

Close #232
This commit is contained in:
Radon Rosborough 2023-11-11 16:32:40 -08:00 committed by GitHub
parent 598ef3c7cb
commit ddea9bea70
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 1 deletions

View file

@ -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

View file

@ -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"))

22
scripts/formatters/apheleia-mix Executable file
View file

@ -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 "$@"