Fix bug running Prettier on modified buffer

This commit is contained in:
Radon Rosborough 2019-12-31 10:16:32 -07:00
parent 61f9335756
commit 26ddaf3597
2 changed files with 69 additions and 53 deletions

View file

@ -3,6 +3,15 @@
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog].
## Unreleased
### Bugs fixed
* Previously, weirdness could happen if manually running Prettier via
`M-x apheleia-format-buffer` on a buffer which was modified from
what was written to disk. Now we simply abort running a command that
uses the `file` keyword if the buffer is modified, since it will not
produce correct results. This should not affect normal usage of
Apheleia.
## 1.0 (released 2019-09-20)
### Added
* Package `apheleia`

View file

@ -362,7 +362,11 @@ as its sole argument."
"Run a code formatter on the current buffer.
The formatter is specified by COMMAND, a list of strings or
symbols (see `apheleia-format-buffer'). Invoke CALLBACK with one
argument, a buffer containing the output of the formatter."
argument, a buffer containing the output of the formatter.
If COMMAND uses the symbol `file' and the current buffer is
modified from what is written to disk, then don't do anything."
(cl-block nil
(let ((input-fname nil)
(output-fname nil)
(npx nil))
@ -373,7 +377,8 @@ argument, a buffer containing the output of the formatter."
(error "Command cannot start with %S" (car command)))
(when npx
(when-let ((project-dir
(locate-dominating-file default-directory "node_modules")))
(locate-dominating-file
default-directory "node_modules")))
(let ((binary
(expand-file-name
(car command)
@ -387,7 +392,9 @@ argument, a buffer containing the output of the formatter."
(when (memq 'file command)
(setq command (mapcar (lambda (arg)
(if (eq arg 'file)
buffer-file-name
(prog1 buffer-file-name
(when (buffer-modified-p)
(cl-return)))
arg))
command)))
(when (memq 'input command)
@ -417,7 +424,7 @@ argument, a buffer containing the output of the formatter."
(when output-fname
(erase-buffer)
(insert-file-contents-literally output-fname))
(funcall callback stdout)))))
(funcall callback stdout))))))
(defcustom apheleia-formatters
'((black . ("black" "-"))