Description
Issue:
As per the README:
;; Create a buffer-local hook to run elixir-format on save, only when we enable elixir-mode.
(add-hook 'elixir-mode-hook
(lambda () (add-hook 'before-save-hook 'elixir-format nil t)))
However, when the formatter throws an error due to bad syntax, the before-save-hook
reports the error which can be seen in the *Messages*
buffer, but:
• In the minibuffer, the error message quickly goes away when the regular "Wrote <file>
" message is displayed.
• The *elixir-format-errors*
buffer does not pop up as when elixir-format
is called interactively.
Software Versions:
- Darwin Kernel Version 17.7.0: Thu Jun 21 22:53:14 PDT 2018; root:xnu-4570.71.2~1/RELEASE_X86_64 x86_64
- GNU Emacs 26.1 (build 1, x86_64-apple-darwin14.5.0, NS appkit-1348.17 Version 10.10.5 (Build 14F2511)) of 2018-05-30
- Elixir-Mode version: 2.3.1 (package: 20180711.1245)
What I've tried to solve this myself:
This StackOverflow answer suggests using write-file-functions
or write-contents-functions
as hooks instead.
I've tried the following code:
(add-hook 'elixir-mode-hook
(lambda ()
(add-hook 'write-file-functions 'elixir-format)))
And when saving a file with syntactical errors, the file is not saved and a short error appears in the Emacs minibuffer (with details written to a separate buffer):
elixir-format failed: see *elixir-format-errors*
However, that actually doesn't work with files that are already formatted, as elixir-format
returns (message "File is already formatted")
preventing the save.
Possible Solutions:
elixir-format
could returnnil
instead of a message, which would allow thewrite-file-functions
hook to work. Bear in mind, using this hook disallows saving which might annoy some people, but is how e.g. VS Code formatting works in my experience.- Somehow show the error message in the minibuffer after the file is written? Should the
after-save-hook
be used instead? (In which case you'd still see "File is already formatted" every time you save a clean file—again, unlessnil
is returned as suggested above.) - The
*elixir-format-errors*
buffer could open even ifelixir-format
was called non-interactively.