From dae0231fed07535248a9506007d86c1e930ceee3 Mon Sep 17 00:00:00 2001 From: Lucius Hu Date: Wed, 17 Mar 2021 23:41:34 -0400 Subject: [PATCH] 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` --- layers/+lang/c-c++/config.el | 4 +-- layers/+lang/c-c++/funcs.el | 63 +++++++++++++++------------------- layers/+lang/c-c++/layers.el | 13 +++---- layers/+lang/c-c++/packages.el | 6 ++-- 4 files changed, 39 insertions(+), 47 deletions(-) diff --git a/layers/+lang/c-c++/config.el b/layers/+lang/c-c++/config.el index b50129c7f..4c2d271ac 100644 --- a/layers/+lang/c-c++/config.el +++ b/layers/+lang/c-c++/config.el @@ -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 diff --git a/layers/+lang/c-c++/funcs.el b/layers/+lang/c-c++/funcs.el index 7d107be97..4fd351821 100644 --- a/layers/+lang/c-c++/funcs.el +++ b/layers/+lang/c-c++/funcs.el @@ -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 diff --git a/layers/+lang/c-c++/layers.el b/layers/+lang/c-c++/layers.el index 88e8ed762..d0f28fee1 100644 --- a/layers/+lang/c-c++/layers.el +++ b/layers/+lang/c-c++/layers.el @@ -21,9 +21,10 @@ ;; along with this program. If not, see . -(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))))) diff --git a/layers/+lang/c-c++/packages.el b/layers/+lang/c-c++/packages.el index 0f569f34a..5cfedb42c 100644 --- a/layers/+lang/c-c++/packages.el +++ b/layers/+lang/c-c++/packages.el @@ -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))