Skip to content

Commit bd96e46

Browse files
anildigitalTrevoke
authored andcommitted
Better upgrade/onboarding experience
Done changes for better upgrade/onboard experience. If needed config variables are not set, It now displays a error message in a buffer.
1 parent 028fe6c commit bd96e46

File tree

1 file changed

+66
-29
lines changed

1 file changed

+66
-29
lines changed

elixir-format.el

Lines changed: 66 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,29 @@
2525

2626
(require 'ansi-color)
2727

28-
(defcustom elixir-format-elixir-path "elixir"
29-
"Path to the Elixir interpreter."
28+
(defcustom elixir-format-elixir-path ""
29+
"Path to the Elixir executable. Usually it is /usr/local/bin/elixir. You can type `which elixir` in terminal to find out the elixir binary path in your system.
30+
31+
Customize the elixir path
32+
33+
In Emacs, run following command to customize option
34+
35+
M-x customize-option
36+
37+
Customize-variable: elixir-format-elixir-path"
3038
:type 'string
3139
:group 'elixir-format)
3240

33-
(defcustom elixir-format-mix-path "/usr/bin/mix"
34-
"Path to the 'mix' executable."
41+
(defcustom elixir-format-mix-path ""
42+
"Path to the 'mix' executable. Usually it is /usr/local/bin/mix. You can type `which mix` in terminal to find out the mix binary path in your system
43+
44+
Customize the mix path
45+
46+
In Emacs, run following command to customize option
47+
48+
M-x customize-option
49+
50+
Customize-variable: elixir-format-mix-path"
3551
:type 'string
3652
:group 'elixir-format)
3753

@@ -125,6 +141,24 @@ Shamelessly stolen from go-mode (https://github.com/dominikh/go-mode.el)"
125141
(error "Invalid rcs patch or internal error in elixir-format--apply-rcs-patch"))))))))
126142
)
127143

144+
(defun elixir-format-elixir-and-mix-path-not-set-p ()
145+
(or (= (length elixir-format-mix-path) 0) (= (length elixir-format-elixir-path) 0))
146+
)
147+
148+
(defun elixir-format-display-missing-configuration-error (errbuff is-interactive)
149+
(with-current-buffer errbuff
150+
(progn
151+
(setq buffer-read-only nil)
152+
(erase-buffer)
153+
(insert "elixir or mix binary path not set. Please run `C-h v a-variable RET` for `elixir-format-elixir-path` and `elixir-format-mix-path` variables to see docs to customize necessary variables for elixir and mix.")
154+
(setq buffer-read-only t)
155+
(ansi-color-apply-on-region (point-min) (point-max))
156+
(special-mode)
157+
(if is-interactive
158+
(display-buffer errbuff)
159+
(error "elixir-format-configuration-missing: see %s" (buffer-name errbuff))))
160+
))
161+
128162
;;;###autoload
129163
(defun elixir-format (&optional is-interactive)
130164
(interactive "p")
@@ -144,35 +178,38 @@ Shamelessly stolen from go-mode (https://github.com/dominikh/go-mode.el)"
144178
(setq buffer-read-only nil)
145179
(erase-buffer))
146180

147-
(write-region nil nil tmpfile)
181+
(if (elixir-format-elixir-and-mix-path-not-set-p)
182+
(elixir-format-display-missing-configuration-error errbuff is-interactive)
183+
184+
(write-region nil nil tmpfile)
148185

149-
(run-hooks 'elixir-format-hook)
186+
(run-hooks 'elixir-format-hook)
150187

151-
(when elixir-format-arguments
152-
(setq our-elixir-format-arguments (append our-elixir-format-arguments elixir-format-arguments)))
153-
(setq our-elixir-format-arguments (append our-elixir-format-arguments (list tmpfile)))
188+
(when elixir-format-arguments
189+
(setq our-elixir-format-arguments (append our-elixir-format-arguments elixir-format-arguments)))
190+
(setq our-elixir-format-arguments (append our-elixir-format-arguments (list tmpfile)))
191+
192+
(if (zerop (apply #'call-process elixir-format-elixir-path nil errbuff nil our-elixir-format-arguments))
193+
(progn
194+
(if (zerop (call-process-region (point-min) (point-max) "diff" nil outbuff nil "-n" "-" tmpfile))
195+
(message "File is already formatted")
196+
(progn
197+
(elixir-format--apply-rcs-patch outbuff)
198+
(message "elixir-format format applied")))
199+
(kill-buffer errbuff))
154200

155-
(if (zerop (apply #'call-process elixir-format-elixir-path nil errbuff nil our-elixir-format-arguments))
156201
(progn
157-
(if (zerop (call-process-region (point-min) (point-max) "diff" nil outbuff nil "-n" "-" tmpfile))
158-
(message "File is already formatted")
159-
(progn
160-
(elixir-format--apply-rcs-patch outbuff)
161-
(message "mix format applied")))
162-
(kill-buffer errbuff))
163-
164-
(progn
165-
(with-current-buffer errbuff
166-
(setq buffer-read-only t)
167-
(ansi-color-apply-on-region (point-min) (point-max))
168-
(special-mode))
169-
170-
(if is-interactive
171-
(display-buffer errbuff)
172-
(error "elixir-format failed: see %s" (buffer-name errbuff)))))
173-
174-
(delete-file tmpfile)
175-
(kill-buffer outbuff)))))
202+
(with-current-buffer errbuff
203+
(setq buffer-read-only t)
204+
(ansi-color-apply-on-region (point-min) (point-max))
205+
(special-mode))
206+
207+
(if is-interactive
208+
(display-buffer errbuff)
209+
(error "elixir-format failed: see %s" (buffer-name errbuff)))))
210+
211+
(delete-file tmpfile)
212+
(kill-buffer outbuff))))))
176213

177214
(provide 'elixir-format)
178215

0 commit comments

Comments
 (0)