Go back to default pet.el so that I can modify it from scratch

This commit is contained in:
Vedang Manerikar 2024-12-06 09:39:59 +05:30
parent d201de5854
commit 49dc540735
2 changed files with 230 additions and 208 deletions

View file

@ -113,6 +113,16 @@ and nil otherwise."
:group 'pet :group 'pet
:type '(repeat string)) :type '(repeat string))
(defcustom pet-fd-command "fd"
"The \"fd\" command in the system."
:type 'string
:group 'pet)
(defcustom pet-fd-command-args '("-tf" "-cnever" "-H" "-a" "-g")
"The arguments to pass to the \"fd\" command."
:type '(repeat string)
:group 'pet)
(defun pet--executable-find (command &optional remote) (defun pet--executable-find (command &optional remote)
@ -191,21 +201,25 @@ FILE is a file name or a wildcard.
Return absolute path to FILE if found, nil otherwise." Return absolute path to FILE if found, nil otherwise."
(condition-case err (condition-case err
(when-let ((root (pet-project-root)) (when-let ((root (pet-project-root)))
(fileset (if (executable-find pet-fd-command)
(cond ((functionp 'projectile-dir-files) (car (cl-remove-if
(mapcar (apply-partially #'concat root) #'string-empty-p
(projectile-dir-files (pet-project-root)))) (apply #'process-lines `(,pet-fd-command ,@pet-fd-command-args ,file ,root))))
((functionp 'project-files) (when-let ((fileset
(project-files (project-current))) (cond ((functionp 'projectile-dir-files)
(t (directory-files-recursively (mapcar (apply-partially #'concat root)
(pet-project-root) (projectile-dir-files (pet-project-root))))
(wildcard-to-regexp file)))))) ((functionp 'project-files)
(seq-find (lambda (f) (project-files (project-current)))
(string-match-p (t (directory-files-recursively
(wildcard-to-regexp file) (pet-project-root)
(file-name-nondirectory f))) (wildcard-to-regexp file))))))
(sort fileset 'string<))) (seq-find (lambda (f)
(string-match-p
(wildcard-to-regexp file)
(file-name-nondirectory f)))
(sort fileset 'string<)))))
(error (pet-report-error err)))) (error (pet-report-error err))))
(defun pet-find-file-from-project (file) (defun pet-find-file-from-project (file)
@ -539,35 +553,6 @@ must both be installed into the current project first."
(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)))
bin-dir)
(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 ;;;###autoload
(defun pet-executable-find (executable) (defun pet-executable-find (executable)
"Find the correct EXECUTABLE for the current Python project. "Find the correct EXECUTABLE for the current Python project.
@ -579,9 +564,31 @@ whatever environment if found by `pet-virtualenv-root', then
The executable will only be searched in an environment created by The executable will only be searched in an environment created by
a Python virtualenv management tool if the project is set up to a Python virtualenv management tool if the project is set up to
use it." use it."
(pet-adjust-paths-executable-find executable) (cond ((and (pet-use-pre-commit-p)
(or (pet--executable-find executable t) (not (string-prefix-p "python" executable))
(pet--executable-find (concat executable "3") t))) (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)))))
(defvar pet-project-virtualenv-cache nil) (defvar pet-project-virtualenv-cache nil)
@ -775,70 +782,75 @@ default otherwise."
(defvar eglot-workspace-configuration)
(declare-function jsonrpc--process "ext:jsonrpc") (declare-function jsonrpc--process "ext:jsonrpc")
(declare-function eglot--executable-find "ext:eglot")
(declare-function eglot--uri-to-path "ext:eglot")
(declare-function eglot--workspace-configuration-plist "ext:eglot") (declare-function eglot--workspace-configuration-plist "ext:eglot")
(declare-function eglot--guess-contact "ext:eglot") (declare-function eglot--guess-contact "ext:eglot")
(defun pet-eglot--adjust-paths-advice () (defun pet-eglot--executable-find-advice (fn &rest args)
"Adjust paths BEFORE looking up Python language servers. "Look up Python language servers using `pet-executable-find'.
We advice `eglot-ensure' with this function, below." FN is `eglot--executable-find', ARGS is the arguments to
(when (derived-mode-p (if (functionp 'python-base-mode) 'python-base-mode 'python-mode)) `eglot--executable-find'."
;; The command passed to adjust-paths is a dummy, we just want the appropriate variables to be set. (pcase-let ((`(,command . ,_) args))
(pet-adjust-paths-executable-find "pylsp"))) (if (member command '("pylsp" "pyls" "pyright-langserver" "jedi-language-server" "ruff-lsp"))
(pet-executable-find command)
(apply fn args))))
(defun pet-lookup-eglot-server-initialization-options (command) (defun pet-lookup-eglot-server-initialization-options (command)
"Return LSP initializationOptions for Eglot. "Return LSP initializationOptions for Eglot.
COMMAND is the name of the Python language server command." COMMAND is the name of the Python language server command."
(cond ((string-match-p "pylsp" command) (cond
`(:pylsp ((not
(:plugins (stringp command))
(:jedi 'nil)
(:environment ((string-match "pylsp" command)
,(pet-virtualenv-root)) (let nil
:ruff `(:pylsp
(:executable (:plugins
,(pet-executable-find "ruff")) (:jedi
:pylsp_mypy (:environment ,(pet-virtualenv-root))
(:overrides :ruff
["--python-executable" ,(pet-executable-find "python") t]) (:executable ,(pet-executable-find "ruff"))
:flake8 :pylsp_mypy
(:executable (:overrides
,(pet-executable-find "flake8")) ["--python-executable"
:pylint (\,
(:executable (pet-executable-find "python"))
,(pet-executable-find "pylint")))))) t])
((string-match-p "pyls" command) :flake8
`(:pyls (:executable ,(pet-executable-find "flake8"))
(:plugins :pylint
(:jedi (:executable ,(pet-executable-find "pylint")))))))
(:environment ((string-match "pyls" command)
,(pet-virtualenv-root)) (let nil
:pylint `(:pyls
(:executable (:plugins
,(pet-executable-find "pylint")))))) (:jedi
((string-match-p "pyright-langserver" command) (:environment ,(pet-virtualenv-root))
`(:python :pylint
(:pythonPath (:executable ,(pet-executable-find "pylint")))))))
,(pet-executable-find "python") ((string-match "pyright-langserver" command)
:venvPath (let nil
,(pet-virtualenv-root)))) `(:python
((string-match-p "jedi-language-server" command) (:pythonPath ,(pet-executable-find "python")
`(:jedi :venvPath ,(pet-virtualenv-root)))))
(:executable ((string-match "jedi-language-server" command)
(:command (let nil
,(pet-executable-find "jedi-language-server")) `(:jedi
:workspace (:executable
(:environmentPath (:command ,(pet-executable-find "jedi-language-server"))
,(pet-executable-find "python"))))) :workspace
((string-match-p "ruff-lsp" command) (:environmentPath ,(pet-executable-find "python"))))))
`(:settings ((string-match "ruff-lsp" command)
(:interpreter (let nil
,(pet-executable-find "python") `(:settings
:path (:interpreter ,(pet-executable-find "python")
,(pet-executable-find "ruff")))) :path ,(pet-executable-find "ruff")))))
(t nil))) (t 'nil)))
(defalias 'pet--proper-list-p 'proper-list-p) (defalias 'pet--proper-list-p 'proper-list-p)
(eval-when-compile (eval-when-compile
@ -871,7 +883,7 @@ COMMAND is the name of the Python language server command."
(copy-tree b t))) (copy-tree b t)))
(defun pet-eglot--workspace-configuration-plist-advice (fn &rest args) (defun pet-eglot--workspace-configuration-plist-advice (fn &rest args)
"Enrich `eglot--workspace-configuration-plist' with paths found by `pet'. "Enrich `eglot-workspace-configuration' with paths found by `pet'.
FN is `eglot--workspace-configuration-plist', ARGS is the FN is `eglot--workspace-configuration-plist', ARGS is the
arguments to `eglot--workspace-configuration-plist'." arguments to `eglot--workspace-configuration-plist'."
@ -913,13 +925,13 @@ FN is `eglot--guess-contact', ARGS is the arguments to
(defun pet-eglot-setup () (defun pet-eglot-setup ()
"Set up Eglot to use server executables and virtualenvs found by PET." "Set up Eglot to use server executables and virtualenvs found by PET."
(advice-add 'eglot-ensure :before #'pet-eglot--adjust-paths-advice) (advice-add 'eglot--executable-find :around #'pet-eglot--executable-find-advice)
(advice-add 'eglot--workspace-configuration-plist :around #'pet-eglot--workspace-configuration-plist-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)) (advice-add 'eglot--guess-contact :around #'pet-eglot--guess-contact-advice))
(defun pet-eglot-teardown () (defun pet-eglot-teardown ()
"Tear down PET advices to Eglot." "Tear down PET advices to Eglot."
(advice-remove 'eglot-ensure #'pet-eglot--adjust-paths-advice) (advice-remove 'eglot--executable-find #'pet-eglot--executable-find-advice)
(advice-remove 'eglot--workspace-configuration-plist #'pet-eglot--workspace-configuration-plist-advice) (advice-remove 'eglot--workspace-configuration-plist #'pet-eglot--workspace-configuration-plist-advice)
(advice-remove 'eglot--guess-contact #'pet-eglot--guess-contact-advice)) (advice-remove 'eglot--guess-contact #'pet-eglot--guess-contact-advice))

View file

@ -6268,6 +6268,16 @@ Also see `prot-window-delete-popup-frame'." command)
:group 'pet :group 'pet
:type '(repeat string)) :type '(repeat string))
(defcustom pet-fd-command "fd"
"The \"fd\" command in the system."
:type 'string
:group 'pet)
(defcustom pet-fd-command-args '("-tf" "-cnever" "-H" "-a" "-g")
"The arguments to pass to the \"fd\" command."
:type '(repeat string)
:group 'pet)
(defun pet--executable-find (command &optional remote) (defun pet--executable-find (command &optional remote)
@ -6346,21 +6356,25 @@ Also see `prot-window-delete-popup-frame'." command)
Return absolute path to FILE if found, nil otherwise." Return absolute path to FILE if found, nil otherwise."
(condition-case err (condition-case err
(when-let ((root (pet-project-root)) (when-let ((root (pet-project-root)))
(fileset (if (executable-find pet-fd-command)
(cond ((functionp 'projectile-dir-files) (car (cl-remove-if
(mapcar (apply-partially #'concat root) #'string-empty-p
(projectile-dir-files (pet-project-root)))) (apply #'process-lines `(,pet-fd-command ,@pet-fd-command-args ,file ,root))))
((functionp 'project-files) (when-let ((fileset
(project-files (project-current))) (cond ((functionp 'projectile-dir-files)
(t (directory-files-recursively (mapcar (apply-partially #'concat root)
(pet-project-root) (projectile-dir-files (pet-project-root))))
(wildcard-to-regexp file)))))) ((functionp 'project-files)
(seq-find (lambda (f) (project-files (project-current)))
(string-match-p (t (directory-files-recursively
(wildcard-to-regexp file) (pet-project-root)
(file-name-nondirectory f))) (wildcard-to-regexp file))))))
(sort fileset 'string<))) (seq-find (lambda (f)
(string-match-p
(wildcard-to-regexp file)
(file-name-nondirectory f)))
(sort fileset 'string<)))))
(error (pet-report-error err)))) (error (pet-report-error err))))
(defun pet-find-file-from-project (file) (defun pet-find-file-from-project (file)
@ -6694,35 +6708,6 @@ 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 ;;;###autoload
(defun pet-executable-find (executable) (defun pet-executable-find (executable)
"Find the correct EXECUTABLE for the current Python project. "Find the correct EXECUTABLE for the current Python project.
@ -6734,9 +6719,31 @@ Also see `prot-window-delete-popup-frame'." command)
The executable will only be searched in an environment created by The executable will only be searched in an environment created by
a Python virtualenv management tool if the project is set up to a Python virtualenv management tool if the project is set up to
use it." use it."
(pet-adjust-paths-executable-find executable) (cond ((and (pet-use-pre-commit-p)
(or (pet--executable-find executable t) (not (string-prefix-p "python" executable))
(pet--executable-find (concat executable "3") t))) (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)))))
(defvar pet-project-virtualenv-cache nil) (defvar pet-project-virtualenv-cache nil)
@ -6930,71 +6937,75 @@ Also see `prot-window-delete-popup-frame'." command)
(defvar eglot-workspace-configuration)
(declare-function jsonrpc--process "ext:jsonrpc") (declare-function jsonrpc--process "ext:jsonrpc")
(declare-function eglot--executable-find "ext:eglot")
(declare-function eglot--uri-to-path "ext:eglot")
(declare-function eglot--workspace-configuration-plist "ext:eglot") (declare-function eglot--workspace-configuration-plist "ext:eglot")
(declare-function eglot--guess-contact "ext:eglot") (declare-function eglot--guess-contact "ext:eglot")
(defun pet-eglot--adjust-path-advice (fn &rest args) (defun pet-eglot--executable-find-advice (fn &rest args)
"Adjust paths before looking up Python language servers. "Look up Python language servers using `pet-executable-find'.
FN is `eglot-ensure'. ARGS is the arguments to FN." FN is `eglot--executable-find', ARGS is the arguments to
`eglot--executable-find'."
(pcase-let ((`(,command . ,_) args)) (pcase-let ((`(,command . ,_) args))
(if (member command '("pylsp" "pyls" "basedpyright-langserver" "pyright-langserver" "jedi-language-server" "ruff-lsp")) (if (member command '("pylsp" "pyls" "pyright-langserver" "jedi-language-server" "ruff-lsp"))
(pet-adjust-paths-executable-find command) (pet-executable-find command)
(apply fn args)))) (apply fn args))))
(defun pet-lookup-eglot-server-initialization-options (command) (defun pet-lookup-eglot-server-initialization-options (command)
"Return LSP initializationOptions for Eglot. "Return LSP initializationOptions for Eglot.
COMMAND is the name of the Python language server command." COMMAND is the name of the Python language server command."
(cond ((string-match-p "pylsp" command) (cond
`(:pylsp ((not
(:plugins (stringp command))
(:jedi 'nil)
(:environment ((string-match "pylsp" command)
,(pet-virtualenv-root)) (let nil
:ruff `(:pylsp
(:executable (:plugins
,(pet-executable-find "ruff")) (:jedi
:pylsp_mypy (:environment ,(pet-virtualenv-root))
(:overrides :ruff
["--python-executable" ,(pet-executable-find "python") t]) (:executable ,(pet-executable-find "ruff"))
:flake8 :pylsp_mypy
(:executable (:overrides
,(pet-executable-find "flake8")) ["--python-executable"
:pylint (\,
(:executable (pet-executable-find "python"))
,(pet-executable-find "pylint")))))) t])
((string-match-p "pyls" command) :flake8
`(:pyls (:executable ,(pet-executable-find "flake8"))
(:plugins :pylint
(:jedi (:executable ,(pet-executable-find "pylint")))))))
(:environment ((string-match "pyls" command)
,(pet-virtualenv-root)) (let nil
:pylint `(:pyls
(:executable (:plugins
,(pet-executable-find "pylint")))))) (:jedi
((string-match-p "pyright-langserver" command) (:environment ,(pet-virtualenv-root))
`(:python :pylint
(:pythonPath (:executable ,(pet-executable-find "pylint")))))))
,(pet-executable-find "python") ((string-match "pyright-langserver" command)
:venvPath (let nil
,(pet-virtualenv-root)))) `(:python
((string-match-p "jedi-language-server" command) (:pythonPath ,(pet-executable-find "python")
`(:jedi :venvPath ,(pet-virtualenv-root)))))
(:executable ((string-match "jedi-language-server" command)
(:command (let nil
,(pet-executable-find "jedi-language-server")) `(:jedi
:workspace (:executable
(:environmentPath (:command ,(pet-executable-find "jedi-language-server"))
,(pet-executable-find "python"))))) :workspace
((string-match-p "ruff-lsp" command) (:environmentPath ,(pet-executable-find "python"))))))
`(:settings ((string-match "ruff-lsp" command)
(:interpreter (let nil
,(pet-executable-find "python") `(:settings
:path (:interpreter ,(pet-executable-find "python")
,(pet-executable-find "ruff")))) :path ,(pet-executable-find "ruff")))))
(t nil))) (t 'nil)))
(defalias 'pet--proper-list-p 'proper-list-p) (defalias 'pet--proper-list-p 'proper-list-p)
(eval-when-compile (eval-when-compile
@ -7027,7 +7038,7 @@ Also see `prot-window-delete-popup-frame'." command)
(copy-tree b t))) (copy-tree b t)))
(defun pet-eglot--workspace-configuration-plist-advice (fn &rest args) (defun pet-eglot--workspace-configuration-plist-advice (fn &rest args)
"Enrich `eglot--workspace-configuration-plist' with paths found by `pet'. "Enrich `eglot-workspace-configuration' with paths found by `pet'.
FN is `eglot--workspace-configuration-plist', ARGS is the FN is `eglot--workspace-configuration-plist', ARGS is the
arguments to `eglot--workspace-configuration-plist'." arguments to `eglot--workspace-configuration-plist'."
@ -7069,13 +7080,13 @@ Also see `prot-window-delete-popup-frame'." command)
(defun pet-eglot-setup () (defun pet-eglot-setup ()
"Set up Eglot to use server executables and virtualenvs found by PET." "Set up Eglot to use server executables and virtualenvs found by PET."
(advice-add 'eglot-ensure :around #'pet-eglot--adjust-path-advice) (advice-add 'eglot--executable-find :around #'pet-eglot--executable-find-advice)
(advice-add 'eglot--workspace-configuration-plist :around #'pet-eglot--workspace-configuration-plist-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)) (advice-add 'eglot--guess-contact :around #'pet-eglot--guess-contact-advice))
(defun pet-eglot-teardown () (defun pet-eglot-teardown ()
"Tear down PET advices to Eglot." "Tear down PET advices to Eglot."
(advice-remove 'eglot-ensure #'pet-eglot--adjust-path-advice) (advice-remove 'eglot--executable-find #'pet-eglot--executable-find-advice)
(advice-remove 'eglot--workspace-configuration-plist #'pet-eglot--workspace-configuration-plist-advice) (advice-remove 'eglot--workspace-configuration-plist #'pet-eglot--workspace-configuration-plist-advice)
(advice-remove 'eglot--guess-contact #'pet-eglot--guess-contact-advice)) (advice-remove 'eglot--guess-contact #'pet-eglot--guess-contact-advice))
@ -7295,7 +7306,6 @@ Also see `prot-window-delete-popup-frame'." command)
(provide 'vedang-pet) (provide 'vedang-pet)
;;; pet.el ends here ;;; pet.el ends here
#+end_src #+end_src
** The =vedang-personal.el= module ** The =vedang-personal.el= module
:PROPERTIES: :PROPERTIES: