Skip to content

Commit 4911345

Browse files
committed
try to reduce complexity of elixir-format
Function with arity 1 is now called with an argument reindent tests
1 parent 415e5e5 commit 4911345

File tree

2 files changed

+105
-98
lines changed

2 files changed

+105
-98
lines changed

elixir-format.el

Lines changed: 80 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,85 @@ Customize-variable: elixir-format-mix-path"
6262
:group 'elixir-format)
6363

6464

65-
;;; Code
65+
;;; Code:
66+
67+
(defun elixir-format--errbuff ()
68+
(get-buffer-create "*elixir-format-errors*"))
69+
70+
(defun elixir-format--outbuff ()
71+
(get-buffer-create "*elixir-format-output*"))
72+
73+
(defun elixir-format--clean-output-buffers ()
74+
(with-current-buffer (elixir-format--outbuff)
75+
(erase-buffer))
76+
77+
(with-current-buffer (elixir-format--errbuff)
78+
(setq buffer-read-only nil)
79+
(erase-buffer)))
80+
81+
;;;###autoload
82+
(defun elixir-format (&optional called-interactively-p)
83+
(interactive "p")
84+
85+
(let ((tmpfile (make-temp-file "elixir-format" nil ".ex"))
86+
(our-elixir-format-arguments (list elixir-format-mix-path "format"))
87+
(output nil))
88+
89+
(if (elixir-format--elixir-and-mix-path-not-set-p)
90+
(elixir-format--display-missing-configuration-error called-interactively-p)
91+
(unwind-protect
92+
(save-restriction
93+
(elixir-format--clean-output-buffers)
94+
(elixir-format--run-format called-interactively-p tmpfile))))))
95+
96+
(defun elixir-format--elixir-and-mix-path-not-set-p ()
97+
(or (= (length elixir-format-mix-path) 0)
98+
(= (length elixir-format-elixir-path) 0)))
99+
100+
(defun elixir-format--display-missing-configuration-error (called-interactively-p)
101+
(with-current-buffer (elixir-format--errbuff)
102+
(setq buffer-read-only nil)
103+
(erase-buffer)
104+
(insert "Elixir or mix binary path not set, please check the documentation for `elixir-format-elixir-path` and `elixir-format-mix-path` with `C-h v <VARNAME> RET` to set the variables appropriately")
105+
(setq buffer-read-only t)
106+
(ansi-color-apply-on-region (point-min) (point-max))
107+
(special-mode)
108+
(if called-interactively-p
109+
(display-buffer elixir-format--errbuff)
110+
(error "elixir-format-configuration-missing: see %s" (buffer-name (elixir-format--errbuff))))))
111+
112+
(defun elixir-format--run-format (called-interactively-p tmpfile)
113+
(write-region nil nil tmpfile)
114+
115+
(run-hooks 'elixir-format-hook)
116+
117+
(when elixir-format-arguments
118+
(setq our-elixir-format-arguments (append our-elixir-format-arguments elixir-format-arguments)))
119+
(setq our-elixir-format-arguments (append our-elixir-format-arguments (list tmpfile)))
120+
121+
(if (zerop (apply #'call-process elixir-format-elixir-path nil (elixir-format--errbuff) nil our-elixir-format-arguments))
122+
(elixir-format--call-format-command tmpfile)
123+
(elixir-format--failed-to-format called-interactively-p))
124+
125+
(delete-file tmpfile)
126+
(kill-buffer (elixir-format--outbuff)))
127+
128+
(defun elixir-format--call-format-command (tmpfile)
129+
(if (zerop (call-process-region (point-min) (point-max) "diff" nil (elixir-format--outbuff) nil "-n" "-" tmpfile))
130+
(message "File is already formatted")
131+
(elixir-format--apply-rcs-patch (elixir-format--outbuff))
132+
(message "elixir-format format applied"))
133+
(kill-buffer (elixir-format--errbuff)))
134+
135+
(defun elixir-format--failed-to-format (called-interactively-p)
136+
(with-current-buffer (elixir-format--errbuff)
137+
(setq buffer-read-only t)
138+
(ansi-color-apply-on-region (point-min) (point-max))
139+
(special-mode))
140+
141+
(if called-interactively-p
142+
(display-buffer (elixir-format--errbuff))
143+
(error "elixir-format failed: see %s" (buffer-name (elixir-format--errbuff)))))
66144

67145
(defun elixir-format--goto-line (line)
68146
(goto-char (point-min))
@@ -138,78 +216,7 @@ Shamelessly stolen from go-mode (https://github.com/dominikh/go-mode.el)"
138216
(cl-incf line-offset len)
139217
(elixir-format--delete-whole-line len)))
140218
(t
141-
(error "Invalid rcs patch or internal error in elixir-format--apply-rcs-patch"))))))))
142-
)
143-
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-
162-
;;;###autoload
163-
(defun elixir-format (&optional is-interactive)
164-
(interactive "p")
165-
166-
(let ((outbuff (get-buffer-create "*elixir-format-output*"))
167-
(errbuff (get-buffer-create "*elixir-format-errors*"))
168-
(tmpfile (make-temp-file "elixir-format" nil ".ex"))
169-
(our-elixir-format-arguments (list elixir-format-mix-path "format"))
170-
(output nil))
171-
172-
(unwind-protect
173-
(save-restriction
174-
(with-current-buffer outbuff
175-
(erase-buffer))
176-
177-
(with-current-buffer errbuff
178-
(setq buffer-read-only nil)
179-
(erase-buffer))
180-
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)
185-
186-
(run-mode-hooks 'elixir-format-hook)
187-
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))
200-
201-
(progn
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))))))
219+
(error "Invalid rcs patch or internal error in elixir-format--apply-rcs-patch")))))))))
213220

214221
(provide 'elixir-format)
215222

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)