mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 11:19:45 +08:00
Added emacs func and keymap for auto-insert directives
50
Contrib.md
50
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 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)
|
||||
```
|
Reference in New Issue
Block a user