diff --git a/layers/+lang/shell-scripts/README.org b/layers/+lang/shell-scripts/README.org index d53de1338..84d17aa49 100644 --- a/layers/+lang/shell-scripts/README.org +++ b/layers/+lang/shell-scripts/README.org @@ -11,6 +11,7 @@ - [[#layer][Layer]] - [[#linting][Linting]] - [[#style-checking][Style checking]] + - [[#format][Format]] - [[#format-on-save][Format on save]] - [[#backends][Backends]] - [[#shell-script-mode][Shell-script-mode]] @@ -45,6 +46,14 @@ In order to enable =sh= scripts linting, install [[https://www.shellcheck.net/][ ** Style checking 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 To enable automatic formatting on save, set the layer variable ~shell-scripts-format-on-save~ to ~t~: diff --git a/layers/+lang/shell-scripts/funcs.el b/layers/+lang/shell-scripts/funcs.el index 070e680df..a886ea7d3 100644 --- a/layers/+lang/shell-scripts/funcs.el +++ b/layers/+lang/shell-scripts/funcs.el @@ -20,20 +20,25 @@ ;; You should have received a copy of the GNU General Public License ;; along with this program. If not, see . + +;; lsp (defun spacemacs//shell-scripts-setup-backend () "Conditionally setup shell-scripts backend." (when (eq shell-scripts-backend 'lsp) - (spacemacs//shell-scripts-setup-lsp))) + (lsp-deferred))) - -;; lsp - -(defun spacemacs//shell-scripts-setup-lsp () - "Setup lsp backend." - (if (configuration-layer/layer-used-p 'lsp) - (lsp-deferred) - (message "`lsp' layer is not installed, please add `lsp' layer to your dotfile."))) +(defun spacemacs//shell-scripts-setup-company () + "Conditionally setup company based on backend." + ;; Activate lsp company explicitly to activate + ;; standard backends as well + (if (eq shell-scripts-backend 'lsp) + (spacemacs|add-company-backends + :backends company-capf + :modes sh-mode) + (spacemacs|add-company-backends + :backends (company-shell company-shell-env) + :modes sh-mode))) ;; shebang diff --git a/layers/+lang/shell-scripts/layers.el b/layers/+lang/shell-scripts/layers.el index 5175e2fa0..7ab6b693d 100644 --- a/layers/+lang/shell-scripts/layers.el +++ b/layers/+lang/shell-scripts/layers.el @@ -20,7 +20,6 @@ ;; You should have received a copy of the GNU General Public License ;; along with this program. If not, see . - (when (and (boundp 'shell-scripts-backend) (eq shell-scripts-backend 'lsp)) (configuration-layer/declare-layer-dependencies '(lsp))) diff --git a/layers/+lang/shell-scripts/packages.el b/layers/+lang/shell-scripts/packages.el index 3a2b1fa45..6bb6095d6 100644 --- a/layers/+lang/shell-scripts/packages.el +++ b/layers/+lang/shell-scripts/packages.el @@ -21,35 +21,34 @@ ;; along with this program. If not, see . -(setq shell-scripts-packages - '( - (company-shell :requires company) - fish-mode - flycheck - flycheck-bashate - ggtags - counsel-gtags - helm-gtags - insert-shebang - org - (sh-script :location built-in) - (shfmt :toggle shell-scripts-format-on-save) - )) +(defconst shell-scripts-packages + '( + company + (company-shell :requires company) + fish-mode + flycheck + flycheck-bashate + ggtags + counsel-gtags + helm-gtags + insert-shebang + org + (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 () (use-package company-shell :defer t :init - (progn - (spacemacs|add-company-backends - :backends (company-shell company-shell-env) - :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)) + (spacemacs|add-company-backends + :backends (company-shell company-shell-env company-fish-shell) + :modes fish-mode))) (defun shell-scripts/init-flycheck-bashate () (use-package flycheck-bashate @@ -64,50 +63,54 @@ (use-package sh-script :defer t :init - (progn - ;; Add meaningful names for prefix categories - (spacemacs/declare-prefix-for-mode 'sh-mode "mi" "insert") - (spacemacs/declare-prefix-for-mode 'sh-mode "mg" "goto") + ;; Add meaningful names for prefix categories + (spacemacs/declare-prefix-for-mode 'sh-mode "mi" "insert") + (unless (eq shell-scripts-backend 'lsp) + (spacemacs/declare-prefix-for-mode 'sh-mode "mg" "goto")) - ;; Add standard key bindings for insert commands - (spacemacs/set-leader-keys-for-major-mode 'sh-mode - "\\" 'sh-backslash-region - "ic" 'sh-case - "ii" 'sh-if - "if" 'sh-function - "io" 'sh-for - "ie" 'sh-indexed-loop - "iw" 'sh-while - "ir" 'sh-repeat - "is" 'sh-select - "iu" 'sh-until - "ig" 'sh-while-getopts) + ;; Add standard key bindings for insert commands + (spacemacs/set-leader-keys-for-major-mode 'sh-mode + "\\" 'sh-backslash-region + "ic" 'sh-case + "ii" 'sh-if + "if" 'sh-function + "io" 'sh-for + "ie" 'sh-indexed-loop + "iw" 'sh-while + "ir" 'sh-repeat + "is" 'sh-select + "iu" 'sh-until + "ig" 'sh-while-getopts) - ;; Use sh-mode when opening `.zsh' files, and when opening Prezto runcoms. - (dolist (pattern '("\\.zsh\\'" - "zlogin\\'" - "zlogout\\'" - "zpreztorc\\'" - "zprofile\\'" - "zshenv\\'" - "zshrc\\'")) - (add-to-list 'auto-mode-alist (cons pattern 'sh-mode))) + ;; Use sh-mode when opening `.zsh' files, and when opening Prezto runcoms. + (dolist (pattern '("\\.zsh\\'" + "zlogin\\'" + "zlogout\\'" + "zpreztorc\\'" + "zprofile\\'" + "zshenv\\'" + "zshrc\\'")) + (add-to-list 'auto-mode-alist (cons pattern 'sh-mode))) - (defun spacemacs//setup-shell () - (when (and buffer-file-name - (string-match-p "\\.zsh\\'" buffer-file-name)) - (sh-set-shell "zsh"))) - (add-hook 'sh-mode-hook 'spacemacs//setup-shell) - (add-hook 'sh-mode-hook 'spacemacs//shell-scripts-setup-backend)))) + (defun spacemacs//setup-shell () + (when (and buffer-file-name + (string-match-p "\\.zsh\\'" buffer-file-name)) + (sh-set-shell "zsh"))) + (add-hook 'sh-mode-hook 'spacemacs//setup-shell) + (add-hook 'sh-mode-hook 'spacemacs//shell-scripts-setup-backend))) (defun shell-scripts/init-shfmt () (use-package shfmt :defer t :init - (progn - (add-hook 'sh-mode-hook 'shfmt-on-save-mode) - (spacemacs/set-leader-keys-for-major-mode 'sh-mode - "=" 'shfmt-buffer)))) + (when shell-scripts-format-on-save + (add-hook 'sh-mode-hook 'shfmt-on-save-mode)) + + ;; "=" 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 () (add-hook 'sh-mode-local-vars-hook #'spacemacs/ggtags-mode-enable)) @@ -126,11 +129,10 @@ (use-package insert-shebang :defer t :init - (progn - ;; Insert shebang must be available for non shell modes like python or - ;; groovy but also in the major mode menu with shell specific inserts - (spacemacs/set-leader-keys-for-major-mode 'sh-mode - "i!" 'spacemacs/insert-shebang) - (spacemacs/set-leader-keys "i!" 'spacemacs/insert-shebang) - ;; we don't want to insert shebang lines automatically - (remove-hook 'find-file-hook 'insert-shebang)))) + ;; Insert shebang must be available for non shell modes like python or + ;; groovy but also in the major mode menu with shell specific inserts + (spacemacs/set-leader-keys-for-major-mode 'sh-mode + "i!" 'spacemacs/insert-shebang) + (spacemacs/set-leader-keys "i!" 'spacemacs/insert-shebang) + ;; we don't want to insert shebang lines automatically + (remove-hook 'find-file-hook 'insert-shebang)))