Improve the eglot configuration according to the updated README.md

This commit is contained in:
Gerard Vermeulen 2022-09-18 11:14:56 +02:00
parent 02cfb36efc
commit d6212ff231
1 changed files with 51 additions and 30 deletions

View File

@ -3874,47 +3874,64 @@ and listing [[lst:setting-python-shell-virtualenv-root]] to set
:CUSTOM_ID: sec:eglot-python
:END:
Listing [[lst:configure-eglot+python-lsp-server-for-python]] configures [[https://github.com/joaotavora/eglot][eglot]] for
[[https://www.python.org][Python]] using the [[https://github.com/python-lsp/python-lsp-server][python-lsp-server]]. In order to enable all builtin
Listing [[lst:configure-eglot+pylsp-with-pyls-flake8]] configures [[https://github.com/joaotavora/eglot][eglot]] for [[https://www.python.org][Python]]
using the [[https://github.com/python-lsp/python-lsp-server][python-lsp-server]] with the [[https://github.com/emanspeaks/pyls-flake8#readme][pyls-flake8]] plugin for the easiest way to
let [[https://github.com/python-lsp/python-lsp-server][python-lsp-server]] use [[https://github.com/PyCQA/flake8][flake8]]. In order to enable all builtin
[[https://github.com/python-lsp/python-lsp-server][python-lsp-server]] capabilities, ensure installation of the Python packages
[[https://github.com/hhatto/autopep8#readme][autopep8]], [[https://github.com/PyCQA/flake8][flake8]], [[https://github.com/PyCQA/pydocstyle#readme][pydocstyle]], [[https://github.com/PyCQA/pylint#readme][pylint]], [[https://github.com/python-lsp/python-lsp-server][python-lsp-server]], [[https://github.com/python-rope/rope#readme][rope]], and [[https://github.com/google/yapf#readme][yapf]]. In
addition, install the [[https://github.com/emanspeaks/pyls-flake8#readme][pyls-flake8]] plugin to let [[https://github.com/python-lsp/python-lsp-server][python-lsp-server]] use [[https://github.com/PyCQA/flake8][flake8]].
The latest [[https://github.com/python-lsp/python-lsp-server#readme][python-lsp-server]] documentation tells to let it use [[https://github.com/PyCQA/flake8][flake8]] as in
[[lst:broken-configure-eglot+python-lsp-server-for-python]], but it does not work.
[[https://github.com/hhatto/autopep8#readme][autopep8]], [[https://github.com/PyCQA/flake8][flake8]], [[https://github.com/PyCQA/pydocstyle#readme][pydocstyle]], [[https://github.com/PyCQA/pylint#readme][pylint]], [[https://github.com/python-lsp/python-lsp-server][python-lsp-server]], [[https://github.com/python-rope/rope#readme][rope]], and [[https://github.com/google/yapf#readme][yapf]]. The
latest [[https://github.com/python-lsp/python-lsp-server#readme][python-lsp-server]] documentation tells to configure it for [[https://github.com/PyCQA/flake8][flake8]] as in
listing [[lst:configure-eglot+pylsp-sans-pyls-flake8]]. This method is under
investigation (it feels flaky since it shows some error messages twice).
Listing [[lst:eglot-directory-variables-for-python]] shows a proper [[info:emacs#Directory Variables][.dir-locals.el]]
file in the root directory of any [[https://www.python.org][Python]] project to start [[https://github.com/joaotavora/eglot][eglot]] automatically
according to the configuration in listing [[lst:eglot-maybe-ensure]].
according to the configuration in listing [[lst:eglot-maybe-ensure]]. Type
{{{kbd(M-x eglot-show-workspace-configuration)}}} to dump a =JSON=
representation of src_emacs-lisp{eglot-workspace-configuration} for debugging.
The comment in listing [[lst:ensure-eglot-installation]] also shows how to increase
the the verbosity of [[https://github.com/python-lsp/python-lsp-server][python-lsp-server]] log output for debugging.
#+caption[Configure =eglot= with =python-lsp-server= for Python]:
#+caption: Configure =eglot= with =python-lsp-server= for Python.
#+name: lst:configure-eglot+python-lsp-server-for-python
#+caption[Configure =eglot= with =python-lsp-server= with =pyls_flake8=]:
#+caption: Configure =eglot= with =python-lsp-server= with =pyls_flake8=.
#+name: lst:configure-eglot+pylsp-with-pyls-flake8
#+begin_src emacs-lisp
(with-eval-after-load 'eglot
(setq-default
eglot-workspace-configuration
'(;; Disable the `:pyls_flake8' plugin to fall back to pycodestyle.
(:pylsp . (:plugins (:pyls_flake8 (:enabled t))))
(:pylsp . (:plugins (:jedi (:auto_import_modules ["numpy"]))))
(:pylsp . (:plugins (:jedi_completion (:cache_for ["astropy"])))))))
;; Disable the `:pyls_flake8' plugin to fall back to pycodestyle.
`((:pylsp . (:plugins
(:pyls_flake8
(:enabled t)
:jedi
(:auto_import_modules ["numpy"])
:jedi_completion
(:cache_for ["astropy"])))))))
#+end_src
#+caption[Broken configure =eglot= with =python-lsp-server= for Python]:
#+caption: Broken configure =eglot= with =python-lsp-server= for Python.
#+name: lst:broken-configure-eglot+python-lsp-server-for-python
#+caption[Configure =eglot= with =python-lsp-server= sans =pyls_flake8=]:
#+caption: Configure =eglot= with =python-lsp-server= sans =pyls_flake8=.
#+name: lst:configure-eglot+pylsp-sans-pyls-flake8
#+begin_src emacs-lisp :tangle no
(with-eval-after-load 'eglot
(setq-default
eglot-workspace-configuration
'(;; To use flake8 instead of pycodestyle, see:
'(;; How to use flake8 instead of pycodestyle officially, see:
;; https://github.com/python-lsp/python-lsp-server#configuration
(:pylsp . (:configurationSources ["flake8"]))
(:pylsp . (:plugins (:pycodestyle (:enabled :json-false))))
(:pylsp . (:plugins (:mccabe (:enabled :json-false))))
(:pylsp . (:plugins (:pyflakes (:enabled :json-false))))
(:pylsp . (:plugins (:flake8 (:enabled t))))
(:pylsp . (:plugins (:jedi (:auto_import_modules ["numpy"]))))
(:pylsp . (:plugins (:jedi_completion (:cache_for ["astropy"])))))))
(:pylsp . (:configurationSources
["flake8"]
:plugins
(:pycodestyle
(:enabled :json-false)
:mccabe
(:enabled :json-false)
:pyflakes
(:enabled
:json-false)
:flake8
(:enabled t)
:jedi
(:auto_import_modules ["numpy"])
:jedi_completion
(:cache_for ["astropy"])))))))
#+end_src
#+caption[Propose =directory-variables= to launch =eglot=]:
@ -3925,10 +3942,14 @@ according to the configuration in listing [[lst:eglot-maybe-ensure]].
;; Proposal for a .dir-locals.el file in the root of any Python project.
((python-mode
. ((eglot-workspace-configuration
. (;; Disable the `:pyls_flake8' plugin to fall back to pycodestyle.
(:pylsp . (:plugins (:pyls_flake8 (:enabled t))))
(:pylsp . (:plugins (:jedi (:auto_import_modules ["numpy"]))))
(:pylsp . (:plugins (:jedi_completion (:cache_for ["astropy"])))))))))
;; Disable the `:pyls_flake8' plugin to fall back to pycodestyle.
. ((:pylsp . (:plugins
(:pyls_flake8
(:enabled t)
:jedi
(:auto_import_modules ["numpy"])
:jedi_completion
(:cache_for ["astropy"])))))))))
#+end_src
#+attr_latex: :booktabs yes :float table