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

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

[nongnu] elpa/nix-mode fb6c1ca5dd 1/2: Replace custom nixfmt wrapping wi


From: ELPA Syncer
Subject: [nongnu] elpa/nix-mode fb6c1ca5dd 1/2: Replace custom nixfmt wrapping with reformatter
Date: Thu, 12 Jan 2023 13:59:53 -0500 (EST)

branch: elpa/nix-mode
commit fb6c1ca5dd22580267142f19f78608a99de77552
Author: Joseph LaFreniere <joseph@lafreniere.xyz>
Commit: Joseph LaFreniere <joseph@lafreniere.xyz>

    Replace custom nixfmt wrapping with reformatter
---
 flake.nix     |  1 +
 nix-format.el | 49 ++++++++++++-------------------------------------
 2 files changed, 13 insertions(+), 37 deletions(-)

diff --git a/flake.nix b/flake.nix
index bffea7cd0d..53aa5e9dbe 100644
--- a/flake.nix
+++ b/flake.nix
@@ -16,6 +16,7 @@
           f
           magit-section
           transient
+          reformatter
         ]);
       in stdenvNoCC.mkDerivation {
         pname = "nix-mode";
diff --git a/nix-format.el b/nix-format.el
index 75e679e46e..679ad02629 100644
--- a/nix-format.el
+++ b/nix-format.el
@@ -3,54 +3,29 @@
 ;; This file is NOT part of GNU Emacs.
 
 ;; Homepage: https://github.com/NixOS/nix-mode
-;; Package-Requires: ((emacs "24.1"))
+;; Package-Requires: ((emacs "24.1") (reformatter "0.6"))
 ;; Version: 1.5.0
 
 ;;; Commentary:
 
 ;;; Code:
+(require 'reformatter)
 
 (defcustom nix-nixfmt-bin "nixfmt"
   "Path to nixfmt executable."
   :group 'nix
   :type 'string)
 
-(if (fboundp 'replace-buffer-contents)
-    (defun nix--replace-buffer-contents (src dst)
-      (with-current-buffer dst (replace-buffer-contents src)))
-  (defun nix--replace-buffer-contents (src dst)
-    (if (not (string= (with-current-buffer src (buffer-string))
-                     (with-current-buffer dst (buffer-string))))
-       (with-current-buffer src
-         (copy-to-buffer dst (point-min) (point-max))))))
-
-(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) nixfmt-bin t t 
nil))
-       (nix--replace-buffer-contents (current-buffer) buf)
-      (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)
-  (nix--format-call (current-buffer) (nix--find-nixfmt))
-  (message "Formatted buffer with nixfmt."))
-
-;;;###autoload
-(defun nix-format-before-save ()
-  "Add this to `before-save-hook' to run nixfmt when saving."
-  (when (derived-mode-p 'nix-mode)
-    (nix-format-buffer)))
+;;;###autoload (autoload 'nixfmt-buffer "nix-format")
+;;;###autoload (autoload 'nixfmt-region "nix-format")
+;;;###autoload (autoload 'nixfmt-on-save-mode "nix-format")
+(reformatter-define nixfmt
+  :program nix-nixfmt-bin
+  :args (list input-file)
+  :stdin nil
+  :stdout nil
+  :input-file (reformatter-temp-file-in-current-directory)
+  :group 'nix)
 
 (provide 'nix-format)
 ;;; nix-format.el ends here



reply via email to

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