[PYTHON] Use pyright with CentOS7, emacs lsp-mode

Use pyright with CentOS7, emacs lsp-mode

How to use pyright in lsp-mode of CentOS7, emacs.

Main characters

--pyright: Microsoft python language server - https://github.com/microsoft/pyright --For language server and language server protocol, please see this article. -About language server protocol (Part 1) --lsp-mode: emacs minor mode to use language server functionality with language server protocol. --lsp-ui-mode: emacs minor mode that adds UI functions such as pop-up display to the functions of lsp-mode. --python-mode: emacs major mode for python.

environment

Insert node.js

pyright is written in node.js.

You can look around here. https://github.com/nodesource/distributions/blob/master/README.md#enterprise-linux-based-distributions

If you don't want to add the yum repository, download the rpm from here and put it in. https://rpm.nodesource.com/pub_14.x/el/7/x86_64/

$ node --version
v14.7.0

$ npm --version
6.14.5

Enter pyright

$ npm -g install pyright
$ pyright --version
pyright 1.1.61

It is pyright-langserver that is started from emacs. pyright-langserver is installed with pyright.

Put the package in emacs

lsp-pyright, lsp-mode, lsp-ui, company, imenu-list, flycheck can all be installed from melpa.

M-x package-install lsp-mode ...

lsp-mode

https://emacs-lsp.github.io/lsp-mode/

This is a package for using the functions of lsp with emacs. Not for Python, but generic. When used with Python, it is used as a minor mode of python-mode.

Company is recommended for code completion. http://company-mode.github.io/

company-lsp is no longer supported in lsp-mode 7.0.1. company is enough. https://github.com/emacs-lsp/lsp-mode/blob/master/CHANGELOG.org

Dropped support for company-lsp, the suggested provider is company-capf.

lsp-ui

A package that assists lsp-mode.

lsp-pyright

https://github.com/emacs-lsp/lsp-python-ms

An add-on package that allows you to use pyright in lsp-mode.

company

A package that provides IntelliSense-like code completion. Complementation is enhanced by including various backend packages.

lsp-mode (or rather language-server) acts as the backend for the company.

imenu-list

