Repository: rust-lang/rust-mode (language: rust)

Issue:
Add rust-format-diff-buffer
This command displays the output of rustfmt-diff.


Discussion / hints:

Fix patch:
diff --git a/rust-mode.el b/rust-mode.el
index c81f4212..f2c4d862 100644
--- a/rust-mode.el
+++ b/rust-mode.el
@@ -1456,6 +1456,35 @@ This is written mainly to be used as `end-of-defun-function' for Rust."
             (forward-char columns)))
         (min (point) max-pos)))))
 
+(defun rust-format-diff-buffer ()
+  "Show diff to current buffer from rustfmt.
+
+Return the created process."
+  (interactive)
+  (unless (executable-find rust-rustfmt-bin)
+    (error "Could not locate executable \%s\"" rust-rustfmt-bin))
+  (let ((proc
+         (start-process "rustfmt-diff"
+                        (with-current-buffer
+                            (get-buffer-create "*rustfmt-diff*")
+                          (let ((inhibit-read-only t))
+                            (erase-buffer))
+                          (current-buffer))
+                        rust-rustfmt-bin
+                        "--check"
+                        (buffer-file-name))))
+    (set-process-sentinel proc 'rust-format-diff-buffer-sentinel)
+    proc))
+
+(defun rust-format-diff-buffer-sentinel (process _e)
+  (when (eq 'exit (process-status process))
+    (if (> (process-exit-status process) 0)
+        (with-current-buffer "*rustfmt-diff*"
+          (let ((inhibit-read-only t))
+            (diff-mode))
+          (pop-to-buffer (current-buffer)))
+      (message "rustfmt check passed."))))
+
 (defun rust-format-buffer ()
   "Format the current buffer using rustfmt."
   (interactive)


Test patch: