From df96fede202220a79002053136e25eeb89f92371 Mon Sep 17 00:00:00 2001 From: Eshel Yaron Date: Thu, 30 May 2024 21:36:51 +0200 Subject: [PATCH] Add fix suggestions to shellcheck diagnostics in 'sh-mode' * lisp/progmodes/sh-script.el (sh-shellcheck-fix): New function. (sh-shellcheck-flymake): Use it. --- lisp/progmodes/flymake.el | 16 +++++++++------- lisp/progmodes/sh-script.el | 19 ++++++++++++++++++- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/lisp/progmodes/flymake.el b/lisp/progmodes/flymake.el index edf9e95063b..cf5349765db 100644 --- a/lisp/progmodes/flymake.el +++ b/lisp/progmodes/flymake.el @@ -862,10 +862,11 @@ Return to original margin width if ORIG-WIDTH is non-nil." (defun flymake-diagnostic-context-menu (menu click) "Extend MENU with fix suggestions for diagnostic at CLICK." - (when-let ((diag (mouse-posn-property (event-start click) - 'flymake-diagnostic)) - (fix-fun (flymake--diag-fix-function diag)) - (fixes (funcall fix-fun (flymake--diag-data diag))) + (when-let ((diag (seq-find #'flymake--diag-fix-function + (flymake-diagnostics + (posn-point (event-start click))))) + (fixes (funcall (flymake--diag-fix-function diag) + (flymake--diag-data diag))) (i 1)) (dolist (fix fixes) (define-key menu (vector (intern (format "flymake-fix-%d" i))) @@ -881,9 +882,10 @@ Return to original margin width if ORIG-WIDTH is non-nil." "Fix Flymake diagnostic at POS." (interactive "d") ;; TODO - fix _all_ diagnostics at point. - (if-let ((diag (car (flymake-diagnostics pos)))) - (if-let ((fix-fun (flymake--diag-fix-function diag)) - (fixes (funcall fix-fun (flymake--diag-data diag)))) + (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)))) (refactor-apply-edits (car (if (cdr fixes) (alist-get diff --git a/lisp/progmodes/sh-script.el b/lisp/progmodes/sh-script.el index a348e9ba6fd..fccee14af82 100644 --- a/lisp/progmodes/sh-script.el +++ b/lisp/progmodes/sh-script.el @@ -3195,6 +3195,21 @@ shell command and conveniently use this command." (defvar-local sh--shellcheck-process nil) +(defun sh-shellcheck-fix (data) + "Format DATA, a cons cell (TITLE . FIX), as a Flymake fix suggestion." + `((,(car data) + ((,(current-buffer) + ,@(mapcar + (lambda (rep) + (let-alist rep + (without-restriction + (save-excursion + (goto-char (point-min)) + (list (1- (+ (pos-bol .line) .column)) + (1- (+ (pos-bol .endLine) .endColumn)) + .replacement))))) + (alist-get 'replacements (cdr data)))))))) + (defun sh-shellcheck-flymake (report-fn &rest _args) "Flymake backend using the shellcheck program. Takes a Flymake callback REPORT-FN as argument, as expected of a @@ -3236,7 +3251,9 @@ member of `flymake-diagnostic-functions'." ("error" :error) ("warning" :warning) (_ :note)) - (format "SC%s: %s" .code .message))))) + (format "SC%s: %s" .code .message) + (cons .message .fix) nil + (when (consp .fix) #'sh-shellcheck-fix))))) (funcall report-fn)))) (kill-buffer (process-buffer proc))))))) (unless dialect -- 2.39.2