This is a package that displays the contents of ʻimenu` in an IDE-like side frame.

flycheck

A generic linter framework.

https://www.flycheck.org/en/latest/

Configure emacs

I'm using ʻuse-package. I cut and paste it from my ʻinit.el, so it may not make sense.

(use-package python
  :mode
  ("\\.py\\'" . python-mode)
  )

(use-package lsp-mode
  :config

  ;; .venv, .mypy_Exclude cache from watch
  (dolist (dir '(
                 "[/\\\\]\\.venv$"
                 "[/\\\\]\\.mypy_cache$"
                 "[/\\\\]__pycache__$"
                 ))
    (push dir lsp-file-watch-ignored))

  ;; lsp-See here for mode settings.
  ;; https://emacs-lsp.github.io/lsp-mode/page/settings/

  (setq lsp-auto-configure t)
  (setq lsp-enable-completion-at-point t)

  ;; imenu-I use list so I don't use imenu integration
  (setq lsp-enable-imenu nil)

  ;;Enable integration with cross-references
  ;; xref-find-definitions
  ;; xref-find-references
  (setq lsp-enable-xref t)

  ;;Use flycheck as linter framework
  (setq lsp-diagnostics-provider :flycheck)

  ;;Function information display in minibuffer
  (setq lsp-eldoc-enable-hover t)

  ;; nii:Make function information in the minibuffer only signature
  ;; t:Function information in the minibuffer, doc-Show string body
  (setq lsp-eldoc-render-all nil)

  ;; breadcrumb
  ;;Display the breadcrumb trail.
  (setq lsp-headerline-breadcrumb-enable t)
  (setq lsp-headerline-breadcrumb-segments '(project file symbols))

  ;; snippet
  (setq lsp-enable-snippet t)

  ;;Hook function definition
  ;; python-For mode, lsp-mode config
  (defun lsp/python-mode-hook
    ()
    (when (fboundp 'company-mode)
      ;;configure company
      (setq
       ;;Activate completion with one character
       company-minimum-prefix-length 1
       ;; default is 0.2
       company-idle-delay 0.0
       )
      )
    )

  :commands (lsp lsp-deferred)
  :hook
  (python-mode . lsp) ; python-lsp in mode-Enable mode
  (python-mode . lsp/python-mode-hook) ; python-Set up a hook function for mode
  )

(use-package lsp-pyright
  :init

  (defun lsp-pyright/python-mode-hook
    ()
    ;; lsp-Enable pyright
    (require 'lsp-pyright)
    (when (fboundp 'flycheck-mode)
        ;;lint with pyright, so python-I don't use mypy.
        (setq flycheck-disabled-checkers '(python-mypy))
        )
    )

  :hook (python-mode . lsp-pyright/python-mode-hook)
  )

(use-package lsp-ui
  :after lsp-mode
  :config

  ;; ui-Enable peek
  (setq lsp-ui-peek-enable t)

  ;;Even if there is only one candidate, peek is always displayed.
  (setq lsp-ui-peek-always-show t)

  ;;Display information such as flycheck on the sideline
  (setq lsp-ui-sideline-show-diagnostics t)
  ;;Show code actions on sideline
  (setq lsp-ui-sideline-show-code-actions t)
  ;;What is displayed on the hover is displayed on the sideline instead of the hover
  ;;(setq lsp-ui-sideline-show-hover t)

  :bind
  (:map lsp-ui-mode-map
        ;;Default xref-find-With definitions, you can jump, but ui-I can't use peek.
        ("M-." . lsp-ui-peek-find-definitions)

        ;;Default xref-find-With references, you can jump, but ui-I can't use peek.
        ("M-?" . lsp-ui-peek-find-references)
        )
  :hook
  (lsp-mode . lsp-ui-mode)
)

(use-package imenu-list)

(use-package company
  :init
  (global-company-mode t)
  )

(use-package flycheck
  :init
  (global-flycheck-mode)
  )

Recommended Posts

Use pyright with CentOS7, emacs lsp-mode
Use pyright with Spacemacs
Note until you use emacs with WSL
Play with CentOS 8
Use RTX 3090 with PyTorch
Use ansible with cygwin
Use pipdeptree with virtualenv
[Python] Use JSON with Python
Use Mock with pytest
Use indicator with pd.merge
Use Gentelella with django
Use mecab with Python3
Use tensorboard with Chainer
Use DynamoDB with Python
Use pip with MSYS2
Use Python 3.8 with Anaconda
Use python with docker
Use TypeScript with django-compressor
Use LESS with Django
Use MySQL with Django
Use Enums with SQLAlchemy
Use tensorboard with NNabla
Use GPS with Edison
Use nim with Jupyter
Use Trello API with python
Use shared memory with shared libraries
Use "$ in" operator with mongo-go-driver
Use custom tags with PyYAML
Use directional graphs with networkx
Use TensorFlow with Intellij IDEA
Use Twitter API with Python
Use pip with Jupyter Notebook
Use DATE_FORMAT with SQLAlchemy filter
Use TUN / TAP with Python
Use sqlite3 with NAO (Pepper)
Use sqlite load_extensions with Pyramid
Use Windows 10 fonts with WSL
Use chainer with Jetson TK1
Use SSL with Celery + Redis
Use Cython with Jupyter Notebook
Use Maxout + CNN with Pylearn2
Use WDC-433SU2M2 with Manjaro Linux
Use OpenBLAS with numpy, scipy
Use subsonic API with python3
Use Sonicwall NetExtener with Systemd
Use prefetch_related conveniently with Django
Use AWS interpreter with Pycharm
Use Bokeh with IPython Notebook
Use Python-like range with Rust