From 26eb3df2944f4de30c98c0c0ec3599e30a89c53d Mon Sep 17 00:00:00 2001 From: Lucius Hu Date: Thu, 18 Mar 2021 00:53:49 -0400 Subject: [PATCH] python: refactor - Moved backend determination to `config.el` - Replaced unnecessary backquote construct with simple quotation - Replaced `pcase` form with only one-arm with `when` or `unless` form - Replaccd `(cond ((eq foo) bar))` form with `(pcase (foo bar))` --- layers/+lang/python/config.el | 8 ++--- layers/+lang/python/funcs.el | 53 +++++++++++++-------------------- layers/+lang/python/packages.el | 22 +++++++------- 3 files changed, 35 insertions(+), 48 deletions(-) diff --git a/layers/+lang/python/config.el b/layers/+lang/python/config.el index 065108286..5714e1b27 100644 --- a/layers/+lang/python/config.el +++ b/layers/+lang/python/config.el @@ -26,7 +26,7 @@ (spacemacs|define-jump-handlers python-mode) (spacemacs|define-jump-handlers cython-mode anaconda-mode-goto) -(defvar python-backend 'nil +(defvar python-backend (if (configuration-layer/layer-used-p 'lsp) 'lsp 'anaconda) "The backend to use for IDE features. Possible values are `anaconda'and `lsp'. If `nil' then `anaconda' is the default backend unless `lsp' layer is used.") @@ -44,9 +44,9 @@ and `mspyls'") (defvar python-poetry-activate nil "If non-nil, activate poetry before enabling backend") -(defvar python-formatter nil - "The formatter to use. Possible values are `yapf', - `black' and `lsp'.") +(defvar python-formatter (if (configuration-layer/layer-used-p 'lsp) 'lsp 'yapf) + "The formatter to use. Possible values are `yapf', `black' and `lsp'. +If nil then `yapf' is the default formatter unless `lsp' layer is used.") (defvar python-format-on-save nil "If non-nil, automatically format code with formatter selected diff --git a/layers/+lang/python/funcs.el b/layers/+lang/python/funcs.el index 214d370ff..81b35a2a0 100644 --- a/layers/+lang/python/funcs.el +++ b/layers/+lang/python/funcs.el @@ -21,22 +21,6 @@ ;; along with this program. If not, see . -(defun spacemacs//python-backend () - "Returns selected backend." - (if python-backend - python-backend - (cond - ((configuration-layer/layer-used-p 'lsp) 'lsp) - (t 'anaconda)))) - -(defun spacemacs//python-formatter () - "Returns selected backend." - (if python-formatter - python-formatter - (cond - ((configuration-layer/layer-used-p 'lsp) 'lsp) - (t 'yapf)))) - (defun spacemacs//poetry-activate () "Attempt to activate Poetry only if its configuration file is found." (let ((root-path (locate-dominating-file default-directory "pyproject.toml"))) @@ -49,26 +33,27 @@ "Conditionally setup python backend." (when python-pipenv-activate (pipenv-activate) python-poetry-activate (spacemacs//poetry-activate)) - (pcase (spacemacs//python-backend) - (`anaconda (spacemacs//python-setup-anaconda)) - (`lsp (spacemacs//python-setup-lsp)))) + (pcase python-backend + ('anaconda (spacemacs//python-setup-anaconda)) + ('lsp (spacemacs//python-setup-lsp)))) (defun spacemacs//python-setup-company () "Conditionally setup company based on backend." - (pcase (spacemacs//python-backend) - (`anaconda (spacemacs//python-setup-anaconda-company)))) + (when (eq python-backend 'anaconda) + (spacemacs//python-setup-anaconda-company))) (defun spacemacs//python-setup-dap () "Conditionally setup elixir DAP integration." ;; currently DAP is only available using LSP - (pcase (spacemacs//python-backend) - (`lsp (spacemacs//python-setup-lsp-dap)))) + (when (eq python-backend 'lsp) + (spacemacs//python-setup-lsp-dap))) (defun spacemacs//python-setup-eldoc () "Conditionally setup eldoc based on backend." - (pcase (spacemacs//python-backend) + (when (eq python-backend 'anaconda) ;; lsp setup eldoc on its own - (`anaconda (spacemacs//python-setup-anaconda-eldoc)))) + (spacemacs//python-setup-anaconda-eldoc))) + ;; anaconda @@ -104,8 +89,9 @@ "Setup lsp backend." (if (configuration-layer/layer-used-p 'lsp) (progn - (cond ((eq python-lsp-server 'mspyls) (require 'lsp-python-ms)) - ((eq python-lsp-server 'pyright) (require 'lsp-pyright))) + (require (pcase python-lsp-server + ('mspyls 'lsp-python-ms) + ('pyright 'lsp-pyright))) (lsp)) (message "`lsp' layer is not installed, please add `lsp' layer to your dotfile."))) @@ -404,17 +390,18 @@ to be called for each testrunner. " "Bind the python formatter keys. Bind formatter to '==' for LSP and '='for all other backends." (spacemacs/set-leader-keys-for-major-mode 'python-mode - (if (eq (spacemacs//python-backend) 'lsp) + (if (eq python-backend 'lsp) "==" - "=") 'spacemacs/python-format-buffer)) + "=") + 'spacemacs/python-format-buffer)) (defun spacemacs/python-format-buffer () "Bind possible python formatters." (interactive) - (pcase (spacemacs//python-formatter) - (`yapf (yapfify-buffer)) - (`black (blacken-buffer)) - (`lsp (lsp-format-buffer)) + (pcase python-formatter + ('yapf (yapfify-buffer)) + ('black (blacken-buffer)) + ('lsp (lsp-format-buffer)) (code (message "Unknown formatter: %S" code)))) diff --git a/layers/+lang/python/packages.el b/layers/+lang/python/packages.el index f9d13cc24..8aaed6283 100644 --- a/layers/+lang/python/packages.el +++ b/layers/+lang/python/packages.el @@ -66,7 +66,7 @@ (defun python/init-anaconda-mode () (use-package anaconda-mode - :if (eq (spacemacs//python-backend) 'anaconda) + :if (eq python-backend 'anaconda) :defer t :init (setq anaconda-mode-installation-directory @@ -110,7 +110,7 @@ (defun python/init-company-anaconda () (use-package company-anaconda - :if (eq (spacemacs//python-backend) 'anaconda) + :if (eq python-backend 'anaconda) :defer t)) ;; see `spacemacs//python-setup-anaconda-company' @@ -121,7 +121,7 @@ (progn (spacemacs//bind-python-formatter-keys) (when (and python-format-on-save - (eq 'black (spacemacs//python-formatter))) + (eq 'black python-formatter)) (add-hook 'python-mode-hook 'blacken-mode))) :config (spacemacs|hide-lighter blacken-mode))) @@ -129,14 +129,14 @@ (use-package cython-mode :defer t :config - (when (eq (spacemacs//python-backend) 'anaconda) + (when (eq python-backend 'anaconda) (spacemacs/set-leader-keys-for-major-mode 'cython-mode "hh" 'anaconda-mode-show-doc "gu" 'anaconda-mode-find-references)))) (defun python/pre-init-dap-mode () - (pcase (spacemacs//python-backend) - (`lsp (add-to-list 'spacemacs--dap-supported-modes 'python-mode))) + (when (eq python-backend 'lsp) + (add-to-list 'spacemacs--dap-supported-modes 'python-mode)) (add-hook 'python-mode-local-vars-hook #'spacemacs//python-setup-dap)) (defun python/post-init-eldoc () @@ -287,11 +287,11 @@ :init (progn (pcase python-auto-set-local-pyenv-version - (`on-visit + ('on-visit (dolist (m spacemacs--python-pyenv-modes) (add-hook (intern (format "%s-hook" m)) 'spacemacs//pyenv-mode-set-local-version))) - (`on-project-switch + ('on-project-switch (add-hook 'projectile-after-switch-project-hook 'spacemacs//pyenv-mode-set-local-version))) ;; setup shell correctly on environment switch @@ -310,11 +310,11 @@ (progn (add-hook 'python-mode-hook #'pyvenv-tracking-mode) (pcase python-auto-set-local-pyvenv-virtualenv - (`on-visit + ('on-visit (dolist (m spacemacs--python-pyvenv-modes) (add-hook (intern (format "%s-hook" m)) 'spacemacs//pyvenv-mode-set-local-virtualenv))) - (`on-project-switch + ('on-project-switch (add-hook 'projectile-after-switch-project-hook 'spacemacs//pyvenv-mode-set-local-virtualenv))) (dolist (m spacemacs--python-pyvenv-modes) @@ -476,7 +476,7 @@ fix this issue." (progn (spacemacs//bind-python-formatter-keys) (when (and python-format-on-save - (eq 'yapf (spacemacs//python-formatter))) + (eq 'yapf python-formatter)) (add-hook 'python-mode-hook 'yapf-mode))) :config (spacemacs|hide-lighter yapf-mode)))