Skip to content

Commit c18a24e

Browse files
committed
Revert "Replace custom nixfmt wrapping with reformatter"
This reverts commit fb6c1ca.
1 parent 9fc7749 commit c18a24e

File tree

3 files changed

+37
-13
lines changed

3 files changed

+37
-13
lines changed

flake.nix

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
f
1717
magit-section
1818
transient
19-
reformatter
2019
]);
2120
in stdenvNoCC.mkDerivation {
2221
pname = "nix-mode";

nix-format.el

+36-11
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,48 @@
77
;;; Commentary:
88

99
;;; Code:
10-
(require 'reformatter)
1110

1211
(defcustom nix-nixfmt-bin "nixfmt"
1312
"Path to nixfmt executable."
1413
:group 'nix
1514
:type 'string)
1615

17-
;;;###autoload (autoload 'nixfmt-buffer "nix-format")
18-
;;;###autoload (autoload 'nixfmt-region "nix-format")
19-
;;;###autoload (autoload 'nixfmt-on-save-mode "nix-format")
20-
(reformatter-define nixfmt
21-
:program nix-nixfmt-bin
22-
:args (list input-file)
23-
:stdin nil
24-
:stdout nil
25-
:input-file (reformatter-temp-file-in-current-directory)
26-
:group 'nix)
16+
(if (fboundp 'replace-buffer-contents)
17+
(defun nix--replace-buffer-contents (src dst)
18+
(with-current-buffer dst (replace-buffer-contents src)))
19+
(defun nix--replace-buffer-contents (src dst)
20+
(if (not (string= (with-current-buffer src (buffer-string))
21+
(with-current-buffer dst (buffer-string))))
22+
(with-current-buffer src
23+
(copy-to-buffer dst (point-min) (point-max))))))
24+
25+
(defun nix--format-call (buf nixfmt-bin)
26+
"Format BUF using nixfmt."
27+
(with-current-buffer (get-buffer-create "*nixfmt*")
28+
(erase-buffer)
29+
(insert-buffer-substring buf)
30+
(if (zerop (call-process-region (point-min) (point-max) nixfmt-bin t t nil))
31+
(nix--replace-buffer-contents (current-buffer) buf)
32+
(error "Nixfmt failed, see *nixfmt* buffer for details"))))
33+
34+
(defun nix--find-nixfmt ()
35+
"Find the nixfmt binary, or error if it's missing."
36+
(let ((nixfmt-bin (executable-find nix-nixfmt-bin)))
37+
(unless nixfmt-bin
38+
(error "Could not locate executable %S" nix-nixfmt-bin))
39+
nixfmt-bin))
40+
41+
(defun nix-format-buffer ()
42+
"Format the current buffer using nixfmt."
43+
(interactive)
44+
(nix--format-call (current-buffer) (nix--find-nixfmt))
45+
(message "Formatted buffer with nixfmt."))
46+
47+
;;;###autoload
48+
(defun nix-format-before-save ()
49+
"Add this to `before-save-hook' to run nixfmt when saving."
50+
(when (derived-mode-p 'nix-mode)
51+
(nix-format-buffer)))
2752

2853
(provide 'nix-format)
2954
;;; nix-format.el ends here

nix-mode.el

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
;; Homepage: https://github.com/NixOS/nix-mode
55
;; Version: 1.5.0
66
;; Keywords: nix, languages, tools, unix
7-
;; Package-Requires: ((emacs "25.1") magit-section (transient "0.3") (reformatter "0.6"))
7+
;; Package-Requires: ((emacs "25.1") magit-section (transient "0.3"))
88

99
;; This file is NOT part of GNU Emacs.
1010

0 commit comments

Comments
 (0)