c-c++: refactor

- Moved backend determination to `config.el`
- Replaced `(when (not foo) bar)` with `(unless foo bar)`
- Replaced unnecessary backquote construct with simple quotation
- Replaced `pcase` form with only one-arm with `when` or `unless` form
- Replaced `(set (make-local-variable 'foo) bar)` with `(setq-local foo bar)`
- Removed unnecessary `progn`
This commit is contained in:
Lucius Hu 2021-03-17 23:41:34 -04:00 committed by duianto
parent f372297755
commit dae0231fed
4 changed files with 39 additions and 47 deletions

View File

@ -26,7 +26,7 @@
(spacemacs|define-jump-handlers c++-mode) (spacemacs|define-jump-handlers c++-mode)
(spacemacs|define-jump-handlers c-mode) (spacemacs|define-jump-handlers c-mode)
(defvar c-c++-backend nil (defvar c-c++-backend (when (configuration-layer/layer-used-p 'lsp) 'lsp-clangd)
"The backend to use for IDE features. "The backend to use for IDE features.
Possible values are `lsp-ccls', `lsp-clangd', `rtags' and `ycmd'.") Possible values are `lsp-ccls', `lsp-clangd', `rtags' and `ycmd'.")
@ -90,7 +90,7 @@ Add `dap-gdb-lldb' for the WebFreak Native Debug extension.")
;; misc ;; misc
(defvar c-c++-default-mode-for-headers (when (not (functionp 'c-or-c++-mode)) 'c-mode) (defvar c-c++-default-mode-for-headers (unless (functionp 'c-or-c++-mode) 'c-mode)
"Default mode to open header files. Can be `c-mode' or `c++-mode', or `c-or-c++-mode' for Emacs > 26+.") "Default mode to open header files. Can be `c-mode' or `c++-mode', or `c-or-c++-mode' for Emacs > 26+.")
(defvar c-c++-adopt-subprojects nil (defvar c-c++-adopt-subprojects nil

View File

@ -24,58 +24,50 @@
(require 'cl-lib) (require 'cl-lib)
(require 'subr-x) (require 'subr-x)
(defun spacemacs//c-c++-backend ()
"Returns selected backend."
(if c-c++-backend
c-c++-backend
(cond
((configuration-layer/layer-used-p 'lsp) 'lsp-clangd)
(t nil))))
(defun spacemacs//c-c++-setup-backend () (defun spacemacs//c-c++-setup-backend ()
"Conditionally setup c-c++ backend." "Conditionally setup c-c++ backend."
(pcase (spacemacs//c-c++-backend) (pcase c-c++-backend
(`lsp-clangd (spacemacs//c-c++-setup-lsp-clangd)) ('lsp-clangd (spacemacs//c-c++-setup-lsp-clangd))
(`lsp-ccls (spacemacs//c-c++-setup-lsp-ccls)) ('lsp-ccls (spacemacs//c-c++-setup-lsp-ccls))
(`rtags (spacemacs//c-c++-setup-rtags)) ('rtags (spacemacs//c-c++-setup-rtags))
(`ycmd (spacemacs//c-c++-setup-ycmd)))) ('ycmd (spacemacs//c-c++-setup-ycmd))))
(defun spacemacs//c-c++-setup-company () (defun spacemacs//c-c++-setup-company ()
"Conditionally setup C/C++ company integration based on backend." "Conditionally setup C/C++ company integration based on backend."
(pcase (spacemacs//c-c++-backend) (pcase c-c++-backend
(`rtags (spacemacs//c-c++-setup-rtags-company)) ('rtags (spacemacs//c-c++-setup-rtags-company))
(`ycmd (spacemacs//c-c++-setup-ycmd-company)))) ('ycmd (spacemacs//c-c++-setup-ycmd-company))))
(defun spacemacs//c-c++-setup-dap () (defun spacemacs//c-c++-setup-dap ()
"Conditionally setup C/C++ DAP integration based on backend." "Conditionally setup C/C++ DAP integration based on backend."
;; currently DAP is only available using LSP ;; currently DAP is only available using LSP
(pcase (spacemacs//c-c++-backend) (pcase c-c++-backend
(`lsp-clangd (spacemacs//c-c++-setup-lsp-dap)) ('lsp-clangd (spacemacs//c-c++-setup-lsp-dap))
(`lsp-ccls (spacemacs//c-c++-setup-lsp-dap)))) ('lsp-ccls (spacemacs//c-c++-setup-lsp-dap))))
(defun spacemacs//c-c++-setup-eldoc () (defun spacemacs//c-c++-setup-eldoc ()
"Conditionally setup C/C++ eldoc integration based on backend." "Conditionally setup C/C++ eldoc integration based on backend."
(pcase (spacemacs//c-c++-backend) ;; lsp setup eldoc on its own
;; lsp setup eldoc on its own (when (eq c-c++-backend 'ycmd)
(`ycmd (spacemacs//c-c++-setup-ycmd-eldoc)))) (spacemacs//c-c++-setup-ycmd-eldoc)))
(defun spacemacs//c-c++-setup-flycheck () (defun spacemacs//c-c++-setup-flycheck ()
"Conditionally setup C/C++ flycheck integration based on backend." "Conditionally setup C/C++ flycheck integration based on backend."
(pcase (spacemacs//c-c++-backend) (pcase c-c++-backend
(`rtags (spacemacs//c-c++-setup-rtags-flycheck)) ('rtags (spacemacs//c-c++-setup-rtags-flycheck))
(`ycmd (spacemacs//c-c++-setup-ycmd-flycheck)))) ('ycmd (spacemacs//c-c++-setup-ycmd-flycheck))))
(defun spacemacs//c-c++-setup-format () (defun spacemacs//c-c++-setup-format ()
"Conditionally setup format based on backend." "Conditionally setup format based on backend."
(pcase (spacemacs//c-c++-backend) (pcase c-c++-backend
(`lsp-clangd (spacemacs//c-c++-setup-clang-format)) ('lsp-clangd (spacemacs//c-c++-setup-clang-format))
(`lsp-ccls (spacemacs//c-c++-setup-clang-format)))) ('lsp-ccls (spacemacs//c-c++-setup-clang-format))))
(defun spacemacs//c-c++-setup-semantic () (defun spacemacs//c-c++-setup-semantic ()
"Conditionally setup semantic based on backend." "Conditionally setup semantic based on backend."
(pcase (spacemacs//c-c++-backend) (pcase c-c++-backend
(`rtags (spacemacs//c-c++-setup-rtags-semantic)) ('rtags (spacemacs//c-c++-setup-rtags-semantic))
(`ycmd (spacemacs//c-c++-setup-ycmd-semantic)))) ('ycmd (spacemacs//c-c++-setup-ycmd-semantic))))
;; lsp ;; lsp
@ -88,7 +80,7 @@
(spacemacs/lsp-define-extensions (spacemacs/lsp-define-extensions
"c-c++" "lsp-clangd" "c-c++" "lsp-clangd"
'clangd-other-file "textDocument/switchSourceHeader" 'buffer-file-name) 'clangd-other-file "textDocument/switchSourceHeader" 'buffer-file-name)
(set (make-local-variable 'lsp-disabled-clients) '(ccls)) (setq-local lsp-disabled-clients '(ccls))
(lsp)) (lsp))
;; ccls ;; ccls
@ -172,7 +164,7 @@
;;(evil-set-initial-state 'ccls--tree-mode 'emacs) ;;(evil-set-initial-state 'ccls--tree-mode 'emacs)
;;(evil-make-overriding-map 'ccls-tree-mode-map) ;;(evil-make-overriding-map 'ccls-tree-mode-map)
(set (make-local-variable 'lsp-disabled-clients) '(clangd)) (setq-local lsp-disabled-clients '(clangd))
(lsp)) (lsp))
(defun spacemacs//c-c++-setup-lsp-dap () (defun spacemacs//c-c++-setup-lsp-dap ()
@ -224,7 +216,7 @@
"gS" 'rtags-display-summary "gS" 'rtags-display-summary
"gt" 'rtags-symbol-type "gt" 'rtags-symbol-type
"gT" 'rtags-taglist "gT" 'rtags-taglist
"gu" 'rtags-dependency-tree "gu" 'rtags-dependency-tree
"gv" 'rtags-find-virtuals-at-point "gv" 'rtags-find-virtuals-at-point
"gV" 'rtags-print-enum-value-at-point "gV" 'rtags-print-enum-value-at-point
"gX" 'rtags-fix-fixit-at-point "gX" 'rtags-fix-fixit-at-point
@ -369,9 +361,8 @@
(progn (progn
(clang-format-region (region-beginning) (region-end) style) (clang-format-region (region-beginning) (region-end) style)
(message "Formatted region")) (message "Formatted region"))
(progn
(clang-format-buffer style) (clang-format-buffer style)
(message "Formatted buffer %s" (buffer-name)))))) (message "Formatted buffer %s" (buffer-name)))))
(defun spacemacs//clang-format-on-save () (defun spacemacs//clang-format-on-save ()
"Format the current buffer with clang-format on save when "Format the current buffer with clang-format on save when

View File

@ -21,9 +21,10 @@
;; 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 (fboundp 'spacemacs//c-c++-backend) (when (boundp c-c++-backend)
(pcase (spacemacs//c-c++-backend) (configuration-layer/declare-layer-dependencies
(`lsp-clangd (configuration-layer/declare-layer-dependencies '(lsp dap))) (pcase c-c++-backend
(`lsp-ccls (configuration-layer/declare-layer-dependencies '(lsp dap))) ('lsp-clangd '(lsp dap))
(`rtags (configuration-layer/declare-layer-dependencies '(ggtags))) ('lsp-ccls '(lsp dap))
(`ycmd (configuration-layer/declare-layer-dependencies '(ycmd))))) ('rtags '(ggtags))
('ycmd '(ycmd)))))

View File

@ -130,10 +130,10 @@
"ri" #'spacemacs/c++-organize-includes)))) "ri" #'spacemacs/c++-organize-includes))))
(defun c-c++/pre-init-dap-mode () (defun c-c++/pre-init-dap-mode ()
(pcase (spacemacs//c-c++-backend) (pcase c-c++-backend
(`lsp-clangd (add-to-list 'spacemacs--dap-supported-modes 'c-mode) ('lsp-clangd (add-to-list 'spacemacs--dap-supported-modes 'c-mode)
(add-to-list 'spacemacs--dap-supported-modes 'c++-mode)) (add-to-list 'spacemacs--dap-supported-modes 'c++-mode))
(`lsp-ccls (add-to-list 'spacemacs--dap-supported-modes 'c-mode) ('lsp-ccls (add-to-list 'spacemacs--dap-supported-modes 'c-mode)
(add-to-list 'spacemacs--dap-supported-modes 'c++-mode))) (add-to-list 'spacemacs--dap-supported-modes 'c++-mode)))
(add-hook 'c-mode-local-vars-hook #'spacemacs//c-c++-setup-dap) (add-hook 'c-mode-local-vars-hook #'spacemacs//c-c++-setup-dap)
(add-hook 'c++-mode-local-vars-hook #'spacemacs//c-c++-setup-dap)) (add-hook 'c++-mode-local-vars-hook #'spacemacs//c-c++-setup-dap))