completion/ivy: switch from ag to ripgrep

This commit is contained in:
Henrik Lissner 2017-05-06 16:41:17 +02:00
parent 8600d2e8d2
commit 434a5efeda
5 changed files with 75 additions and 43 deletions

View file

@ -1,11 +1,11 @@
;;; completion/ivy/autoload/evil.el
(defvar +ivy--ag-last-search nil)
(defvar +ivy--file-last-search nil)
;;;###autoload (autoload '+ivy:ag-search "completion/ivy/autoload/evil" nil t)
(evil-define-operator +ivy:ag-search (beg end search regex-p &optional dir)
"Preform a counsel search with SEARCH. If SEARCH is nil and in visual mode,
use the selection, otherwise activate live ag searching in helm.
;;;###autoload (autoload '+ivy:file-search "completion/ivy/autoload/evil" nil t)
(evil-define-operator +ivy:file-search (beg end search regex-p &optional dir)
"Preform a `counsel-rg' search with SEARCH. If SEARCH is nil and in visual
mode, use the selection, otherwise activate live ag searching in helm.
If REGEX-P is non-nil, SEARCH will be treated as a regular expression.
DIR specifies the default-directory from which ag is run."
@ -13,20 +13,19 @@ DIR specifies the default-directory from which ag is run."
(interactive "<r><a><!>")
(let ((search (or search
(and (evil-visual-state-p)
(and beg end
(let ((str (buffer-substring-no-properties beg end)))
(if regex-p (rxt-quote-pcre str) str))))
+ivy--ag-last-search)))
(setq +ivy--ag-last-search search)
(counsel-ag (if regex-p search (regexp-quote search))
(and beg end (buffer-substring-no-properties beg end)))
+ivy--file-last-search)))
(setq +ivy--file-last-search search)
(counsel-rg search
(or dir (doom-project-root))
(concat "--nocolor --nogroup" (if regex-p " -Q")))))
(unless regex-p " -F")
(format "File search (%s)" (if regex-p "regex" "literal")))))
;;;###autoload (autoload '+ivy:ag-search-cwd "completion/ivy/autoload/evil" nil t)
(evil-define-operator +ivy:ag-search-cwd (beg end search regex-p)
;;;###autoload (autoload '+ivy:file-search-cwd "completion/ivy/autoload/evil" nil t)
(evil-define-operator +ivy:file-search-cwd (beg end search regex-p)
:type inclusive :repeat nil
(interactive "<r><a><!>")
(+ivy:ag-search beg end search regex-p default-directory))
(+ivy:file-search beg end search regex-p default-directory))
;;;###autoload (autoload '+ivy:swiper "completion/ivy/autoload/evil" nil t)
(evil-define-command +ivy:swiper (&optional search)

View file

@ -77,37 +77,67 @@ limit to buffers in the current workspace."
;;;###autoload
(defun +ivy/tasks ()
"Search through all TODO/FIXME tags in the current project using
`counsel-rg'."
(interactive)
;; TODO Make nicer
(counsel-ag " (TODO|FIXME|NOTE) " (doom-project-root)))
(counsel-rg "\\(TODO|FIXME\\)\\s" (doom-project-root) "--case-sensitive -w"))
;;;###autoload
(defun +ivy*counsel-ag-function (string base-cmd extra-ag-args)
"Advice to get rid of the character limit from `counsel-ag-function', which
interferes with my custom :ag ex command `+ivy:ag-search'."
"Advice to 1) get rid of the character limit from `counsel-ag-function' and 2)
disable ivy's over-zealous parentheses quoting behavior, both of which
interferes with my custom :[ar]g ex command `+ivy:file-search'."
(when (null extra-ag-args)
(setq extra-ag-args ""))
(if (< (length string) 1)
(if (< (length string) 1) ;; #1
(counsel-more-chars 1)
(let ((default-directory counsel--git-grep-dir)
(regex (counsel-unquote-regex-parens
(setq ivy--old-re
(ivy--regex string)))))
(let ((ag-cmd (format base-cmd
(concat extra-ag-args
" -- "
(shell-quote-argument regex)))))
(ivy--regex
(counsel-unquote-regex-parens string)))))) ;; #2
(let* ((args-end (string-match " -- " extra-ag-args))
(file (if args-end
(substring-no-properties extra-ag-args (+ args-end 3))
""))
(extra-ag-args (if args-end
(substring-no-properties extra-ag-args 0 args-end)
extra-ag-args))
(ag-cmd (format base-cmd
(concat extra-ag-args
" -- "
(shell-quote-argument regex)
file))))
(if (file-remote-p default-directory)
(split-string (shell-command-to-string ag-cmd) "\n" t)
(counsel--async-command ag-cmd)
nil)))))
;;;###autoload
(defun +ivy/counsel-ag-occur ()
"Invoke the search+replace wgrep buffer on the current ag search results."
(defun +ivy/wgrep-occur ()
"Invoke the search+replace wgrep buffer on the current ag/rg search results."
(interactive)
(require 'wgrep)
(call-interactively 'ivy-occur))
(if (not (window-minibuffer-p))
(user-error "No completion session is active")
(require 'wgrep)
(let* ((caller (ivy-state-caller ivy-last))
(occur-fn (plist-get ivy--occurs-list caller))
(buffer
(generate-new-buffer
(format "*ivy-occur%s \"%s\"*"
(if caller (concat " " (prin1-to-string caller)) "")
ivy-text))))
(with-current-buffer buffer
(let ((inhibit-read-only t))
(erase-buffer)
(funcall occur-fn))
(setf (ivy-state-text ivy-last) ivy-text)
(setq ivy-occur-last ivy-last)
(setq-local ivy--directory ivy--directory))
(ivy-exit-with-action
`(lambda (_)
(pop-to-buffer ,buffer)
(ivy-wgrep-change-to-wgrep-mode))))))
;;;###autoload
(defun +ivy-yas-prompt (prompt choices &optional display-fn)