From 24a562c20b9900a301fa15945f46b1147e5b1704 Mon Sep 17 00:00:00 2001 From: "Matthew O. Persico" Date: Wed, 18 Apr 2018 12:09:40 -0400 Subject: [PATCH] Added emacs func and keymap for auto-insert directives --- Contrib.md | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/Contrib.md b/Contrib.md index 61bfb3d..eaca07b 100644 --- a/Contrib.md +++ b/Contrib.md @@ -23,4 +23,52 @@ This works on Docker for Mac as well as a linux system running docker. I have t - `cat filename.sh | shellcheck -` - `shellcheck -V` -No modification of my editor settings was needed for this to integrate (I checked Vim through syntastic and VSCode). \ No newline at end of file +No modification of my editor settings was needed for this to integrate (I checked Vim through syntastic and VSCode). + +### Emacs auto-fix script + +By [@matthewpersico](https://github.com/matthewpersico) + +This function and keymap add the ability to auto-insert the appropriate shellcheck disable directive in your script. + +```lisp +;; The following is thanks to https://github.com/flycheck/flycheck/issues/1436 +(defun shellcheck-disable-error-at-point (&optional pos) + "Insert a shellcheck disable directive at the current error in the code." + (interactive) + (-when-let* ((error (tabulated-list-get-id pos)) + (buffer (flycheck-error-buffer error)) + (id (flycheck-error-id error)) + ;;(message (flycheck-error-message error)) + ) + (when (buffer-live-p buffer) + (if (eq (window-buffer) (get-buffer flycheck-error-list-buffer)) + ;; When called from within the error list, keep the error list, + ;; otherwise replace the current buffer. + (pop-to-buffer buffer 'other-window) + (switch-to-buffer buffer)) + (let ((pos (flycheck-error-pos error))) + (unless (eq (goto-char pos) (point)) + ;; If widening gets in the way of moving to the right place, remove it + ;; and try again + (widen) + (goto-char pos)) + + ;; Move to start of line with error position. + (beginning-of-line-text) + + ;; The only error I know of where the disable directive is AFTER the + ;; error position, not before. + (when (string-equal id "SC2148") + (forward-line) + ) + ;; Insert the disable line + (insert (format "# shellcheck disable=%s\n" id)) + ;; Indent it + (indent-for-tab-command)) + + ;; Re-highlight the errors + (flycheck-error-list-highlight-errors 'preserve-pos)))) + +(define-key flycheck-error-list-mode-map (kbd "d") #'shellcheck-disable-error-at-point) +``` \ No newline at end of file