84
84
(setq buffer-read-only nil )
85
85
(erase-buffer )))
86
86
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
+
87
94
(defun elixir-format--temp-file-path ()
88
95
" Make a temp file in the current directory, because mix format
89
96
applies rules based on path patterns and looks for .formatter.exs
90
97
files 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))))
94
102
95
103
(defun elixir-format--run-format (called-interactively-p )
96
104
(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" )))
98
106
99
107
(write-region nil nil tmpfile)
100
108
(run-hooks 'elixir-format-hook )
@@ -103,7 +111,7 @@ files in subdirectories."
103
111
(setq our-elixir-format-arguments (append our-elixir-format-arguments elixir-format-arguments)))
104
112
(setq our-elixir-format-arguments (append our-elixir-format-arguments (list tmpfile)))
105
113
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))
107
115
(elixir-format--call-format-command tmpfile)
108
116
(elixir-format--failed-to-format called-interactively-p))
109
117
(delete-file tmpfile)
@@ -203,12 +211,12 @@ Shamelessly stolen from go-mode (https://github.com/dominikh/go-mode.el)"
203
211
(progn (forward-visible-line arg) (point ))))))
204
212
205
213
206
- (defun elixir-format--from-mix-root (elixir -path errbuff format-arguments )
214
+ (defun elixir-format--from-mix-root (mix -path errbuff format-arguments )
207
215
" Run mix format where `mix.exs' is located, because mix is
208
216
meant to be run from the project root. Otherwise, run in the
209
217
current directory."
210
218
(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" )))
212
220
213
221
(when mix-dir
214
222
(setq default-directory (expand-file-name mix-dir)))
@@ -218,7 +226,7 @@ current directory."
218
226
(mapconcat 'identity format-arguments " " )))
219
227
220
228
(let ((result (apply #'call-process
221
- elixir -path nil errbuff nil format-arguments)))
229
+ mix -path nil errbuff nil format-arguments)))
222
230
(setq default-directory original-default-directory)
223
231
result)))
224
232
0 commit comments