elixir-editors / elixir-editors/emacs-elixir
Recommended elixir-format hook hides formatting errors
- Dominant language
- Emacs Lisp
- Stars
- 454
- Forks
- 93
- PR merge metrics
- No merged PRs in 30d
Description
**Issue:**
As per the [README](https://github.com/elixir-editors/emacs-elixir/blob/master/README.md):
``` elisp
;; 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](https://github.com/elixir-editors/emacs-elixir/blob/master/elixir-format.el#L119) which can be seen in the `*Messages*` buffer, but:
• In the minibuffer, the error message quickly goes away when the regular "Wrote ``" 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](https://emacs.stackexchange.com/a/20771/12694) suggests using `write-file-functions` or `write-contents-functions` as hooks instead.
I've tried the following code:
``` elisp
(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:**
1. `elixir-format` could return `nil` instead of a message, which would allow the `write-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.
2. 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, unless `nil` is returned as suggested above.)
3. The `*elixir-format-errors*` buffer could open even if `elixir-format` was called non-interactively.
Contributor guide
Assessment
This issue has not been assessed yet.