From b78656397c5b754ab617960ba9080404236a3c99 Mon Sep 17 00:00:00 2001 From: pakelley Date: Fri, 17 Feb 2023 15:06:17 -0800 Subject: [PATCH 1/8] Add pyright/eglot config for emacs --- docs/docs/usage/advanced.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/docs/docs/usage/advanced.md b/docs/docs/usage/advanced.md index 0c908d67bb..8348e60cb5 100644 --- a/docs/docs/usage/advanced.md +++ b/docs/docs/usage/advanced.md @@ -222,6 +222,28 @@ Below is a sample code snippet showing how to make PDM work with [lsp-python-ms] (lsp)))) ; or lsp-deferred ``` +## Work with pyright and eglot in Emacs + +Using [pyright](https://github.com/microsoft/pyright) and [eglot](https://github.com/joaotavora/eglot) (which is included in Emacs 29), add the following to your config: + +```emacs-lisp +(defun maybe-from-dir (cmd &optional dir) + (if dir + (format "cd %s && %s" dir cmd) + cmd)) + +(defun get-packages-path (&optional dir) + (let* ((get-packages-cmd-str (maybe-from-dir "pdm info --packages" dir)) + (get-packages-cmd (string-trim (shell-command-to-string get-packages-cmd-str)))) + (concat get-packages-cmd "/lib"))) + +(defun my/eglot-workspace-config (server) + (list :python.analysis + (list :extraPaths (vector (get-packages-path))))) + +(setq-default eglot-workspace-configuration #'my/eglot-workspace-config) +``` + ## 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. From 07892697a5527260f1d19cc61ded9d7eef8ec15c Mon Sep 17 00:00:00 2001 From: pakelley Date: Fri, 17 Feb 2023 15:11:19 -0800 Subject: [PATCH 2/8] Create 1721.doc.md --- news/1721.doc.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 news/1721.doc.md diff --git a/news/1721.doc.md b/news/1721.doc.md new file mode 100644 index 0000000000..6ac45dce63 --- /dev/null +++ b/news/1721.doc.md @@ -0,0 +1 @@ +Add config example for Emacs using eglot + pyright From e28dfe6a040817b7c7f067cea06b3c8b16938e57 Mon Sep 17 00:00:00 2001 From: pakelley Date: Fri, 17 Feb 2023 15:15:31 -0800 Subject: [PATCH 3/8] Clean up formatting --- docs/docs/usage/advanced.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/docs/docs/usage/advanced.md b/docs/docs/usage/advanced.md index 8348e60cb5..4aaa299ea4 100644 --- a/docs/docs/usage/advanced.md +++ b/docs/docs/usage/advanced.md @@ -182,7 +182,9 @@ CMD ["python", "-m", "project"] ## Integrate with other IDE or editors -### Work with lsp-python-ms in Emacs +### Emacs + +#### 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). @@ -222,7 +224,7 @@ Below is a sample code snippet showing how to make PDM work with [lsp-python-ms] (lsp)))) ; or lsp-deferred ``` -## Work with pyright and eglot in Emacs +#### eglot + pyright Using [pyright](https://github.com/microsoft/pyright) and [eglot](https://github.com/joaotavora/eglot) (which is included in Emacs 29), add the following to your config: From 41da44bca0ca522432f267411b3a25959f6faa1f Mon Sep 17 00:00:00 2001 From: pakelley Date: Fri, 17 Feb 2023 16:57:35 -0800 Subject: [PATCH 4/8] Clean up pyright/eglot config code a bit --- docs/docs/usage/advanced.md | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/docs/docs/usage/advanced.md b/docs/docs/usage/advanced.md index 4aaa299ea4..8ee391049d 100644 --- a/docs/docs/usage/advanced.md +++ b/docs/docs/usage/advanced.md @@ -229,20 +229,15 @@ Below is a sample code snippet showing how to make PDM work with [lsp-python-ms] Using [pyright](https://github.com/microsoft/pyright) and [eglot](https://github.com/joaotavora/eglot) (which is included in Emacs 29), add the following to your config: ```emacs-lisp -(defun maybe-from-dir (cmd &optional dir) - (if dir - (format "cd %s && %s" dir cmd) - cmd)) - -(defun get-packages-path (&optional dir) - (let* ((get-packages-cmd-str (maybe-from-dir "pdm info --packages" dir)) - (get-packages-cmd (string-trim (shell-command-to-string get-packages-cmd-str)))) - (concat get-packages-cmd "/lib"))) +(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) - (list :python.analysis - (list :extraPaths (vector (get-packages-path))))) - + "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) ``` From 5df87f1f1b589bbf5f1122cd65762135e6024afb Mon Sep 17 00:00:00 2001 From: pakelley Date: Fri, 17 Feb 2023 21:19:52 -0800 Subject: [PATCH 5/8] Add emacs to pep582 docs --- docs/docs/usage/pep582.md | 68 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/docs/docs/usage/pep582.md b/docs/docs/usage/pep582.md index 0a498331ef..f7c5f7e13f 100644 --- a/docs/docs/usage/pep582.md +++ b/docs/docs/usage/pep582.md @@ -161,4 +161,70 @@ project's `pyproject.toml`. extraPaths = ["__pypackages__//lib/"] ``` -### [Seek for other IDEs or editors](advanced.md#integrate-with-other-ide-or-editors) +### Emacs + +#### Using `pyproject.toml` and pyright + +Add this to your project's `pyproject.toml`: + +```toml +[tool.pyright] +extraPaths = ["__pypackages__//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) +``` + +#### 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 +``` From 84539ec84bfd3faaf72ba6abe0c2a2e3aa2f8a40 Mon Sep 17 00:00:00 2001 From: pakelley Date: Fri, 17 Feb 2023 21:20:14 -0800 Subject: [PATCH 6/8] Remove emacs section from advanced usage docs --- docs/docs/usage/advanced.md | 61 ------------------------------------- 1 file changed, 61 deletions(-) diff --git a/docs/docs/usage/advanced.md b/docs/docs/usage/advanced.md index 8ee391049d..5e702a75b4 100644 --- a/docs/docs/usage/advanced.md +++ b/docs/docs/usage/advanced.md @@ -180,67 +180,6 @@ COPY --from=builder /project/__pypackages__/3.8/lib /project/pkgs CMD ["python", "-m", "project"] ``` -## Integrate with other IDE or editors - -### Emacs - -#### 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 -``` - -#### eglot + pyright - -Using [pyright](https://github.com/microsoft/pyright) and [eglot](https://github.com/joaotavora/eglot) (which is 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) -``` - ## 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. From 2c1b8dcf286f0d3b0ff5c0c13bebf1a38d00f93d Mon Sep 17 00:00:00 2001 From: pakelley Date: Fri, 17 Feb 2023 21:29:21 -0800 Subject: [PATCH 7/8] Add note to emacs config for installing pyright --- docs/docs/usage/pep582.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docs/docs/usage/pep582.md b/docs/docs/usage/pep582.md index f7c5f7e13f..5773f43418 100644 --- a/docs/docs/usage/pep582.md +++ b/docs/docs/usage/pep582.md @@ -189,6 +189,12 @@ Using [pyright](https://github.com/microsoft/pyright) and [eglot](https://github (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). From 77681b34ca8c44b5a64448fbe1b64ad781986b7a Mon Sep 17 00:00:00 2001 From: pakelley Date: Fri, 17 Feb 2023 21:40:36 -0800 Subject: [PATCH 8/8] Clarify that each of the emacs sections are independent --- docs/docs/usage/pep582.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/docs/usage/pep582.md b/docs/docs/usage/pep582.md index 5773f43418..599b1a08de 100644 --- a/docs/docs/usage/pep582.md +++ b/docs/docs/usage/pep582.md @@ -163,6 +163,8 @@ extraPaths = ["__pypackages__//lib/"] ### 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`: