From 04366a90dfc1063c4a22ea2e4ccaa48303a457e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ellis=20Keny=C5=91?= Date: Sun, 21 Aug 2022 23:21:21 +0100 Subject: [PATCH] Only process when there is output to return (#116) * fix: only process when there is output to return scalafmt only returns stdout when there are errors, this could also help in other cases where a formatter returns incorrectly. Ideally, this would also be improved by basic error checking heuristics to work out if we need to update the buffer. * Update changelog Co-authored-by: Radon Rosborough Co-authored-by: Radon Rosborough --- CHANGELOG.md | 5 +++++ apheleia.el | 15 ++++++++------- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e81d6e7..bcf1458 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,10 @@ The format is based on [Keep a Changelog]. fixes issues with formatters that behave differently when receiving input on stdin versus being run on a tty ([#119]). +### Bugs fixed +* When a formatter has a bug and fails to return anything on stdout + (e.g. scalafmt), do not erase the buffer ([#116]). + ### Formatters * [bean-format](https://github.com/beancount/beancount) for Beancount ([#101]). @@ -21,6 +25,7 @@ The format is based on [Keep a Changelog]. [#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 +[#116]: https://github.com/radian-software/apheleia/pull/116 [#119]: https://github.com/radian-software/apheleia/pull/119 ## 3.0 (released 2022-06-01) diff --git a/apheleia.el b/apheleia.el index 37d6c89..332499e 100644 --- a/apheleia.el +++ b/apheleia.el @@ -1034,13 +1034,14 @@ function: %s" command))) buffer remote (lambda (stdout) - (if (cdr formatters) - ;; Forward current stdout to remaining formatters, passing along - ;; the current callback and using the current formatters output - ;; as stdin. - (apheleia--run-formatters - (cdr formatters) buffer remote callback stdout) - (funcall callback stdout))) + (unless (string-empty-p (with-current-buffer stdout (buffer-string))) + (if (cdr formatters) + ;; Forward current stdout to remaining formatters, passing along + ;; the current callback and using the current formatters output + ;; as stdin. + (apheleia--run-formatters + (cdr formatters) buffer remote callback stdout) + (funcall callback stdout)))) stdin (car formatters))))