Skip to content

Recommended elixir-format hook hides formatting errors #427

Open
@felideon

Description

@felideon

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions