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)
(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.
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
(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+.")
(defvar c-c++-adopt-subprojects nil

View File

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

View File

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