diff options
Diffstat (limited to 'modules/prog-webdev.el')
| -rw-r--r-- | modules/prog-webdev.el | 42 |
1 files changed, 33 insertions, 9 deletions
diff --git a/modules/prog-webdev.el b/modules/prog-webdev.el index 74cff732..8832446a 100644 --- a/modules/prog-webdev.el +++ b/modules/prog-webdev.el @@ -75,21 +75,45 @@ Install with: sudo pacman -S prettier") (executable-find ts-language-server-path)) (lsp-deferred))) -(defun cj/--webdev-format-command (file) - "Return the prettier command that formats FILE's contents on stdin." - (format "prettier --stdin-filepath %s" (shell-quote-argument file))) +(defun cj/--webdev-format-args (file) + "Return the prettier argv list that formats FILE's contents on stdin. +No shell quoting is needed: the args are passed to prettier directly +via `call-process-region', so FILE can contain spaces or shell +metacharacters without risk." + (list "--stdin-filepath" file)) + +(defun cj/--webdev-format-region (program &rest args) + "Replace the buffer with PROGRAM ARGS run over its contents, via argv. +Runs PROGRAM (with ARGS) on the whole buffer through +`call-process-region' — no shell, so no quoting or word-splitting. +The buffer is replaced only when PROGRAM exits zero; on a non-zero +exit the buffer is left untouched and an error is signalled with +the program's stderr text. Point is preserved as closely as the +reformatted size allows. Returns t on success." + (let* ((point (point)) + (src (current-buffer)) + (out (generate-new-buffer " *webdev-format-out*")) + (status (apply #'call-process-region + (point-min) (point-max) program + nil out nil args))) + (unwind-protect + (if (and (integerp status) (zerop status)) + (progn + (with-current-buffer src + (replace-buffer-contents out) + (goto-char (min point (point-max)))) + t) + (user-error "%s failed: %s" program + (string-trim (with-current-buffer out (buffer-string))))) + (kill-buffer out)))) (defun cj/webdev-format-buffer () "Format the current buffer with prettier. Detects the file type automatically from the filename." (interactive) (if (executable-find prettier-path) - (let ((point (point))) - (shell-command-on-region (point-min) (point-max) - (cj/--webdev-format-command - (or buffer-file-name "file.ts")) - nil t) - (goto-char (min point (point-max)))) + (apply #'cj/--webdev-format-region prettier-path + (cj/--webdev-format-args (or buffer-file-name "file.ts"))) (user-error "prettier not found; install with: sudo pacman -S prettier"))) (defun cj/webdev-keybindings () |
