Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pyright/eglot config for emacs #1721

Merged
merged 8 commits into from
Feb 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 0 additions & 42 deletions docs/docs/usage/advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,48 +180,6 @@ COPY --from=builder /project/__pypackages__/3.8/lib /project/pkgs
CMD ["python", "-m", "project"]
```

## Integrate with other IDE or editors

### Work with lsp-python-ms in Emacs

Below is a sample code snippet showing how to make PDM work with [lsp-python-ms](https://github.com/emacs-lsp/lsp-python-ms) in Emacs. Contributed by [@linw1995](https://github.com/pdm-project/pdm/discussions/372#discussion-3303501).

```emacs-lisp
;; TODO: Cache result
(defun linw1995/pdm-get-python-executable (&optional dir)
(let ((pdm-get-python-cmd "pdm info --python"))
(string-trim
(shell-command-to-string
(if dir
(concat "cd "
dir
" && "
pdm-get-python-cmd)
pdm-get-python-cmd)))))

(defun linw1995/pdm-get-packages-path (&optional dir)
(let ((pdm-get-packages-cmd "pdm info --packages"))
(concat (string-trim
(shell-command-to-string
(if dir
(concat "cd "
dir
" && "
pdm-get-packages-cmd)
pdm-get-packages-cmd)))
"/lib")))

(use-package lsp-python-ms
:ensure t
:init (setq lsp-python-ms-auto-install-server t)
:hook (python-mode
. (lambda ()
(setq lsp-python-ms-python-executable (linw1995/pdm-get-python-executable))
(setq lsp-python-ms-extra-paths (vector (linw1995/pdm-get-packages-path)))
(require 'lsp-python-ms)
(lsp)))) ; or lsp-deferred
```

## Hooks for `pre-commit`

[`pre-commit`](https://pre-commit.com/) is a powerful framework for managing git hooks in a centralized fashion. PDM already uses `pre-commit` [hooks](https://github.com/pdm-project/pdm/blob/main/.pre-commit-config.yaml) for its internal QA checks. PDM exposes also several hooks that can be run locally or in CI pipelines.
Expand Down
76 changes: 75 additions & 1 deletion docs/docs/usage/pep582.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,78 @@ project's `pyproject.toml`.
extraPaths = ["__pypackages__/<major.minor>/lib/"]
```

### [Seek for other IDEs or editors](advanced.md#integrate-with-other-ide-or-editors)
### Emacs

You have a few options, but basically you'll want to tell an LSP client to add `__pypackages__` to the paths it looks at. Here are a few options that are available:

#### Using `pyproject.toml` and pyright

Add this to your project's `pyproject.toml`:

```toml
[tool.pyright]
extraPaths = ["__pypackages__/<major.minor>/lib/"]
```

#### eglot + pyright

Using [pyright](https://github.com/microsoft/pyright) and [eglot](https://github.com/joaotavora/eglot) (included in Emacs 29), add the following to your config:

```emacs-lisp
(defun get-pdm-packages-path ()
"For the current PDM project, find the path to the packages."
(let ((packages-path (string-trim (shell-command-to-string "pdm info --packages"))))
(concat packages-path "/lib")))

(defun my/eglot-workspace-config (server)
"For the current PDM project, dynamically generate a python lsp config."
`(:python\.analysis (:extraPaths ,(vector (get-pdm-packages-path)))))

(setq-default eglot-workspace-configuration #'my/eglot-workspace-config)
```

You'll want pyright installed either globally, or in your project (probably as a dev dependency). You can add this with, for example:

```bash
pdm add --dev --group devel pyright
```

#### LSP-Mode + lsp-python-ms

Below is a sample code snippet showing how to make PDM work with [lsp-python-ms](https://github.com/emacs-lsp/lsp-python-ms) in Emacs. Contributed by [@linw1995](https://github.com/pdm-project/pdm/discussions/372#discussion-3303501).

```emacs-lisp
;; TODO: Cache result
(defun linw1995/pdm-get-python-executable (&optional dir)
(let ((pdm-get-python-cmd "pdm info --python"))
(string-trim
(shell-command-to-string
(if dir
(concat "cd "
dir
" && "
pdm-get-python-cmd)
pdm-get-python-cmd)))))

(defun linw1995/pdm-get-packages-path (&optional dir)
(let ((pdm-get-packages-cmd "pdm info --packages"))
(concat (string-trim
(shell-command-to-string
(if dir
(concat "cd "
dir
" && "
pdm-get-packages-cmd)
pdm-get-packages-cmd)))
"/lib")))

(use-package lsp-python-ms
:ensure t
:init (setq lsp-python-ms-auto-install-server t)
:hook (python-mode
. (lambda ()
(setq lsp-python-ms-python-executable (linw1995/pdm-get-python-executable))
(setq lsp-python-ms-extra-paths (vector (linw1995/pdm-get-packages-path)))
(require 'lsp-python-ms)
(lsp)))) ; or lsp-deferred
```
1 change: 1 addition & 0 deletions news/1721.doc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add config example for Emacs using eglot + pyright