Skip to content

Commit 9a38968

Browse files
committed
Merge branch 'anildigital-master'
* anildigital-master: remove variables for elixir and mix paths try to reduce complexity of elixir-format use run-mode-hooks as recommended by emacs Better upgrade/onboarding experience
2 parents 028fe6c + c42fcad commit 9a38968

File tree

3 files changed

+135
-149
lines changed

3 files changed

+135
-149
lines changed

README.md

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -151,41 +151,6 @@ You can use [web-mode.el](http://web-mode.org) to edit elixir templates (eex fil
151151

152152
## Elixir Format
153153

154-
### Setup of elixir-format
155-
Customize the elixir and mix paths
156-
157-
In Emacs, run following command to customize option
158-
``` elisp
159-
M-x customize-option
160-
161-
Customize-variable: elixir-format-elixir-path
162-
```
163-
and set your elixir executable path there. After that run:
164-
``` elisp
165-
M-x customize-option
166-
167-
Customize-variable: elixir-format-mix-path
168-
```
169-
and set your mix executable path there.
170-
171-
Your machine's elixir and mix executable paths can be found with `which` command as shown below
172-
173-
``` shell
174-
$ which elixir
175-
/usr/local/bin/elixir
176-
177-
$ which mix
178-
/usr/local/bin/mix
179-
```
180-
Alternavively you can define variables as below
181-
182-
``` elisp
183-
(setq elixir-format-elixir-path "/usr/local/bin/elixir")
184-
(setq elixir-format-mix-path "/usr/local/bin/mix")
185-
```
186-
187-
### Use it
188-
189154
``` elisp
190155
M-x elixir-format
191156
```
@@ -215,7 +180,8 @@ or you set `elixir-format-arguments` in a hook like this:
215180
(setq elixir-format-arguments nil))))
216181
```
217182

218-
In this example we use [Projectile](https://github.com/bbatsov/projectile) to get the project root and set `elixir-format-arguments` accordingly.
183+
In this example we use [Projectile](https://github.com/bbatsov/projectile) to determine if we are in a project and then set `elixir-format-arguments` accordingly.
184+
Please note that this code snippet may cause unhappiness if there is no `.formatter.exs` file available.
219185

220186

221187
## History

elixir-format.el

Lines changed: 108 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,6 @@
2525

2626
(require 'ansi-color)
2727

28-
(defcustom elixir-format-elixir-path "elixir"
29-
"Path to the Elixir interpreter."
30-
:type 'string
31-
:group 'elixir-format)
32-
33-
(defcustom elixir-format-mix-path "/usr/bin/mix"
34-
"Path to the 'mix' executable."
35-
:type 'string
36-
:group 'elixir-format)
37-
3828
(defcustom elixir-format-arguments nil
3929
"Additional arguments to 'mix format'"
4030
:type '(repeat string)
@@ -46,39 +36,87 @@
4636
:group 'elixir-format)
4737

4838

49-
;;; Code
39+
;;; Code:
5040

51-
(defun elixir-format--goto-line (line)
52-
(goto-char (point-min))
53-
(forward-line (1- line)))
41+
(defun elixir-format--errbuff ()
42+
(get-buffer-create "*elixir-format-errors*"))
5443

55-
(defun elixir-format--delete-whole-line (&optional arg)
56-
"Delete the current line without putting it in the `kill-ring'.
57-
Derived from function `kill-whole-line'. ARG is defined as for that
58-
function.
44+
(defun elixir-format--outbuff ()
45+
(get-buffer-create "*elixir-format-output*"))
5946

60-
Shamelessly stolen from go-mode (https://github.com/dominikh/go-mode.el)"
61-
(setq arg (or arg 1))
62-
(if (and (> arg 0)
63-
(eobp)
64-
(save-excursion (forward-visible-line 0) (eobp)))
65-
(signal 'end-of-buffer nil))
66-
(if (and (< arg 0)
67-
(bobp)
68-
(save-excursion (end-of-visible-line) (bobp)))
69-
(signal 'beginning-of-buffer nil))
70-
(cond ((zerop arg)
71-
(delete-region (progn (forward-visible-line 0) (point))
72-
(progn (end-of-visible-line) (point))))
73-
((< arg 0)
74-
(delete-region (progn (end-of-visible-line) (point))
75-
(progn (forward-visible-line (1+ arg))
76-
(unless (bobp)
77-
(backward-char))
78-
(point))))
79-
(t
80-
(delete-region (progn (forward-visible-line 0) (point))
81-
(progn (forward-visible-line arg) (point))))))
47+
(defun elixir-format--elixir-executable ()
48+
(executable-find "elixir"))
49+
50+
(defun elixir-format--mix-executable ()
51+
(executable-find "mix"))
52+
53+
;;;###autoload
54+
(defun elixir-format (&optional called-interactively-p)
55+
(interactive "p")
56+
(if (not (elixir-format--elixir-and-mix-path-set-p))
57+
(elixir-format--display-missing-executables-error called-interactively-p)
58+
(unwind-protect
59+
(save-restriction
60+
(elixir-format--clean-output-buffers)
61+
(elixir-format--run-format called-interactively-p)))))
62+
63+
(defun elixir-format--elixir-and-mix-path-set-p ()
64+
(and (elixir-format--elixir-executable)
65+
(elixir-format--mix-executable)))
66+
67+
(defun elixir-format--display-missing-executables-error (called-interactively-p)
68+
(with-current-buffer (elixir-format--errbuff)
69+
(setq buffer-read-only nil)
70+
(erase-buffer)
71+
(insert "Emacs is unable to find the executables for elixir and/or mix. Either they are not installed on your system or emacs' PATH is not as wide as it needs to be. The latter is most likely to happen on OSX, in which case the simplest answer may be to add the exec-path-from-shell package to your configuration.")
72+
(setq buffer-read-only t)
73+
(ansi-color-apply-on-region (point-min) (point-max))
74+
(special-mode)
75+
(if called-interactively-p
76+
(display-buffer (elixir-format--errbuff))
77+
(error "Elixir Format error see %s" (elixir-format--errbuff)))))
78+
79+
(defun elixir-format--clean-output-buffers ()
80+
(with-current-buffer (elixir-format--outbuff)
81+
(erase-buffer))
82+
83+
(with-current-buffer (elixir-format--errbuff)
84+
(setq buffer-read-only nil)
85+
(erase-buffer)))
86+
87+
(defun elixir-format--run-format (called-interactively-p)
88+
(let ((tmpfile (make-temp-file "elixir-format" nil ".ex"))
89+
(our-elixir-format-arguments (list (elixir-format--mix-executable) "format")))
90+
91+
(write-region nil nil tmpfile)
92+
(run-hooks 'elixir-format-hook)
93+
94+
(when elixir-format-arguments
95+
(setq our-elixir-format-arguments (append our-elixir-format-arguments elixir-format-arguments)))
96+
(setq our-elixir-format-arguments (append our-elixir-format-arguments (list tmpfile)))
97+
98+
(if (zerop (apply #'call-process (elixir-format--elixir-executable) nil (elixir-format--errbuff) nil our-elixir-format-arguments))
99+
(elixir-format--call-format-command tmpfile)
100+
(elixir-format--failed-to-format called-interactively-p))
101+
(delete-file tmpfile)
102+
(kill-buffer (elixir-format--outbuff))))
103+
104+
(defun elixir-format--call-format-command (tmpfile)
105+
(if (zerop (call-process-region (point-min) (point-max) "diff" nil (elixir-format--outbuff) nil "-n" "-" tmpfile))
106+
(message "File is already formatted")
107+
(elixir-format--apply-rcs-patch (elixir-format--outbuff))
108+
(message "elixir-format format applied"))
109+
(kill-buffer (elixir-format--errbuff)))
110+
111+
(defun elixir-format--failed-to-format (called-interactively-p)
112+
(with-current-buffer (elixir-format--errbuff)
113+
(setq buffer-read-only t)
114+
(ansi-color-apply-on-region (point-min) (point-max))
115+
(special-mode))
116+
117+
(if called-interactively-p
118+
(display-buffer (elixir-format--errbuff))
119+
(error "elixir-format failed: see %s" (buffer-name (elixir-format--errbuff)))))
82120

83121
(defun elixir-format--apply-rcs-patch (patch-buffer)
84122
"Apply an RCS-formatted diff from PATCH-BUFFER to the current buffer.
@@ -122,57 +160,39 @@ Shamelessly stolen from go-mode (https://github.com/dominikh/go-mode.el)"
122160
(cl-incf line-offset len)
123161
(elixir-format--delete-whole-line len)))
124162
(t
125-
(error "Invalid rcs patch or internal error in elixir-format--apply-rcs-patch"))))))))
126-
)
163+
(error "Invalid rcs patch or internal error in elixir-format--apply-rcs-patch")))))))))
127164

128-
;;;###autoload
129-
(defun elixir-format (&optional is-interactive)
130-
(interactive "p")
165+
(defun elixir-format--goto-line (line)
166+
(goto-char (point-min))
167+
(forward-line (1- line)))
131168

132-
(let ((outbuff (get-buffer-create "*elixir-format-output*"))
133-
(errbuff (get-buffer-create "*elixir-format-errors*"))
134-
(tmpfile (make-temp-file "elixir-format" nil ".ex"))
135-
(our-elixir-format-arguments (list elixir-format-mix-path "format"))
136-
(output nil))
169+
(defun elixir-format--delete-whole-line (&optional arg)
170+
"Delete the current line without putting it in the `kill-ring'.
171+
Derived from function `kill-whole-line'. ARG is defined as for that
172+
function.
137173
138-
(unwind-protect
139-
(save-restriction
140-
(with-current-buffer outbuff
141-
(erase-buffer))
142-
143-
(with-current-buffer errbuff
144-
(setq buffer-read-only nil)
145-
(erase-buffer))
146-
147-
(write-region nil nil tmpfile)
148-
149-
(run-hooks 'elixir-format-hook)
150-
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)))
154-
155-
(if (zerop (apply #'call-process elixir-format-elixir-path nil errbuff nil our-elixir-format-arguments))
156-
(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)))))
174+
Shamelessly stolen from go-mode (https://github.com/dominikh/go-mode.el)"
175+
(setq arg (or arg 1))
176+
(if (and (> arg 0)
177+
(eobp)
178+
(save-excursion (forward-visible-line 0) (eobp)))
179+
(signal 'end-of-buffer nil))
180+
(if (and (< arg 0)
181+
(bobp)
182+
(save-excursion (end-of-visible-line) (bobp)))
183+
(signal 'beginning-of-buffer nil))
184+
(cond ((zerop arg)
185+
(delete-region (progn (forward-visible-line 0) (point))
186+
(progn (end-of-visible-line) (point))))
187+
((< arg 0)
188+
(delete-region (progn (end-of-visible-line) (point))
189+
(progn (forward-visible-line (1+ arg))
190+
(unless (bobp)
191+
(backward-char))
192+
(point))))
193+
(t
194+
(delete-region (progn (forward-visible-line 0) (point))
195+
(progn (forward-visible-line arg) (point))))))
176196

177197
(provide 'elixir-format)
178198

test/elixir-format-test.el

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,43 @@
22

33
;;; Code:
44

5-
(ert-deftest indents-a-buffer ()
5+
(ert-deftest elixir-format-indents-a-buffer ()
66
(when elixir-formatter-supported
7-
(ert-with-test-buffer (:name "(Expected)indents-a-buffer")
8-
(insert elixir-format-test-example)
9-
(elixir-format)
10-
(should (equal (buffer-string) elixir-format-formatted-test-example)))))
7+
(ert-with-test-buffer (:name "(Expected)indents-a-buffer")
8+
(insert elixir-format-test-example)
9+
(elixir-format)
10+
(should (equal (buffer-string) elixir-format-formatted-test-example)))))
1111

12-
(ert-deftest indents-a-buffer-and-undoes-changes ()
12+
(ert-deftest elixir-format-indents-a-buffer-and-undoes-changes ()
1313
(when elixir-formatter-supported
14-
(ert-with-test-buffer ()
15-
(buffer-enable-undo)
16-
(setq buffer-undo-list nil)
14+
(ert-with-test-buffer ()
15+
(buffer-enable-undo)
16+
(setq buffer-undo-list nil)
1717

18-
(insert elixir-format-test-example)
18+
(insert elixir-format-test-example)
1919

20-
(undo-boundary)
21-
(elixir-format)
20+
(undo-boundary)
21+
(elixir-format)
2222

23-
(should (equal (buffer-string) elixir-format-formatted-test-example))
24-
(undo 0)
25-
(should (equal (buffer-string) elixir-format-test-example)))))
23+
(should (equal (buffer-string) elixir-format-formatted-test-example))
24+
(undo 0)
25+
(should (equal (buffer-string) elixir-format-test-example)))))
2626

2727
(ert-deftest elixir-format-should-run-hook-before-formatting ()
2828
(when elixir-formatter-supported
29-
(ert-with-test-buffer ()
30-
(let ((has-been-run nil))
31-
(insert elixir-format-test-example)
32-
(add-hook 'elixir-format-hook (lambda () (setq has-been-run t)))
33-
(elixir-format)
34-
(should (equal has-been-run t))))))
29+
(ert-with-test-buffer ()
30+
(let ((has-been-run nil))
31+
(insert elixir-format-test-example)
32+
(add-hook 'elixir-format-hook (lambda () (setq has-been-run t)))
33+
(elixir-format)
34+
(should (equal has-been-run t))))))
3535

3636
(ert-deftest elixir-format-should-message-on-error ()
3737
(when elixir-formatter-supported
38-
(ert-with-test-buffer ()
39-
(insert elixir-format-wrong-test-example)
40-
(should-error
41-
(elixir-format)))))
38+
(ert-with-test-buffer ()
39+
(insert elixir-format-wrong-test-example)
40+
(should-error
41+
(elixir-format)))))
4242

4343
(provide 'elixir-format-test)
4444

0 commit comments

Comments
 (0)