[shell-scripts] Install shfmt unconditionally

Fixes #15562
This commit is contained in:
Maxi Wolff 2022-06-10 14:33:47 +02:00
parent fa28a4f302
commit 87823c3081
No known key found for this signature in database
GPG key ID: 2DD07025BFDBD89A
4 changed files with 93 additions and 78 deletions

View file

@ -11,6 +11,7 @@
- [[#layer][Layer]] - [[#layer][Layer]]
- [[#linting][Linting]] - [[#linting][Linting]]
- [[#style-checking][Style checking]] - [[#style-checking][Style checking]]
- [[#format][Format]]
- [[#format-on-save][Format on save]] - [[#format-on-save][Format on save]]
- [[#backends][Backends]] - [[#backends][Backends]]
- [[#shell-script-mode][Shell-script-mode]] - [[#shell-script-mode][Shell-script-mode]]
@ -45,6 +46,14 @@ In order to enable =sh= scripts linting, install [[https://www.shellcheck.net/][
** Style checking ** Style checking
In order to enable =sh= scripts style checking, install [[https://github.com/openstack-dev/bashate][bashate]]. In order to enable =sh= scripts style checking, install [[https://github.com/openstack-dev/bashate][bashate]].
** Format
To support formatting of buffers you need to install the application
shfmt. This can be done like shown below
#+BEGIN_SRC sh
go install mvdan.cc/sh/v3/cmd/shfmt@latest
#+END_SRC
** Format on save ** Format on save
To enable automatic formatting on save, set the layer variable To enable automatic formatting on save, set the layer variable
~shell-scripts-format-on-save~ to ~t~: ~shell-scripts-format-on-save~ to ~t~:

View file

@ -20,20 +20,25 @@
;; You should have received a copy of the GNU General Public License ;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;; lsp
(defun spacemacs//shell-scripts-setup-backend () (defun spacemacs//shell-scripts-setup-backend ()
"Conditionally setup shell-scripts backend." "Conditionally setup shell-scripts backend."
(when (eq shell-scripts-backend 'lsp) (when (eq shell-scripts-backend 'lsp)
(spacemacs//shell-scripts-setup-lsp))) (lsp-deferred)))
(defun spacemacs//shell-scripts-setup-company ()
;; lsp "Conditionally setup company based on backend."
;; Activate lsp company explicitly to activate
(defun spacemacs//shell-scripts-setup-lsp () ;; standard backends as well
"Setup lsp backend." (if (eq shell-scripts-backend 'lsp)
(if (configuration-layer/layer-used-p 'lsp) (spacemacs|add-company-backends
(lsp-deferred) :backends company-capf
(message "`lsp' layer is not installed, please add `lsp' layer to your dotfile."))) :modes sh-mode)
(spacemacs|add-company-backends
:backends (company-shell company-shell-env)
:modes sh-mode)))
;; shebang ;; shebang

View file

@ -20,7 +20,6 @@
;; You should have received a copy of the GNU General Public License ;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
(when (and (boundp 'shell-scripts-backend) (when (and (boundp 'shell-scripts-backend)
(eq shell-scripts-backend 'lsp)) (eq shell-scripts-backend 'lsp))
(configuration-layer/declare-layer-dependencies '(lsp))) (configuration-layer/declare-layer-dependencies '(lsp)))

View file

@ -21,35 +21,34 @@
;; along with this program. If not, see <http://www.gnu.org/licenses/>. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
(setq shell-scripts-packages (defconst shell-scripts-packages
'( '(
(company-shell :requires company) company
fish-mode (company-shell :requires company)
flycheck fish-mode
flycheck-bashate flycheck
ggtags flycheck-bashate
counsel-gtags ggtags
helm-gtags counsel-gtags
insert-shebang helm-gtags
org insert-shebang
(sh-script :location built-in) org
(shfmt :toggle shell-scripts-format-on-save) (sh-script :location built-in)
)) shfmt))
(defun shell-scripts/post-init-company ()
(spacemacs//shell-scripts-setup-company))
(defun shell-scripts/post-init-flycheck ()
(spacemacs/enable-flycheck 'sh-mode))
(defun shell-scripts/init-company-shell () (defun shell-scripts/init-company-shell ()
(use-package company-shell (use-package company-shell
:defer t :defer t
:init :init
(progn (spacemacs|add-company-backends
(spacemacs|add-company-backends :backends (company-shell company-shell-env company-fish-shell)
:backends (company-shell company-shell-env) :modes fish-mode)))
:modes sh-mode)
(spacemacs|add-company-backends
:backends (company-shell company-shell-env company-fish-shell)
:modes fish-mode))))
(defun shell-scripts/post-init-flycheck ()
(spacemacs/enable-flycheck 'sh-mode))
(defun shell-scripts/init-flycheck-bashate () (defun shell-scripts/init-flycheck-bashate ()
(use-package flycheck-bashate (use-package flycheck-bashate
@ -64,50 +63,54 @@
(use-package sh-script (use-package sh-script
:defer t :defer t
:init :init
(progn ;; Add meaningful names for prefix categories
;; Add meaningful names for prefix categories (spacemacs/declare-prefix-for-mode 'sh-mode "mi" "insert")
(spacemacs/declare-prefix-for-mode 'sh-mode "mi" "insert") (unless (eq shell-scripts-backend 'lsp)
(spacemacs/declare-prefix-for-mode 'sh-mode "mg" "goto") (spacemacs/declare-prefix-for-mode 'sh-mode "mg" "goto"))
;; Add standard key bindings for insert commands ;; Add standard key bindings for insert commands
(spacemacs/set-leader-keys-for-major-mode 'sh-mode (spacemacs/set-leader-keys-for-major-mode 'sh-mode
"\\" 'sh-backslash-region "\\" 'sh-backslash-region
"ic" 'sh-case "ic" 'sh-case
"ii" 'sh-if "ii" 'sh-if
"if" 'sh-function "if" 'sh-function
"io" 'sh-for "io" 'sh-for
"ie" 'sh-indexed-loop "ie" 'sh-indexed-loop
"iw" 'sh-while "iw" 'sh-while
"ir" 'sh-repeat "ir" 'sh-repeat
"is" 'sh-select "is" 'sh-select
"iu" 'sh-until "iu" 'sh-until
"ig" 'sh-while-getopts) "ig" 'sh-while-getopts)
;; Use sh-mode when opening `.zsh' files, and when opening Prezto runcoms. ;; Use sh-mode when opening `.zsh' files, and when opening Prezto runcoms.
(dolist (pattern '("\\.zsh\\'" (dolist (pattern '("\\.zsh\\'"
"zlogin\\'" "zlogin\\'"
"zlogout\\'" "zlogout\\'"
"zpreztorc\\'" "zpreztorc\\'"
"zprofile\\'" "zprofile\\'"
"zshenv\\'" "zshenv\\'"
"zshrc\\'")) "zshrc\\'"))
(add-to-list 'auto-mode-alist (cons pattern 'sh-mode))) (add-to-list 'auto-mode-alist (cons pattern 'sh-mode)))
(defun spacemacs//setup-shell () (defun spacemacs//setup-shell ()
(when (and buffer-file-name (when (and buffer-file-name
(string-match-p "\\.zsh\\'" buffer-file-name)) (string-match-p "\\.zsh\\'" buffer-file-name))
(sh-set-shell "zsh"))) (sh-set-shell "zsh")))
(add-hook 'sh-mode-hook 'spacemacs//setup-shell) (add-hook 'sh-mode-hook 'spacemacs//setup-shell)
(add-hook 'sh-mode-hook 'spacemacs//shell-scripts-setup-backend)))) (add-hook 'sh-mode-hook 'spacemacs//shell-scripts-setup-backend)))
(defun shell-scripts/init-shfmt () (defun shell-scripts/init-shfmt ()
(use-package shfmt (use-package shfmt
:defer t :defer t
:init :init
(progn (when shell-scripts-format-on-save
(add-hook 'sh-mode-hook 'shfmt-on-save-mode) (add-hook 'sh-mode-hook 'shfmt-on-save-mode))
(spacemacs/set-leader-keys-for-major-mode 'sh-mode
"=" 'shfmt-buffer)))) ;; "=" is a group of commands for lsp users
;; therefore bind this function to "==" instead
(if (eq shell-scripts-backend 'lsp)
(spacemacs/set-leader-keys-for-major-mode 'sh-mode "==" 'shfmt-buffer)
(spacemacs/set-leader-keys-for-major-mode 'sh-mode "=" 'shfmt-buffer))))
(defun shell-scripts/post-init-ggtags () (defun shell-scripts/post-init-ggtags ()
(add-hook 'sh-mode-local-vars-hook #'spacemacs/ggtags-mode-enable)) (add-hook 'sh-mode-local-vars-hook #'spacemacs/ggtags-mode-enable))
@ -126,11 +129,10 @@
(use-package insert-shebang (use-package insert-shebang
:defer t :defer t
:init :init
(progn ;; Insert shebang must be available for non shell modes like python or
;; Insert shebang must be available for non shell modes like python or ;; groovy but also in the major mode menu with shell specific inserts
;; groovy but also in the major mode menu with shell specific inserts (spacemacs/set-leader-keys-for-major-mode 'sh-mode
(spacemacs/set-leader-keys-for-major-mode 'sh-mode "i!" 'spacemacs/insert-shebang)
"i!" 'spacemacs/insert-shebang) (spacemacs/set-leader-keys "i!" 'spacemacs/insert-shebang)
(spacemacs/set-leader-keys "i!" 'spacemacs/insert-shebang) ;; we don't want to insert shebang lines automatically
;; we don't want to insert shebang lines automatically (remove-hook 'find-file-hook 'insert-shebang)))
(remove-hook 'find-file-hook 'insert-shebang))))