Refactor helm project search API

+ Heavily refactored +helm-file-search
+ Removed -z flag from all engines by default
+ Changed the behavior of the universal argument for from-cwd
  interactive commands (e.g. +helm/rg-from-cwd). It used to enable
  recursive searches, but now enables inclusion of hidden and compressed
  files in the search instead. *-from-cwd searches are always recursive
  now.
+ Now generates +helm/X and +helm/X-from-cwd commands dynamically.
+ Split +helm/project-search into +helm/project-search-from-cwd.
  Universal arguments are passed from these commands to their delegated
  engine command.
This commit is contained in:
Henrik Lissner 2018-08-13 01:59:25 +02:00
parent b0c71c2492
commit 7d617f1541
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395

View file

@ -43,6 +43,66 @@ workspace."
;; Project search
;;
(defun +helm-ag-search-args (all-files-p recursive-p)
(list "ag -S"
(if IS-WINDOWS "--vimgrep" "--nocolor --nogroup")
(if all-files-p "-z -a")
(unless recursive-p "--depth 1")))
(defun +helm-rg-search-args (all-files-p recursive-p)
(list "rg -S --no-heading --line-number --color never"
(when all-files-p "-z -uu")
(unless recursive-p "--maxdepth 1")))
(defun +helm-pt-search-args (all-files-p recursive-p)
(list "pt -S --nocolor --nogroup -e"
(if all-files-p "-z -a")
(unless recursive-p "--depth 1")))
;;
(defun +helm--grep-source ()
(helm-build-async-source (capitalize (helm-grep-command t))
:header-name (lambda (_name) "Helm Projectile Grep (C-c ? Help)")
:candidates-process #'helm-grep-collect-candidates
:filter-one-by-one #'helm-grep-filter-one-by-one
:candidate-number-limit 9999
:nohighlight t
:keymap helm-grep-map
:history 'helm-grep-history
:action (apply #'helm-make-actions helm-projectile-grep-or-ack-actions)
:persistent-action 'helm-grep-persistent-action
:persistent-help "Jump to line (`C-u' Record in mark ring)"
:requires-pattern 2))
(defun +helm--grep-search (directory query prompt &optional all-files-p recursive-p)
(let* ((default-directory directory)
(helm-ff-default-directory directory)
(helm-grep-in-recurse recursive-p)
(helm-grep-ignored-files
(unless all-files-p
(cl-union (projectile-ignored-files-rel) grep-find-ignored-files)))
(helm-grep-ignored-directories
(unless all-files-p
(cl-union (mapcar 'directory-file-name (projectile-ignored-directories-rel))
grep-find-ignored-directories)))
(helm-grep-default-command
(if (and nil (eq (projectile-project-vcs) 'git))
(format "git --no-pager grep --no-color -n%%c -e %%p %s -- %%f"
(if recursive-p "" "--max-depth 1 "))
(format "grep -si -a%s %%e -n%%cH -e %%p %%f %s"
(if recursive-p " -R" "")
(if recursive-p "." "./*"))))
(helm-grep-default-recurse-command helm-grep-default-command))
(setq helm-source-grep (+helm--grep-source))
(helm :sources 'helm-source-grep
:input query
:prompt prompt
:buffer "*helm grep*"
:default-directory directory
:keymap helm-grep-map
:history 'helm-grep-history
:truncate-lines helm-grep-truncate-lines)))
;;;###autoload
(cl-defun +helm-file-search (engine &key query in all-files (recursive t))
"Conduct a file search using ENGINE, which can be any of: rg, ag, pt, and
@ -65,13 +125,12 @@ order.
(helm-ag--default-directory directory)
(helm-ag--default-target (list directory))
(engine (or engine
(cl-loop for tool in +helm-project-search-engines
if (executable-find (symbol-name tool))
return tool)
(cl-find-if #'executable-find +helm-project-search-engines
:key #'symbol-name)
(and (or (executable-find "grep")
(executable-find "git"))
'grep)
(error "No search engine specified (is ag, rg, pt or git installed?)")))
(user-error "No search engine specified (is ag, rg, pt or git installed?)")))
(query (or query
(when (use-region-p)
(let ((beg (or (bound-and-true-p evil-visual-beginning) (region-beginning)))
@ -81,67 +140,18 @@ order.
""))
(prompt (format "%s%%s %s"
(symbol-name engine)
(cond ((equal directory default-directory)
(cond ((file-equal-p directory default-directory)
"./")
((equal directory project-root)
((file-equal-p directory project-root)
(projectile-project-name))
(t
(file-relative-name directory project-root)))))
((file-relative-name directory project-root)))))
(command
(pcase engine
('grep
(let* ((helm-ff-default-directory directory)
(helm-grep-in-recurse recursive)
(helm-grep-ignored-files
(unless all-files
(cl-union (projectile-ignored-files-rel) grep-find-ignored-files)))
(helm-grep-ignored-directories
(unless all-files
(cl-union (mapcar 'directory-file-name (projectile-ignored-directories-rel))
grep-find-ignored-directories)))
(helm-grep-default-command
(if (and nil (eq (projectile-project-vcs) 'git))
(format "git --no-pager grep --no-color -n%%c -e %%p %s -- %%f"
(if recursive "" "--max-depth 1 "))
(format "grep -si -a%s %%e -n%%cH -e %%p %%f %s"
(if recursive " -R" "")
(if recursive "." "./*"))))
(helm-grep-default-recurse-command helm-grep-default-command))
(setq helm-source-grep
(helm-build-async-source (capitalize (helm-grep-command t))
:header-name (lambda (_name) "Helm Projectile Grep (C-c ? Help)")
:candidates-process #'helm-grep-collect-candidates
:filter-one-by-one #'helm-grep-filter-one-by-one
:candidate-number-limit 9999
:nohighlight t
:keymap helm-grep-map
:history 'helm-grep-history
:action (apply #'helm-make-actions helm-projectile-grep-or-ack-actions)
:persistent-action 'helm-grep-persistent-action
:persistent-help "Jump to line (`C-u' Record in mark ring)"
:requires-pattern 2))
(helm :sources 'helm-source-grep
:input query
:prompt prompt
:buffer "*helm grep*"
:default-directory directory
:keymap helm-grep-map
:history 'helm-grep-history
:truncate-lines helm-grep-truncate-lines))
(cl-return t))
(`ag
(list "ag -zS"
(if IS-WINDOWS "--vimgrep" "--nocolor --nogroup")
(when all-files "-a")
(unless recursive "--depth 1")))
(`rg
(list "rg -zS --no-heading --line-number --color never"
(when all-files "-uu")
(unless recursive "--maxdepth 1")))
(`pt
(list "pt -zS --nocolor --nogroup -e"
(when all-files "-a")
(unless recursive "--depth 1")))))
(`ag (+helm-ag-search-args all-files recursive))
(`rg (+helm-rg-search-args all-files recursive))
(`pt (+helm-pt-search-args all-files recursive))
('grep (+helm--grep-search directory query prompt all-files recursive)
(cl-return t))))
(helm-ag-base-command (string-join command " ")))
(if (and (eq engine 'ag)
(equal query ""))
@ -156,67 +166,54 @@ order.
:keymap helm-ag-map
:history 'helm-ag--helm-history))))
(defun +helm--get-command (format)
(cl-loop for tool in (cl-remove-duplicates +helm-project-search-engines :from-end t)
if (executable-find (symbol-name tool))
return (intern (format format tool))))
;;;###autoload
(defun +helm/project-search (arg)
"Performs a project search using the first available search backend from a
list of: ripgrep, ag, pt, git-grep and grep. If ARG (universal argument),
preform search from current directory."
(defun +helm/project-search (&optional all-files-p)
"Performs a project search from the project root.
Uses the first available search backend from `+helm-project-search-engines'. If
ALL-FILES-P (universal argument), include all files, even hidden or compressed
ones, in the search."
(interactive "P")
(call-interactively
(or (cl-loop for tool in (cl-remove-duplicates +helm-project-search-engines :from-end t)
if (executable-find (symbol-name tool))
return (intern (format "+helm/%s%s" tool (if arg "-from-cwd" ""))))
(if arg
#'+helm/grep-from-cwd
#'+helm/grep))))
(or (+helm--get-command "+helm/%s")
#'+helm/grep)))
;;;###autoload
(defun +helm/project-search-from-cwd (&optional all-files-p)
"Performs a project search recursively from the current directory.
Uses the first available search backend from `+helm-project-search-engines'. If
ALL-FILES-P (universal argument), include all files, even hidden or compressed
ones."
(interactive "P")
(call-interactively
(or (+helm--get-command "+helm/%s-from-cwd")
#'+helm/grep-from-cwd)))
;; Relative to project root
;;;###autoload
(defun +helm/rg (all-files-p &optional query directory)
"TODO"
(interactive "P")
(+helm-file-search 'rg :query query :in directory :all-files all-files-p))
;;;###autoload (autoload '+helm/rg "completion/helm/autoload/helm")
;;;###autoload (autoload '+helm/rg-from-cwd "completion/helm/autoload/helm")
;;;###autoload (autoload '+helm/ag "completion/helm/autoload/helm")
;;;###autoload (autoload '+helm/ag-from-cwd "completion/helm/autoload/helm")
;;;###autoload (autoload '+helm/pt "completion/helm/autoload/helm")
;;;###autoload (autoload '+helm/pt-from-cwd "completion/helm/autoload/helm")
;;;###autoload (autoload '+helm/grep "completion/helm/autoload/helm")
;;;###autoload (autoload '+helm/grep-from-cwd "completion/helm/autoload/helm")
;;;###autoload
(defun +helm/ag (all-files-p &optional query directory)
"TODO"
(dolist (engine (cl-remove-duplicates +helm-project-search-engines :from-end t))
(defalias (intern (format "+helm/%s" engine))
(lambda (all-files-p &optional query directory)
(interactive "P")
(+helm-file-search 'ag :query query :in directory :all-files all-files-p))
;;;###autoload
(defun +helm/pt (all-files-p &optional query directory)
"TODO"
(+helm-file-search engine :query query :in directory :all-files all-files-p))
"TODO")
(defalias (intern (format "+helm/%s-from-cwd" engine))
(lambda (all-files-p &optional query directory)
(interactive "P")
(+helm-file-search 'pt :query query :in directory :all-files all-files-p))
;;;###autoload
(defun +helm/grep (all-files-p &optional query directory)
"TODO"
(interactive "P")
(+helm-file-search 'grep :query query :in directory :all-files all-files-p))
;; Relative to current directory
;;;###autoload
(defun +helm/rg-from-cwd (recurse-p &optional query)
"TODO"
(interactive "P")
(+helm-file-search 'rg :query query :in default-directory :recursive recurse-p))
;;;###autoload
(defun +helm/ag-from-cwd (recurse-p &optional query)
"TODO"
(interactive "P")
(+helm-file-search 'ag :query query :in default-directory :recursive recurse-p))
;;;###autoload
(defun +helm/pt-from-cwd (recurse-p &optional query)
"TODO"
(interactive "P")
(+helm-file-search 'pt :query query :in default-directory :recursive recurse-p))
;;;###autoload
(defun +helm/grep-from-cwd (recurse-p &optional query)
"TODO"
(interactive "P")
(+helm-file-search 'grep :query query :in default-directory :recursive recurse-p))
(+helm-file-search engine :query query :in default-directory :all-files all-files-p))
"TODO"))