Skip to content

Commit

Permalink
python: add support for Language Server Protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
syl20bnr committed Feb 19, 2018
1 parent 66afcce commit 81a931f
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 241 deletions.
57 changes: 50 additions & 7 deletions layers/+lang/python/README.org
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
- [[#features][Features:]]
- [[#install][Install]]
- [[#layer][Layer]]
- [[#dependencies][Dependencies]]
- [[#auto-completion-anaconda-dependencies][Auto-completion: Anaconda dependencies]]
- [[#choosing-a-backend][Choosing a backend]]
- [[#backends][Backends]]
- [[#anaconda][Anaconda]]
- [[#language-server-protocol][Language Server Protocol]]
- [[#additional-tools][Additional tools]]
- [[#syntax-checking][Syntax checking]]
- [[#test-runner][Test runner]]
- [[#automatic-buffer-formatting-on-save][Automatic buffer formatting on save]]
Expand Down Expand Up @@ -37,9 +40,12 @@
This layer adds support for the Python language.

** Features:
- Auto-completion using [[https://github.com/proofit404/anaconda-mode][anaconda-mode]]
- Code Navigation using [[https://github.com/proofit404/anaconda-mode][anaconda-mode]]
- Documentation Lookup using [[https://github.com/proofit404/anaconda-mode][anaconda-mode]] and [[https://github.com/tsgates/pylookup][pylookup]]
- Support for the following backends:
- [[https://github.com/proofit404/anaconda-mode][anaconda]] (default),
- [[https://github.com/emacs-lsp/lsp-python][Language Server Protocol]] (experimental),
- Auto-completion
- Code Navigation
- Documentation Lookup using [[https://github.com/proofit404/anaconda-mode][anaconda-mode]] and [[https://github.com/tsgates/pylookup][pylookup]]
- Test Runners using [[https://github.com/syl20bnr/nose.el][nose.el]] or [[https://github.com/ionrock/pytest-el][pytest]]
- Virtual Environment using [[https://github.com/jorgenschaefer/pyvenv][pyvenv]] and [[https://github.com/yyuu/pyenv][pyenv]]
- semantic mode is enabled
Expand All @@ -57,8 +63,28 @@ To use this configuration layer, add it to your =~/.spacemacs=. You will need to
add =python= to the existing =dotspacemacs-configuration-layers= list in this
file.

** Dependencies
*** Auto-completion: Anaconda dependencies
** Choosing a backend
To choose a default backend set the layer variable =python-backend=:

#+BEGIN_SRC elisp
(python :variables python-backend 'anaconda)
#+END_SRC

Backend can be chosen on a per project basis using directory local variables
(files named =.dir-locals.el= at the root of a project), an example to use the
=lsp= backend:

#+BEGIN_SRC elisp
;;; Directory Local Variables
;;; For more information see (info "(emacs) Directory Variables")

((python-mode (python-backend . lsp)))
#+END_SRC

*Note:* you can easily add a directory local variable with ~SPC f v d~.

* Backends
** Anaconda
=anaconda-mode= tries to install the dependencies itself but sometimes
it does not work and you may encounter the following message when
opening a python buffer:
Expand All @@ -82,6 +108,23 @@ If you are facing errors such as "Unable to run anaconda-mode server", try
setting your =PYTHONPATH= as explained at
https://github.com/proofit404/anaconda-mode#pythonpath

** Language Server Protocol
You just have to install python language server package:

#+begin_src sh
pip install python-language-server
#+end_src

Additionally you can install the following other packages:

#+begin_src sh
# for import sorting
pip install pyls-isort
# for mypy checking (python 3.4+ is needed)
pip install pyls-mypy
#+end_src

* Additional tools
*** Syntax checking
Syntax checking uses =flake8= package:

Expand Down
4 changes: 4 additions & 0 deletions layers/+lang/python/config.el
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
(spacemacs|define-jump-handlers python-mode)
(spacemacs|define-jump-handlers cython-mode anaconda-mode-goto)

(defvar python-backend 'anaconda
"The backend to use for IDE features. Possible values are `anaconda'
and `lsp'.")

(defvar python-enable-yapf-format-on-save nil
"If non-nil, automatically format code with YAPF on save.")

Expand Down
98 changes: 76 additions & 22 deletions layers/+lang/python/funcs.el
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,82 @@
;;
;;; License: GPLv3

(defun spacemacs//python-setup-backend ()
"Conditionally setup python backend."
(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 python-backend
(`anaconda (spacemacs//python-setup-anaconda-company))
(`lsp (spacemacs//python-setup-lsp-company))))

(defun spacemacs//python-setup-eldoc ()
"Conditionally setup eldoc based on backend."
(pcase python-backend
;; lsp setup eldoc on its own
(`anaconda (spacemacs//python-setup-anaconda-eldoc))))


;; anaconda

(defun spacemacs//python-setup-anaconda ()
"Setup anaconda backend."
(anaconda-mode)
(add-to-list 'spacemacs-jump-handlers-python-mode
'(anaconda-mode-find-definitions :async t)))

(defun spacemacs//python-setup-anaconda-company ()
"Setup anaconda auto-completion."
(spacemacs|add-company-backends
:backends company-anaconda
:modes python-mode)
(company-mode))

(defun spacemacs//python-setup-anaconda-eldoc ()
"Setup anaconda eldoc."
(eldoc-mode)
(when (configuration-layer/package-used-p 'anaconda-mode)
(anaconda-eldoc-mode)))

(defun spacemacs/anaconda-view-forward-and-push ()
"Find next button and hit RET"
(interactive)
(forward-button 1)
(call-interactively #'push-button))

(defun spacemacs//disable-semantic-idle-summary-mode ()
"Disable semantic-idle-summary in Python mode.
Anaconda provides more useful information but can not do it properly
when this mode is enabled since the minibuffer is cleared all the time."
(semantic-idle-summary-mode 0))


;; lsp

(defun spacemacs//python-setup-lsp ()
"Setup lsp backend."
(if (configuration-layer/layer-used-p 'lsp)
(progn
(require 'lsp-python)
(lsp-python-enable))
(message "`lsp' layer is not installed, please add `lsp' layer to your dofile.")))

(defun spacemacs//python-setup-lsp-company ()
"Setup lsp auto-completion."
(if (configuration-layer/layer-used-p 'lsp)
(progn
(spacemacs|add-company-backends
:backends company-lsp
:modes python-mode)
(company-mode))
(message "`lsp' layer is not installed, please add `lsp' layer to your dofile.")))


;; others

(defun spacemacs//python-default ()
"Defaut settings for python buffers"
(setq mode-name "Python"
Expand Down Expand Up @@ -138,12 +214,6 @@ as the pyenv version then also return nil. This works around https://github.com/

;; Tests

(defun spacemacs//disable-semantic-idle-summary-mode ()
"Disable semantic-idle-summary in Python mode.
Anaconda provides more useful information but can not do it properly
when this mode is enabled since the minibuffer is cleared all the time."
(semantic-idle-summary-mode 0))

(defun spacemacs//python-imenu-create-index-use-semantic-maybe ()
"Use semantic if the layer is enabled."
(setq imenu-create-index-function 'spacemacs/python-imenu-create-index))
Expand Down Expand Up @@ -251,14 +321,6 @@ to be called for each testrunner. "
(derived-mode-p 'python-mode))
(py-isort-before-save)))


;;* Anaconda
(defun spacemacs/anaconda-view-forward-and-push ()
"Find next button and hit RET"
(interactive)
(forward-button 1)
(call-interactively #'push-button))


;; REPL

Expand Down Expand Up @@ -336,11 +398,3 @@ to be called for each testrunner. "
(advice-add 'wisent-python-default-setup :after
#'spacemacs//python-imenu-create-index-use-semantic-maybe))


;; Eldoc

(defun spacemacs//init-eldoc-python-mode ()
"Initialize elddoc for python buffers"
(eldoc-mode)
(when (configuration-layer/package-used-p 'anaconda-mode)
(anaconda-eldoc-mode)))
32 changes: 18 additions & 14 deletions layers/+lang/python/packages.el
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
hy-mode
importmagic
live-py-mode
(lsp-python :requires lsp-mode)
(nose :location local)
org
pip-requirements
Expand All @@ -46,13 +47,8 @@
(defun python/init-anaconda-mode ()
(use-package anaconda-mode
:defer t
:init
(progn
(setq anaconda-mode-installation-directory
(concat spacemacs-cache-directory "anaconda-mode"))
(add-hook 'python-mode-hook 'anaconda-mode)
(add-to-list 'spacemacs-jump-handlers-python-mode
'(anaconda-mode-find-definitions :async t)))
:init (setq anaconda-mode-installation-directory
(concat spacemacs-cache-directory "anaconda-mode"))
:config
(progn
(spacemacs/set-leader-keys-for-major-mode 'python-mode
Expand All @@ -75,6 +71,8 @@
(evil--jumps-push)))))

(defun python/post-init-company ()
;; backend specific
(add-hook 'python-mode-local-vars-hook #'spacemacs//python-setup-company)
(spacemacs|add-company-backends
:backends (company-files company-capf)
:modes inferior-python-mode
Expand All @@ -89,9 +87,8 @@
(defun python/init-company-anaconda ()
(use-package company-anaconda
:defer t
:init (spacemacs|add-company-backends
:backends company-anaconda
:modes python-mode)))
;; see `spacemacs//python-setup-anaconda-company'
))

(defun python/init-cython-mode ()
(use-package cython-mode
Expand All @@ -103,7 +100,7 @@
"gu" 'anaconda-mode-find-references))))

(defun python/post-init-eldoc ()
(add-hook 'python-mode-hook 'spacemacs//init-eldoc-python-mode))
(add-hook 'python-mode-local-vars-hook #'spacemacs//python-setup-eldoc))

(defun python/post-init-evil-matchit ()
(add-hook `python-mode-hook `turn-on-evil-matchit-mode))
Expand Down Expand Up @@ -164,6 +161,10 @@
(spacemacs/set-leader-keys-for-major-mode 'python-mode
"l" 'live-py-mode)))

(defun python/init-lsp-python ()
(use-package lsp-python
:commands lsp-python-enable))

(defun python/init-nose ()
(use-package nose
:commands (nosetests-one
Expand Down Expand Up @@ -282,10 +283,13 @@
:mode (("SConstruct\\'" . python-mode) ("SConscript\\'" . python-mode))
:init
(progn
(spacemacs/register-repl 'python 'spacemacs/python-start-or-switch-repl "python")
(spacemacs/register-repl 'python
'spacemacs/python-start-or-switch-repl "python")
(add-hook 'inferior-python-mode-hook
#'spacemacs//inferior-python-setup-hook)
(add-hook 'python-mode-hook #'spacemacs//python-default)
'spacemacs//inferior-python-setup-hook)
(spacemacs/add-to-hook 'python-mode-hook
'(spacemacs//python-setup-backend
spacemacs//python-default))
;; call `spacemacs//python-setup-shell' once, don't put it in a hook
;; (see issue #5988)
(spacemacs//python-setup-shell))
Expand Down
48 changes: 0 additions & 48 deletions layers/+tools/lsp-python/README.org

This file was deleted.

29 changes: 0 additions & 29 deletions layers/+tools/lsp-python/config.el

This file was deleted.

Loading

0 comments on commit 81a931f

Please sign in to comment.