Implement helm project search functionality #644

Adds +helm/project-search, as well as +helm/ag and +helm/ag-from-cwd,
and variants for rg, pt and grep/git-grep, to mirror the functionality
available to :completion ivy.

Also updates the evil ex commands and keybinds
This commit is contained in:
Henrik Lissner 2018-06-02 20:32:52 +02:00
parent b72764c6ea
commit 6e6dfc2215
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
6 changed files with 247 additions and 53 deletions

View file

@ -7,57 +7,58 @@
(interactive "<a><!>")
(helm-swoop :$query search :$multiline bang))
(defun +helm--file-search (beg end query &optional directory options)
(require 'helm-ag)
(helm-ag--init-state)
(let ((helm-ag--default-directory (or directory (doom-project-root)))
(query (or query
(if (evil-visual-state-p)
(and beg end
(> (abs (- end beg)) 1)
(rxt-quote-pcre (buffer-substring-no-properties beg end)))
+helm--file-last-query)
+helm--file-last-query))
(helm-ag-command-option (concat helm-ag-command-option " " (string-join options " "))))
(setq helm-ag--last-query query)
(helm-attrset 'search-this-file nil helm-ag-source)
(helm-attrset 'name (helm-ag--helm-header helm-ag--default-directory) helm-ag-source)
(helm :sources '(helm-ag-source)
:input query
:buffer "*helm-ag*"
:keymap helm-ag-map
:history 'helm-ag--helm-history)))
(defvar +helm--file-last-search nil)
;; --- file searching ---------------------
;;;###autoload (autoload '+helm:pt "completion/helm/autoload/evil" nil t)
(evil-define-command +helm:pt (all-files-p query)
"Ex interface for `+helm/pt'"
(interactive "<!><a>")
(+helm/pt all-files-p query))
;;;###autoload (autoload '+helm:grep "completion/helm/autoload/evil" nil t)
(evil-define-command +helm:grep (all-files-p query)
"Ex interface for `+helm/grep'"
(interactive "<!><a>")
(+helm/grep all-files-p query))
;;;###autoload (autoload '+helm:ag "completion/helm/autoload/evil" nil t)
(evil-define-command +helm:ag (beg end query &optional bang)
"TODO"
(interactive "<r><a><!>")
(+helm--file-search beg end query nil
(if bang (list "-a" "--hidden"))))
;;;###autoload (autoload '+helm:ag-cwd "completion/helm/autoload/evil" nil t)
(evil-define-command +helm:ag-cwd (beg end query &optional bang)
"TODO"
(interactive "<r><a><!>")
(+helm--file-search beg end query default-directory
(list "-n" (if bang "-a"))))
(evil-define-command +helm:ag (all-files-p query)
"Ex interface for `+helm/ag'"
(interactive "<!><a>")
(+helm/ag all-files-p query))
;;;###autoload (autoload '+helm:rg "completion/helm/autoload/evil" nil t)
(evil-define-command +helm:rg (beg end query &optional bang)
"TODO"
(interactive "<r><a><!>")
(let ((helm-ag-base-command "rg --no-heading"))
(+helm--file-search beg end query nil
(if bang (list "-uu")))))
(evil-define-command +helm:rg (all-files-p query)
"Ex interface for `+helm/rg'"
(interactive "<!><a>")
(+helm/rg all-files-p query))
;;;###autoload (autoload '+helm:pt-from-cwd "completion/helm/autoload/evil" nil t)
(evil-define-command +helm:pt-from-cwd (query &optional recurse-p)
"Ex interface for `+helm/pt-from-cwd'."
(interactive "<a><!>")
(+helm/pt-from-cwd (not recurse-p) query))
;;;###autoload (autoload '+helm:grep-from-cwd "completion/helm/autoload/evil" nil t)
(evil-define-command +helm:grep-from-cwd (query &optional recurse-p)
"Ex interface for `+helm/grep-from-cwd'."
(interactive "<a><!>")
(+helm/grep-from-cwd (not recurse-p) query))
;;;###autoload (autoload '+helm:ag-from-cwd "completion/helm/autoload/evil" nil t)
(evil-define-command +helm:ag-from-cwd (query &optional recurse-p)
"Ex interface for `+helm/ag-from-cwd'."
(interactive "<a><!>")
(+helm/ag-from-cwd (not recurse-p) query))
;;;###autoload (autoload '+helm:rg-from-cwd "completion/helm/autoload/evil" nil t)
(evil-define-command +helm:rg-from-cwd (query &optional recurse-p)
"Ex interface for `+helm/rg-from-cwd'."
(interactive "<a><!>")
(+helm/rg-from-cwd (not recurse-p) query))
;;;###autoload (autoload '+helm:rg-cwd "completion/helm/autoload/evil" nil t)
(evil-define-command +helm:rg-cwd (beg end query &optional bang)
"TODO"
(interactive "<r><a><!>")
(let ((helm-ag-base-command "rg --no-heading --maxdepth 1"))
(+helm--file-search beg end query default-directory
(if bang (list "-uu")))))
;;;###autoload
(defun +helm--set-prompt-display (pos)

View file

@ -0,0 +1,172 @@
;;; completion/helm/autoload/helm.el -*- lexical-binding: t; -*-
;;;###autoload
(defun +helm/tasks (&optional _arg)
(interactive "P")
;; TODO Implement `+helm/tasks'
(error "Not implemented yet"))
;;
;; Project search
;;
(cl-defun +helm--file-search (engine &key query in all-files (recursive t))
(require 'helm-ag)
(helm-ag--init-state)
(let* ((project-root (doom-project-root))
(directory (or in project-root))
(default-directory directory)
(helm-ag--default-directory directory)
(engine (or engine
(and (executable-find "rg") 'rg)
(and (executable-find "ag") 'ag)
(and (executable-find "pt") 'pt)
(and (or (executable-find "grep")
(executable-find "git"))
'grep)
(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)))
(end (or (bound-and-true-p evil-visual-end) (region-end))))
(when (> (abs (- end beg)) 1)
(rxt-quote-pcre (buffer-substring-no-properties beg end)))))
""))
(prompt (format "%s%%s %s"
(symbol-name engine)
(cond ((equal directory default-directory)
"./")
((equal directory project-root)
(projectile-project-name))
(t
(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))
(message "-- %s (%s)" helm-grep-default-command query)
(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 --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")))))
(helm-ag-base-command (string-join command " ")))
(setq helm-ag--last-query query)
(helm-attrset 'search-this-file nil helm-ag-source)
(helm-attrset 'name (helm-ag--helm-header helm-ag--default-directory) helm-ag-source)
(helm :sources '(helm-ag-source)
:input query
:prompt prompt
:buffer "*helm-ag*"
:keymap helm-ag-map
:history 'helm-ag--helm-history)))
;;;###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."
(interactive "P")
(call-interactively
(cond ((executable-find "rg") (if arg #'+helm/rg-from-cwd #'+helm/rg))
((executable-find "ag") (if arg #'+helm/ag-from-cwd #'+helm/ag))
((executable-find "pt") (if arg #'+helm/pt-from-cwd #'+helm/pt))
(arg #'+helm/grep-from-cwd)
(#'+helm/grep))))
;; 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
(defun +helm/ag (all-files-p &optional query directory)
"TODO"
(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"
(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))

View file

@ -116,8 +116,11 @@
;; `helm-ag'
(map! :after helm-ag
:map helm-ag-edit-map [remap quit-window] #'helm-ag--edit-abort)
(after! helm-ag
(define-key helm-ag-edit-map [remap quit-window] #'helm-ag--edit-abort)
(set! :popup "^\\*helm-ag-edit"
'((size . 0.35))
'((transient . 0) (quit))))
;; `helm-css-scss' -- https://github.com/ShingoFukuyama/helm-css-scss

View file

@ -497,9 +497,14 @@
:desc "Spelling correction" :n "S" #'flyspell-correct-word-generic)
(:desc "search" :prefix "/"
:desc "Project" :nv "p" #'+ivy/project-search
:desc "Directory" :nv "d" (λ! (+ivy/project-search t))
(:when (featurep! :completion ivy)
:desc "Buffer" :nv "b" #'swiper
:desc "Project" :nv "p" #'+ivy/project-search
:desc "Directory" :nv "d" (λ! (+ivy/project-search t)))
(:when (featurep! :completion helm)
:desc "Buffer" :nv "b" #'helm-swoop
:desc "Project" :nv "p" #'+helm/project-search
:desc "Directory" :nv "d" (λ! (+helm/project-search t)))
:desc "Symbols" :nv "i" #'imenu
:desc "Symbols across buffers" :nv "I" #'imenu-anywhere
:desc "Online providers" :nv "o" #'+lookup/online-select)

View file

@ -103,9 +103,13 @@ command from the current directory instead of the project root."
(ex! "todo" #'+ivy:todo))
((featurep! :completion helm)
(ex! "ag" #'+helm:ag)
(ex! "agc[wd]" #'+helm:ag-cwd)
(ex! "agc[wd]" #'+helm:ag-from-cwd)
(ex! "rg" #'+helm:rg)
(ex! "rgc[wd]" #'+helm:rg-cwd)
(ex! "rgc[wd]" #'+helm:rg-from-cwd)
(ex! "pt" #'+helm:pt)
(ex! "ptc[wd]" #'+helm:pt-from-cwd)
(ex! "grep" #'+helm:grep)
(ex! "grepc[wd]" #'+helm:grep-from-cwd)
(ex! "sw[oop]" #'+helm:swoop)
(ex! "todo" #'+helm:todo)))
;; Project tools

View file

@ -167,6 +167,15 @@ the command buffer."
(advice-add #'helpful--navigate :override #'+popup*helpful--navigate))
;; `helm-ag'
(after! helm-ag
(defun +helm*pop-to-buffer (orig-fn &rest args)
(pop-to-buffer
(save-window-excursion (apply orig-fn args)
(current-buffer))))
(advice-add #'helm-ag--edit :around #'+helm*pop-to-buffer))
;; `Info'
(defun +popup*switch-to-info-window (&rest _)
(when-let* ((win (get-buffer-window "*info*")))