aboutsummaryrefslogtreecommitdiff
path: root/apheleia-formatters.el
diff options
context:
space:
mode:
authorFrédéric Giquel <frederic.giquel@laposte.net>2025-12-23 20:24:34 +0100
committerGitHub <noreply@github.com>2025-12-23 11:24:34 -0800
commit426616cf1799dbce89800ae2fe9f155311436503 (patch)
tree897ca1ec66b9d8d1ffbbea33aaf9ca3e2d1a9142 /apheleia-formatters.el
parent8a6b75008d0cb5c5dabb08281ad2393ba4341d1c (diff)
apheleia-npx: return silently when formatter is missing (#383)
Behavior of `aphaleia-npx` currently differs from other formatters by triggering an error when formatter is missing (causing a message to be displayed in minibuffer and an error buffer to be created). This PR adds a test to check if command is on user's PATH before executing it in order to make `aphaleia-npx` silently ends when the formatter command is not found at project and system level.
Diffstat (limited to 'apheleia-formatters.el')
-rw-r--r--apheleia-formatters.el28
1 files changed, 25 insertions, 3 deletions
diff --git a/apheleia-formatters.el b/apheleia-formatters.el
index 9bc39f8..53e7387 100644
--- a/apheleia-formatters.el
+++ b/apheleia-formatters.el
@@ -467,6 +467,23 @@ in the buffer."
'mhtml-submode)
#'mhtml-mode))
+;; Expected exit code and stderr output for script when real
+;; formatter is not available
+(defvar apheleia-script--formatter-not-available
+ '(:exit-code 100 :stderr "formatter_not_available"))
+
+(defun apheleia-script--formatter-not-available-p (ctx stderr)
+ "Check if script reports that no formatter has been found.
+CTX is a formatter process context.
+STDERR is the stderr output of process."
+ (and (eq (apheleia-formatter--exit-status ctx)
+ (plist-get apheleia-script--formatter-not-available :exit-code))
+ (string= stderr
+ (concat
+ (apheleia-formatter--arg1 ctx) ":"
+ (plist-get apheleia-script--formatter-not-available
+ :stderr)))))
+
;;;###autoload
(defcustom apheleia-mode-predicates '(apheleia-mhtml-mode-predicate)
"List of predicates that check for sneaky major modes.
@@ -747,9 +764,14 @@ its exit status is 0."
proc-exit-status)
(let ((exit-ok (and
(not proc-interrupted)
- (funcall
- (or exit-status #'zerop)
- (apheleia-formatter--exit-status ctx)))))
+ (or
+ (funcall
+ (or exit-status #'zerop)
+ (apheleia-formatter--exit-status ctx))
+ (apheleia-script--formatter-not-available-p
+ ctx
+ (with-current-buffer stderr
+ (string-trim (buffer-string))))))))
;; Append standard-error from current formatter
;; to log buffer when
;; `apheleia-log-only-errors' is nil or the