Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recommended elixir-format hook hides formatting errors #427

Open
felideon opened this issue Sep 14, 2018 · 4 comments
Open

Recommended elixir-format hook hides formatting errors #427

felideon opened this issue Sep 14, 2018 · 4 comments

Comments

@felideon
Copy link

felideon commented Sep 14, 2018

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:

  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.
@felideon felideon changed the title Recommended elixir-format hook silently hides formatting errors Recommended elixir-format hook hides formatting errors Sep 14, 2018
@felideon
Copy link
Author

So I checked to see how dart-mode.el handles this, and they seem to have adapted what go-mode.el does, which is actually customizable:

https://github.com/bradyt/dart-mode/blob/master/dart-mode.el#L1710-L1721

Note the warning in the docstring about using the echo area. Other pertinent code:

https://github.com/bradyt/dart-mode/blob/master/dart-mode.el#L1770-L1774
https://github.com/bradyt/dart-mode/blob/master/dart-mode.el#L1831-L1847

@Trevoke
Copy link
Contributor

Trevoke commented Nov 7, 2018

Thanks for doing all this investigative work!

I'm comfortable enough with the solution used by go-mode and dart-mode. Are you comfortable enough with elisp to submit a pull request with tests for this?

@felideon
Copy link
Author

felideon commented Nov 8, 2018

@Trevoke Not necessarily but I could potentially cargo cult it and get it working. :)

Tests would probably go in test/elixir-format-test.el, correct?

@Trevoke
Copy link
Contributor

Trevoke commented Nov 8, 2018 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants