emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/nix-mode 7556c032dc 421/500: Use the located nixfmt binary


From: ELPA Syncer
Subject: [nongnu] elpa/nix-mode 7556c032dc 421/500: Use the located nixfmt binary for formatting.
Date: Sat, 29 Jan 2022 08:27:51 -0500 (EST)

branch: elpa/nix-mode
commit 7556c032dc0c84a7d3b982169205102dcbf0dc42
Author: Zachary Newman <z@znewman.net>
Commit: Zachary Newman <z@znewman.net>

    Use the located nixfmt binary for formatting.
    
    Allows use with `nixfmt` installed using direnv ([envrc-mode]).
    
    Before:
    
    1. `nix-format-buffer` checks for `nixfmt`: `(executable-find 
nix-nixfmt-bin)`
    2. If it's missing, error. Otherwise, disregard the path to `nixfmt`
       that we just found.
    3. Open a new buffer (if using `envrc-mode`, with a totally different
       `$PATH`) and try to run `nix-nixfmt-bin`.
    
    If you had `nixfmt` installed in your direnv but not globally, this
    gives an error: "Searching for program: No such file or directory, nixfmt".
    
    Now, we use the path that we just found and `call-process-region` on that.
    
    [envrc-mode]: https://github.com/purcell/envrc
---
 nix-format.el | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/nix-format.el b/nix-format.el
index f43dc4d908..1f97e8a086 100644
--- a/nix-format.el
+++ b/nix-format.el
@@ -13,24 +13,29 @@
   :group 'nix
   :type 'string)
 
-(defun nix--format-call (buf)
+(defun nix--format-call (buf nixfmt-bin)
   "Format BUF using nixfmt."
   (with-current-buffer (get-buffer-create "*nixfmt*")
     (erase-buffer)
     (insert-buffer-substring buf)
-    (if (zerop (call-process-region (point-min) (point-max) nix-nixfmt-bin t t 
nil))
+    (if (zerop (call-process-region (point-min) (point-max) nixfmt-bin t t 
nil))
         (progn
           (if (not (string= (buffer-string) (with-current-buffer buf 
(buffer-string))))
               (copy-to-buffer buf (point-min) (point-max)))
           (kill-buffer))
       (error "Nixfmt failed, see *nixfmt* buffer for details"))))
 
+(defun nix--find-nixfmt ()
+  "Find the nixfmt binary, or error if it's missing."
+  (let ((nixfmt-bin (executable-find nix-nixfmt-bin)))
+    (unless nixfmt-bin
+      (error "Could not locate executable \"%s\"" nix-nixfmt-bin))
+    nixfmt-bin))
+
 (defun nix-format-buffer ()
   "Format the current buffer using nixfmt."
   (interactive)
-  (unless (executable-find nix-nixfmt-bin)
-    (error "Could not locate executable \"%s\"" nix-nixfmt-bin))
-  (nix--format-call (current-buffer))
+  (nix--format-call (current-buffer) (nix--find-nixfmt))
   (message "Formatted buffer with nixfmt."))
 
 (provide 'nix-format)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]