aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnders Kaseorg <andersk@mit.edu>2022-08-14 16:28:01 -0700
committerGitHub <noreply@github.com>2022-08-14 16:28:01 -0700
commit0c5e40e8bc0e837b778452b3f061ff85e39a967a (patch)
tree232cc9dd07a349e5af1b960502edb41e17a596ae
parent12804a50206c708570104637472411288d6133f5 (diff)
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>
-rw-r--r--CHANGELOG.md4
-rw-r--r--apheleia.el23
2 files changed, 13 insertions, 14 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 067d6c6..9212f50 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -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
diff --git a/apheleia.el b/apheleia.el
index ce188f7..70609cd 100644
--- a/apheleia.el
+++ b/apheleia.el
@@ -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"))