Allow formatter using 'filepath on a modified buffer (#110)

* Run bean-format on stdin (fixing incorrect 'filepath use)

'filepath is supposed to indicate that the formatter doesn’t actually
read from the named file, meaning it’s safe to use on a modified
buffer.

Signed-off-by: Anders Kaseorg <andersk@mit.edu>

* Allow formatter using 'filepath on a modified buffer

Fixes #109.

Signed-off-by: Anders Kaseorg <andersk@mit.edu>

* Add links to changelog

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
Co-authored-by: Radon Rosborough <radon.neon@gmail.com>
This commit is contained in:
Anders Kaseorg 2022-08-14 16:28:01 -07:00 committed by GitHub
parent 12804a5020
commit 0c5e40e8bc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 14 deletions

View file

@ -6,6 +6,8 @@ The format is based on [Keep a Changelog].
## Unreleased
### Enhancements
* shfmt uses 4 spaces instead of tabs by default.
* Formatters using `'filepath` (OCamlFormat and Prettier) are no
longer prevented from running on a modified buffer ([#109], [#110]).
### Formatters
* [bean-format](https://github.com/beancount/beancount) for Beancount
@ -14,6 +16,8 @@ The format is based on [Keep a Changelog].
[#101]: https://github.com/radian-software/apheleia/pull/101
[#105]: https://github.com/radian-software/apheleia/pull/105
[#109]: https://github.com/radian-software/apheleia/issues/109
[#110]: https://github.com/radian-software/apheleia/pull/110
## 3.0 (released 2022-06-01)
### Breaking changes

View file

@ -767,26 +767,21 @@ cmd is to be run."
it's first in the sequence"))
(unless remote-match
(error "Formatter uses `file' but process will run on different \
machine from the machine file is available on")))
machine from the machine file is available on"))
(setq stdin nil)
;; If `buffer-file-name' is nil then there is no backing
;; file, so `buffer-modified-p' should be ignored (it always
;; returns non-nil).
(when (and (buffer-modified-p) buffer-file-name)
(cl-return)))
;; We always strip out the remote-path prefix for file/filepath.
(let ((file-name (apheleia--strip-remote
(or buffer-file-name
(concat default-directory
(apheleia--safe-buffer-name))))))
(setq command (mapcar (lambda (arg)
(when (eq arg 'file)
(setq stdin nil))
(if (memq arg '(file filepath))
(prog1 file-name
;; If `buffer-file-name' is
;; nil then there is no
;; backing file, so
;; `buffer-modified-p' should
;; be ignored (it always
;; returns non-nil).
(when (and (buffer-modified-p)
buffer-file-name)
(cl-return)))
file-name
arg))
command))))
(when (or (memq 'input command) (memq 'inplace command))
@ -919,7 +914,7 @@ being run, for diagnostic purposes."
(apply-partially #'kill-buffer scratch)))))
(defcustom apheleia-formatters
'((bean-format . ("bean-format" filepath))
'((bean-format . ("bean-format"))
(black . ("black" "-"))
(brittany . ("brittany"))
(clang-format . ("clang-format"))