From 2c8b952ef6c01667d40080b64d729518b793385e Mon Sep 17 00:00:00 2001 From: Eshel Yaron Date: Wed, 12 Jun 2024 11:20:52 +0200 Subject: [PATCH] New command 'flymake-diagnostics-buffer-fix-diagnostic' --- lisp/progmodes/flymake.el | 48 +++++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/lisp/progmodes/flymake.el b/lisp/progmodes/flymake.el index 9277e597d58..6cc7d5c43aa 100644 --- a/lisp/progmodes/flymake.el +++ b/lisp/progmodes/flymake.el @@ -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 -- 2.39.2