8484 (setq buffer-read-only nil )
8585 (erase-buffer )))
8686
87+ (defun elixir-format--target-file-name ()
88+ " Returns the file name of current visited file.
89+
90+ If the buffer is not visiting any file (like during tests) then
91+ it returns a file name based on the name of the buffer."
92+ (or buffer-file-name (concat (secure-hash 'md5 (buffer-name )) " .ex" )))
93+
8794(defun elixir-format--temp-file-path ()
8895 " Make a temp file in the current directory, because mix format
8996applies rules based on path patterns and looks for .formatter.exs
9097files in subdirectories."
91- (concat (file-name-sans-extension buffer-file-name)
92- " -emacs-elixir-format."
93- (file-name-extension buffer-file-name)))
98+ (let ((target-file-name (elixir-format--target-file-name)))
99+ (concat (file-name-sans-extension target-file-name)
100+ " -emacs-elixir-format."
101+ (file-name-extension target-file-name))))
94102
95103(defun elixir-format--run-format (called-interactively-p )
96104 (let ((tmpfile (elixir-format--temp-file-path))
97- (our-elixir-format-arguments (list (elixir-format--mix-executable) " format" )))
105+ (our-elixir-format-arguments (list " format" )))
98106
99107 (write-region nil nil tmpfile)
100108 (run-hooks 'elixir-format-hook )
@@ -103,7 +111,7 @@ files in subdirectories."
103111 (setq our-elixir-format-arguments (append our-elixir-format-arguments elixir-format-arguments)))
104112 (setq our-elixir-format-arguments (append our-elixir-format-arguments (list tmpfile)))
105113
106- (if (zerop (elixir-format--from-mix-root (elixir-format--elixir -executable) (elixir-format--errbuff) our-elixir-format-arguments))
114+ (if (zerop (elixir-format--from-mix-root (elixir-format--mix -executable) (elixir-format--errbuff) our-elixir-format-arguments))
107115 (elixir-format--call-format-command tmpfile)
108116 (elixir-format--failed-to-format called-interactively-p))
109117 (delete-file tmpfile)
@@ -203,12 +211,12 @@ Shamelessly stolen from go-mode (https://github.com/dominikh/go-mode.el)"
203211 (progn (forward-visible-line arg) (point ))))))
204212
205213
206- (defun elixir-format--from-mix-root (elixir -path errbuff format-arguments )
214+ (defun elixir-format--from-mix-root (mix -path errbuff format-arguments )
207215 " Run mix format where `mix.exs' is located, because mix is
208216meant to be run from the project root. Otherwise, run in the
209217current directory."
210218 (let ((original-default-directory default-directory)
211- (mix-dir (locate-dominating-file buffer- file-name " mix.exs" )))
219+ (mix-dir (locate-dominating-file (elixir-format--target- file-name) " mix.exs" )))
212220
213221 (when mix-dir
214222 (setq default-directory (expand-file-name mix-dir)))
@@ -218,7 +226,7 @@ current directory."
218226 (mapconcat 'identity format-arguments " " )))
219227
220228 (let ((result (apply #'call-process
221- elixir -path nil errbuff nil format-arguments)))
229+ mix -path nil errbuff nil format-arguments)))
222230 (setq default-directory original-default-directory)
223231 result)))
224232
0 commit comments