]> git.eshelyaron.com Git - emacs.git/commitdiff
Add fix suggestions to shellcheck diagnostics in 'sh-mode'
authorEshel Yaron <me@eshelyaron.com>
Thu, 30 May 2024 19:36:51 +0000 (21:36 +0200)
committerEshel Yaron <me@eshelyaron.com>
Thu, 30 May 2024 19:36:51 +0000 (21:36 +0200)
* lisp/progmodes/sh-script.el (sh-shellcheck-fix): New function.
(sh-shellcheck-flymake): Use it.

lisp/progmodes/flymake.el
lisp/progmodes/sh-script.el

index edf9e95063bb827b484d52f01b9f19dec4062281..cf5349765dbe8109976d1e2891ebe32912e97dc4 100644 (file)
@@ -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
index a348e9ba6fdb73993d7b0a9233d4e1b98648d5af..fccee14af822e49f2a3073dfc3e23a647757afca 100644 (file)
@@ -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