]> git.eshelyaron.com Git - emacs.git/commitdiff
New command 'flymake-diagnostics-buffer-fix-diagnostic'
authorEshel Yaron <me@eshelyaron.com>
Wed, 12 Jun 2024 09:20:52 +0000 (11:20 +0200)
committerEshel Yaron <me@eshelyaron.com>
Wed, 12 Jun 2024 09:20:52 +0000 (11:20 +0200)
lisp/progmodes/flymake.el

index 9277e597d5846db284cab79c60967b5f1e15213a..6cc7d5c43aab5109e2fee9893f39ce9d1631d2a4 100644 (file)
@@ -878,22 +878,28 @@ Return to original margin width if ORIG-WIDTH is non-nil."
       (cl-incf i)))
   menu)
 
+(defun flymake--fix-diagnostic (diag)
+  "Fix diagnostic DIAG."
+  (when-let* ((fixfn (flymake--diag-fix-function diag))
+              (fixes (funcall fixfn (flymake--diag-data diag)))
+              (edits (car (if (cdr fixes)
+                              (alist-get
+                               (completing-read (format-prompt "Fix" (caar fixes))
+                                                fixes nil t nil nil (caar fixes))
+                               fixes nil nil #'string=)
+                            (cdar fixes)))))
+    (if (functionp edits) (funcall edits) (refactor-apply-edits edits))
+    t))
+
 (defun flymake-fix (pos)
   "Fix Flymake diagnostic at POS."
   (interactive "d")
   ;; TODO - fix _all_ diagnostics at point.
   (if-let ((diags (flymake-diagnostics pos)))
-      (if-let ((diag (seq-find #'flymake--diag-fix-function diags))
-               (fixes (funcall (flymake--diag-fix-function diag)
-                               (flymake--diag-data diag)))
-               (edits (car (if (cdr fixes)
-                               (alist-get
-                                (completing-read (format-prompt "Fix" (caar fixes))
-                                                 fixes nil t nil nil (caar fixes))
-                                fixes nil nil #'string=)
-                             (cdar fixes)))))
-          (if (functionp edits) (funcall edits) (refactor-apply-edits edits))
-        (message "No fix available for this diagnostic"))
+      (let ((diag (seq-find #'flymake--diag-fix-function diags)))
+        (if (and diag (flymake--fix-diagnostic diag))
+            (flymake--update-diagnostics-listings (current-buffer))
+          (message "No fix available for this diagnostic")))
     (user-error "No diagnostic at this position")))
 
 (cl-defun flymake--highlight-line (diagnostic &optional foreign)
@@ -1862,13 +1868,14 @@ TYPE is usually keyword `:error', `:warning' or `:note'."
 (defvar-keymap flymake-diagnostics-buffer-mode-map
   :doc "Keymap for Flyamke diagnostics list buffers."
   "RET" #'flymake-diagnostics-buffer-goto-diagnostic
-  "SPC" #'flymake-diagnostics-buffer-show-diagnostic)
+  "SPC" #'flymake-diagnostics-buffer-show-diagnostic
+  "C-c C-f" #'flymake-diagnostics-buffer-fix-diagnostic)
 
 (defun flymake-diagnostics-buffer-show-diagnostic (pos &optional action)
   "Show location of diagnostic at POS.
 
 Optional argument ACTION is passed to `display-buffer', which see."
-  (interactive "d")
+  (interactive "d" flymake-diagnostics-buffer-mode)
   (let* ((id (or (tabulated-list-get-id pos)
                  (user-error "No diagnostic at this position")))
          (diag (plist-get id :diagnostic))
@@ -1890,10 +1897,20 @@ Optional argument ACTION is passed to `display-buffer', which see."
                  (goto-char bbeg)))))
       (current-buffer))))
 
+(defun flymake-diagnostics-buffer-fix-diagnostic (pos)
+  "Fix Flymake diagnostic at POS."
+  (interactive "d" flymake-diagnostics-buffer-mode)
+  (let* ((id (or (tabulated-list-get-id pos)
+                 (user-error "No diagnostic at this position")))
+         (diag (plist-get id :diagnostic)))
+    (if (flymake--fix-diagnostic diag)
+        (revert-buffer)
+      (user-error "No fix available for this diagnostic"))))
+
 (defun flymake-diagnostics-buffer-goto-diagnostic (pos)
   "Show location of diagnostic at POS.
 POS can be a buffer position or a button"
-  (interactive "d")
+  (interactive "d" flymake-diagnostics-buffer-mode)
   (pop-to-buffer
    (flymake-diagnostics-buffer-show-diagnostic
     (if (button-type pos) (button-start pos) pos))))
@@ -1928,6 +1945,7 @@ filename of the diagnostic relative to that directory."
           t nil))
    for type = (flymake-diagnostic-type diag)
    for backend = (flymake-diagnostic-backend diag)
+   for fixfn = (flymake--diag-fix-function diag)
    for bname = (or (ignore-errors (symbol-name backend))
                    "(anonymous function)")
    for data-vec = `[,(format "%s" line)
@@ -1943,6 +1961,7 @@ filename of the diagnostic relative to that directory."
                                                     "\\1\\2" bname)
                         "(anon)")
                       'help-echo (format "From `%s' backend" backend))
+                    ,(if fixfn "Yes" "")
                     (,(flymake-diagnostic-oneliner diag t)
                      mouse-face highlight
                      help-echo "mouse-2: visit this diagnostic"
@@ -1990,6 +2009,7 @@ buffer."
                  (< (plist-get (car l1) :severity)
                     (plist-get (car l2) :severity))))
     ("Backend" 8 t)
+    ("Fix" 3 t)
     ("Message" 0 t)])
 
 (define-derived-mode flymake-diagnostics-buffer-mode tabulated-list-mode