FG42/core/cubes/python.el

130 lines
3.9 KiB
EmacsLisp

;;; PythonCubes --- The Python cubes for FG42 -*- lexical-binding: t; -*-
;;
;; Copyright (c) 2010-2021 Sameer Rahmani & Contributors
;;
;; Author: Sameer Rahmani <lxsameer@gnu.org>
;; URL: https://gitlab.com/FG42/FG42
;; Version: 3.0.0
;;
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;
;;; Commentary:
;;; Code:
(require 'fpkg)
(require 'fg42/cube)
(require 'fg42/utils)
(defun fg42/py-workon-project-venv ()
"Call pyenv-workon with the current projectile project name.
This will return the full path of the associated virtual
environment found in $WORKON_HOME, or nil if the environment does
not exist."
(require 'pyenv)
(let ((pname (projectile-project-name)))
(pyvenv-workon pname)
(if (file-directory-p pyvenv-virtual-env)
pyvenv-virtual-env
(pyvenv-deactivate))))
(defun fg42/py-auto-lsp ()
"Turn on lsp mode in a Python project with some automated logic.
Try to automatically determine which pyenv virtual environment to
activate based on the project name, using
`dd/py-workon-project-venv'. If successful, call `lsp'. If we
cannot determine the virtualenv automatically, first call the
interactive `pyvenv-workon' function before `lsp'"
(interactive)
(let ((pvenv (fg42/py-workon-project-venv)))
(if pvenv
(lsp)
(progn
(call-interactively #'pyvenv-workon)
(lsp)))))
(defcube fg42/python-black-cube
"This cube reformats python code using black formatter tool."
(:title "Python lang server via pyls"
:flag python-black
:flag-default t)
(fpkg/use python-black
:demand t
:after python
:hook (python-mode . (lambda ()
(python-black-on-save-mode)))))
(defcube fg42/python-cube-pyls
"Python language server using pyls"
(:title "Python lang server via pyls"
:flag python-pyls)
(when-flag lsp
(with-eval-after-load "lsp"
(lsp-register-custom-settings
'(("pyls.plugins.pyls_mypy.enabled" t t)
("pyls.plugins.flake8.enabled" t t)
("pyls.plugins.pyls_mypy.live_mode" nil t)
("pyls.plugins.pyls_black.enabled" t t)
("pyls.plugins.pyls_isort.enabled" t t)))
(add-hook 'python-mode #'fg42/py-auto-lsp))))
(defcube fg42/python-cube-pyright
"Python language server using pyright."
(:title "Python lang server via pyright"
:flag python-pyright)
(fpkg/use lsp-pyright
:ensure t
:after (python lsp-mode)
:hook
(python-mode . (lambda ()
;; Pyls and pylsp can be annoying
(add-to-list 'lsp-disabled-clients 'pyls)
(add-to-list 'lsp-disabled-clients 'pylsp)
;;(fg42/py-auto-lsp)
(require 'lsp-pyright)
(lsp)))))
(defcube fg42/python-cube
"Python support cube."
(:title "Python cube"
:flag python)
(fpkg/use pyvenv
:defer t
:after python
:custom
(pyvenv-workon "env")
:hook
((python-mode-hook . pyvenv-mode)
(pyvenv-post-activate-hooks . pyvenv-restart-python)
(pyvenv-post-deactivate-hooks . pyvenv-restart-python)))
(fpkg/use pyenv-mode
:ensure t
:after python
:hook
(python-mode-hook . pyenv-mode))
(fg42/python-black-cube)
(fg42/python-cube-pyls)
(fg42/python-cube-pyright))
(provide 'cubes/python)
;;; python.el ends here