refactor: generalize fd/ripgrep vars & options

I intend to eventually replace projectile with project.el, so these
doom-projectile-* variables need to be generalized, starting with the
fd/ripgrep executable paths.

ALong with that, this refactors Doom's projectile-generic-command to
lean more on built-in fd support in projectile, where possible (fewer
wheels reinvented).

Ref: doomemacs/core#1
This commit is contained in:
Henrik Lissner 2024-08-09 20:54:23 -04:00
parent 939fc0d322
commit 4fcf332749
No known key found for this signature in database
GPG key ID: B60957CA074D39A3
5 changed files with 48 additions and 33 deletions

View file

@ -219,7 +219,7 @@ in."
file (/ size 1024 1024.0)) file (/ size 1024 1024.0))
(explain! "Consider deleting it from your system (manually)")))) (explain! "Consider deleting it from your system (manually)"))))
(unless (ignore-errors (executable-find doom-projectile-fd-binary)) (unless (ignore-errors (executable-find doom-fd-executable))
(warn! "Couldn't find the `fd' binary; project file searches will be slightly slower")) (warn! "Couldn't find the `fd' binary; project file searches will be slightly slower"))
(require 'projectile) (require 'projectile)

View file

@ -12,12 +12,17 @@ Emacs.")
(defvar doom-projectile-cache-purge-non-projects nil (defvar doom-projectile-cache-purge-non-projects nil
"If non-nil, non-projects are purged from the cache on `kill-emacs-hook'.") "If non-nil, non-projects are purged from the cache on `kill-emacs-hook'.")
(defvar doom-projectile-fd-binary (define-obsolete-variable-alias 'doom-projectile-fd-binary 'doom-fd-executable "v3.0.0")
(cl-find-if #'executable-find (list "fdfind" "fd")) (defvar doom-fd-executable (cl-find-if #'executable-find (list "fdfind" "fd"))
"The filename of the `fd' executable. On some distros it's 'fdfind' (ubuntu, "The filename of the fd executable.
debian, and derivatives). On most it's 'fd'.")
(defvar doom-projects--fd-version nil) On some distros it's fdfind (ubuntu, debian, and derivatives). On most it's fd.
Is nil if no executable is found in your PATH during startup.")
(defvar doom-ripgrep-executable (executable-find "rg")
"The filename of the Ripgrep executable.
Is nil if no executable is found in your PATH during startup.")
;; ;;
@ -52,7 +57,8 @@ debian, and derivatives). On most it's 'fd'.")
projectile-kill-buffers-filter 'kill-only-files projectile-kill-buffers-filter 'kill-only-files
projectile-known-projects-file (concat doom-cache-dir "projectile.projects") projectile-known-projects-file (concat doom-cache-dir "projectile.projects")
projectile-ignored-projects '("~/") projectile-ignored-projects '("~/")
projectile-ignored-project-function #'doom-project-ignored-p) projectile-ignored-project-function #'doom-project-ignored-p
projectile-fd-executable doom-fd-executable)
(global-set-key [remap evil-jump-to-tag] #'projectile-find-tag) (global-set-key [remap evil-jump-to-tag] #'projectile-find-tag)
(global-set-key [remap find-tag] #'projectile-find-tag) (global-set-key [remap find-tag] #'projectile-find-tag)
@ -177,38 +183,47 @@ And if it's a function, evaluate it."
(get 'projectile-git-submodule-command 'initial-value))) (get 'projectile-git-submodule-command 'initial-value)))
(funcall fn vcs)))) (funcall fn vcs))))
;; `projectile-generic-command' doesn't typically support a function, but my ;; HACK: `projectile-generic-command' doesn't typically support a function,
;; `doom--only-use-generic-command-a' advice allows this. I do it this way so ;; but my `doom--only-use-generic-command-a' advice allows this. I do it
;; that projectile can adapt to remote systems (over TRAMP), rather then look ;; this way to make it easier for folks to undo the change (if not set to a
;; for fd/ripgrep on the remote system simply because it exists on the host. ;; function, projectile will revert to default behavior).
;; It's faster too.
(put 'projectile-git-submodule-command 'initial-value projectile-git-submodule-command) (put 'projectile-git-submodule-command 'initial-value projectile-git-submodule-command)
(setq projectile-git-submodule-command nil (setq projectile-git-submodule-command nil
;; Include and follow symlinks in file listings.
projectile-git-fd-args (concat "-L -tl " projectile-git-fd-args " --ignore-file .project")
projectile-indexing-method 'hybrid projectile-indexing-method 'hybrid
projectile-generic-command projectile-generic-command
(lambda (_) (lambda (_)
;; If fd exists, use it for git and generic projects. fd is a rust ;; If fd or ripgrep exists, use it to produce file listings for
;; program that is significantly faster than git ls-files or find, and ;; projectile commands. fd is a rust program that is significantly
;; it respects .gitignore. This is recommended in the projectile docs. ;; faster than git ls-files, find, or the various VCS commands
;; projectile is configured to use. Plus, it respects .gitignore.
(cond (cond
((when-let* ((when-let*
((bin (if (ignore-errors (file-remote-p default-directory nil t)) ((doom-fd-executable)
(cl-find-if (doom-rpartial #'executable-find t) (projectile-git-use-fd)
(list "fdfind" "fd"))
doom-projectile-fd-binary))
;; REVIEW Temporary fix for #6618. Improve me later. ;; REVIEW Temporary fix for #6618. Improve me later.
(version (with-memoization doom-projects--fd-version (version (with-memoization (get 'doom-fd-executable 'version)
(cadr (split-string (cdr (doom-call-process bin "--version")) (cadr (split-string (cdr (doom-call-process doom-fd-executable "--version"))
" " t)))) " " t))))
((ignore-errors (version-to-list version)))) ((ignore-errors (version-to-list version))))
(concat (format "%s . -0 -H --color=never --type file --type symlink --follow --exclude .git %s" (string-join
bin (if (version< version "8.3.0") (delq
"" "--strip-cwd-prefix")) nil (list doom-fd-executable "."
(if doom--system-windows-p " --path-separator=/")))) (if (version< version "8.3.0")
(replace-regexp-in-string "--strip-cwd-prefix" "" projectile-git-fd-args t t)
projectile-git-fd-args)
(if doom--system-windows-p " --path-separator=/" "")))
" ")))
;; Otherwise, resort to ripgrep, which is also faster than find ;; Otherwise, resort to ripgrep, which is also faster than find
((executable-find "rg" t) (doom-ripgrep-executable
(concat "rg -0 --files --follow --color=never --hidden -g!.git" (string-join
(if doom--system-windows-p " --path-separator=/"))) (delq
nil (list doom-ripgrep-executable
"-0 --files --follow --color=never --hidden"
(if doom--system-windows-p " --path-separator=/")))
" "))
((not doom--system-windows-p) "find . -type f | cut -c3- | tr '\\n' '\\0'")
("find . -type f -print0")))) ("find . -type f -print0"))))
(defadvice! doom--projectile-default-generic-command-a (fn &rest args) (defadvice! doom--projectile-default-generic-command-a (fn &rest args)

View file

@ -286,7 +286,7 @@ workable results ripgrep produces, despite the error."
"Change `counsel-file-jump' to use fd or ripgrep, if they are available." "Change `counsel-file-jump' to use fd or ripgrep, if they are available."
:override #'counsel--find-return-list :override #'counsel--find-return-list
(cl-destructuring-bind (find-program . args) (cl-destructuring-bind (find-program . args)
(cond ((when-let (fd (executable-find (or doom-projectile-fd-binary "fd") t)) (cond ((when-let (fd (executable-find (or doom-fd-executable "fd") t))
(append (list fd "--hidden" "--type" "file" "--type" "symlink" "--follow" "--color=never") (append (list fd "--hidden" "--type" "file" "--type" "symlink" "--follow" "--color=never")
(cl-loop for dir in projectile-globally-ignored-directories (cl-loop for dir in projectile-globally-ignored-directories
collect "--exclude" collect "--exclude"

View file

@ -211,15 +211,15 @@ targets."
;;;###autoload ;;;###autoload
(defun +vertico/consult-fd-or-find (&optional dir initial) (defun +vertico/consult-fd-or-find (&optional dir initial)
"Runs consult-fd if fd version > 8.6.0 exists, consult-find otherwise. "Runs consult-fd if fd version > 8.6.0 exists, consult-find otherwise.
See URL `https://github.com/minad/consult/issues/770'." See minad/consult#770."
(interactive "P") (interactive "P")
;; TODO this condition was adapted from a similar one in lisp/doom-projects.el, to be replaced with a more robust check post v3 ;; TODO this condition was adapted from a similar one in lisp/doom-projects.el, to be replaced with a more robust check post v3
(if (when-let* (if (when-let*
((bin (if (ignore-errors (file-remote-p default-directory nil t)) ((bin (if (ignore-errors (file-remote-p default-directory nil t))
(cl-find-if (doom-rpartial #'executable-find t) (cl-find-if (doom-rpartial #'executable-find t)
(list "fdfind" "fd")) (list "fdfind" "fd"))
doom-projectile-fd-binary)) doom-fd-executable))
(version (with-memoization doom-projects--fd-version (version (with-memoization (get 'doom-fd-executable 'version)
(cadr (split-string (cdr (doom-call-process bin "--version")) (cadr (split-string (cdr (doom-call-process bin "--version"))
" " t)))) " " t))))
((ignore-errors (version-to-list version)))) ((ignore-errors (version-to-list version))))

View file

@ -211,7 +211,7 @@ we have to clean it up ourselves."
(use-package! fd-dired (use-package! fd-dired
:when doom-projectile-fd-binary :when doom-fd-executable
:defer t :defer t
:init :init
(global-set-key [remap find-dired] #'fd-dired) (global-set-key [remap find-dired] #'fd-dired)