Skip to content

Commit

Permalink
Change to mix.exs directory before format
Browse files Browse the repository at this point in the history
- Set the `default-directory` to where mix.exs is located, because mix
  is meant to be run from the project root directory. Also, mix format
  looks for .formatter.exs files in subdirectories.

- If mix.exs is NOT found, the original `default-directory` and
  default formatter are used for mix format.

- Create the temp file in the current directory, because mix format
  uses path patterns.

See:

- elixir-lang/elixir#7328 (comment)

- elixir-lang/elixir#7398 (comment)
  • Loading branch information
michaelvobrien committed Jun 5, 2018
1 parent 9a38968 commit 60c4727
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions elixir-format.el
Expand Up @@ -84,8 +84,16 @@
(setq buffer-read-only nil)
(erase-buffer)))

(defun elixir-format--temp-file-path ()
"Make a temp file in the current directory, because mix format
applies rules based on path patterns and looks for .formatter.exs
files in subdirectories."
(concat (file-name-sans-extension buffer-file-name)
"-emacs-elixir-format."
(file-name-extension buffer-file-name)))

(defun elixir-format--run-format (called-interactively-p)
(let ((tmpfile (make-temp-file "elixir-format" nil ".ex"))
(let ((tmpfile (elixir-format--temp-file-path))
(our-elixir-format-arguments (list (elixir-format--mix-executable) "format")))

(write-region nil nil tmpfile)
Expand All @@ -95,7 +103,7 @@
(setq our-elixir-format-arguments (append our-elixir-format-arguments elixir-format-arguments)))
(setq our-elixir-format-arguments (append our-elixir-format-arguments (list tmpfile)))

(if (zerop (apply #'call-process (elixir-format--elixir-executable) nil (elixir-format--errbuff) nil our-elixir-format-arguments))
(if (zerop (elixir-format--from-mix-root (elixir-format--elixir-executable) (elixir-format--errbuff) our-elixir-format-arguments))
(elixir-format--call-format-command tmpfile)
(elixir-format--failed-to-format called-interactively-p))
(delete-file tmpfile)
Expand Down Expand Up @@ -194,6 +202,26 @@ Shamelessly stolen from go-mode (https://github.com/dominikh/go-mode.el)"
(delete-region (progn (forward-visible-line 0) (point))
(progn (forward-visible-line arg) (point))))))


(defun elixir-format--from-mix-root (elixir-path errbuff format-arguments)
"Run mix format where `mix.exs' is located, because mix is
meant to be run from the project root. Otherwise, run in the
current directory."
(let ((original-default-directory default-directory)
(mix-dir (locate-dominating-file buffer-file-name "mix.exs")))

(when mix-dir
(setq default-directory (expand-file-name mix-dir)))

(message (concat "Run "
(abbreviate-file-name default-directory) ": "
(mapconcat 'identity format-arguments " ")))

(let ((result (apply #'call-process
elixir-path nil errbuff nil format-arguments)))
(setq default-directory original-default-directory)
result)))

(provide 'elixir-format)

;;; elixir-format.el ends here

0 comments on commit 60c4727

Please sign in to comment.