completion/ivy: refactor ag/rg/pt/git-grep advice functions

They are no isolated to their evil use cases.
This commit is contained in:
Henrik Lissner 2018-03-18 22:00:14 -04:00
parent ff2f30f9b4
commit 8d72257aa6
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
3 changed files with 74 additions and 56 deletions

View file

@ -48,37 +48,41 @@
(default-directory directory)) (default-directory directory))
(setq +ivy--file-last-search query) (setq +ivy--file-last-search query)
(require 'counsel) (require 'counsel)
(pcase engine (cl-letf (((symbol-function 'counsel-ag-function)
('grep (symbol-function '+ivy*counsel-ag-function))
(let ((args (if recursion-p " -r")) ((symbol-function 'counsel-git-grep-function)
(counsel-projectile-grep-initial-input query) (symbol-function '+ivy*counsel-git-grep-function)))
(default-directory directory)) (pcase engine
(if all-files-p ('grep
(cl-letf (((symbol-function #'projectile-ignored-directories-rel) (let ((args (if recursion-p " -r"))
(symbol-function #'ignore)) (counsel-projectile-grep-initial-input query)
((symbol-function #'projectile-ignored-files-rel) (default-directory directory))
(symbol-function #'ignore))) (if all-files-p
(counsel-projectile-grep args)) (cl-letf (((symbol-function #'projectile-ignored-directories-rel)
(counsel-projectile-grep args)))) (symbol-function #'ignore))
('ag ((symbol-function #'projectile-ignored-files-rel)
(let ((args (concat " -S" ; smart-case (symbol-function #'ignore)))
(if all-files-p " -a") (counsel-projectile-grep args))
(unless recursion-p " --depth 1")))) (counsel-projectile-grep args))))
(counsel-ag query directory args (format prompt args)))) ('ag
('rg (let ((args (concat " -S" ; smart-case
(let ((args (concat " -S" ; smart-case (if all-files-p " -a")
(if all-files-p " -uu") (unless recursion-p " --depth 1"))))
(unless recursion-p " --maxdepth 1")))) (counsel-ag query directory args (format prompt args))))
(counsel-rg query directory args (format prompt args)))) ('rg
('pt (let ((args (concat " -S" ; smart-case
(let ((counsel-pt-base-command (if all-files-p " -uu")
(concat counsel-pt-base-command (unless recursion-p " --maxdepth 1"))))
" -S" ; smart-case (counsel-rg query directory args (format prompt args))))
(if all-files-p " -U") ('pt
(unless recursion-p " --depth=1"))) (let ((counsel-pt-base-command
(default-directory directory)) (concat counsel-pt-base-command
(counsel-pt query))) " -S" ; smart-case
(_ (error "No search engine specified"))))) (if all-files-p " -U")
(unless recursion-p " --depth=1")))
(default-directory directory))
(counsel-pt query)))
(_ (error "No search engine specified"))))))
;;;###autoload (autoload '+ivy:pt "completion/ivy/autoload/evil" nil t) ;;;###autoload (autoload '+ivy:pt "completion/ivy/autoload/evil" nil t)
(evil-define-operator +ivy:pt (beg end query &optional all-files-p directory) (evil-define-operator +ivy:pt (beg end query &optional all-files-p directory)
@ -160,3 +164,41 @@ NOTE: ripgrep doesn't support multiline searches (yet)."
(interactive "<r><a><!>") (interactive "<r><a><!>")
(let ((+ivy--file-search-recursion-p (not bang))) (let ((+ivy--file-search-recursion-p (not bang)))
(+ivy:rg beg end query t default-directory))) (+ivy:rg beg end query t default-directory)))
;;
;; Advice
;;
;;;###autoload
(defun +ivy*counsel-ag-function (string)
"Advice to get rid of the character limit from `counsel-ag-function'.
NOTE This may need to be updated frequently, to meet changes upstream (in
counsel-rg)."
(if (< (length string) 1) ; <-- modified the character limit
(counsel-more-chars 1) ; <--
(let ((default-directory (ivy-state-directory ivy-last))
(regex (counsel-unquote-regex-parens
(setq ivy--old-re
(ivy--regex string)))))
(counsel--async-command (format counsel-ag-command
(shell-quote-argument regex)))
nil)))
;;;###autoload
(defun +ivy*counsel-git-grep-function (string)
"Advice to get rid of the character limit from `counsel-git-grep-function'.
NOTE This may need to be updated frequently, to meet changes upstream (in
counsel-git-grep)."
(if (and (> counsel--git-grep-count counsel--git-grep-count-threshold)
(< (length string) 1)) ; <-- modified the character limit
(counsel-more-chars 1) ; <--
(let* ((default-directory (ivy-state-directory ivy-last))
(cmd (format counsel-git-grep-cmd
(setq ivy--old-re (ivy--regex string t)))))
(if (<= counsel--git-grep-count counsel--git-grep-count-threshold)
(split-string (shell-command-to-string cmd) "\n" t)
(counsel--gg-candidates (ivy--regex string))
nil))))

View file

@ -155,23 +155,6 @@ search current file. See `+ivy-task-tags' to customize what this searches for."
:action #'+ivy--tasks-open-action :action #'+ivy--tasks-open-action
:caller '+ivy/tasks)) :caller '+ivy/tasks))
;;;###autoload
(defun +ivy*counsel-ag-function (string)
"Advice to get rid of the character limit from `counsel-ag-function' and use
`evil-set-jump' if evil is active.
NOTE This may need to be updated frequently, to meet changes upstream (in
counsel-rg)."
(if (< (length string) 1) ; <-- modified the character limit
(counsel-more-chars 1) ; <--
(let ((default-directory (ivy-state-directory ivy-last))
(regex (counsel-unquote-regex-parens
(setq ivy--old-re
(ivy--regex string)))))
(counsel--async-command (format counsel-ag-command
(shell-quote-argument regex)))
nil)))
;;;###autoload ;;;###autoload
(defun +ivy/wgrep-occur () (defun +ivy/wgrep-occur ()
"Invoke the search+replace wgrep buffer on the current ag/rg search results." "Invoke the search+replace wgrep buffer on the current ag/rg search results."

View file

@ -107,14 +107,7 @@ immediately runs it on the current candidate (ending the ivy session)."
(dolist (cmd '(counsel-ag counsel-rg counsel-pt)) (dolist (cmd '(counsel-ag counsel-rg counsel-pt))
(ivy-add-actions (ivy-add-actions
cmd cmd
'(("O" +ivy-git-grep-other-window-action "open in other window")))) '(("O" +ivy-git-grep-other-window-action "open in other window")))))
;; Removes character limit from `counsel-ag-function' and sets a jump point
;; for `evil'.
;;
;; This may need to be updated frequently, to meet changes upstream.
;; counsel-ag, counsel-rg and counsel-pt all use this function
(advice-add #'counsel-ag-function :override #'+ivy*counsel-ag-function))
(def-package! counsel-projectile (def-package! counsel-projectile