Update vedang-pet code in the org file so tangle works correctly

This commit is contained in:
Vedang Manerikar 2024-11-28 23:24:43 +05:30
parent 97c52b8fb1
commit 7baf6588b8
2 changed files with 40 additions and 44 deletions

View file

@ -914,7 +914,7 @@ FN is `eglot--guess-contact', ARGS is the arguments to
(defun pet-eglot-setup ()
"Set up Eglot to use server executables and virtualenvs found by PET."
(advice-add 'eglot-ensure :before #'pet-eglot--adjust-path-advice)
(advice-add 'eglot-ensure :around #'pet-eglot--adjust-path-advice)
(advice-add 'eglot--workspace-configuration-plist :around #'pet-eglot--workspace-configuration-plist-advice)
(advice-add 'eglot--guess-contact :around #'pet-eglot--guess-contact-advice))

View file

@ -6674,6 +6674,35 @@ Also see `prot-window-delete-popup-frame'." command)
(defun pet--adjust-path (bin-dir)
"Add BIN-DIR to the various places that `executable-find' looks at, when
it looks for an executable."
(when (not (memql (concat bin-dir "/") exec-path))
(setq-local exec-path (cons (concat bin-dir "/") exec-path)))
(when (not (memql bin-dir tramp-remote-path))
(setq-local tramp-remote-path (cons bin-dir tramp-remote-path)))
(setenv "PATH" (string-join exec-path path-separator)))
(defun pet-adjust-paths-executable-find (executable)
"Adjust paths so that we can find the correct EXECUTABLE for the current
Python project."
(cond ((and (pet-use-pre-commit-p)
(not (string-prefix-p "python" executable))
(pet-pre-commit-config-has-hook-p executable))
(condition-case err
(let* ((venv (or (pet-pre-commit-virtualenv-path executable)
(user-error "`pre-commit' is configured but the hook `%s' does not appear to be installed" executable))))
(pet--adjust-path (concat (file-name-as-directory venv) (pet-system-bin-dir))))
(error (pet-report-error err))))
((pet-virtualenv-root)
(pet--adjust-path (concat (file-name-as-directory (pet-virtualenv-root)) (pet-system-bin-dir))))
((and (pet--executable-find "pyenv" t)
(condition-case err
(car (process-lines "pyenv" "which" executable))
(error (pet-report-error err))))
(pet--adjust-path (file-name-parent-directory (car (process-lines "pyenv" "which" executable)))))
(t (pet-report-error "No path adjustment required"))))
;;;###autoload
(defun pet-executable-find (executable)
"Find the correct EXECUTABLE for the current Python project.
@ -6685,31 +6714,9 @@ Also see `prot-window-delete-popup-frame'." command)
The executable will only be searched in an environment created by
a Python virtualenv management tool if the project is set up to
use it."
(cond ((and (pet-use-pre-commit-p)
(not (string-prefix-p "python" executable))
(pet-pre-commit-config-has-hook-p executable))
(condition-case err
(let* ((venv (or (pet-pre-commit-virtualenv-path executable)
(user-error "`pre-commit' is configured but the hook `%s' does not appear to be installed" executable)))
(bin-dir (concat (file-name-as-directory venv) (pet-system-bin-dir)))
(bin-path (concat bin-dir "/" executable)))
(if (file-exists-p bin-path)
bin-path
(user-error "`pre-commit' is configured but `%s' is not found in %s" executable bin-dir)))
(error (pet-report-error err))))
((when-let* ((venv (pet-virtualenv-root))
(path (list (concat (file-name-as-directory venv) (pet-system-bin-dir))))
(exec-path path)
(tramp-remote-path path)
(process-environment (copy-sequence process-environment)))
(setenv "PATH" (string-join exec-path path-separator))
(pet--executable-find executable t)))
((when (pet--executable-find "pyenv" t)
(condition-case err
(car (process-lines "pyenv" "which" executable))
(error (pet-report-error err)))))
(t (or (pet--executable-find executable t)
(pet--executable-find (concat executable "3") t)))))
(pet-adjust-paths-executable-find executable)
(or (pet--executable-find executable t)
(pet--executable-find (concat executable "3") t)))
(defvar pet-project-virtualenv-cache nil)
@ -6903,20 +6910,17 @@ Also see `prot-window-delete-popup-frame'." command)
(defvar eglot-workspace-configuration)
(declare-function jsonrpc--process "ext:jsonrpc")
(declare-function eglot--executable-find "ext:eglot")
(declare-function eglot--workspace-configuration-plist "ext:eglot")
(declare-function eglot--guess-contact "ext:eglot")
(defun pet-eglot--executable-find-advice (fn &rest args)
"Look up Python language servers using `pet-executable-find'.
(defun pet-eglot--adjust-path-advice (fn &rest args)
"Adjust paths before looking up Python language servers.
FN is `eglot--executable-find', or `executable-find' (1.17+), depending
on the version of `eglot' being used. ARGS is the arguments to FN."
FN is `eglot-ensure'. ARGS is the arguments to FN."
(pcase-let ((`(,command . ,_) args))
(if (member command '("pylsp" "pyls" "basedpyright-langserver" "pyright-langserver" "jedi-language-server" "ruff-lsp"))
(pet-executable-find command)
(pet-adjust-paths-executable-find command)
(apply fn args))))
(defun pet-lookup-eglot-server-initialization-options (command)
@ -7003,7 +7007,7 @@ Also see `prot-window-delete-popup-frame'." command)
(copy-tree b t)))
(defun pet-eglot--workspace-configuration-plist-advice (fn &rest args)
"Enrich `eglot-workspace-configuration' with paths found by `pet'.
"Enrich `eglot--workspace-configuration-plist' with paths found by `pet'.
FN is `eglot--workspace-configuration-plist', ARGS is the
arguments to `eglot--workspace-configuration-plist'."
@ -7045,21 +7049,13 @@ Also see `prot-window-delete-popup-frame'." command)
(defun pet-eglot-setup ()
"Set up Eglot to use server executables and virtualenvs found by PET."
(if (fboundp 'eglot--executable-find)
;; Eglot version 1.16 or below
(advice-add 'eglot--executable-find :around #'pet-eglot--executable-find-advice)
;; Eglot version 1.17 and above
(advice-add 'executable-find :around #'pet-eglot--executable-find-advice))
(advice-add 'eglot-ensure :before #'pet-eglot--adjust-path-advice)
(advice-add 'eglot--workspace-configuration-plist :around #'pet-eglot--workspace-configuration-plist-advice)
(advice-add 'eglot--guess-contact :around #'pet-eglot--guess-contact-advice))
(defun pet-eglot-teardown ()
"Tear down PET advices to Eglot."
(if (fboundp 'eglot--executable-find)
;; Eglot version 1.16 or below
(advice-remove 'eglot--executable-find #'pet-eglot--executable-find-advice)
;; Eglot version 1.17 and above
(advice-remove 'executable-find #'pet-eglot--executable-find-advice))
(advice-remove 'eglot-ensure #'pet-eglot--adjust-path-advice)
(advice-remove 'eglot--workspace-configuration-plist #'pet-eglot--workspace-configuration-plist-advice)
(advice-remove 'eglot--guess-contact #'pet-eglot--guess-contact-advice))