Skip to content

Instantly share code, notes, and snippets.

@victorolinasc
Created January 12, 2020 18:48
Show Gist options
  • Save victorolinasc/fa44dcb6e05b3a4bb33b033bfb3da414 to your computer and use it in GitHub Desktop.
Save victorolinasc/fa44dcb6e05b3a4bb33b033bfb3da414 to your computer and use it in GitHub Desktop.
Reformatter elixir configuration
(use-package reformatter
:ensure t
:config
; Adds a reformatter configuration called "+elixir-format"
; This uses "mix format -"
(reformatter-define +elixir-format
:program "mix"
:args '("format" "-"))
; defines a function that looks for the .formatter.exs file used by mix format
(defun +set-default-directory-to-mix-project-root (original-fun &rest args)
(if-let* ((mix-project-root (and buffer-file-name
(locate-dominating-file buffer-file-name
".formatter.exs"))))
(let ((default-directory mix-project-root))
(apply original-fun args))
(apply original-fun args)))
; adds an advice to the generated function +elxir-format-region that sets the proper root dir
; mix format needs to be run from the root directory otherwise it wont use the formatter configuration
(advice-add '+elixir-format-region :around #'+set-default-directory-to-mix-project-root)
; Adds a hook to the major-mode that will add the generated function +elixir-format-on-save-mode
; So, every time we save an elixir file it will try to find a .formatter.exs and then run mix format from
; that file's directory
(add-hook 'elixir-mode-hook #'+elixir-format-on-save-mode))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment