Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use lsp-mode to complete Python under Emacs

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Today, I will talk to you about how to use lsp-mode to complete Python under Emacs. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

What is LSP?

LSP, whose full name is Language Server protocol, is a general language analyzer protocol developed by Microsoft. Originally developed for Visual Studio Code, it is now an open standard 1. It is a JSON-RPC-based protocol 2 for communication between editors or IDE and services that provide functions such as language analysis and completion. Editor / IDE and language services can even be used on different machines. Richard Stallman also supported the agreement.

Prepare the Python environment side

Python Language Server is the implementation of Python language service, complying with the LSP protocol. Using jedi provides functions such as completion.

Use pipenv to manage the Python environment

Cd project_floderpipenv install python-language-server [all] # pipenv install python-language-server [all]-- skip-lockEmacs editor side

The following packages need to be installed. If you use the elpa management package, you can install Mmurx package-install after adding the melpa source.

LSP protocol library under lsp-mode # Emacs

Company-lsp # uses company to provide a complete backend

Lsp-ui # provides features such as flycheck

The preparatory phase of Python and Emacs is over.

Emacs configuration

Use-package is required to be installed to use the following configuration

(use-package lsp-mode: config (add-hook 'python-mode-hook (lambda () (lsp-python-enable);; call the Python Language Server installed above pyls No need to install lsp-python The following lines provide the same functionality as lsp-python (lsp-define-stdio-client lsp-python "python" (lsp-make-traverser #'(lambda (dir)) (directory-files dir nil "\ (_ _ init__\\ | setup\)\) .py\\ | Pipfile "))'(" pyls ") (use-package lsp-imenu: init ; enable lsp-imenu integration (add-hook 'lsp-after-open-hook' lsp-enable-imenu) (use-package lsp-ui: init; enable lsp-ui (add-hook 'lsp-mode-hook' lsp-ui-mode); enable flycheck (add-hook 'python-mode-hook' flycheck-mode)) (use-package company-lsp: config Set company-lsp to backend (push 'company-lsp company-backends) other

Python Language Server does not have to be installed in every environment. Take pipenv as an example, install Python Language Server into the global Python, and then pipenv shell enters the environment in each environment to complete the package in that environment.

(lsp-define-stdio-client lsp-python "python" (lsp-make-traverser #'(lambda (dir) (directory-files dir nil "\ (_ _ init__\\ | setup\)\. Py\\ | Pipfile"))'("pyls")

This configuration explains the need to find files such as _ _ init__.py in the directory and use them as project root. If the corresponding file is not found, the Warning that cannot find the project root directory will be thrown, and it will be displayed in the form of * Warnings* buffer. Add the following sentence after lsp-mode: config, you can display Warning in the form of Message instead of buffer.

(setq lsp-message-project-root-warning t) after reading the above, do you have any further understanding of how to use lsp-mode to complete Python under Emacs? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report